Example #1
0
 public FrameGrabber(DsDevice camDevice)
 {
     IFilterGraph2 filterGraph;
     ICaptureGraphBuilder2 graphBuilder;
     IBaseFilter camBase, nullRenderer;
     ISampleGrabber sampleGrabber;
     filterGraph = new FilterGraph() as IFilterGraph2;
     mediaCtrl = filterGraph as IMediaControl;
     graphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;
     HRCheck(graphBuilder.SetFiltergraph(filterGraph));
     // Add camera
     HRCheck(filterGraph.AddSourceFilterForMoniker(
         camDevice.Mon, null, camDevice.Name, out camBase));
     // Add sample grabber
     sampleGrabber = new SampleGrabber() as ISampleGrabber;
     var mType = new AMMediaType()
     {
         majorType = MediaType.Video,
         subType = MediaSubType.RGB24,
         formatType = FormatType.VideoInfo
     };
     HRCheck(sampleGrabber.SetMediaType(mType));
     DsUtils.FreeAMMediaType(mType);
     HRCheck(sampleGrabber.SetCallback(this, 1));
     HRCheck(filterGraph.AddFilter(sampleGrabber as IBaseFilter, "CamGrabber"));
     // Add null renderer
     nullRenderer = new NullRenderer() as IBaseFilter;
     HRCheck(filterGraph.AddFilter(nullRenderer, "Null renderer"));
     // Render the webcam through the grabber and the renderer
     HRCheck(graphBuilder.RenderStream(PinCategory.Capture, MediaType.Video,
         camBase, sampleGrabber as IBaseFilter, nullRenderer));
     // Get resulting picture size
     mType = new AMMediaType();
     HRCheck(sampleGrabber.GetConnectedMediaType(mType));
     if (mType.formatType != FormatType.VideoInfo || mType.formatPtr == IntPtr.Zero)
     {
         throw new NotSupportedException("Unknown grabber media format");
     }
     var videoInfoHeader = Marshal.PtrToStructure(mType.formatPtr,
         typeof(VideoInfoHeader)) as VideoInfoHeader;
     width = videoInfoHeader.BmiHeader.Width;
     height = videoInfoHeader.BmiHeader.Height;
     Console.WriteLine("{0} x {1}", width, height); 
     stride = width * (videoInfoHeader.BmiHeader.BitCount / 8);
     DsUtils.FreeAMMediaType(mType);
     HRCheck(mediaCtrl.Run());
 }
Example #2
0
        protected virtual void AddRenderers(IFilterGraph2 graphBuilder)
        {
            int hr = 0;
            Guid iid = typeof(IBaseFilter).GUID;

            if (this.AudioRendererDevice != null)
            {
                try
                {
                    this.audioRenderer = null;

                    IBaseFilter tmp;

                    object o;
                    this.AudioRendererDevice.Mon.BindToObject(null, null, ref iid, out o);
                    tmp = o as IBaseFilter;

                    //Add the Video input device to the graph
                    hr = graphBuilder.AddFilter(tmp, this.AudioRendererDevice.Name);
                    if (hr >= 0)
                    {
                        // Got it !
                        this.audioRenderer = tmp;
                    }
                    else
                    {
                        // Try another...
                        int hr1 = graphBuilder.RemoveFilter(tmp);
                        Marshal.ReleaseComObject(tmp);
                        //DsError.ThrowExceptionForHR(hr);
                    }
                }
                catch { }
            }

            if (this.audioRenderer == null)
            {
                // Add default audio renderer
                this.audioRenderer = (IBaseFilter)new DSoundRender();
                hr = graphBuilder.AddFilter(this.audioRenderer, "DirectSound Renderer");
                ThrowExceptionForHR("Adding the DirectSound Renderer: ", hr);
            }

            // To see something

            if (useWPF)
            {
                // Get the SampleGrabber interface
                ISampleGrabber sampleGrabber = new SampleGrabber() as ISampleGrabber;
                this.videoRenderer = sampleGrabber as IBaseFilter;

                // Set the media type to Video
                AMMediaType media = new AMMediaType();
                media.majorType = MediaType.Video;
                //media.subType = MediaSubType.YUY2; // RGB24;
                //media.formatType = FormatType.Null;
                //media.sampleSize = 1;
                //media.temporalCompression = false;
                //media.fixedSizeSamples = false;
                //media.unkPtr = IntPtr.Zero;
                //media.formatType = FormatType.None;
                //media.formatSize = 0;
                //media.formatPtr = IntPtr.Zero;
                media.subType = MediaSubType.RGB32; // RGB24;
                media.formatType = FormatType.VideoInfo;
                hr = sampleGrabber.SetMediaType(media);
                ThrowExceptionForHR("Setting the MediaType on the SampleGrabber: ", hr);
                DsUtils.FreeAMMediaType(media);

                // Configure the samplegrabber
                hr = sampleGrabber.SetCallback(this, 1);
                DsError.ThrowExceptionForHR(hr);

                // Add the frame grabber to the graph
                hr = graphBuilder.AddFilter(this.videoRenderer, "SampleGrabber");
                ThrowExceptionForHR("Adding the SampleGrabber: ", hr);

                //hr = ConnectFilters(this.videoRenderer, nullRenderer);
            }
            else
            {
                try
                {
                    this.videoRenderer = (IBaseFilter)new EnhancedVideoRenderer();
                    hr = graphBuilder.AddFilter(this.videoRenderer, "Enhanced Video Renderer");
                    ThrowExceptionForHR("Adding the EVR: ", hr);
                    useEVR = true;
                }
                catch (Exception) { }
                if (!useEVR)
                {
                    this.videoRenderer = (IBaseFilter)new VideoMixingRenderer9();
                    hr = graphBuilder.AddFilter(this.videoRenderer, "Video Mixing Renderer 9");
                    ThrowExceptionForHR("Adding the VMR9: ", hr);
                }
            }
            //IReferenceClock clock = this.audioRenderer as IReferenceClock;
            //if(clock != null)
            //{
            //    // Set the graph clock.
            //    this.FilterGraph.SetDefaultSyncSource(
            //    pGraph->QueryInterface(IID_IMediaFilter, (void**)&pMediaFilter);
            //    pMediaFilter->SetSyncSource(pClock);
            //    pClock->Release();
            //    pMediaFilter->Release();
            //}
        }
Example #3
0
        private void SetupSampleGrabber()
        {
            if (_graph == null)
                return;

            int hr;

            //Get directsound filter
            IBaseFilter directSoundFilter;
            hr = _graph.FindFilterByName(DEFAULT_AUDIO_RENDERER_NAME, out directSoundFilter);
            DsError.ThrowExceptionForHR(hr);

            IPin rendererPinIn = DsFindPin.ByConnectionStatus(directSoundFilter, PinConnectedStatus.Connected, 0);

            if (rendererPinIn != null)
            {
                IPin audioPinOut;
                hr = rendererPinIn.ConnectedTo(out audioPinOut);
                DsError.ThrowExceptionForHR(hr);

                if (audioPinOut != null)
                {
                    //Disconect audio decoder to directsound renderer
                    hr = audioPinOut.Disconnect();
                    DsError.ThrowExceptionForHR(hr);

                    hr = _graph.RemoveFilter(directSoundFilter);
                    DsError.ThrowExceptionForHR(hr);

                    //Add Sample Grabber
                    ISampleGrabber sampleGrabber = new SampleGrabber() as ISampleGrabber;
                    hr = sampleGrabber.SetCallback(this, 1);
                    DsError.ThrowExceptionForHR(hr);

                    AMMediaType media;
                    media = new AMMediaType();
                    media.majorType = MediaType.Audio;
                    media.subType = MediaSubType.PCM;
                    media.formatType = FormatType.WaveEx;
                    hr = sampleGrabber.SetMediaType(media);
                    DsError.ThrowExceptionForHR(hr);

                    IPin sampleGrabberPinIn = DsFindPin.ByDirection((IBaseFilter)sampleGrabber, PinDirection.Input, 0);
                    IPin sampleGrabberPinOut = DsFindPin.ByDirection((IBaseFilter)sampleGrabber, PinDirection.Output, 0);
                    hr = _graph.AddFilter((IBaseFilter)sampleGrabber, "SampleGrabber");
                    DsError.ThrowExceptionForHR(hr);

                    PinInfo pinInfo;
                    hr = audioPinOut.QueryPinInfo(out pinInfo);
                    DsError.ThrowExceptionForHR(hr);

                    FilterInfo filterInfo;
                    hr = pinInfo.filter.QueryFilterInfo(out filterInfo);
                    DsError.ThrowExceptionForHR(hr);

                    hr = _graph.Connect(audioPinOut, sampleGrabberPinIn);
                    DsError.ThrowExceptionForHR(hr);

                    //Add null renderer
                    NullRenderer nullRenderer = new NullRenderer();
                    hr = _graph.AddFilter((IBaseFilter)nullRenderer, "NullRenderer");
                    DsError.ThrowExceptionForHR(hr);

                    IPin nullRendererPinIn = DsFindPin.ByDirection((IBaseFilter)nullRenderer, PinDirection.Input, 0);
                    hr = _graph.Connect(sampleGrabberPinOut, nullRendererPinIn);
                    DsError.ThrowExceptionForHR(hr);

                    _audioEngine.Setup(this.GetSampleGrabberFormat(sampleGrabber));
                }
            }
        }
Example #4
0
        private void SetupSampleGrabber()
        {
            if (_graph == null)
            {
                return;
            }

            int hr;

            //Get directsound filter
            IBaseFilter directSoundFilter;

            hr = _graph.FindFilterByName(DEFAULT_AUDIO_RENDERER_NAME, out directSoundFilter);
            DsError.ThrowExceptionForHR(hr);

            IPin rendererPinIn = DsFindPin.ByConnectionStatus(directSoundFilter, PinConnectedStatus.Connected, 0);

            if (rendererPinIn != null)
            {
                IPin audioPinOut;
                hr = rendererPinIn.ConnectedTo(out audioPinOut);
                DsError.ThrowExceptionForHR(hr);

                if (audioPinOut != null)
                {
                    //Disconect audio decoder to directsound renderer
                    hr = audioPinOut.Disconnect();
                    DsError.ThrowExceptionForHR(hr);

                    hr = _graph.RemoveFilter(directSoundFilter);
                    DsError.ThrowExceptionForHR(hr);

                    //Add Sample Grabber
                    ISampleGrabber sampleGrabber = new SampleGrabber() as ISampleGrabber;
                    hr = sampleGrabber.SetCallback(this, 1);
                    DsError.ThrowExceptionForHR(hr);

                    AMMediaType media;
                    media            = new AMMediaType();
                    media.majorType  = MediaType.Audio;
                    media.subType    = MediaSubType.PCM;
                    media.formatType = FormatType.WaveEx;
                    hr = sampleGrabber.SetMediaType(media);
                    DsError.ThrowExceptionForHR(hr);

                    IPin sampleGrabberPinIn  = DsFindPin.ByDirection((IBaseFilter)sampleGrabber, PinDirection.Input, 0);
                    IPin sampleGrabberPinOut = DsFindPin.ByDirection((IBaseFilter)sampleGrabber, PinDirection.Output, 0);
                    hr = _graph.AddFilter((IBaseFilter)sampleGrabber, "SampleGrabber");
                    DsError.ThrowExceptionForHR(hr);

                    PinInfo pinInfo;
                    hr = audioPinOut.QueryPinInfo(out pinInfo);
                    DsError.ThrowExceptionForHR(hr);

                    FilterInfo filterInfo;
                    hr = pinInfo.filter.QueryFilterInfo(out filterInfo);
                    DsError.ThrowExceptionForHR(hr);

                    hr = _graph.Connect(audioPinOut, sampleGrabberPinIn);
                    DsError.ThrowExceptionForHR(hr);

                    //Add null renderer
                    NullRenderer nullRenderer = new NullRenderer();
                    hr = _graph.AddFilter((IBaseFilter)nullRenderer, "NullRenderer");
                    DsError.ThrowExceptionForHR(hr);

                    IPin nullRendererPinIn = DsFindPin.ByDirection((IBaseFilter)nullRenderer, PinDirection.Input, 0);
                    hr = _graph.Connect(sampleGrabberPinOut, nullRendererPinIn);
                    DsError.ThrowExceptionForHR(hr);

                    _audioEngine.Setup(this.GetSampleGrabberFormat(sampleGrabber));
                }
            }
        }
Example #5
0
            public void Open(bool run = true)
            {
                DsDevice device = null;

                foreach (DsDevice d in DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice))
                {
                    if (d.Name == this.Name)
                    {
                        device = d; break;
                    }
                }

                if (device == null)
                {
                    throw new NullReferenceException("DsDevice");
                }

                try
                {
                    // Create

                    this.captureGraphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;
                    if (this.captureGraphBuilder == null)
                    {
                        throw new NullReferenceException("ICaptureGraphBuilder2");
                    }

                    this.filterGraph = new FilterGraph() as IFilterGraph2;
                    if (this.filterGraph == null)
                    {
                        throw new NullReferenceException("IFilterGraph2");
                    }

                    this.mediaControl = this.filterGraph as IMediaControl;
                    if (this.mediaControl == null)
                    {
                        throw new NullReferenceException("IMediaControl");
                    }

                    // Filter Graph (Video Capture -> Sample Grabber -> Null Renderer)

                    int hr = this.captureGraphBuilder.SetFiltergraph(this.filterGraph);
                    if (hr < 0)
                    {
                        throw new COMException("ICaptureGraphBuilder2::SetFiltergraph", hr);
                    }

                    // Video Capture

                    hr = this.filterGraph.AddSourceFilterForMoniker(device.Mon, null, device.Name, out this.videoCapture);
                    if (hr < 0)
                    {
                        throw new COMException("IFilterGraph2::AddSourceFilterForMoniker", hr);
                    }

                    if (run)
                    {
                        hr = this.captureGraphBuilder.FindInterface(PinCategory.Capture, DirectShowLib.MediaType.Video, this.videoCapture, typeof(IAMStreamConfig).GUID, out object intrface);
                        if (hr < 0)
                        {
                            throw new COMException("ICaptureGraphBuilder2::FindInterface::IAMStreamConfig", hr);
                        }

                        IAMStreamConfig streamConfig = intrface as IAMStreamConfig;
                        if (streamConfig == null)
                        {
                            throw new NullReferenceException("IAMStreamConfig");
                        }

                        hr = streamConfig.GetFormat(out AMMediaType media);
                        if (hr < 0)
                        {
                            throw new COMException("IAMStreamConfig::GetFormat", hr);
                        }

                        if (this.MediaType == null)
                        {
                            this.MediaType = new VideoInfoHeader();
                            Marshal.PtrToStructure(media.formatPtr, this.MediaType);
                            DsUtils.FreeAMMediaType(media); media = null;
                        }
                        else
                        {
                            Marshal.StructureToPtr(this.MediaType, media.formatPtr, false);
                            hr = streamConfig.SetFormat(media);
                            DsUtils.FreeAMMediaType(media); media = null;
                            if (hr < 0)
                            {
                                throw new COMException("IAMStreamConfig::SetFormat", hr);
                            }
                        }

                        this.Width     = this.MediaType.BmiHeader.Width;
                        this.Height    = this.MediaType.BmiHeader.Height;
                        this.FrameRate = 10000000.0 / this.MediaType.AvgTimePerFrame;

                        // Sample Grabber

                        ISampleGrabber sampleGrabber = new SampleGrabber() as ISampleGrabber;
                        media            = new AMMediaType();
                        media.majorType  = DirectShowLib.MediaType.Video;
                        media.subType    = MediaSubType.RGB24;
                        media.formatType = FormatType.VideoInfo;
                        hr = sampleGrabber.SetMediaType(media);
                        DsUtils.FreeAMMediaType(media); media = null;
                        if (hr < 0)
                        {
                            throw new COMException("ISampleGrabber::SetMediaType", hr);
                        }

                        hr = sampleGrabber.SetCallback(this, 1);
                        if (hr < 0)
                        {
                            throw new COMException("ISampleGrabber::SetCallback", hr);
                        }

                        hr = this.filterGraph.AddFilter(sampleGrabber as IBaseFilter, "SampleGrabber");
                        if (hr < 0)
                        {
                            throw new COMException("IFilterGraph2::AddFilter::SampleGrabber", hr);
                        }

                        // Null Renderer

                        NullRenderer nullRenderer = new NullRenderer();
                        hr = this.filterGraph.AddFilter(nullRenderer as IBaseFilter, "NullRenderer");
                        if (hr < 0)
                        {
                            throw new COMException("IFilterGraph2::AddFilter::NullRenderer", hr);
                        }

                        hr = this.captureGraphBuilder.RenderStream(PinCategory.Capture, DirectShowLib.MediaType.Video, this.videoCapture, sampleGrabber as IBaseFilter, nullRenderer as IBaseFilter);
                        if (hr < 0)
                        {
                            throw new COMException("ICaptureGraphBuilder2::RenderStream", hr);
                        }

                        // ROT (Running Object Table) Entry

                        this.rotEntry = new DsROTEntry(this.filterGraph);

                        // Frames

                        this.frames = new LinkedList <Frame>();

                        for (int b = 0; b < this.Backtrace; b++)
                        {
                            this.frames.AddLast(new Frame());
                        }

                        // Run Filter Graph

                        hr = this.mediaControl.Run();
                        if (hr < 0)
                        {
                            throw new COMException("IMediaControl::Run", hr);
                        }
                    }
                }
                catch (Exception e)
                {
                    this.Close(); throw e;
                }
            }