예제 #1
0
        /// <summary>
        ///  Completely tear down a filter graph and
        ///  release all associated resources.
        /// </summary>
        protected void DestroyGraph()
        {
            // Derender the graph (This will stop the graph
            // and release preview window. It also destroys
            // half of the graph which is unnecessary but
            // harmless here.) (ignore errors)
            try { DerenderGraph(); }
            catch { }

            // Update the state after derender because it
            // depends on correct status. But we also want to
            // update the state as early as possible in case
            // of error.
            ActualGraphState  = GraphState.Null;
            IsPreviewRendered = false;

            // Remove filters from the graph
            // This should be unnecessary but the Nvidia WDM
            // video driver cannot be used by this application
            // again unless we remove it. Ideally, we should
            // simply enumerate all the filters in the graph
            // and remove them. (ignore errors)
            if (VideoCompressorFilter != null)
            {
                GraphBuilder.RemoveFilter(VideoCompressorFilter);
            }
            if (VideoDeviceFilter != null)
            {
                GraphBuilder.RemoveFilter(VideoDeviceFilter);
            }

            // Cleanup
            if (GraphBuilder != null)
            {
                Marshal.ReleaseComObject(GraphBuilder);
            }
            GraphBuilder = null;
            if (CaptureGraphBuilder != null)
            {
                Marshal.ReleaseComObject(CaptureGraphBuilder);
            }
            CaptureGraphBuilder = null;
            if (VideoDeviceFilter != null)
            {
                Marshal.ReleaseComObject(VideoDeviceFilter);
            }
            VideoDeviceFilter = null;
            if (VideoCompressorFilter != null)
            {
                Marshal.ReleaseComObject(VideoCompressorFilter);
            }
            VideoCompressorFilter = null;

            // These are copies of graphBuilder
            MediaControl = null;
            VideoWindow  = null;

            // For unmanaged objects we haven't released explicitly
            GC.Collect();
        }
예제 #2
0
        /// <summary>
        ///  Removes all filters downstream from a filter from the graph.
        ///  This is called only by DerenderGraph() to remove everything
        ///  from the graph except the devices and compressors. The parameter
        ///  "removeFirstFilter" is used to keep a compressor (that should
        ///  be immediately downstream of the device) if one is begin used.
        /// </summary>
        protected void RemoveDownstream(CoreStreaming.IBaseFilter filter, bool removeFirstFilter)
        {
            // Get a pin enumerator off the filter
            CoreStreaming.IEnumPins pinEnum;
            var hr = filter.EnumPins(out pinEnum);

            if (pinEnum != null)
            {
                pinEnum.Reset();

                if ((hr == 0))
                {
                    // Loop through each pin
                    var pins = new CoreStreaming.IPin[1];
                    int f;
                    do
                    {
                        // Get the next pin
                        hr = pinEnum.Next(1, pins, out f);
                        if ((hr == 0) && (pins[0] != null))
                        {
                            // Get the pin it is connected to
                            CoreStreaming.IPin pinTo = null;
                            pins[0].ConnectedTo(out pinTo);
                            if (pinTo != null)
                            {
                                // Is this an input pin?
                                var info = new CoreStreaming.PinInfo();
                                hr = pinTo.QueryPinInfo(out info);
                                if ((hr == 0) && (info.dir == CoreStreaming.PinDirection.Input))
                                {
                                    // Recurse down this branch
                                    RemoveDownstream(info.filter, true);

                                    // Disconnect
                                    GraphBuilder.Disconnect(pinTo);
                                    GraphBuilder.Disconnect(pins[0]);

                                    // Remove this filter
                                    // but don't remove the video or audio compressors
                                    if (info.filter != VideoCompressorFilter)
                                    {
                                        GraphBuilder.RemoveFilter(info.filter);
                                    }
                                }

                                Marshal.ReleaseComObject(info.filter);
                                Marshal.ReleaseComObject(pinTo);
                            }

                            Marshal.ReleaseComObject(pins[0]);
                        }
                    } while (hr == 0);

                    Marshal.ReleaseComObject(pinEnum); pinEnum = null;
                }
            }
        }
예제 #3
0
        /// <summary>
        ///  Create a new filter graph and add filters (devices, compressors, misc),
        ///  but leave the filters unconnected. Call RenderGraph()
        ///  to connect the filters.
        /// </summary>
        protected void CreateGraph()
        {
            //Skip if already created
            if ((int)ActualGraphState < (int)GraphState.Created)
            {
                // Make a new filter graph
                GraphBuilder = (ExtendStreaming.IGraphBuilder)Activator.CreateInstance(Type.GetTypeFromCLSID(Uuid.Clsid.FilterGraph, true));

                // Get the Capture Graph Builder
                var clsid = Uuid.Clsid.CaptureGraphBuilder2;
                var riid  = typeof(ExtendStreaming.ICaptureGraphBuilder2).GUID;
                CaptureGraphBuilder = (ExtendStreaming.ICaptureGraphBuilder2)Workaround.CreateDsInstance(ref clsid, ref riid);

                // Link the CaptureGraphBuilder to the filter graph
                var hr = CaptureGraphBuilder.SetFiltergraph(GraphBuilder);
                if (hr < 0)
                {
                    Marshal.ThrowExceptionForHR(hr);
                }

                var comType = Type.GetTypeFromCLSID(Uuid.Clsid.SampleGrabber);
                if (comType == null)
                {
                    throw new NotImplementedException(@"DirectShow SampleGrabber not installed/registered!");
                }
                var comObj = Activator.CreateInstance(comType);
                SampGrabber = (EditStreaming.ISampleGrabber)comObj; comObj = null;

                _baseGrabFlt = (CoreStreaming.IBaseFilter)SampGrabber;

                var media = new CoreStreaming.AMMediaType();
                // Get the video device and add it to the filter graph
                if (VideoDevice != null)
                {
                    VideoDeviceFilter = (CoreStreaming.IBaseFilter)Marshal.BindToMoniker(VideoDevice.MonikerString);

                    hr = GraphBuilder.AddFilter(VideoDeviceFilter, "Video Capture Device");
                    if (hr < 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }

                    media.majorType           = Uuid.MediaType.Video;
                    media.subType             = Uuid.MediaSubType.RGB32; //RGB24;
                    media.formatType          = Uuid.FormatType.VideoInfo;
                    media.temporalCompression = true;                    //New

                    hr = SampGrabber.SetMediaType(media);

                    if (hr < 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }

                    hr = GraphBuilder.AddFilter(_baseGrabFlt, "Grabber");
                    if (hr < 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }
                }

                // Retrieve the stream control interface for the video device
                // FindInterface will also add any required filters
                // (WDM devices in particular may need additional
                // upstream filters to function).

                // Try looking for an interleaved media type
                object o;
                var    cat = Uuid.PinCategory.Capture;
                var    med = Uuid.MediaType.Interleaved;
                var    iid = typeof(ExtendStreaming.IAMStreamConfig).GUID;
                hr = CaptureGraphBuilder.FindInterface(ref cat, ref med, VideoDeviceFilter, ref iid, out o);

                if (hr != 0)
                {
                    // If not found, try looking for a video media type
                    med = Uuid.MediaType.Video;
                    hr  = CaptureGraphBuilder.FindInterface(ref cat, ref med, VideoDeviceFilter, ref iid, out o);

                    if (hr != 0)
                    {
                        o = null;
                    }
                }

                VideoStreamConfig = o as ExtendStreaming.IAMStreamConfig;

                // Retreive the media control interface (for starting/stopping graph)
                MediaControl = (ControlStreaming.IMediaControl)GraphBuilder;

                // Reload any video crossbars
                //if (videoSources != null) videoSources.Dispose(); videoSources = null;

                _videoInfoHeader = (EditStreaming.VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(EditStreaming.VideoInfoHeader));
                Marshal.FreeCoTaskMem(media.formatPtr); media.formatPtr = IntPtr.Zero;

                hr = SampGrabber.SetBufferSamples(false);
                if (hr == 0)
                {
                    hr = SampGrabber.SetOneShot(false);
                }
                if (hr == 0)
                {
                    hr = SampGrabber.SetCallback(null, 0);
                }
                if (hr < 0)
                {
                    Marshal.ThrowExceptionForHR(hr);
                }
            }

            // Update the state now that we are done
            ActualGraphState = GraphState.Created;
        }
예제 #4
0
        /// <summary>
        ///  Completely tear down a filter graph and 
        ///  release all associated resources.
        /// </summary>
        protected void DestroyGraph()
        {
            // Derender the graph (This will stop the graph
            // and release preview window. It also destroys
            // half of the graph which is unnecessary but
            // harmless here.) (ignore errors)
            try { DerenderGraph(); }
            catch { }

            // Update the state after derender because it
            // depends on correct status. But we also want to
            // update the state as early as possible in case
            // of error.
            ActualGraphState = GraphState.Null;
            IsPreviewRendered = false;

            // Remove filters from the graph
            // This should be unnecessary but the Nvidia WDM
            // video driver cannot be used by this application 
            // again unless we remove it. Ideally, we should
            // simply enumerate all the filters in the graph
            // and remove them. (ignore errors)
            if (VideoCompressorFilter != null)
                GraphBuilder.RemoveFilter(VideoCompressorFilter);
            if (VideoDeviceFilter != null)
                GraphBuilder.RemoveFilter(VideoDeviceFilter);

            // Cleanup
            if (GraphBuilder != null)
                Marshal.ReleaseComObject(GraphBuilder); GraphBuilder = null;
            if (CaptureGraphBuilder != null)
                Marshal.ReleaseComObject(CaptureGraphBuilder); CaptureGraphBuilder = null;
            if (VideoDeviceFilter != null)
                Marshal.ReleaseComObject(VideoDeviceFilter); VideoDeviceFilter = null;
            if (VideoCompressorFilter != null)
                Marshal.ReleaseComObject(VideoCompressorFilter); VideoCompressorFilter = null;

            // These are copies of graphBuilder
            MediaControl = null;
            VideoWindow = null;

            // For unmanaged objects we haven't released explicitly
            GC.Collect();
        }
예제 #5
0
        /// <summary> 
        ///  Create a new filter graph and add filters (devices, compressors, misc),
        ///  but leave the filters unconnected. Call RenderGraph()
        ///  to connect the filters.
        /// </summary>
        protected void CreateGraph()
        {
            //Skip if already created
            if ((int)ActualGraphState < (int)GraphState.Created)
            {
                // Make a new filter graph
                GraphBuilder = (ExtendStreaming.IGraphBuilder)Activator.CreateInstance(Type.GetTypeFromCLSID(Uuid.Clsid.FilterGraph, true));

                // Get the Capture Graph Builder
                Guid clsid = Uuid.Clsid.CaptureGraphBuilder2;
                Guid riid = typeof(ExtendStreaming.ICaptureGraphBuilder2).GUID;
                CaptureGraphBuilder = (ExtendStreaming.ICaptureGraphBuilder2)Workaround.CreateDsInstance(ref clsid, ref riid);

                // Link the CaptureGraphBuilder to the filter graph
                int hr = CaptureGraphBuilder.SetFiltergraph(GraphBuilder);
                if (hr < 0) Marshal.ThrowExceptionForHR(hr);

                Type comType = Type.GetTypeFromCLSID(Uuid.Clsid.SampleGrabber);
                if (comType == null)
                    throw new NotImplementedException(@"DirectShow SampleGrabber not installed/registered!");
                object comObj = Activator.CreateInstance(comType);
                SampGrabber = (EditStreaming.ISampleGrabber)comObj; comObj = null;

                _baseGrabFlt = (CoreStreaming.IBaseFilter)SampGrabber;

                var media = new CoreStreaming.AMMediaType();
                // Get the video device and add it to the filter graph
                if (VideoDevice != null)
                {
                    VideoDeviceFilter = (CoreStreaming.IBaseFilter)Marshal.BindToMoniker(VideoDevice.MonikerString);

                    hr = GraphBuilder.AddFilter(VideoDeviceFilter, "Video Capture Device");
                    if (hr < 0) Marshal.ThrowExceptionForHR(hr);

                    media.majorType = Uuid.MediaType.Video;
                    media.subType = Uuid.MediaSubType.RGB32;//RGB24;
                    media.formatType = Uuid.FormatType.VideoInfo;
                    media.temporalCompression = true; //New

                    hr = SampGrabber.SetMediaType(media);

                    if (hr < 0)
                        Marshal.ThrowExceptionForHR(hr);

                    hr = GraphBuilder.AddFilter(_baseGrabFlt, "Grabber");
                    if (hr < 0) Marshal.ThrowExceptionForHR(hr);
                }

                // Retrieve the stream control interface for the video device
                // FindInterface will also add any required filters
                // (WDM devices in particular may need additional
                // upstream filters to function).

                // Try looking for an interleaved media type
                object o;
                Guid cat = Uuid.PinCategory.Capture;
                Guid med = Uuid.MediaType.Interleaved;
                Guid iid = typeof(ExtendStreaming.IAMStreamConfig).GUID;
                hr = CaptureGraphBuilder.FindInterface(ref cat, ref med, VideoDeviceFilter, ref iid, out o);

                if (hr != 0)
                {
                    // If not found, try looking for a video media type
                    med = Uuid.MediaType.Video;
                    hr = CaptureGraphBuilder.FindInterface(
                        ref cat, ref med, VideoDeviceFilter, ref iid, out o);

                    if (hr != 0)
                        o = null;
                }

                VideoStreamConfig = o as ExtendStreaming.IAMStreamConfig;

                // Retreive the media control interface (for starting/stopping graph)
                MediaControl = (ControlStreaming.IMediaControl)GraphBuilder;

                // Reload any video crossbars
                //if (videoSources != null) videoSources.Dispose(); videoSources = null;

                _videoInfoHeader = (EditStreaming.VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(EditStreaming.VideoInfoHeader));
                Marshal.FreeCoTaskMem(media.formatPtr); media.formatPtr = IntPtr.Zero;

                hr = SampGrabber.SetBufferSamples(false);
                if (hr == 0)
                    hr = SampGrabber.SetOneShot(false);
                if (hr == 0)
                    hr = SampGrabber.SetCallback(null, 0);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
            }

            // Update the state now that we are done
            ActualGraphState = GraphState.Created;
        }