private void BuildGraph(string fileName) { int hr = 0; try { graphBuilder = (IFilterGraph2)new FilterGraph(); mediaControl = (IMediaControl)graphBuilder; mediaSeeking = (IMediaSeeking)graphBuilder; mediaPosition = (IMediaPosition)graphBuilder; vmr9 = (IBaseFilter)new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); } catch (Exception e) { CloseInterfaces(); MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Configure() { int hr; IFilterGraph2 filterGraph = (IFilterGraph2) new FilterGraph(); URLReader u = new URLReader(); m_iop = (IAMOpenProgress)u; IFileSourceFilter fsf = (IFileSourceFilter)u; IFileSourceFilter fsf2 = (IFileSourceFilter)u; hr = filterGraph.AddFilter((IBaseFilter)m_iop, "url"); DsError.ThrowExceptionForHR(hr); ThreadStart o2 = new ThreadStart(this.ThreadProc); Thread thread; thread = new Thread(o2); thread.Name = "cancellor"; thread.Start(); hr = fsf.Load(@"http://192.168.1.77/DShow/foo.avi", null); m_bAbort = (hr == -2147467260); // Aborted Marshal.ReleaseComObject(u); Marshal.ReleaseComObject(filterGraph); }
private void BuildGraph(string fileName) { int hr = 0; try { graphBuilder = (IFilterGraph2) new FilterGraph(); mediaControl = (IMediaControl)graphBuilder; ISampleGrabber sampGrabber = null; IBaseFilter baseGrabFlt = null; // Get the SampleGrabber interface sampGrabber = (ISampleGrabber) new SampleGrabber(); vmr9 = (IBaseFilter) new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); mediaPosition = (IMediaPosition)graphBuilder; m_MediaEvent = graphBuilder as IMediaEvent; hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); } catch (Exception e) { CloseInterfaces(); MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void SetupCapturePin(int RequestedHeight, int RequestedWidth, short iBPP) { int hr; if (RequestedHeight + RequestedWidth + iBPP > 0) { SetPinParameters(m_pinCapture, RequestedWidth, RequestedHeight, iBPP); } captureSG = new SampleGrabber() as ISampleGrabber; IBaseFilter baseCaptureGrabFlt = captureSG as IBaseFilter; ConfigureSampleGrabber(captureSG, 1); pCaptureSampleIn = DsFindPin.ByDirection(baseCaptureGrabFlt, PinDirection.Input, 0); hr = m_FilterGraph.AddFilter(baseCaptureGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.Connect(m_pinCapture, pCaptureSampleIn); DsError.ThrowExceptionForHR(hr); pCaptureSampleOut = DsFindPin.ByDirection(baseCaptureGrabFlt, PinDirection.Output, 0); //SetupDummyWindow(); }
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; int v; int oldv; IBaseFilter pFilter; IBaseFilter pRender = (IBaseFilter) new VideoRendererDefault(); ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); m_FilterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry ds = new DsROTEntry(m_FilterGraph); IGraphVersion igv = (IGraphVersion)m_FilterGraph; hr = igv.QueryVersion(out v); DsError.ThrowExceptionForHR(hr); Debug.Assert(v == 0, "Version1"); oldv = v; hr = icgb.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); hr = m_FilterGraph.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out pFilter); DsError.ThrowExceptionForHR(hr); hr = igv.QueryVersion(out v); DsError.ThrowExceptionForHR(hr); Debug.Assert(v > oldv, "Version2"); oldv = v; hr = m_FilterGraph.AddFilter(pRender, "renderererer"); DsError.ThrowExceptionForHR(hr); hr = igv.QueryVersion(out v); DsError.ThrowExceptionForHR(hr); Debug.Assert(v > oldv, "Version2"); oldv = v; hr = icgb.RenderStream(null, null, pFilter, null, pRender); DsError.ThrowExceptionForHR(hr); hr = igv.QueryVersion(out v); DsError.ThrowExceptionForHR(hr); Debug.Assert(v > oldv, "Version4"); oldv = v; Marshal.ReleaseComObject(icgb); }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight, string FileName) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter baseGrabFlt = null; IBaseFilter capFilter = null; IBaseFilter muxFilter = null; IFileSinkFilter fileWriterFilter = null; ICaptureGraphBuilder2 capGraph = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; m_mediaCtrl = m_FilterGraph as IMediaControl; #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif try { // 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 device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter); DsError.ThrowExceptionForHR( hr ); baseGrabFlt = (IBaseFilter) sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.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); } // Create a filter for the output avi file hr = capGraph.SetOutputFileName(MediaSubType.Avi, FileName, out muxFilter, out fileWriterFilter); DsError.ThrowExceptionForHR( hr ); // Connect everything together hr = capGraph.RenderStream( PinCategory.Capture, MediaType.Video, capFilter, baseGrabFlt, muxFilter); DsError.ThrowExceptionForHR( hr ); // Now that sizes are fixed, store the sizes SaveSizeInfo(sampGrabber); } finally { if (fileWriterFilter != null) { Marshal.ReleaseComObject(fileWriterFilter); fileWriterFilter = null; } if (muxFilter != null) { Marshal.ReleaseComObject(muxFilter); muxFilter = null; } if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } } }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, AMMediaType media) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; // Get the graphbuilder object m_FilterGraph = (IFilterGraph2) new FilterGraph(); m_mediaCtrl = m_FilterGraph as IMediaControl; try { // Get the ICaptureGraphBuilder2 capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); /* // check for crossbar var capDevices2 = DsDevice.GetDevicesOfCat(FilterCategory.AMKSCrossbar); if (capDevices2.Length > 0) { IBaseFilter cross; hr = m_FilterGraph.AddSourceFilterForMoniker(capDevices2[0].Mon, null, "crossbar", out cross); ISpecifyPropertyPages pProp = cross as ISpecifyPropertyPages; //Get the name of the filter from the FilterInfo struct FilterInfo filterInfo; hr = cross.QueryFilterInfo(out filterInfo); DsError.ThrowExceptionForHR(hr); // Get the propertypages from the property bag DsCAUUID caGUID; hr = pProp.GetPages(out caGUID); DsError.ThrowExceptionForHR(hr); //Create and display the OlePropertyFrame object oDevice = (object)cross; hr = NativeMethods.OleCreatePropertyFrame(IntPtr.Zero, 0, 0, filterInfo.achName, 1, ref oDevice, caGUID.cElems, caGUID.pElems, 0, 0, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(oDevice); //IAMCrossbar crossbar2 = cross as IAMCrossbar; //int inputPinCount, outputPinCount; //crossbar2.get_PinCounts(out inputPinCount, out outputPinCount); //crossbar2.Route(0, (int)PhysicalConnectorType.Video_Composite); cross = null; }*/ // Get the SampleGrabber interface sampGrabber = (ISampleGrabber) new SampleGrabber(); // Start building the graph hr = capGraph.SetFiltergraph( m_FilterGraph ); DsError.ThrowExceptionForHR( hr ); // Add the video device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, "Video input", out capFilter); DsError.ThrowExceptionForHR( hr ); // add video crossbar // thanks to Andrew Fernie - this is to get tv tuner cards working IAMCrossbar crossbar = null; object o; hr = capGraph.FindInterface(PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMCrossbar).GUID, out o); if (hr >= 0) { crossbar = (IAMCrossbar)o; int oPin, iPin; int ovLink, ivLink; ovLink = ivLink = 0; crossbar.get_PinCounts(out oPin, out iPin); int pIdxRel; PhysicalConnectorType tp; for (int i = 0; i < iPin; i++) { crossbar.get_CrossbarPinInfo(true, i, out pIdxRel, out tp); if (tp == PhysicalConnectorType.Video_Composite) ivLink = i; } for (int i = 0; i < oPin; i++) { crossbar.get_CrossbarPinInfo(false, i, out pIdxRel, out tp); if (tp == PhysicalConnectorType.Video_VideoDecoder) ovLink = i; } try { crossbar.Route(ovLink, ivLink); o = null; } catch { throw new Exception("Failed to get IAMCrossbar"); } } //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 ); SetConfigParms(capGraph, capFilter, media); hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, pAVIDecompressor, baseGrabFlt); if (hr < 0) { hr = capGraph.RenderStream(PinCategory.Capture, 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 iWidth, int iHeight, short iBPP, Control hControl) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin pCaptureOut = null; IPin pSampleIn = null; IPin pRenderIn = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; try { #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif // add the video input device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); // Find the still pin m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0); // Didn't find one. Is there a preview pin? if (m_pinStill == null) { m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0); } // Still haven't found one. Need to put a splitter in so we have // one stream to capture the bitmap from, and one to display. Ok, we // don't *have* to do it that way, but we are going to anyway. if (m_pinStill == null) { IPin pRaw = null; IPin pSmart = null; // There is no still pin m_VidControl = null; // Add a splitter IBaseFilter iSmartTee = (IBaseFilter)new SmartTee(); try { hr = m_FilterGraph.AddFilter(iSmartTee, "SmartTee"); DsError.ThrowExceptionForHR(hr); // Find the find the capture pin from the video device and the // input pin for the splitter, and connnect them pRaw = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); pSmart = DsFindPin.ByDirection(iSmartTee, PinDirection.Input, 0); hr = m_FilterGraph.Connect(pRaw, pSmart); DsError.ThrowExceptionForHR(hr); // Now set the capture and still pins (from the splitter) m_pinStill = DsFindPin.ByName(iSmartTee, "Preview"); pCaptureOut = DsFindPin.ByName(iSmartTee, "Capture"); // If any of the default config items are set, perform the config // on the actual video device (rather than the splitter) if (iHeight + iWidth + iBPP > 0) { SetConfigParms(pRaw, iWidth, iHeight, iBPP); } } finally { if (pRaw != null) { Marshal.ReleaseComObject(pRaw); } if (pRaw != pSmart) { Marshal.ReleaseComObject(pSmart); } if (pRaw != iSmartTee) { Marshal.ReleaseComObject(iSmartTee); } } } else { // Get a control pointer (used in Click()) m_VidControl = capFilter as IAMVideoControl; pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); // If any of the default config items are set if (iHeight + iWidth + iBPP > 0) { SetConfigParms(m_pinStill, iWidth, iHeight, iBPP); } } // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; // Configure the sample grabber IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); // Get the default video renderer IBaseFilter pRenderer = new VideoRendererDefault() as IBaseFilter; hr = m_FilterGraph.AddFilter(pRenderer, "Renderer"); DsError.ThrowExceptionForHR(hr); pRenderIn = DsFindPin.ByDirection(pRenderer, PinDirection.Input, 0); // Add the sample grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); if (m_VidControl == null) { // Connect the Still pin to the sample grabber hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); // Connect the capture pin to the renderer hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn); DsError.ThrowExceptionForHR(hr); } else { // Connect the capture pin to the renderer hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn); DsError.ThrowExceptionForHR(hr); // Connect the Still pin to the sample grabber hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); } // Learn the video properties SaveSizeInfo(sampGrabber); ConfigVideoWindow(hControl); // Start the graph IMediaControl mediaCtrl = m_FilterGraph as IMediaControl; hr = mediaCtrl.Run(); DsError.ThrowExceptionForHR(hr); } finally { if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (pCaptureOut != null) { Marshal.ReleaseComObject(pCaptureOut); pCaptureOut = null; } if (pRenderIn != null) { Marshal.ReleaseComObject(pRenderIn); pRenderIn = null; } if (pSampleIn != null) { Marshal.ReleaseComObject(pSampleIn); pSampleIn = null; } } }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; // Get the graphbuilder object m_FilterGraph = (IFilterGraph2) new FilterGraph(); m_mediaCtrl = m_FilterGraph as IMediaControl; try { // 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 device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, "Video input", out capFilter); 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 ); // If any of the default config items are set if (iFrameRate + iHeight + iWidth > 0) { SetConfigParms(capGraph, capFilter, iFrameRate, iWidth, iHeight); } hr = capGraph.RenderStream( PinCategory.Capture, 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(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; } } }
/// <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; } } }
/// <summary> /// Returns the <see cref="CameraInfo"/> for the given <see cref="DsDevice"/>. /// </summary> /// <param name="dev">A <see cref="DsDevice"/> to parse name and capabilities for.</param> /// <returns>The <see cref="CameraInfo"/> for the given device.</returns> private CameraInfo Caps(DsDevice dev) { var camerainfo = new CameraInfo(); try { // Get the graphbuilder object m_graphBuilder = (IFilterGraph2) new FilterGraph(); // Get the ICaptureGraphBuilder2 capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); // Add the video device int hr = m_graphBuilder.AddSourceFilterForMoniker(dev.Mon, null, "Video input", out capFilter); //DsError.ThrowExceptionForHR(hr); if (hr != 0) { return null; } hr = capGraph.SetFiltergraph(m_graphBuilder); DsError.ThrowExceptionForHR(hr); hr = m_graphBuilder.AddFilter(capFilter, "Ds.NET Video Capture Device"); DsError.ThrowExceptionForHR(hr); object o = null; DsGuid cat = PinCategory.Capture; DsGuid type = MediaType.Interleaved; DsGuid iid = typeof (IAMStreamConfig).GUID; // Check if Video capture filter is in use hr = capGraph.RenderStream(cat, MediaType.Video, capFilter, null, null); if (hr != 0) { return null; } //hr = capGraph.FindInterface(PinCategory.Capture, MediaType.Interleaved, capFilter, typeof(IAMStreamConfig).GUID, out o); //if (hr != 0) //{ hr = capGraph.FindInterface(PinCategory.Capture, MediaType.Video, capFilter, typeof (IAMStreamConfig).GUID, out o); DsError.ThrowExceptionForHR(hr); //} var videoStreamConfig = o as IAMStreamConfig; int iCount = 0; int iSize = 0; try { if (videoStreamConfig != null) videoStreamConfig.GetNumberOfCapabilities(out iCount, out iSize); } catch (Exception ex) { //ErrorLogger.ProcessException(ex, false); return null; } pscc = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof (VideoStreamConfigCaps))); camerainfo.Name = dev.Name; camerainfo.DirectshowDevice = dev; for (int i = 0; i < iCount; i++) { VideoStreamConfigCaps scc; try { AMMediaType curMedType; if (videoStreamConfig != null) hr = videoStreamConfig.GetStreamCaps(i, out curMedType, pscc); Marshal.ThrowExceptionForHR(hr); scc = (VideoStreamConfigCaps) Marshal.PtrToStructure(pscc, typeof (VideoStreamConfigCaps)); var CSF = new CamSizeFPS(); CSF.FPS = (int) (10000000/scc.MinFrameInterval); CSF.Height = scc.InputSize.Height; CSF.Width = scc.InputSize.Width; if (!InSizeFpsList(camerainfo.SupportedSizesAndFPS, CSF)) if (ParametersOK(CSF)) camerainfo.SupportedSizesAndFPS.Add(CSF); } catch (Exception ex) { //ErrorLogger.ProcessException(ex, false); } } } finally { } return camerainfo; }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, int iSampleRate, int iChannels) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; IBaseFilter baseGrabFlt = null; IBaseFilter nullrenderer = null; IMediaFilter mediaFilt = m_FilterGraph as IMediaFilter; // Get the graphbuilder object m_FilterGraph = (IFilterGraph2)new FilterGraph(); m_mediaCtrl = m_FilterGraph as IMediaControl; try { // 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 audio device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, "Audio input", out capFilter); DsError.ThrowExceptionForHR(hr); // If any of the default config items are set if (iSampleRate + iChannels > 0) { SetConfigParms(capGraph, capFilter, iSampleRate, iChannels); } // 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); // Read and cache the resulting settings 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> /// Adds the MPEG muxer filter /// </summary> /// <param name="_graphBuilder">GraphBuilder</param> /// <param name="_capture">Capture</param> /// <returns></returns> private bool AddMpegMuxer(IFilterGraph2 _graphBuilder, Capture _capture) { Log.Log.WriteFile("analog:AddMpegMuxer()"); try { const string monikerPowerDirectorMuxer = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{7F2BBEAF-E11C-4D39-90E8-938FB5A86045}"; const string monikerPowerDvdMuxer = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{6770E328-9B73-40C5-91E6-E2F321AEDE57}"; const string monikerPowerDvdMuxer2 = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{370E9701-9DC5-42C8-BE29-4E75F0629EED}"; _filterMpegMuxer = Marshal.BindToMoniker(monikerPowerDirectorMuxer) as IBaseFilter; int hr = _graphBuilder.AddFilter(_filterMpegMuxer, "CyberLink MPEG Muxer"); if (hr != 0) { _filterMpegMuxer = Marshal.BindToMoniker(monikerPowerDvdMuxer) as IBaseFilter; hr = _graphBuilder.AddFilter(_filterMpegMuxer, "CyberLink MPEG Muxer"); if (hr != 0) { _filterMpegMuxer = Marshal.BindToMoniker(monikerPowerDvdMuxer2) as IBaseFilter; hr = _graphBuilder.AddFilter(_filterMpegMuxer, "CyberLink MPEG Muxer"); if (hr != 0) { Log.Log.WriteFile("analog:AddMpegMuxer returns:0x{0:X}", hr); //throw new TvException("Unable to add Cyberlink MPEG Muxer"); } } } Log.Log.WriteFile("analog:connect pinvideo {0} ->mpeg muxer", FilterGraphTools.LogPinInfo(_pinVideo)); if (!FilterGraphTools.ConnectPin(_graphBuilder, _pinVideo, _filterMpegMuxer, 0)) { Log.Log.WriteFile("analog: unable to connect pinvideo->mpeg muxer"); throw new TvException("Unable to connect pins"); } _pinVideoConnected = true; Log.Log.WriteFile("analog: connected pinvideo->mpeg muxer"); // Some Adaptec devices use the LPCM pin for audio so we check this can connect if applicable. // Note that this does *not* apply to the Adaptec AVC-3610. bool isAdaptec = false; if (!_capture.VideoCaptureDevicePath.ToLower().StartsWith(@"@device:pnp:\\?\usb#vid_03f3&pid_0091#") && // Adaptec AVC-3610 tuner 1 !_capture.VideoCaptureDevicePath.ToLower().StartsWith(@"@device:pnp:\\?\usb#vid_03f3&pid_0093#") && // Adaptec AVC-3610 tuner 2 ( _capture.VideoCaptureName.Contains("Adaptec USB Capture Device") || _capture.VideoCaptureName.Contains("Adaptec PCI Capture Device") || _capture.AudioCaptureName.Contains("Adaptec USB Capture Device") || _capture.AudioCaptureName.Contains("Adaptec PCI Capture Device") ) ) { Log.Log.WriteFile("analog: AddMpegMuxer, Adaptec device found using LPCM"); isAdaptec = true; } if (isAdaptec) { if (!FilterGraphTools.ConnectPin(_graphBuilder, _pinLPCM, _filterMpegMuxer, 1)) { Log.Log.WriteFile("analog: AddMpegMuxer, unable to connect pinLPCM->mpeg muxer"); throw new TvException("Unable to connect pins"); } Log.Log.WriteFile("analog: AddMpegMuxer, connected pinLPCM->mpeg muxer"); } else { Log.Log.WriteFile("analog:connect pinaudio {0} ->mpeg muxer", FilterGraphTools.LogPinInfo(_pinAudio)); if (!FilterGraphTools.ConnectPin(_graphBuilder, _pinAudio, _filterMpegMuxer, 1)) { Log.Log.WriteFile("analog:AddMpegMuxer, unable to connect pinaudio->mpeg muxer"); throw new TvException("Unable to connect pins"); } Log.Log.WriteFile("analog:AddMpegMuxer, connected pinaudio->mpeg muxer"); } return true; } catch (Exception ex) { throw new TvException("Cyberlink MPEG Muxer filter (mpgmux.ax) not installed " + ex.Message); } }
public bool Play(string fileName, Form form) { fileName += ".tsbuffer"; Log.WriteFile("play:{0}", fileName); _graphBuilder = (IFilterGraph2)new FilterGraph(); _rotEntry = new DsROTEntry(_graphBuilder); Log.WriteFile("add tsfilesource"); _tsFileSource = new TsFileSource(); _graphBuilder.AddFilter((IBaseFilter)_tsFileSource, "TsFileSource"); #region add mpeg-2 demux filter Log.WriteFile("add mpeg-2 demux"); MPEG2Demultiplexer demux = new MPEG2Demultiplexer(); _mpegDemux = (IBaseFilter)demux; int hr = _graphBuilder.AddFilter(_mpegDemux, "MPEG-2 Demultiplexer"); #endregion #region create mpeg2 demux pins Log.WriteFile("create mpeg-2 demux pins"); //create mpeg-2 demux output pins IMpeg2Demultiplexer demuxer = _mpegDemux as IMpeg2Demultiplexer; if (demuxer != null) hr = demuxer.CreateOutputPin(GetAudioMpg2Media(), "Audio", out _pinAudio); if (hr != 0) { Log.WriteFile("unable to create audio pin"); return false; } if (demuxer != null) hr = demuxer.CreateOutputPin(GetVideoMpg2Media(), "Video", out _pinVideo); if (hr != 0) { Log.WriteFile("unable to create video pin"); return false; } #endregion #region load file in tsfilesource Log.WriteFile("load file in tsfilesource"); IFileSourceFilter interfaceFile = (IFileSourceFilter)_tsFileSource; if (interfaceFile == null) { Log.WriteFile("TSStreamBufferPlayer9:Failed to get IFileSourceFilter"); return false; } AMMediaType mpeg2ProgramStream = new AMMediaType(); mpeg2ProgramStream.majorType = MediaType.Stream; mpeg2ProgramStream.subType = MediaSubType.Mpeg2Program; mpeg2ProgramStream.unkPtr = IntPtr.Zero; mpeg2ProgramStream.sampleSize = 0; mpeg2ProgramStream.temporalCompression = false; mpeg2ProgramStream.fixedSizeSamples = true; mpeg2ProgramStream.formatType = FormatType.None; mpeg2ProgramStream.formatSize = 0; mpeg2ProgramStream.formatPtr = IntPtr.Zero; hr = interfaceFile.Load(fileName, mpeg2ProgramStream); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9:Failed to load file"); return false; } #region connect tsfilesource->demux Log.WriteFile("connect tsfilesource->demux"); Log.WriteFile("TSStreamBufferPlayer9:connect tsfilesource->mpeg2 demux"); IPin pinTsOut = DsFindPin.ByDirection((IBaseFilter)_tsFileSource, PinDirection.Output, 0); if (pinTsOut == null) { Log.WriteFile("TSStreamBufferPlayer9:failed to find output pin of tsfilesource"); return false; } IPin pinDemuxIn = DsFindPin.ByDirection(_mpegDemux, PinDirection.Input, 0); if (pinDemuxIn == null) { Log.WriteFile("TSStreamBufferPlayer9:failed to find output pin of tsfilesource"); return false; } hr = _graphBuilder.Connect(pinTsOut, pinDemuxIn); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9:failed to connect tsfilesource->mpeg2 demux:{0:X}", hr); return false; } Release.ComObject(pinTsOut); Release.ComObject(pinDemuxIn); #endregion #region map demux pids Log.WriteFile("map mpeg2 pids"); IMPEG2StreamIdMap pStreamId = (IMPEG2StreamIdMap)_pinVideo; hr = pStreamId.MapStreamId(0xe0, MPEG2Program.ElementaryStream, 0, 0); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9: failed to map pid 0xe0->video pin"); return false; } pStreamId = (IMPEG2StreamIdMap)_pinAudio; hr = pStreamId.MapStreamId(0xc0, MPEG2Program.ElementaryStream, 0, 0); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9: failed to map pid 0xc0->audio pin"); return false; } #endregion #region render demux audio/video pins Log.WriteFile("render pins"); hr = _graphBuilder.Render(_pinAudio); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9:failed to render video output pin:{0:X}", hr); } hr = _graphBuilder.Render(_pinVideo); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9:failed to render audio output pin:{0:X}", hr); } #endregion #endregion _videoWin = _graphBuilder as IVideoWindow; if (_videoWin != null) { _videoWin.put_Visible(OABool.True); _videoWin.put_Owner(form.Handle); _videoWin.put_WindowStyle( (WindowStyle)((int)WindowStyle.Child + (int)WindowStyle.ClipSiblings + (int)WindowStyle.ClipChildren)); _videoWin.put_MessageDrain(form.Handle); _videoWin.SetWindowPosition(190, 250, 150, 150); } Log.WriteFile("run graph"); _mediaCtrl = (IMediaControl)_graphBuilder; hr = _mediaCtrl.Run(); Log.WriteFile("TSStreamBufferPlayer9:running:{0:X}", hr); return true; }
/// <summary> /// Adds a mpeg2 demultiplexer to the graph /// </summary> /// <param name="_graphBuilder">The graph builder</param> private void AddMpeg2Demultiplexer(IFilterGraph2 _graphBuilder) { Log.Log.WriteFile("analog: AddMpeg2Demultiplexer"); if (_filterMpeg2Demux != null) return; if (_pinCapture == null) return; _filterMpeg2Demux = (IBaseFilter)new MPEG2Demultiplexer(); int hr = _graphBuilder.AddFilter(_filterMpeg2Demux, "MPEG2 Demultiplexer"); if (hr != 0) { Log.Log.WriteFile("analog: AddMPEG2DemuxFilter returns:0x{0:X}", hr); throw new TvException("Unable to add MPEG2 demultiplexer"); } Log.Log.WriteFile("analog: connect capture->mpeg2 demux"); IPin pin = DsFindPin.ByDirection(_filterMpeg2Demux, PinDirection.Input, 0); hr = _graphBuilder.Connect(_pinCapture, pin); if (hr != 0) { Log.Log.WriteFile("analog: ConnectFilters returns:0x{0:X}", hr); throw new TvException("Unable to connect capture-> MPEG2 demultiplexer"); } IMpeg2Demultiplexer demuxer = (IMpeg2Demultiplexer)_filterMpeg2Demux; demuxer.CreateOutputPin(FilterGraphTools.GetVideoMpg2Media(), "Video", out _pinVideo); demuxer.CreateOutputPin(FilterGraphTools.GetAudioMpg2Media(), "Audio", out _pinAudio); demuxer.CreateOutputPin(FilterGraphTools.GetAudioLPCMMedia(), "LPCM", out _pinLPCM); IMPEG2StreamIdMap map = (IMPEG2StreamIdMap)_pinVideo; map.MapStreamId(224, MPEG2Program.ElementaryStream, 0, 0); map = (IMPEG2StreamIdMap)_pinAudio; map.MapStreamId(0xC0, MPEG2Program.ElementaryStream, 0, 0); map = (IMPEG2StreamIdMap)_pinLPCM; map.MapStreamId(0xBD, MPEG2Program.ElementaryStream, 0xA0, 7); }
/// <summary> /// Adds the InterVideo muxer and connects the compressor to it. /// This is the preferred muxer for Plextor cards and others. /// It will be used if the InterVideo Audio Encoder is used also. /// </summary> /// <param name="_graphBuilder">GraphBuilder</param> /// <param name="_capture">Capture</param> /// <returns></returns> private bool AddInterVideoMuxer(IFilterGraph2 _graphBuilder, Capture _capture) { IPin pinOut; Log.Log.Info("analog: using intervideo muxer"); string muxVideoIn = "video compressor"; const string monikerInterVideoMuxer = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{317DDB63-870E-11D3-9C32-00104B3801F7}"; _filterAnalogMpegMuxer = Marshal.BindToMoniker(monikerInterVideoMuxer) as IBaseFilter; int hr = _graphBuilder.AddFilter(_filterAnalogMpegMuxer, "InterVideo MPEG Muxer"); if (hr != 0) { Log.Log.WriteFile("analog: add intervideo muxer returns:0x{0:X}", hr); throw new TvException("Unable to add InterVideo Muxer"); } Log.Log.Info("analog: add intervideo muxer successful"); // next connect video compressor->muxer if (_isPlextorConvertX) { muxVideoIn = "Plextor ConvertX"; //no video compressor needed with the Plextor device so we use the first capture pin pinOut = DsFindPin.ByDirection(_capture.VideoFilter, PinDirection.Output, 0); } else { pinOut = DsFindPin.ByDirection(_filterVideoCompressor, PinDirection.Output, 0); } IPin pinIn = DsFindPin.ByDirection(_filterAnalogMpegMuxer, PinDirection.Input, 0); if (pinOut == null) { Log.Log.Info("analog: no output pin found on {0}", muxVideoIn); throw new TvException("no output pin found on video out"); } if (pinIn == null) { Log.Log.Info("analog: no input pin found on intervideo muxer"); throw new TvException("no input pin found on intervideo muxer"); } hr = _graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Log.WriteFile("analog: unable to connect {0}-> intervideo muxer returns:0x{1:X}", muxVideoIn, hr); throw new TvException("Unable to add unable to connect to video in on intervideo muxer"); } Log.Log.WriteFile("analog: connected video -> intervideo muxer"); // next connect audio compressor->muxer pinOut = DsFindPin.ByDirection(_filterAudioCompressor, PinDirection.Output, 0); pinIn = DsFindPin.ByDirection(_filterAnalogMpegMuxer, PinDirection.Input, 1); if (pinOut == null) { Log.Log.Info("analog: no output pin found on audio compressor"); throw new TvException("no output pin found on audio compressor"); } if (pinIn == null) { Log.Log.Info("analog: no input pin found on intervideo muxer"); throw new TvException("no input pin found on intervideo muxer"); } hr = _graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Log.WriteFile("analog:unable to connect audio compressor->intervideo muxer returns:0x{0:X}", hr); throw new TvException("Unable to add unable to connect audio compressor->intervideo muxer"); } Log.Log.WriteFile("analog: connected audio -> intervideo muxer"); //and finally we have a capture pin... _pinCapture = DsFindPin.ByDirection(_filterAnalogMpegMuxer, PinDirection.Output, 0); if (_pinCapture == null) { Log.Log.WriteFile("analog:unable find capture pin"); throw new TvException("unable find capture pin"); } return true; }
/// <summary> /// Adds the mpeg muxer /// </summary> /// <param name="_graphBuilder">GraphBuilder</param> /// <returns></returns> private bool AddAnalogMuxer(IFilterGraph2 _graphBuilder) { Log.Log.Info("analog:AddAnalogMuxer"); const string monikerPowerDirectorMuxer = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{7F2BBEAF-E11C-4D39-90E8-938FB5A86045}"; _filterAnalogMpegMuxer = Marshal.BindToMoniker(monikerPowerDirectorMuxer) as IBaseFilter; int hr = _graphBuilder.AddFilter(_filterAnalogMpegMuxer, "Analog MPEG Muxer"); if (hr != 0) { Log.Log.WriteFile("analog:AddAnalogMuxer returns:0x{0:X}", hr); throw new TvException("Unable to add AddAnalogMuxer"); } // next connect audio compressor->muxer IPin pinOut = DsFindPin.ByDirection(_filterAudioCompressor, PinDirection.Output, 0); IPin pinIn = DsFindPin.ByDirection(_filterAnalogMpegMuxer, PinDirection.Input, 1); if (pinOut == null) { Log.Log.Info("analog:no output pin found on audio compressor"); throw new TvException("no output pin found on audio compressor"); } if (pinIn == null) { Log.Log.Info("analog:no input pin found on analog muxer"); throw new TvException("no input pin found on muxer"); } hr = _graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Log.WriteFile("analog:unable to connect audio compressor->muxer returns:0x{0:X}", hr); throw new TvException("Unable to add unable to connect audio compressor->muxer"); } Log.Log.WriteFile("analog: connected audio -> muxer"); // next connect video compressor->muxer pinOut = DsFindPin.ByDirection(_filterVideoCompressor, PinDirection.Output, 0); pinIn = DsFindPin.ByDirection(_filterAnalogMpegMuxer, PinDirection.Input, 0); if (pinOut == null) { Log.Log.Info("analog:no output pin found on video compressor"); throw new TvException("no output pin found on video compressor"); } if (pinIn == null) { Log.Log.Info("analog:no input pin found on analog muxer"); throw new TvException("no input pin found on muxer"); } hr = _graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Log.WriteFile("analog:unable to connect video compressor->muxer returns:0x{0:X}", hr); throw new TvException("Unable to add unable to connect video compressor->muxer"); } //and finally we have a capture pin... Log.Log.WriteFile("analog: connected video -> muxer"); _pinCapture = DsFindPin.ByDirection(_filterAnalogMpegMuxer, PinDirection.Output, 0); if (_pinCapture == null) { Log.Log.WriteFile("analog:unable find capture pin"); throw new TvException("unable find capture pin"); } return true; }
private void BuildGraph(Control hControl) { int hr; DsDevice [] devs; IBaseFilter ibfSource = null; IBaseFilter dmoFilter = null; IBaseFilter ibfRender = null; IDMOWrapperFilter dmoWrapperFilter = null; ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); graphBuilder = (IFilterGraph2) new FilterGraph(); #if DEBUG m_rot = new DsROTEntry(graphBuilder); #endif hr = icgb.SetFiltergraph(graphBuilder); DsError.ThrowExceptionForHR(hr); devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); if (devs.Length < 1) { throw new Exception("This sample requires a capture device. If you don't " + "have a capture device, change the BuildGraph routine to use AddSourceFilter " + "and use a file."); } // Add a source filter hr = graphBuilder.AddSourceFilterForMoniker(devs[0].Mon, null, "Video Input Device", out ibfSource); DsError.ThrowExceptionForHR(hr); // Add a DMO Wrapper Filter dmoFilter = (IBaseFilter) new DMOWrapperFilter(); dmoWrapperFilter = (IDMOWrapperFilter) dmoFilter; // Since I know the guid of the DMO I am looking for, I *could* do this. //hr = dmoWrapperFilter.Init(new Guid("{7EF28FD7-E88F-45bb-9CDD-8A62956F2D75}"), DMOCatergory.VideoEffect); //DMOError.ThrowExceptionForHR(hr); // But it is more useful to show how to scan for the DMO Guid g = FindGuid("DmoFlip", DMOCategory.VideoEffect); hr = dmoWrapperFilter.Init(g, DMOCategory.VideoEffect); DMOError.ThrowExceptionForHR(hr); SetDMOParams(dmoFilter); // Add it to the Graph hr = graphBuilder.AddFilter(dmoFilter, "DMO Filter"); DsError.ThrowExceptionForHR(hr); ibfRender = (IBaseFilter)new VideoRenderer(); hr = graphBuilder.AddFilter(ibfRender, "renderer"); DsError.ThrowExceptionForHR(hr); hr = icgb.RenderStream(null, null, ibfSource, dmoFilter, ibfRender); DsError.ThrowExceptionForHR(hr); ConfigVideo(graphBuilder as IVideoWindow, hControl); Marshal.ReleaseComObject(ibfSource); Marshal.ReleaseComObject(ibfRender); }
private void BuildGraph(string fileName) { int hr = 0; try { graphBuilder = (IFilterGraph2) new FilterGraph(); mediaControl = (IMediaControl) graphBuilder; vmr9 = (IBaseFilter) new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); mixerBitmap = (IVMRMixerBitmap9) vmr9; menuMixer.Enabled = true; mixerEnabled = true; usingGDI = false; UpdateMixerMenu(); SetMixerSettings(); } catch(Exception e) { CloseInterfaces(); MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void InitVMR9(string filename) { int hr = 0; // Create a DirectShow FilterGraph graphBuilder = (IFilterGraph2) new FilterGraph(); // Add it in ROT for debug purpose rot = new DsROTEntry(graphBuilder); // Add a notification handler (see WndProc) hr = (graphBuilder as IMediaEventEx).SetNotifyWindow(this.Handle, WM_GRAPHNOTIFY, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); // Create a VMR9 object vmr9 = (IBaseFilter) new VideoMixingRenderer9(); IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9) vmr9; // We want the Renderless mode! hr = filterConfig.SetRenderingMode(VMR9Mode.Renderless); DsError.ThrowExceptionForHR(hr); // Create the Allocator / Presenter object allocator = new Allocator(device); IVMRSurfaceAllocatorNotify9 vmrSurfAllocNotify = (IVMRSurfaceAllocatorNotify9) vmr9; // Notify the VMR9 filter about our allocator hr = vmrSurfAllocNotify.AdviseSurfaceAllocator(IntPtr.Zero, allocator); DsError.ThrowExceptionForHR(hr); // Notify our allocator about the VMR9 filter hr = allocator.AdviseNotify(vmrSurfAllocNotify); DsError.ThrowExceptionForHR(hr); if (config.IsUsingMixing) { // One stream is enough for this sample // This line also load the mixing componant hr = filterConfig.SetNumberOfStreams(1); DsError.ThrowExceptionForHR(hr); IVMRMixerControl9 mixerControl = (IVMRMixerControl9) vmr9; // Select the mixer mode : YUV or RGB if (config.UseYUVMixing) { hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetYUV | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering); } else { hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetRGB | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering); } DsError.ThrowExceptionForHR(hr); Debug.WriteLine("Using VMR9 mixing mode"); } // Add the filter to the graph hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); // Render the file hr = graphBuilder.RenderFile(filename, null); DsError.ThrowExceptionForHR(hr); // Run the graph hr = (graphBuilder as IMediaControl).Run(); DsError.ThrowExceptionForHR(hr); }
/// <summary> /// Initializes the devices and prepares for capture /// </summary> public void Initialize() { _filterGraph = new FilterGraph() as IFilterGraph2; _mediaCtrl = _filterGraph as IMediaControl; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; int status; try { capGraph = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; sampGrabber = new SampleGrabber() as ISampleGrabber; status = capGraph.SetFiltergraph(_filterGraph); DsError.ThrowExceptionForHR(status); //Adds the video device status = _filterGraph.AddSourceFilterForMoniker(_device.DsDevice.Mon, null, "Video input", out capFilter); DsError.ThrowExceptionForHR(status); IBaseFilter baseGrabFilter = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph status = _filterGraph.AddFilter(baseGrabFilter, ".net grabber"); DsError.ThrowExceptionForHR(status); //TODO: Set Framerate, height and width here status = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, null, baseGrabFilter); DsError.ThrowExceptionForHR(status); SaveSizeInfo(sampGrabber); _bitmapMemory = Marshal.AllocCoTaskMem(_stride * _videoHeight); } 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; } } }
public void CreateGraph() { try { int result = 0; // フィルタグラフマネージャ作成 graphBuilder = new FilterGraph() as IFilterGraph2; // キャプチャグラフビルダ作成 captureGraphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; //captureGraphBuilder(キャプチャグラフビルダ)をgraphBuilder(フィルタグラフマネージャ)に追加. result = captureGraphBuilder.SetFiltergraph(graphBuilder); DsError.ThrowExceptionForHR(result); // ソースフィルタ作成 // キャプチャデバイスをソースフィルタに対応付ける captureFilter = null; result = graphBuilder.AddSourceFilterForMoniker( _capDevice.Mon, null, _capDevice.Name, out captureFilter); DsError.ThrowExceptionForHR(result); // サンプルグラバ作成 sampleGrabber = new SampleGrabber() as ISampleGrabber; // フィルタと関連付ける IBaseFilter grabFilter = sampleGrabber as IBaseFilter; // キャプチャするオーディオのフォーマットを設定 AMMediaType amMediaType = new AMMediaType(); amMediaType.majorType = MediaType.Audio; amMediaType.subType = MediaSubType.PCM; amMediaType.formatPtr = IntPtr.Zero; result = sampleGrabber.SetMediaType(amMediaType); DsError.ThrowExceptionForHR(result); DsUtils.FreeAMMediaType(amMediaType); // callback 登録 sampleGrabber.SetOneShot(false); DsError.ThrowExceptionForHR(result); result = sampleGrabber.SetBufferSamples(true); DsError.ThrowExceptionForHR(result); // キャプチャするフォーマットを取得 object o; result = captureGraphBuilder.FindInterface( DsGuid.FromGuid(PinCategory.Capture), DsGuid.FromGuid(MediaType.Audio), captureFilter, typeof(IAMStreamConfig).GUID, out o); DsError.ThrowExceptionForHR(result); IAMStreamConfig config = o as IAMStreamConfig; AMMediaType media; result = config.GetFormat(out media); DsError.ThrowExceptionForHR(result); WaveFormatEx wf = new WaveFormatEx(); Marshal.PtrToStructure(media.formatPtr, wf); CaptureOption opt = new CaptureOption(wf); _sampler = new DSAudioSampler(opt); DsUtils.FreeAMMediaType(media); Marshal.ReleaseComObject(config); result = sampleGrabber.SetCallback(_sampler, 1); DsError.ThrowExceptionForHR(result); //grabFilter(変換フィルタ)をgraphBuilder(フィルタグラフマネージャ)に追加. result = graphBuilder.AddFilter(grabFilter, "Audio Grab Filter"); DsError.ThrowExceptionForHR(result); //キャプチャフィルタをサンプルグラバーフィルタに接続する result = captureGraphBuilder.RenderStream( DsGuid.FromGuid(PinCategory.Capture), DsGuid.FromGuid(MediaType.Audio), captureFilter, null, grabFilter); DsError.ThrowExceptionForHR(result); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message); } }
/// <summary> /// Connects to the property changed events of the camera settings. /// </summary> //private void Initialize() //{ // //Settings.Instance.Camera.OnCameraControlPropertyChanged += OnCameraControlPropertyChanged; // //Settings.Instance.Camera.OnVideoProcAmpPropertyChanged += OnVideoProcAmpPropertyChanged; // //Settings.Instance.Camera.OnVideoControlFlagsChanged += OnVideoControlFlagsChanged; // //stopwatch = new Stopwatch(); //} /// <summary> /// Build the capture graph for grabber. /// </summary> /// <param name="dev">The index of the new capture device.</param> /// <param name="frameRate">The framerate to use.</param> /// <param name="width">The width to use.</param> /// <param name="height">The height to use.</param> /// <returns>True, if successful, otherwise false.</returns> private bool SetupGraph(DsDevice dev, int frameRate, int width, int height) { int hr; fps = frameRate; // Not measured, only to expose FPS externally cameraControl = null; capFilter = null; // Get the graphbuilder object graphBuilder = (IFilterGraph2)new FilterGraph(); mediaControl = graphBuilder as IMediaControl; try { // Create the ICaptureGraphBuilder2 capGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); // Create the SampleGrabber interface sampGrabber = (ISampleGrabber)new SampleGrabber(); // Start building the graph hr = capGraph.SetFiltergraph(graphBuilder); //if (hr != 0) // ErrorLogger.WriteLine("Error in capGraph.SetFiltergraph. Could not build graph. Message: " + // DsError.GetErrorText(hr)); #if DEBUG this.rotEntry = new DsROTEntry(this.graphBuilder); #endif this.capFilter = CreateFilter( FilterCategory.VideoInputDevice, dev.Name); if (this.capFilter != null) { hr = graphBuilder.AddFilter(this.capFilter, "Video Source"); DsError.ThrowExceptionForHR(hr); } //// Add the video device //hr = graphBuilder.AddSourceFilterForMoniker(dev.Mon, null, "Video input", out capFilter); //if (hr != 0) // ErrorLogger.WriteLine( // "Error in m_graphBuilder.AddSourceFilterForMoniker(). Could not add source filter. Message: " + // DsError.GetErrorText(hr)); var baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); //if (hr != 0) // ErrorLogger.WriteLine("Error in m_graphBuilder.AddFilter(). Could not add filter. Message: " + // DsError.GetErrorText(hr)); // turn on the infrared leds ONLY FOR THE GENIUS WEBCAM /* if (!defaultMode) { m_icc = capFilter as IAMCameraControl; CameraControlFlags CamFlags = new CameraControlFlags(); int pMin, pMax, pStep, pDefault; hr = m_icc.GetRange(CameraControlProperty.Focus, out pMin, out pMax, out pStep, out pDefault, out CamFlags); m_icc.Set(CameraControlProperty.Focus, pMax, CameraControlFlags.None); } */ //IBaseFilter smartTee = new SmartTee() as IBaseFilter; //// Add the smart tee filter to the graph //hr = this.graphBuilder.AddFilter(smartTee, "Smart Tee"); //Marshal.ThrowExceptionForHR(hr); // Connect the video source output to the smart tee //hr = capGraph.RenderStream(null, null, capFilter, null, smartTee); hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, null, baseGrabFlt); var errorText = DsError.GetErrorText(hr); cameraControl = capFilter as IAMCameraControl; // Set videoProcAmp object obj; var iid_IBaseFilter = new Guid("56a86895-0ad4-11ce-b03a-0020af0ba770"); DirectShowDevices.Instance.Cameras[deviceNumber].DirectshowDevice.Mon.BindToObject( null, null, ref iid_IBaseFilter, out obj); videoProcAmp = obj as IAMVideoProcAmp; // If any of the default config items are set if (frameRate + height + width > 0) SetConfigParms(capGraph, capFilter, frameRate, width, height); // Check for successful rendering, if this failed the class cannot be used, so dispose the resources and return false. if (hr < 0) { Cleanup(); return false; } else { // Otherwise update the SampleGrabber. SaveSizeInfo(sampGrabber); hr = sampGrabber.SetBufferSamples(false); if (hr == 0) { hr = sampGrabber.SetOneShot(false); hr = sampGrabber.SetCallback(this, 1); } //if (hr < 0) // ErrorLogger.WriteLine("Could not set callback function (SetupGraph) in Camera.Capture()"); } } catch (Exception) { //ErrorLogger.ProcessException(ex, false); Cleanup(); return false; } return true; }
/// <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; } } }
public bool Play(string fileName, Form form) { _form = form; Log.WriteFile("play:{0}", fileName); _graphBuilder = (IFilterGraph2)new FilterGraph(); _rotEntry = new DsROTEntry(_graphBuilder); TsReader reader = new TsReader(); _tsReader = (IBaseFilter)reader; Log.Info("TSReaderPlayer:add TsReader to graph"); _graphBuilder.AddFilter(_tsReader, "TsReader"); #region load file in TsReader Log.WriteFile("load file in Ts"); IFileSourceFilter interfaceFile = (IFileSourceFilter)_tsReader; if (interfaceFile == null) { Log.WriteFile("TSReaderPlayer:Failed to get IFileSourceFilter"); return false; } int hr = interfaceFile.Load(fileName, null); if (hr != 0) { Log.WriteFile("TSReaderPlayer:Failed to load file"); return false; } #endregion #region render pin Log.Info("TSReaderPlayer:render TsReader outputs"); IEnumPins enumPins; _tsReader.EnumPins(out enumPins); IPin[] pins = new IPin[2]; int fetched; while (enumPins.Next(1, pins, out fetched) == 0) { if (fetched != 1) break; PinDirection direction; pins[0].QueryDirection(out direction); if (direction == PinDirection.Input) { Release.ComObject(pins[0]); continue; } _graphBuilder.Render(pins[0]); Release.ComObject(pins[0]); } Release.ComObject(enumPins); #endregion _videoWin = _graphBuilder as IVideoWindow; if (_videoWin != null) { _videoWin.put_Visible(OABool.True); _videoWin.put_Owner(form.Handle); _videoWin.put_WindowStyle( (WindowStyle)((int)WindowStyle.Child + (int)WindowStyle.ClipSiblings + (int)WindowStyle.ClipChildren)); _videoWin.put_MessageDrain(form.Handle); _videoWin.SetWindowPosition(form.ClientRectangle.X, form.ClientRectangle.Y, form.ClientRectangle.Width, form.ClientRectangle.Height); } Log.WriteFile("run graph"); _mediaCtrl = (IMediaControl)_graphBuilder; hr = _mediaCtrl.Run(); Log.WriteFile("TSReaderPlayer:running:{0:X}", hr); return true; }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, int iWidth, int iHeight, short iBPP) { ISampleGrabber sampGrabber = null; IPin pCaptureOut = null; IPin pRenderIn = null; // Get the graphbuilder object m_FilterGraph = (IFilterGraph2)new FilterGraph(); try { // add the video input device IBaseFilter capFilter; int hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); //if (iHeight + iWidth + iBPP > 0) //{ //SetConfigParms(pRaw, iWidth, iHeight, iBPP); //} pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; // Configure the sample grabber IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); // Add the sample grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ares Video Grabber"); DsError.ThrowExceptionForHR(hr); pRenderIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); // Connect the capture pin to the renderer hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn); DsError.ThrowExceptionForHR(hr); // Learn the video properties SaveSizeInfo(sampGrabber); // Start the graph IMediaControl mediaCtrl = (IMediaControl)m_FilterGraph; hr = mediaCtrl.Run(); DsError.ThrowExceptionForHR(hr); } finally { if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); } if (pCaptureOut != null) { Marshal.ReleaseComObject(pCaptureOut); } if (pRenderIn != null) { Marshal.ReleaseComObject(pRenderIn); } } }
public void PrepareCapture(int i_width, int i_height,float i_frame_rate) { const int BBP = 32; //既にキャプチャ中なら諦める。 if (this.m_graphi_active) { throw new Exception(); } //現在確保中のグラフインスタンスを全て削除 CleanupGraphiObjects(); int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin pSampleIn = null; //グラフビルダを作る。 this.m_FilterGraph = new FilterGraph() as IFilterGraph2; try { //フィルタグラフにキャプチャを追加して、capFilterにピンを受け取る。 hr = m_FilterGraph.AddSourceFilterForMoniker(this.m_dev.Mon, null, this.m_dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); //stillピンを探す m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0); //stillピンが無ければPreviewを探す。 if (m_pinStill == null) { m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0); } // Still haven't found one. Need to put a splitter in so we have // one stream to capture the bitmap from, and one to display. Ok, we // don't *have* to do it that way, but we are going to anyway. if (m_pinStill == null) { // There is no still pin m_VidControl = null; m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); }else{ // Get a control pointer (used in Click()) m_VidControl = capFilter as IAMVideoControl; m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); } if (i_height + i_width + BBP > 0) { SetConfigParms(m_pinStill, i_width, i_height,i_frame_rate, BBP); } // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; //sampGrabberの設定 IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); // Add the sample grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); if (m_VidControl == null) { // Connect the Still pin to the sample grabber hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); }else{ // Connect the Still pin to the sample grabber hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); } hr = sampGrabber.GetConnectedMediaType(this._capture_mediatype); DsError.ThrowExceptionForHR(hr); //ビデオフォーマット等の更新 upateVideoInfo(sampGrabber); } finally { if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (pSampleIn != null) { Marshal.ReleaseComObject(pSampleIn); pSampleIn = 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_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 }
protected virtual void AddRenderers(IFilterGraph2 graphBuilder) { int hr = 0; Guid iid = typeof(IBaseFilter).GUID; if (this.AudioRendererDevice != null) { try { this.audioRenderer = null; IBaseFilter tmp; object o; this.AudioRendererDevice.Mon.BindToObject(null, null, ref iid, out o); tmp = o as IBaseFilter; //Add the Video input device to the graph hr = graphBuilder.AddFilter(tmp, this.AudioRendererDevice.Name); if (hr >= 0) { // Got it ! this.audioRenderer = tmp; } else { // Try another... int hr1 = graphBuilder.RemoveFilter(tmp); Marshal.ReleaseComObject(tmp); //DsError.ThrowExceptionForHR(hr); } } catch { } } if (this.audioRenderer == null) { // Add default audio renderer this.audioRenderer = (IBaseFilter)new DSoundRender(); hr = graphBuilder.AddFilter(this.audioRenderer, "DirectSound Renderer"); ThrowExceptionForHR("Adding the DirectSound Renderer: ", hr); } // To see something if (useWPF) { // Get the SampleGrabber interface ISampleGrabber sampleGrabber = new SampleGrabber() as ISampleGrabber; this.videoRenderer = sampleGrabber as IBaseFilter; // Set the media type to Video AMMediaType media = new AMMediaType(); media.majorType = MediaType.Video; //media.subType = MediaSubType.YUY2; // RGB24; //media.formatType = FormatType.Null; //media.sampleSize = 1; //media.temporalCompression = false; //media.fixedSizeSamples = false; //media.unkPtr = IntPtr.Zero; //media.formatType = FormatType.None; //media.formatSize = 0; //media.formatPtr = IntPtr.Zero; media.subType = MediaSubType.RGB32; // RGB24; media.formatType = FormatType.VideoInfo; hr = sampleGrabber.SetMediaType(media); ThrowExceptionForHR("Setting the MediaType on the SampleGrabber: ", hr); DsUtils.FreeAMMediaType(media); // Configure the samplegrabber hr = sampleGrabber.SetCallback(this, 1); DsError.ThrowExceptionForHR(hr); // Add the frame grabber to the graph hr = graphBuilder.AddFilter(this.videoRenderer, "SampleGrabber"); ThrowExceptionForHR("Adding the SampleGrabber: ", hr); //hr = ConnectFilters(this.videoRenderer, nullRenderer); } else { try { this.videoRenderer = (IBaseFilter)new EnhancedVideoRenderer(); hr = graphBuilder.AddFilter(this.videoRenderer, "Enhanced Video Renderer"); ThrowExceptionForHR("Adding the EVR: ", hr); useEVR = true; } catch (Exception) { } if (!useEVR) { this.videoRenderer = (IBaseFilter)new VideoMixingRenderer9(); hr = graphBuilder.AddFilter(this.videoRenderer, "Video Mixing Renderer 9"); ThrowExceptionForHR("Adding the VMR9: ", hr); } } //IReferenceClock clock = this.audioRenderer as IReferenceClock; //if(clock != null) //{ // // Set the graph clock. // this.FilterGraph.SetDefaultSyncSource( // pGraph->QueryInterface(IID_IMediaFilter, (void**)&pMediaFilter); // pMediaFilter->SetSyncSource(pClock); // pClock->Release(); // pMediaFilter->Release(); //} }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, AMMediaType media) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; // Get the graphbuilder object m_FilterGraph = (IFilterGraph2) new FilterGraph(); m_mediaCtrl = m_FilterGraph as IMediaControl; try { // 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 device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, "Video input", out capFilter); DsError.ThrowExceptionForHR( hr ); // add video crossbar // thanks to Andrew Fernie - this is to get tv tuner cards working IAMCrossbar crossbar = null; object o; hr = capGraph.FindInterface(PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMCrossbar).GUID, out o); if (hr >= 0) { crossbar = (IAMCrossbar)o; int oPin, iPin; int ovLink, ivLink; ovLink = ivLink = 0; crossbar.get_PinCounts(out oPin, out iPin); int pIdxRel; PhysicalConnectorType tp; for (int i = 0; i < iPin; i++) { crossbar.get_CrossbarPinInfo(true, i, out pIdxRel, out tp); if (tp == PhysicalConnectorType.Video_Composite) ivLink = i; } for (int i = 0; i < oPin; i++) { crossbar.get_CrossbarPinInfo(false, i, out pIdxRel, out tp); if (tp == PhysicalConnectorType.Video_VideoDecoder) ovLink = i; } try { crossbar.Route(ovLink, ivLink); o = null; } catch { throw new Exception("Failed to get IAMCrossbar"); } } //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 ); SetConfigParms(capGraph, capFilter, media); hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, pAVIDecompressor, baseGrabFlt); if (hr < 0) { hr = capGraph.RenderStream(PinCategory.Capture, 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(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; } } }
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"); } }
private void SetupGraph(Control hWin) { if (theCaptureDevice == null) return; 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; try { // Add the video input device to the graph hr = m_FilterGraph.AddFilter(theCaptureDevice, "source filter"); DsError.ThrowExceptionForHR(hr); if (captureDeviceCrossbar != null) { // Add the video inputs' crossbar to the graph hr = m_FilterGraph.AddFilter(captureDeviceCrossbar, "source crossbar"); DsError.ThrowExceptionForHR(hr); //// Get crossbars's output IPin iPinOutCrossbar = DsFindPin.ByDirection(captureDeviceCrossbar, PinDirection.Output, 0); m_FilterGraph.Render(iPinOutCrossbar); } // Get input device's output iPinOutSource = DsFindPin.ByDirection(theCaptureDevice, PinDirection.Output, 0); 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(iPinOutSource, iPinInDest); DsError.ThrowExceptionForHR(hr); 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 (theCaptureDevice != null) { Marshal.ReleaseComObject(theCaptureDevice); theCaptureDevice = null; } 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; } } }