private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight)
        {
            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            ICaptureGraphBuilder2 capGraph = null;

            graphBuilder = (IFilterGraph2)new FilterGraph();
            mediaCtrl = graphBuilder as IMediaControl;
            try
            {
                capGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
                sampGrabber = (ISampleGrabber)new SampleGrabber();
                var hr = capGraph.SetFiltergraph(graphBuilder);
                DsError.ThrowExceptionForHR(hr);
                hr = graphBuilder.AddSourceFilterForMoniker(dev.Moniker, null, "Video Capture Device", out capFilter);
                DsError.ThrowExceptionForHR(hr);
                var baseGrabFlt = (IBaseFilter)sampGrabber;
                ConfigureSampleGrabber(sampGrabber);
                hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                DsError.ThrowExceptionForHR(hr);
                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);

                if (sampGrabber != null)
                    Marshal.ReleaseComObject(sampGrabber);

                if (capGraph != null)
                    Marshal.ReleaseComObject(capGraph);
            }
        }
示例#2
0
        public static DsDevice[] GetDevicesOfCat(Guid FilterCategory)
        {
            DsDevice[] devret;
            var devs = new ArrayList();
            IEnumMoniker enumMon;
            var enumDev = (ICreateDevEnum)new CreateDevEnum();
            var hr = enumDev.CreateClassEnumerator(FilterCategory, out enumMon, 0);
            DsError.ThrowExceptionForHR(hr);

            if (hr != 1)
            {
                try
                {
                    try
                    {
                        var mon = new IMoniker[1];
                        while ((enumMon.Next(1, mon, IntPtr.Zero) == 0))
                        {
                            try
                            {
                                devs.Add(new DsDevice(mon[0]));
                            }
                            catch
                            {
                                Marshal.ReleaseComObject(mon[0]);
                                throw;
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(enumMon);
                    }

                    devret = new DsDevice[devs.Count];
                    devs.CopyTo(devret);
                }
                catch
                {
                    foreach (DsDevice d in devs)
                    {
                        d.Dispose();
                    }
                    throw;
                }
            }
            else
            {
                devret = new DsDevice[0];
            }

            return devret;
        }