コード例 #1
0
        public void BuildGraph()
        {
            ICaptureGraphBuilder2   captureGraphBuilder2 = (ICaptureGraphBuilder2)null;
            IBaseFilter             ppFilter             = (IBaseFilter)null;
            ISampleGrabber          sampleGrabber        = (ISampleGrabber)null;
            List <DeviceEnumerator> deviceEnumeratorList = (List <DeviceEnumerator>)null;

            try
            {
                Logger.Info("Creating List of devices");
                deviceEnumeratorList = DeviceEnumerator.ListDevices(Guids.VideoInputDeviceCategory);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception in finding Video device. Err : {0}", (object)ex.ToString());
            }
            if (deviceEnumeratorList != null)
            {
                if (deviceEnumeratorList.Count != 0)
                {
                    try
                    {
                        Logger.Info("found {0} Camera, Opening {1}", (object)deviceEnumeratorList.Count, (object)this.m_Unit);
                        DeviceEnumerator deviceEnumerator = this.m_Unit >= deviceEnumeratorList.Count ? deviceEnumeratorList[0] : deviceEnumeratorList[this.m_Unit];
                        this.m_FilterGraph   = (IFilterGraph2) new FilterGraph();
                        this.m_mediaCtrl     = this.m_FilterGraph as IMediaControl;
                        captureGraphBuilder2 = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
                        sampleGrabber        = (ISampleGrabber) new SampleGrabber();
                        ErrorHandler errorHandler1 = (ErrorHandler)captureGraphBuilder2.SetFiltergraph((IGraphBuilder)this.m_FilterGraph);
                        if (errorHandler1.GetError() != 0)
                        {
                            Logger.Error("SetFiltergraph failed with {0:X}..", (object)errorHandler1.GetError());
                        }
                        ErrorHandler errorHandler2 = (ErrorHandler)this.m_FilterGraph.AddSourceFilterForMoniker(deviceEnumerator.Moniker, (IBindCtx)null, "Video input", out ppFilter);
                        if (errorHandler2.GetError() != 0)
                        {
                            Logger.Error("AddSourceFilterForMoniker failed with {0:X}", (object)errorHandler2.GetError());
                        }
                        AMMediaType pmt = new AMMediaType()
                        {
                            majorType = Guids.MediaTypeVideo
                        };
                        if (this.m_color == SupportedColorFormat.YUV2)
                        {
                            pmt.subType = Guids.MediaSubtypeYUY2;
                        }
                        else
                        {
                            if (this.m_color != SupportedColorFormat.RGB24)
                            {
                                throw new Exception("Unsupported color format");
                            }
                            pmt.subType = Guids.MediaSubtypeRGB24;
                        }
                        pmt.formatType = Guids.FormatTypesVideoInfo;
                        ErrorHandler errorHandler3 = (ErrorHandler)sampleGrabber.SetMediaType(pmt);
                        this.FreeAMMedia(pmt);
                        ErrorHandler errorHandler4 = (ErrorHandler)sampleGrabber.SetCallback((ISampleGrabberCB)this, 1);
                        if (errorHandler4.GetError() != 0)
                        {
                            Logger.Error("Grabber setcallback failed with {0:X}", (object)errorHandler4.GetError());
                        }
                        IBaseFilter  baseFilter    = (IBaseFilter)sampleGrabber;
                        ErrorHandler errorHandler5 = (ErrorHandler)this.m_FilterGraph.AddFilter(baseFilter, "FrameGrabber");
                        if (errorHandler5.GetError() != 0)
                        {
                            Logger.Error("AddFilter failed with {0:X}", (object)errorHandler5.GetError());
                        }
                        object       ppint;
                        ErrorHandler errorHandler6 = (ErrorHandler)captureGraphBuilder2.FindInterface(Guids.PinCategoryCapture, Guids.MediaTypeVideo, ppFilter, typeof(IAMStreamConfig).GUID, out ppint);
                        if (errorHandler6.GetError() != 0)
                        {
                            Logger.Error("FindInterface failed with {0:X}", (object)errorHandler6.GetError());
                        }
                        if (!(ppint is IAMStreamConfig amStreamConfig))
                        {
                            throw new Exception("Stream config Error");
                        }
                        errorHandler3 = (ErrorHandler)amStreamConfig.GetFormat(out pmt);
                        VideoInfoHeader videoInfoHeader = new VideoInfoHeader();
                        Marshal.PtrToStructure(pmt.pbFormat, (object)videoInfoHeader);
                        videoInfoHeader.AvgTimePerFrame  = (long)(10000000 / this.m_FrameRate);
                        videoInfoHeader.BmiHeader.Width  = this.m_Width;
                        videoInfoHeader.BmiHeader.Height = this.m_Height;
                        Marshal.StructureToPtr((object)videoInfoHeader, pmt.pbFormat, false);
                        ErrorHandler errorHandler7 = (ErrorHandler)amStreamConfig.SetFormat(pmt);
                        if (errorHandler7.GetError() != 0)
                        {
                            Logger.Error("conf.setformat failed with {0:X}", (object)errorHandler7.GetError());
                        }
                        this.FreeAMMedia(pmt);
                        ErrorHandler errorHandler8 = (ErrorHandler)captureGraphBuilder2.RenderStream(Guids.PinCategoryCapture, Guids.MediaTypeVideo, (object)ppFilter, (IBaseFilter)null, baseFilter);
                        if (errorHandler8.GetError() != 0)
                        {
                            Logger.Error("RenderStream failed with {0:X}", (object)errorHandler8.GetError());
                        }
                        AMMediaType amMediaType = new AMMediaType();
                        errorHandler3 = (ErrorHandler)sampleGrabber.GetConnectedMediaType(amMediaType);
                        if (amMediaType.formatType != Guids.FormatTypesVideoInfo)
                        {
                            throw new ColorFormatNotSupported("Not able to connect to Video Media");
                        }
                        if (amMediaType.pbFormat == IntPtr.Zero)
                        {
                            throw new Exception("Format Array is null");
                        }
                        VideoInfoHeader structure = (VideoInfoHeader)Marshal.PtrToStructure(amMediaType.pbFormat, typeof(VideoInfoHeader));
                        this.m_Width  = structure.BmiHeader.Width;
                        this.m_Height = structure.BmiHeader.Height;
                        this.m_Stride = this.m_Width * ((int)structure.BmiHeader.BitCount / 8);
                        if (this.m_Buffer == IntPtr.Zero)
                        {
                            this.m_Buffer = Marshal.AllocCoTaskMem(this.m_Stride * this.m_Height);
                        }
                        this.FreeAMMedia(amMediaType);
                        return;
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        if (ppFilter != null)
                        {
                            Marshal.ReleaseComObject((object)ppFilter);
                        }
                        if (sampleGrabber != null)
                        {
                            Marshal.ReleaseComObject((object)sampleGrabber);
                        }
                        if (captureGraphBuilder2 != null)
                        {
                            Marshal.ReleaseComObject((object)captureGraphBuilder2);
                        }
                    }
                }
            }
            Logger.Info("CAMERA: Could not find a camera device!");
        }
コード例 #2
0
        public static List <DeviceEnumerator> ListDevices(Guid filterType)
        {
            List <DeviceEnumerator> deviceEnumeratorList = new List <DeviceEnumerator>();
            IEnumMoniker            ppEnumMoniker;
            ErrorHandler            classEnumerator = (ErrorHandler)((ICreateDevEnum) new CreateDevEnum()).CreateClassEnumerator(filterType, out ppEnumMoniker, 0);

            if (ppEnumMoniker != null)
            {
                try
                {
                    IMoniker[] rgelt = (IMoniker[])null;
                    try
                    {
                        while (true)
                        {
                            DeviceEnumerator deviceEnumerator;
                            string           property;
                            do
                            {
                                rgelt = new IMoniker[1];
                                if (ppEnumMoniker.Next(1, rgelt, IntPtr.Zero) != 0)
                                {
                                    Logger.Info("Breaking out of loop..");
                                    goto label_11;
                                }
                                else
                                {
                                    deviceEnumerator = new DeviceEnumerator()
                                    {
                                        m_Moniker = rgelt[0]
                                    };
                                    deviceEnumerator.m_FriendlyName = deviceEnumerator.getProperty("FriendlyName");
                                    property = deviceEnumerator.getProperty("DevicePath");
                                }
                            }while (deviceEnumerator.m_FriendlyName == null || property == null || !property.Contains("\\usb#vid") && !property.Contains("\\pci#ven"));
                            deviceEnumeratorList.Add(deviceEnumerator);
                            Logger.Info("Camera device {0}", (object)deviceEnumerator.m_FriendlyName);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (rgelt != null)
                        {
                            Marshal.ReleaseComObject((object)rgelt[0]);
                        }
                        deviceEnumeratorList = (List <DeviceEnumerator>)null;
                        Logger.Error("Failed to enumerate Video input devices: {0}", (object)ex.ToString());
                        throw;
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject((object)ppEnumMoniker);
                }
            }
            else
            {
                Logger.Error("Cannot enumerate the device");
                deviceEnumeratorList = (List <DeviceEnumerator>)null;
            }
label_11:
            return(deviceEnumeratorList);
        }