CreateFilter() публичный статический Метод

Create an instance of the filter.
The returned filter's object should be released using Marshal.ReleaseComObject().
public static CreateFilter ( string filterMoniker ) : object
filterMoniker string Filter's moniker string.
Результат object
Пример #1
0
        public bool GetCameraPropertyRange(CameraControlProperty property, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out CameraControlFlags controlFlags)
        {
            bool flag = true;

            if (deviceMoniker == null || string.IsNullOrEmpty(deviceMoniker))
            {
                throw new ArgumentException("Video source is not specified.");
            }
            lock (sync)
            {
                object obj = null;
                try
                {
                    obj = FilterInfo.CreateFilter(deviceMoniker);
                }
                catch
                {
                    throw new ApplicationException("Failed creating device object for moniker.");
                }
                if (!(obj is IAMCameraControl))
                {
                    throw new NotSupportedException("The video source does not support camera control.");
                }
                IAMCameraControl iAMCameraControl = (IAMCameraControl)obj;
                int range = iAMCameraControl.GetRange(property, out minValue, out maxValue, out stepSize, out defaultValue, out controlFlags);
                flag = (range >= 0);
                Marshal.ReleaseComObject(obj);
                return(flag);
            }
        }
Пример #2
0
 public void DisplayPropertyPage(IntPtr parentWindow)
 {
     if (deviceMoniker == null || deviceMoniker == string.Empty)
     {
         throw new ArgumentException("Video source is not specified.");
     }
     lock (sync)
     {
         if (IsRunning)
         {
             parentWindowForPropertyPage = parentWindow;
             needToDisplayPropertyPage   = true;
         }
         else
         {
             object obj = null;
             try
             {
                 obj = FilterInfo.CreateFilter(deviceMoniker);
             }
             catch
             {
                 throw new ApplicationException("Failed creating device object for moniker.");
             }
             if (!(obj is ISpecifyPropertyPages))
             {
                 throw new NotSupportedException("The video source does not support configuration property page.");
             }
             DisplayPropertyPage(parentWindow, obj);
             Marshal.ReleaseComObject(obj);
         }
     }
 }
Пример #3
0
        /// <summary>
        /// Display property window for the video capture device providing its configuration
        /// capabilities.
        /// </summary>
        ///
        /// <param name="parentWindow">Handle of parent window.</param>
        ///
        /// <remarks><para><note>If you pass parent window's handle to this method, then the
        /// displayed property page will become modal window and none of the controls from the
        /// parent window will be accessible. In order to make it modeless it is required
        /// to pass <see cref="IntPtr.Zero"/> as parent window's handle.
        /// </note></para>
        /// </remarks>
        ///
        /// <exception cref="NotSupportedException">The video source does not support configuration property page.</exception>
        ///
        public void DisplayPropertyPage(IntPtr parentWindow)
        {
            // check source
            if ((deviceMoniker == null) || (deviceMoniker == string.Empty))
            {
                throw new ArgumentException("Video source is not specified");
            }

            lock (this)
            {
                if (IsRunning)
                {
                    // pass the request to backgroud thread if video source is running
                    parentWindowForPropertyPage = parentWindow;
                    needToDisplayPropertyPage   = true;
                    return;
                }

                object tempSourceObject = null;

                // create source device's object
                try
                {
                    tempSourceObject = FilterInfo.CreateFilter(deviceMoniker);
                }
                catch
                {
                    throw new ApplicationException("Failed creating device object for moniker.");
                }

                if (!(tempSourceObject is ISpecifyPropertyPages))
                {
                    throw new NotSupportedException("The video source does not support configuration property page.");
                }

                // retrieve ISpecifyPropertyPages interface of the device
                ISpecifyPropertyPages pPropPages = (ISpecifyPropertyPages)tempSourceObject;

                // get property pages from the property bag
                CAUUID caGUID;
                pPropPages.GetPages(out caGUID);

                // get filter info
                FilterInfo filterInfo = new FilterInfo(deviceMoniker);

                // create and display the OlePropertyFrame form
                Win32.OleCreatePropertyFrame(parentWindow, 0, 0, filterInfo.Name, 1, ref tempSourceObject, caGUID.cElems, caGUID.pElems, 0, 0, IntPtr.Zero);

                // release COM objects
                Marshal.FreeCoTaskMem(caGUID.pElems);
                Marshal.ReleaseComObject(tempSourceObject);
            }
        }
Пример #4
0
        /// <summary>
        /// Gets the range and default value of a specified camera property.
        /// </summary>
        ///
        /// <param name="property">Specifies the property to query.</param>
        /// <param name="minValue">Receives the minimum value of the property.</param>
        /// <param name="maxValue">Receives the maximum value of the property.</param>
        /// <param name="stepSize">Receives the step size for the property.</param>
        /// <param name="defaultValue">Receives the default value of the property.</param>
        /// <param name="controlFlags">Receives a member of the <see cref="CameraControlFlags"/> enumeration, indicating whether the property is controlled automatically or manually.</param>
        ///
        /// <returns>Returns true on success or false otherwise.</returns>
        ///
        /// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception>
        /// <exception cref="ApplicationException">Failed creating device object for moniker.</exception>
        /// <exception cref="NotSupportedException">The video source does not support camera control.</exception>
        ///
        public bool GetVideoPropertyRange(VideoProcAmpProperty property, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out VideoProcAmpFlags controlFlags)
        {
            bool ret = true;

            // check if source was set
            if ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker)))
            {
                throw new ArgumentException("Video source is not specified.");
            }

            lock (sync)
            {
                object tempSourceObject = null;

                // create source device's object
                try
                {
                    tempSourceObject = FilterInfo.CreateFilter(deviceMoniker);
                }
                catch
                {
                    throw new ApplicationException("Failed creating device object for moniker.");
                }

                if (!(tempSourceObject is IAMVideoProcAmp))
                {
                    throw new NotSupportedException("The video source does not support camera control.");
                }

                IAMVideoProcAmp pCamControl = (IAMVideoProcAmp)tempSourceObject;
                int             hr          = pCamControl.GetRange(property, out minValue, out maxValue, out stepSize, out defaultValue, out controlFlags);

                ret = (hr >= 0);

                Marshal.ReleaseComObject(tempSourceObject);
            }

            return(ret);
        }
Пример #5
0
        private void WorkerThread(bool runGraph)
        {
            ReasonToFinishPlaying reason = ReasonToFinishPlaying.StoppedByUser;
            bool    flag         = false;
            Grabber grabber      = new Grabber(this, snapshotMode: false);
            Grabber grabber2     = new Grabber(this, snapshotMode: true);
            object  obj          = null;
            object  obj2         = null;
            object  obj3         = null;
            object  obj4         = null;
            object  retInterface = null;
            ICaptureGraphBuilder2 captureGraphBuilder = null;
            IFilterGraph2         filterGraph         = null;
            IBaseFilter           baseFilter          = null;
            IBaseFilter           baseFilter2         = null;
            IBaseFilter           baseFilter3         = null;
            ISampleGrabber        sampleGrabber       = null;
            ISampleGrabber        sampleGrabber2      = null;
            IMediaControl         mediaControl        = null;
            IAMVideoControl       iAMVideoControl     = null;
            IMediaEventEx         mediaEventEx        = null;
            IPin        pin         = null;
            IAMCrossbar iAMCrossbar = null;

            try
            {
                Type typeFromCLSID = Type.GetTypeFromCLSID(Clsid.CaptureGraphBuilder2);
                if (typeFromCLSID == null)
                {
                    throw new ApplicationException("Failed creating capture graph builder");
                }
                obj = Activator.CreateInstance(typeFromCLSID);
                captureGraphBuilder = (ICaptureGraphBuilder2)obj;
                typeFromCLSID       = Type.GetTypeFromCLSID(Clsid.FilterGraph);
                if (typeFromCLSID == null)
                {
                    throw new ApplicationException("Failed creating filter graph");
                }
                obj2        = Activator.CreateInstance(typeFromCLSID);
                filterGraph = (IFilterGraph2)obj2;
                captureGraphBuilder.SetFiltergraph((IGraphBuilder)filterGraph);
                sourceObject = FilterInfo.CreateFilter(deviceMoniker);
                if (sourceObject == null)
                {
                    throw new ApplicationException("Failed creating device object for moniker");
                }
                baseFilter = (IBaseFilter)sourceObject;
                try
                {
                    iAMVideoControl = (IAMVideoControl)sourceObject;
                }
                catch
                {
                }
                typeFromCLSID = Type.GetTypeFromCLSID(Clsid.SampleGrabber);
                if (typeFromCLSID == null)
                {
                    throw new ApplicationException("Failed creating sample grabber");
                }
                obj3           = Activator.CreateInstance(typeFromCLSID);
                sampleGrabber  = (ISampleGrabber)obj3;
                baseFilter2    = (IBaseFilter)obj3;
                obj4           = Activator.CreateInstance(typeFromCLSID);
                sampleGrabber2 = (ISampleGrabber)obj4;
                baseFilter3    = (IBaseFilter)obj4;
                filterGraph.AddFilter(baseFilter, "source");
                filterGraph.AddFilter(baseFilter2, "grabber_video");
                filterGraph.AddFilter(baseFilter3, "grabber_snapshot");
                AMMediaType aMMediaType = new AMMediaType();
                aMMediaType.MajorType = MediaType.Video;
                aMMediaType.SubType   = MediaSubType.RGB24;
                sampleGrabber.SetMediaType(aMMediaType);
                sampleGrabber2.SetMediaType(aMMediaType);
                captureGraphBuilder.FindInterface(FindDirection.UpstreamOnly, Guid.Empty, baseFilter, typeof(IAMCrossbar).GUID, out retInterface);
                if (retInterface != null)
                {
                    iAMCrossbar = (IAMCrossbar)retInterface;
                }
                isCrossbarAvailable = (iAMCrossbar != null);
                crossbarVideoInputs = ColletCrossbarVideoInputs(iAMCrossbar);
                if (iAMVideoControl != null)
                {
                    captureGraphBuilder.FindPin(sourceObject, PinDirection.Output, PinCategory.StillImage, MediaType.Video, unconnected: false, 0, out pin);
                    if (pin != null)
                    {
                        iAMVideoControl.GetCaps(pin, out VideoControlFlags flags);
                        flag = ((flags & VideoControlFlags.ExternalTriggerEnable) != 0);
                    }
                }
                sampleGrabber.SetBufferSamples(bufferThem: false);
                sampleGrabber.SetOneShot(oneShot: false);
                sampleGrabber.SetCallback(grabber, 1);
                sampleGrabber2.SetBufferSamples(bufferThem: true);
                sampleGrabber2.SetOneShot(oneShot: false);
                sampleGrabber2.SetCallback(grabber2, 1);
                GetPinCapabilitiesAndConfigureSizeAndRate(captureGraphBuilder, baseFilter, PinCategory.Capture, videoResolution, ref videoCapabilities);
                if (flag)
                {
                    GetPinCapabilitiesAndConfigureSizeAndRate(captureGraphBuilder, baseFilter, PinCategory.StillImage, snapshotResolution, ref snapshotCapabilities);
                }
                else
                {
                    snapshotCapabilities = new VideoCapabilities[0];
                }
                lock (cacheVideoCapabilities)
                {
                    if (videoCapabilities != null && !cacheVideoCapabilities.ContainsKey(deviceMoniker))
                    {
                        cacheVideoCapabilities.Add(deviceMoniker, videoCapabilities);
                    }
                }
                lock (cacheSnapshotCapabilities)
                {
                    if (snapshotCapabilities != null && !cacheSnapshotCapabilities.ContainsKey(deviceMoniker))
                    {
                        cacheSnapshotCapabilities.Add(deviceMoniker, snapshotCapabilities);
                    }
                }
                if (runGraph)
                {
                    captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, baseFilter, null, baseFilter2);
                    if (sampleGrabber.GetConnectedMediaType(aMMediaType) == 0)
                    {
                        VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(aMMediaType.FormatPtr, typeof(VideoInfoHeader));
                        grabber.Width  = videoInfoHeader.BmiHeader.Width;
                        grabber.Height = videoInfoHeader.BmiHeader.Height;
                        aMMediaType.Dispose();
                    }
                    if (flag && provideSnapshots)
                    {
                        captureGraphBuilder.RenderStream(PinCategory.StillImage, MediaType.Video, baseFilter, null, baseFilter3);
                        if (sampleGrabber2.GetConnectedMediaType(aMMediaType) == 0)
                        {
                            VideoInfoHeader videoInfoHeader2 = (VideoInfoHeader)Marshal.PtrToStructure(aMMediaType.FormatPtr, typeof(VideoInfoHeader));
                            grabber2.Width  = videoInfoHeader2.BmiHeader.Width;
                            grabber2.Height = videoInfoHeader2.BmiHeader.Height;
                            aMMediaType.Dispose();
                        }
                    }
                    mediaControl = (IMediaControl)obj2;
                    mediaEventEx = (IMediaEventEx)obj2;
                    mediaControl.Run();
                    if (flag && provideSnapshots)
                    {
                        startTime = DateTime.Now;
                        iAMVideoControl.SetMode(pin, VideoControlFlags.ExternalTriggerEnable);
                    }
                    do
                    {
                        if (mediaEventEx != null && mediaEventEx.GetEvent(out DsEvCode lEventCode, out IntPtr lParam, out IntPtr lParam2, 0) >= 0)
                        {
                            mediaEventEx.FreeEventParams(lEventCode, lParam, lParam2);
                            if (lEventCode == DsEvCode.DeviceLost)
                            {
                                reason = ReasonToFinishPlaying.DeviceLost;
                                break;
                            }
                        }
                        if (needToSetVideoInput)
                        {
                            needToSetVideoInput = false;
                            if (isCrossbarAvailable.Value)
                            {
                                SetCurrentCrossbarInput(iAMCrossbar, crossbarVideoInput);
                                crossbarVideoInput = GetCurrentCrossbarInput(iAMCrossbar);
                            }
                        }
                        if (needToSimulateTrigger)
                        {
                            needToSimulateTrigger = false;
                            if (flag && provideSnapshots)
                            {
                                iAMVideoControl.SetMode(pin, VideoControlFlags.Trigger);
                            }
                        }
                        if (needToDisplayPropertyPage)
                        {
                            needToDisplayPropertyPage = false;
                            DisplayPropertyPage(parentWindowForPropertyPage, sourceObject);
                            if (iAMCrossbar != null)
                            {
                                crossbarVideoInput = GetCurrentCrossbarInput(iAMCrossbar);
                            }
                        }
                        if (needToDisplayCrossBarPropertyPage)
                        {
                            needToDisplayCrossBarPropertyPage = false;
                            if (iAMCrossbar != null)
                            {
                                DisplayPropertyPage(parentWindowForPropertyPage, iAMCrossbar);
                                crossbarVideoInput = GetCurrentCrossbarInput(iAMCrossbar);
                            }
                        }
                    }while (!stopEvent.WaitOne(100, exitContext: false));
                    mediaControl.Stop();
                }
            }
            catch (Exception ex)
            {
                if (this.VideoSourceError != null)
                {
                    this.VideoSourceError(this, new VideoSourceErrorEventArgs(ex.Message));
                }
            }
            finally
            {
                captureGraphBuilder = null;
                filterGraph         = null;
                baseFilter          = null;
                mediaControl        = null;
                iAMVideoControl     = null;
                mediaEventEx        = null;
                pin            = null;
                iAMCrossbar    = null;
                baseFilter2    = null;
                baseFilter3    = null;
                sampleGrabber  = null;
                sampleGrabber2 = null;
                if (obj2 != null)
                {
                    Marshal.ReleaseComObject(obj2);
                    obj2 = null;
                }
                if (sourceObject != null)
                {
                    Marshal.ReleaseComObject(sourceObject);
                    sourceObject = null;
                }
                if (obj3 != null)
                {
                    Marshal.ReleaseComObject(obj3);
                    obj3 = null;
                }
                if (obj4 != null)
                {
                    Marshal.ReleaseComObject(obj4);
                    obj4 = null;
                }
                if (obj != null)
                {
                    Marshal.ReleaseComObject(obj);
                    obj = null;
                }
                if (retInterface != null)
                {
                    Marshal.ReleaseComObject(retInterface);
                    retInterface = null;
                }
            }
            if (this.PlayingFinished != null)
            {
                this.PlayingFinished(this, reason);
            }
        }
Пример #6
0
        private void WorkerThread(bool runGraph)
        {
            // grabber
            Grabber grabber = new Grabber(this);

            // objects
            object captureGraphObject = null;
            object graphObject        = null;
            object grabberObject      = null;

            // interfaces
            ICaptureGraphBuilder2 captureGraph  = null;
            IFilterGraph2         graph         = null;
            IBaseFilter           sourceBase    = null;
            IBaseFilter           grabberBase   = null;
            ISampleGrabber        sampleGrabber = null;
            IMediaControl         mediaControl  = null;

            try
            {
                // get type of capture graph builder
                Type type = Type.GetTypeFromCLSID(Clsid.CaptureGraphBuilder2);
                if (type == null)
                {
                    throw new ApplicationException("Failed creating capture graph builder");
                }

                // create capture graph builder
                captureGraphObject = Activator.CreateInstance(type);
                captureGraph       = (ICaptureGraphBuilder2)captureGraphObject;

                // get type of filter graph
                type = Type.GetTypeFromCLSID(Clsid.FilterGraph);
                if (type == null)
                {
                    throw new ApplicationException("Failed creating filter graph");
                }

                // create filter graph
                graphObject = Activator.CreateInstance(type);
                graph       = (IFilterGraph2)graphObject;

                // set filter graph to the capture graph builder
                captureGraph.SetFiltergraph((IGraphBuilder)graph);

                // create source device's object
                sourceObject = FilterInfo.CreateFilter(deviceMoniker);
                if (sourceObject == null)
                {
                    throw new ApplicationException("Failed creating device object for moniker");
                }

                // get base filter interface of source device
                sourceBase = (IBaseFilter)sourceObject;

                // get type of sample grabber
                type = Type.GetTypeFromCLSID(Clsid.SampleGrabber);
                if (type == null)
                {
                    throw new ApplicationException("Failed creating sample grabber");
                }

                // create sample grabber
                grabberObject = Activator.CreateInstance(type);
                sampleGrabber = (ISampleGrabber)grabberObject;
                grabberBase   = (IBaseFilter)grabberObject;

                // add source and grabber filters to graph
                graph.AddFilter(sourceBase, "source");
                graph.AddFilter(grabberBase, "grabber");

                // set media type
                AMMediaType mediaType = new AMMediaType( );
                mediaType.MajorType = MediaType.Video;
                mediaType.SubType   = MediaSubType.RGB24;

                sampleGrabber.SetMediaType(mediaType);

                // configure sample grabber
                sampleGrabber.SetBufferSamples(false);
                sampleGrabber.SetOneShot(false);
                sampleGrabber.SetCallback(grabber, 1);

                // check if it is required to change capture settings
                if ((desiredFrameRate != 0) || ((desiredFrameSize.Width != 0) && (desiredFrameSize.Height != 0)))
                {
                    object streamConfigObject;
                    // get stream configuration object
                    captureGraph.FindInterface(PinCategory.Capture, MediaType.Video, sourceBase, typeof(IAMStreamConfig).GUID, out streamConfigObject);

                    if (streamConfigObject != null)
                    {
                        IAMStreamConfig streamConfig = (IAMStreamConfig)streamConfigObject;

                        if (videoCapabilities == null)
                        {
                            // get all video capabilities
                            try
                            {
                                videoCapabilities = AForge.Video.DirectShow.VideoCapabilities.FromStreamConfig(streamConfig);
                            }
                            catch
                            {
                            }
                        }

                        // get current format
                        streamConfig.GetFormat(out mediaType);
                        VideoInfoHeader infoHeader = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.FormatPtr, typeof(VideoInfoHeader));

                        // change frame size if required
                        if ((desiredFrameSize.Width != 0) && (desiredFrameSize.Height != 0))
                        {
                            infoHeader.BmiHeader.Width  = desiredFrameSize.Width;
                            infoHeader.BmiHeader.Height = desiredFrameSize.Height;
                        }
                        // change frame rate if required
                        if (desiredFrameRate != 0)
                        {
                            infoHeader.AverageTimePerFrame = 10000000 / desiredFrameRate;
                        }

                        // copy the media structure back
                        Marshal.StructureToPtr(infoHeader, mediaType.FormatPtr, false);

                        // set the new format
                        streamConfig.SetFormat(mediaType);

                        mediaType.Dispose( );
                    }
                }
                else
                {
                    if (videoCapabilities == null)
                    {
                        object streamConfigObject;
                        // get stream configuration object
                        captureGraph.FindInterface(PinCategory.Capture, MediaType.Video, sourceBase, typeof(IAMStreamConfig).GUID, out streamConfigObject);

                        if (streamConfigObject != null)
                        {
                            IAMStreamConfig streamConfig = (IAMStreamConfig)streamConfigObject;
                            // get all video capabilities
                            try
                            {
                                videoCapabilities = AForge.Video.DirectShow.VideoCapabilities.FromStreamConfig(streamConfig);
                            }
                            catch
                            {
                            }
                        }
                    }
                }

                if (runGraph)
                {
                    // render source device on sample grabber
                    captureGraph.RenderStream(PinCategory.Capture, MediaType.Video, sourceBase, null, grabberBase);

                    // get media type
                    if (sampleGrabber.GetConnectedMediaType(mediaType) == 0)
                    {
                        VideoInfoHeader vih = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.FormatPtr, typeof(VideoInfoHeader));

                        grabber.Width  = vih.BmiHeader.Width;
                        grabber.Height = vih.BmiHeader.Height;
                        mediaType.Dispose( );
                    }

                    // get media control
                    mediaControl = (IMediaControl)graphObject;

                    // run
                    mediaControl.Run( );

                    while (!stopEvent.WaitOne(0, true))
                    {
                        Thread.Sleep(100);

                        if (needToDisplayPropertyPage)
                        {
                            needToDisplayPropertyPage = false;

                            try
                            {
                                // retrieve ISpecifyPropertyPages interface of the device
                                ISpecifyPropertyPages pPropPages = (ISpecifyPropertyPages)sourceObject;

                                // get property pages from the property bag
                                CAUUID caGUID;
                                pPropPages.GetPages(out caGUID);

                                // get filter info
                                FilterInfo filterInfo = new FilterInfo(deviceMoniker);

                                // create and display the OlePropertyFrame
                                Win32.OleCreatePropertyFrame(parentWindowForPropertyPage, 0, 0, filterInfo.Name, 1, ref sourceObject, caGUID.cElems, caGUID.pElems, 0, 0, IntPtr.Zero);

                                // release COM objects
                                Marshal.FreeCoTaskMem(caGUID.pElems);
                            }
                            catch
                            {
                            }
                        }
                    }
                    mediaControl.StopWhenReady( );
                }
            }
            catch (Exception exception)
            {
                // provide information to clients
                if (VideoSourceError != null)
                {
                    VideoSourceError(this, new VideoSourceErrorEventArgs(exception.Message));
                }
            }
            finally
            {
                // release all objects
                captureGraph  = null;
                graph         = null;
                sourceBase    = null;
                grabberBase   = null;
                sampleGrabber = null;
                mediaControl  = null;

                if (graphObject != null)
                {
                    Marshal.ReleaseComObject(graphObject);
                    graphObject = null;
                }
                if (sourceObject != null)
                {
                    Marshal.ReleaseComObject(sourceObject);
                    sourceObject = null;
                }
                if (grabberObject != null)
                {
                    Marshal.ReleaseComObject(grabberObject);
                    grabberObject = null;
                }
                if (captureGraphObject != null)
                {
                    Marshal.ReleaseComObject(captureGraphObject);
                    captureGraphObject = null;
                }
            }

            if (PlayingFinished != null)
            {
                PlayingFinished(this, ReasonToFinishPlaying.StoppedByUser);
            }
        }
Пример #7
0
        // Token: 0x0600003F RID: 63 RVA: 0x00002D24 File Offset: 0x00000F24
        private void WorkerThread(bool runGraph)
        {
            ReasonToFinishPlaying reason = ReasonToFinishPlaying.StoppedByUser;
            bool flag = false;

            VideoCaptureDevice.Grabber grabber  = new VideoCaptureDevice.Grabber(this, false);
            VideoCaptureDevice.Grabber grabber2 = new VideoCaptureDevice.Grabber(this, true);
            object obj  = null;
            object obj2 = null;
            object obj3 = null;
            object obj4 = null;
            object obj5 = null;
            ICaptureGraphBuilder2 captureGraphBuilder = null;
            IFilterGraph2         filterGraph         = null;
            IBaseFilter           baseFilter          = null;
            IBaseFilter           baseFilter2         = null;
            IBaseFilter           baseFilter3         = null;
            ISampleGrabber        sampleGrabber       = null;
            ISampleGrabber        sampleGrabber2      = null;
            IAMVideoControl       iamvideoControl     = null;
            IPin        pin         = null;
            IAMCrossbar iamcrossbar = null;

            try
            {
                Type typeFromCLSID = Type.GetTypeFromCLSID(Clsid.CaptureGraphBuilder2);
                if (typeFromCLSID == null)
                {
                    throw new ApplicationException("Failed creating capture graph builder");
                }
                obj = Activator.CreateInstance(typeFromCLSID);
                captureGraphBuilder = (ICaptureGraphBuilder2)obj;
                Type typeFromCLSID2 = Type.GetTypeFromCLSID(Clsid.FilterGraph);
                if (typeFromCLSID2 == null)
                {
                    throw new ApplicationException("Failed creating filter graph");
                }
                obj2        = Activator.CreateInstance(typeFromCLSID2);
                filterGraph = (IFilterGraph2)obj2;
                captureGraphBuilder.SetFiltergraph((IGraphBuilder)filterGraph);
                this.sourceObject1 = FilterInfo.CreateFilter(this.deviceMoniker1);
                if (this.sourceObject1 == null)
                {
                    throw new ApplicationException("Failed creating device object for moniker");
                }
                baseFilter = (IBaseFilter)this.sourceObject1;
                try
                {
                    iamvideoControl = (IAMVideoControl)this.sourceObject1;
                }
                catch
                {
                }
                Type typeFromCLSID3 = Type.GetTypeFromCLSID(Clsid.SampleGrabber);
                if (typeFromCLSID3 == null)
                {
                    throw new ApplicationException("Failed creating sample grabber");
                }
                obj3           = Activator.CreateInstance(typeFromCLSID3);
                sampleGrabber  = (ISampleGrabber)obj3;
                baseFilter2    = (IBaseFilter)obj3;
                obj4           = Activator.CreateInstance(typeFromCLSID3);
                sampleGrabber2 = (ISampleGrabber)obj4;
                baseFilter3    = (IBaseFilter)obj4;
                filterGraph.AddFilter(baseFilter, "source");
                filterGraph.AddFilter(baseFilter2, "grabber_video");
                filterGraph.AddFilter(baseFilter3, "grabber_snapshot");
                AMMediaType ammediaType = new AMMediaType();
                ammediaType.MajorType = MediaType.Video1;
                ammediaType.SubType   = MediaSubType.RGB24;
                sampleGrabber.SetMediaType(ammediaType);
                sampleGrabber2.SetMediaType(ammediaType);
                captureGraphBuilder.FindInterface(FindDirection.UpstreamOnly, Guid.Empty, baseFilter, typeof(IAMCrossbar).GUID, out obj5);
                if (obj5 != null)
                {
                    iamcrossbar = (IAMCrossbar)obj5;
                }
                this.isCrossbarAvailable1 = new bool?(iamcrossbar != null);
                this.crossbarVideoInputs1 = this.ColletCrossbarVideoInputs(iamcrossbar);
                if (iamvideoControl != null)
                {
                    captureGraphBuilder.FindPin(this.sourceObject1, PinDirection.Output, PinCategory.StillImage, MediaType.Video1, false, 0, out pin);
                    if (pin != null)
                    {
                        VideoControlFlags videoControlFlags;
                        iamvideoControl.GetCaps(pin, out videoControlFlags);
                        flag = ((videoControlFlags & VideoControlFlags.ExternalTriggerEnable) > (VideoControlFlags)0);
                    }
                }
                sampleGrabber.SetBufferSamples(false);
                sampleGrabber.SetOneShot(false);
                sampleGrabber.SetCallback(grabber, 1);
                sampleGrabber2.SetBufferSamples(true);
                sampleGrabber2.SetOneShot(false);
                sampleGrabber2.SetCallback(grabber2, 1);
                this.GetPinCapabilitiesAndConfigureSizeAndRate(captureGraphBuilder, baseFilter, PinCategory.Capture, this.videoResolution1, ref this.videoCapabilities1);
                if (flag)
                {
                    this.GetPinCapabilitiesAndConfigureSizeAndRate(captureGraphBuilder, baseFilter, PinCategory.StillImage, this.snapshotResolution1, ref this.snapshotCapabilities1);
                }
                else
                {
                    this.snapshotCapabilities1 = new VideoCapabilities[0];
                }
                Dictionary <string, VideoCapabilities[]> obj6 = VideoCaptureDevice.cacheVideoCapabilities1;
                lock (obj6)
                {
                    if (this.videoCapabilities1 != null && !VideoCaptureDevice.cacheVideoCapabilities1.ContainsKey(this.deviceMoniker1))
                    {
                        VideoCaptureDevice.cacheVideoCapabilities1.Add(this.deviceMoniker1, this.videoCapabilities1);
                    }
                }
                obj6 = VideoCaptureDevice.cacheSnapshotCapabilities1;
                lock (obj6)
                {
                    if (this.snapshotCapabilities1 != null && !VideoCaptureDevice.cacheSnapshotCapabilities1.ContainsKey(this.deviceMoniker1))
                    {
                        VideoCaptureDevice.cacheSnapshotCapabilities1.Add(this.deviceMoniker1, this.snapshotCapabilities1);
                    }
                }
                if (runGraph)
                {
                    captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video1, baseFilter, null, baseFilter2);
                    if (sampleGrabber.GetConnectedMediaType(ammediaType) == 0)
                    {
                        VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(ammediaType.FormatPtr, typeof(VideoInfoHeader));
                        grabber.Width  = videoInfoHeader.BmiHeader.Width;
                        grabber.Height = videoInfoHeader.BmiHeader.Height;
                        ammediaType.Dispose();
                    }
                    if (flag && this.provideSnapshots1)
                    {
                        captureGraphBuilder.RenderStream(PinCategory.StillImage, MediaType.Video1, baseFilter, null, baseFilter3);
                        if (sampleGrabber2.GetConnectedMediaType(ammediaType) == 0)
                        {
                            VideoInfoHeader videoInfoHeader2 = (VideoInfoHeader)Marshal.PtrToStructure(ammediaType.FormatPtr, typeof(VideoInfoHeader));
                            grabber2.Width  = videoInfoHeader2.BmiHeader.Width;
                            grabber2.Height = videoInfoHeader2.BmiHeader.Height;
                            ammediaType.Dispose();
                        }
                    }
                    IMediaControl mediaControl = (IMediaControl)obj2;
                    IMediaEventEx mediaEventEx = (IMediaEventEx)obj2;
                    mediaControl.Run();
                    if (flag && this.provideSnapshots1)
                    {
                        this.startTime1 = DateTime.Now;
                        iamvideoControl.SetMode(pin, VideoControlFlags.ExternalTriggerEnable);
                    }
                    for (;;)
                    {
                        DsEvCode dsEvCode;
                        IntPtr   lParam;
                        IntPtr   lParam2;
                        if (mediaEventEx != null && mediaEventEx.GetEvent(out dsEvCode, out lParam, out lParam2, 0) >= 0)
                        {
                            mediaEventEx.FreeEventParams(dsEvCode, lParam, lParam2);
                            if (dsEvCode == DsEvCode.DeviceLost)
                            {
                                break;
                            }
                        }
                        if (this.needToSetVideoInput1)
                        {
                            this.needToSetVideoInput1 = false;
                            if (this.isCrossbarAvailable1.Value)
                            {
                                this.SetCurrentCrossbarInput(iamcrossbar, this.crossbarVideoInput1);
                                this.crossbarVideoInput1 = this.GetCurrentCrossbarInput(iamcrossbar);
                            }
                        }
                        if (this.needToSimulateTrigger1)
                        {
                            this.needToSimulateTrigger1 = false;
                            if (flag && this.provideSnapshots1)
                            {
                                iamvideoControl.SetMode(pin, VideoControlFlags.Trigger);
                            }
                        }
                        if (this.needToDisplayPropertyPage1)
                        {
                            this.needToDisplayPropertyPage1 = false;
                            this.DisplayPropertyPage(this.parentWindowForPropertyPage1, this.sourceObject1);
                            if (iamcrossbar != null)
                            {
                                this.crossbarVideoInput1 = this.GetCurrentCrossbarInput(iamcrossbar);
                            }
                        }
                        if (this.needToDisplayCrossBarPropertyPage1)
                        {
                            this.needToDisplayCrossBarPropertyPage1 = false;
                            if (iamcrossbar != null)
                            {
                                this.DisplayPropertyPage(this.parentWindowForPropertyPage1, iamcrossbar);
                                this.crossbarVideoInput1 = this.GetCurrentCrossbarInput(iamcrossbar);
                            }
                        }
                        if (this.stopEvent1.WaitOne(100, false))
                        {
                            goto IL_54E;
                        }
                    }
                    reason = ReasonToFinishPlaying.DeviceLost;
IL_54E:
                    mediaControl.Stop1();
                }
            }
            catch (Exception ex)
            {
                if (this.VideoSourceError != null)
                {
                    this.VideoSourceError(this, new VideoSourceErrorEventArgs(ex.Message));
                }
            }
            finally
            {
                captureGraphBuilder = null;
                filterGraph         = null;
                baseFilter          = null;
                iamvideoControl     = null;
                pin            = null;
                iamcrossbar    = null;
                baseFilter2    = null;
                baseFilter3    = null;
                sampleGrabber  = null;
                sampleGrabber2 = null;
                if (obj2 != null)
                {
                    Marshal.ReleaseComObject(obj2);
                    obj2 = null;
                }
                if (this.sourceObject1 != null)
                {
                    Marshal.ReleaseComObject(this.sourceObject1);
                    this.sourceObject1 = null;
                }
                if (obj3 != null)
                {
                    Marshal.ReleaseComObject(obj3);
                    obj3 = null;
                }
                if (obj4 != null)
                {
                    Marshal.ReleaseComObject(obj4);
                    obj4 = null;
                }
                if (obj != null)
                {
                    Marshal.ReleaseComObject(obj);
                    obj = null;
                }
                if (obj5 != null)
                {
                    Marshal.ReleaseComObject(obj5);
                    obj5 = null;
                }
            }
            if (this.PlayingFinished != null)
            {
                this.PlayingFinished(this, reason);
            }
        }