Advertisement
LoonerSF

setupgraph method

Aug 24th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.91 KB | None | 0 0
  1.         private void SetupGraph(DsDevice dev, int iWidth, int iHeight, short iBPP, Control hControl)
  2.         {
  3.             int hr;
  4.  
  5.             ISampleGrabber sampGrabber = null;
  6.             IBaseFilter capFilter = null;
  7.             IPin pCaptureOut = null;
  8.             IPin pSampleIn = null;
  9.             IPin pRenderIn = null;
  10.  
  11.             // Get the graphbuilder object
  12.             m_FilterGraph = new FilterGraph() as IFilterGraph2;
  13.  
  14.             try
  15.             {
  16. #if DEBUG
  17.                 m_rot = new DsROTEntry(m_FilterGraph);
  18. #endif
  19.                 // add the video input device
  20.                 hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
  21.                 DsError.ThrowExceptionForHR(hr);
  22.  
  23.                 // Find the still pin
  24.                 m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0);
  25.  
  26.                 // Didn't find one.  Is there a preview pin?
  27.                 if (m_pinStill == null)
  28.                 {
  29.                     m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0);
  30.                 }
  31.  
  32.                 // Still haven't found one.  Need to put a splitter in so we have
  33.                 // one stream to capture the bitmap from, and one to display.  Ok, we
  34.                 // don't *have* to do it that way, but we are going to anyway.
  35.                 if (m_pinStill == null)
  36.                 {
  37.                     IPin pRaw = null;
  38.                     IPin pSmart = null;
  39.  
  40.                     // There is no still pin
  41.                     m_VidControl = null;
  42.  
  43.                     // Add a splitter
  44.                     IBaseFilter iSmartTee = (IBaseFilter)new SmartTee();
  45.  
  46.                     try
  47.                     {
  48.                         hr = m_FilterGraph.AddFilter(iSmartTee, "SmartTee");
  49.                         DsError.ThrowExceptionForHR(hr);
  50.  
  51.                         // Find the find the capture pin from the video device and the
  52.                         // input pin for the splitter, and connnect them
  53.                         pRaw = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
  54.                         pSmart = DsFindPin.ByDirection(iSmartTee, PinDirection.Input, 0);
  55.  
  56.                         hr = m_FilterGraph.Connect(pRaw, pSmart);
  57.                         DsError.ThrowExceptionForHR(hr);
  58.  
  59.                         // Now set the capture and still pins (from the splitter)
  60.                         m_pinStill = DsFindPin.ByName(iSmartTee, "Preview");
  61.                         pCaptureOut = DsFindPin.ByName(iSmartTee, "Capture");
  62.  
  63.                         // If any of the default config items are set, perform the config
  64.                         // on the actual video device (rather than the splitter)
  65.                         if (iHeight + iWidth + iBPP > 0)
  66.                         {
  67.                             SetConfigParms(pRaw, iWidth, iHeight, iBPP);
  68.                         }
  69.                     }
  70.                     finally
  71.                     {
  72.                         if (pRaw != null)
  73.                         {
  74.                             Marshal.ReleaseComObject(pRaw);
  75.                         }
  76.                         if (pRaw != pSmart)
  77.                         {
  78.                             Marshal.ReleaseComObject(pSmart);
  79.                         }
  80.                         if (pRaw != iSmartTee)
  81.                         {
  82.                             Marshal.ReleaseComObject(iSmartTee);
  83.                         }
  84.                     }
  85.                 }
  86.                 else
  87.                 {
  88.                     // Get a control pointer (used in Click())
  89.                     m_VidControl = capFilter as IAMVideoControl;
  90.  
  91.                     pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
  92.  
  93.                     // If any of the default config items are set
  94.                     if (iHeight + iWidth + iBPP > 0)
  95.                     {
  96.                         SetConfigParms(m_pinStill, iWidth, iHeight, iBPP);
  97.                     }
  98.                 }
  99.  
  100.                 // Get the SampleGrabber interface
  101.                 sampGrabber = new SampleGrabber() as ISampleGrabber;
  102.  
  103.                 // Configure the sample grabber
  104.                 IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
  105.                 ConfigureSampleGrabber(sampGrabber);
  106.                 pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);
  107.  
  108.                 // Get the default video renderer
  109.                 IBaseFilter pRenderer = new VideoRendererDefault() as IBaseFilter;
  110.                 hr = m_FilterGraph.AddFilter(pRenderer, "Renderer");
  111.                 DsError.ThrowExceptionForHR(hr);
  112.  
  113.                 pRenderIn = DsFindPin.ByDirection(pRenderer, PinDirection.Input, 0);
  114.  
  115.                 // Add the sample grabber to the graph
  116.                 hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
  117.                 DsError.ThrowExceptionForHR(hr);
  118.  
  119.                 if (m_VidControl == null)
  120.                 {
  121.                     // Connect the Still pin to the sample grabber
  122.                     hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
  123.                     DsError.ThrowExceptionForHR(hr);
  124.  
  125.                     // Connect the capture pin to the renderer
  126.                     hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn);
  127.                     DsError.ThrowExceptionForHR(hr);
  128.                 }
  129.                 else
  130.                 {
  131.                     // Connect the capture pin to the renderer
  132.                     hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn);
  133.                     DsError.ThrowExceptionForHR(hr);
  134.  
  135.                     // Connect the Still pin to the sample grabber
  136.                     hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
  137.                     DsError.ThrowExceptionForHR(hr);
  138.                 }
  139.  
  140.                 // Learn the video properties
  141.                 SaveSizeInfo(sampGrabber);
  142.                 ConfigVideoWindow(hControl);
  143.  
  144.                 // Start the graph
  145.                 IMediaControl mediaCtrl = m_FilterGraph as IMediaControl;
  146.                 hr = mediaCtrl.Run();
  147.                 DsError.ThrowExceptionForHR(hr);
  148.             }
  149.             finally
  150.             {
  151.                 if (sampGrabber != null)
  152.                 {
  153.                     Marshal.ReleaseComObject(sampGrabber);
  154.                     sampGrabber = null;
  155.                 }
  156.                 if (pCaptureOut != null)
  157.                 {
  158.                     Marshal.ReleaseComObject(pCaptureOut);
  159.                     pCaptureOut = null;
  160.                 }
  161.                 if (pRenderIn != null)
  162.                 {
  163.                     Marshal.ReleaseComObject(pRenderIn);
  164.                     pRenderIn = null;
  165.                 }
  166.                 if (pSampleIn != null)
  167.                 {
  168.                     Marshal.ReleaseComObject(pSampleIn);
  169.                     pSampleIn = null;
  170.                 }
  171.             }
  172.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement