Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement