Exemple #1
0
        private void WorkerThread(object start)
        {
            if (BehaviorVector[(int)BehaviorVectorMask.DispatchOnWorkerThread])
            {
                _dispatchOnWorkerThread = true;
            }
            FrameInfo = ((Func <IFrameInfo>)start)();
            var sampleGrabberCB = new SampleGrabberCB(FrameInfo, _dispatchOnWorkerThread);

            OnEnterWorkerThread();
            try
            {
                var graph         = (Activator.CreateInstance(Type.GetTypeFromCLSID(Registry.FilterGraph)) as IGraphBuilder);
                var sourceObject  = FilterInfo.CreateFilter(_device.Moniker);
                var sampleGrabber = (Activator.CreateInstance(Type.GetTypeFromCLSID(Registry.SampleGrabber)) as ISampleGrabber);
                var grabberObject = (sampleGrabber as IBaseFilter);
                graph.AddFilter(sourceObject, "source");
                graph.AddFilter(grabberObject, "grabber");
                // set media type for our grabber
                using (var mediaType = new AMMediaType())
                {
                    mediaType.MajorType = Registry.MediaTypes.Video;
                    mediaType.SubType   = Registry.MediaSubTypes.RGB32;
                    sampleGrabber.SetMediaType(mediaType);
                    // and then connect device filter to out pin and grabber to in pin. then get capabilities of video received (this stuff comes from your web camera manufacturer)
                    if (graph.Connect(sourceObject.GetPin(PinDirection.Output, 0), grabberObject.GetPin(PinDirection.Input, 0)) >= 0)
                    {
                        if (sampleGrabber.GetConnectedMediaType(mediaType) == 0)
                        {
                            _frameInfo.SetProperties((VideoInfoHeader)Marshal.PtrToStructure(mediaType.FormatPtr, typeof(VideoInfoHeader)));
                        }
                    }
                    // out pin to grabber without buffering and callback to grabber object (this one will get all images from our source).
                    graph.Render(grabberObject.GetPin(PinDirection.Output, 0));
                    sampleGrabber.SetBufferSamples(false);
                    sampleGrabber.SetOneShot(false);
                    sampleGrabber.SetCallback(sampleGrabberCB, 1);
                    // dump output window
                    var wnd = (IVideoWindow)graph;
                    wnd.put_AutoShow(false); wnd = null;
                    // and run the controller
                    var control = (IMediaControl)graph;
                    control.Run();
                    //
                    if (_dispatchOnWorkerThread)
                    {
                        Dispatcher.Run();
                        //while (!WaitHandle.WaitAll(new WaitHandle[] { _stopSignal }, 10, true))
                        while (!_stopEvent.WaitOne(10, true))
                        {
                            Dispatcher.Run();
                        }
                        Dispatcher.ExitAllFrames();
                    }
                    else
                    {
                        while (!_stopEvent.WaitOne(0, true))
                        {
                            Thread.Sleep(10);
                        }
                    }
                    //
                    control.StopWhenReady();
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex);
                Debug.WriteLine(ex);
                throw;
            }
            finally
            {
                // guaranteed exit
                OnExitWorkerThread();
            }
        }