Пример #1
0
        private void CloseInterface()
        {
            //그래프와 콘트롤 해제
            if (pMediaControl != null)
            {
                pMediaControl.StopWhenReady();
            }
            if (pVideoWindow != null)
            {
                pVideoWindow.put_Visible(OABool.False);
                pVideoWindow.put_Owner(IntPtr.Zero);
            }

            Marshal.ReleaseComObject(pSampleGrabber);
            pSampleGrabber = null;
            Marshal.ReleaseComObject(pSampleGrabberFilter);
            pSampleGrabberFilter = null;
            Marshal.ReleaseComObject(pGraphBuilder);
            pGraphBuilder = null;
            Marshal.ReleaseComObject(pMediaControl);
            pMediaControl = null;
            Marshal.ReleaseComObject(pVideoWindow);
            pVideoWindow = null;
            Marshal.ReleaseComObject(pMediaPosition);
            pMediaPosition = null;
            //this.Close(); //close this program
        }
Пример #2
0
    // Save the size parameters for use in SnapShot
    private void SaveSizeInfo(DirectShowLib.ISampleGrabber sampGrabber)
    {
        int hr;

        // Get the media type from the SampleGrabber
        DirectShowLib.AMMediaType media = new DirectShowLib.AMMediaType();

        hr = sampGrabber.GetConnectedMediaType(media);

        hr = sampGrabber.SetMediaType(media);
        DsError.ThrowExceptionForHR(hr);

        try
        {
            if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
            {
                throw new NotSupportedException("Unknown Grabber Media Format");
            }

            // Get the struct
            DirectShowLib.VideoInfoHeader videoInfoHeader = new DirectShowLib.VideoInfoHeader();
            Marshal.PtrToStructure(media.formatPtr, videoInfoHeader);

            // Grab the size info
            m_videoWidth  = videoInfoHeader.BmiHeader.Width;
            m_videoHeight = videoInfoHeader.BmiHeader.Height;
            m_stride      = m_videoWidth * (videoInfoHeader.BmiHeader.BitCount / 8);
            m_ImageSize   = m_videoWidth * m_videoHeight * 3;
        }
        finally
        {
            DirectShowLib.DsUtils.FreeAMMediaType(media);
            media = null;
        }
    }
Пример #3
0
    // Shut down capture
    private void CloseInterfaces()
    {
        int hr;

        lock (this)
        {
            if (m_State != GraphState.Exiting)
            {
                m_State = GraphState.Exiting;

                // Release the thread (if the thread was started)
                if (m_mre != null)
                {
                    m_mre.Set();
                }
            }

            if (m_mediaCtrl != null)
            {
                // Stop the graph
                hr          = m_mediaCtrl.Stop();
                m_mediaCtrl = null;
            }

            if (m_sampGrabber != null)
            {
                Marshal.ReleaseComObject(m_sampGrabber);
                m_sampGrabber = null;
            }

#if DEBUG
            if (m_DsRot != null)
            {
                m_DsRot.Dispose();
            }
#endif

            if (m_graphBuilder != null)
            {
                Marshal.ReleaseComObject(m_graphBuilder);
                m_graphBuilder = null;
            }
        }
        GC.Collect();
    }
Пример #4
0
    private void ConfigureSampleGrabber(DirectShowLib.ISampleGrabber sampGrabber)
    {
        DirectShowLib.AMMediaType media;
        int hr;

        // Set the media type to Video/RBG24
        media            = new DirectShowLib.AMMediaType();
        media.majorType  = DirectShowLib.MediaType.Video;
        media.subType    = DirectShowLib.MediaSubType.RGB24;
        media.formatType = DirectShowLib.FormatType.VideoInfo;
        hr = sampGrabber.SetMediaType(media);
        DsError.ThrowExceptionForHR(hr);

        DirectShowLib.DsUtils.FreeAMMediaType(media);
        media = null;

        // Configure the samplegrabber
        hr = sampGrabber.SetCallback(this, 1);
        DsError.ThrowExceptionForHR(hr);
    }
Пример #5
0
    // Shut down capture
    private void CloseInterfaces()
    {
        int hr;

        lock (this)
        {
            if (m_State != GraphState.Exiting)
            {
                m_State = GraphState.Exiting;

                // Release the thread (if the thread was started)
                if (m_mre != null)
                {
                    m_mre.Set();
                }
            }

            if( m_mediaCtrl != null )
            {
                // Stop the graph
                hr = m_mediaCtrl.Stop();
                m_mediaCtrl = null;
            }

            if (m_sampGrabber != null)
            {
                Marshal.ReleaseComObject(m_sampGrabber);
                m_sampGrabber = null;
            }

#if DEBUG
            if (m_DsRot != null)
            {
                m_DsRot.Dispose();
            }
#endif

            if (m_graphBuilder != null)
            {
                Marshal.ReleaseComObject(m_graphBuilder);
                m_graphBuilder = null;
            }
        }
        GC.Collect();
    }
Пример #6
0
	// Build the capture graph for grabber and renderer.</summary>
	// (Control to show video in, Filename to play)
	private void SetupGraph(string FileName)
	{
		int hr;

		// Get the graphbuilder object
		m_graphBuilder = new DirectShowLib.FilterGraph() as DirectShowLib.IFilterGraph2;

		// Get a ICaptureGraphBuilder2 to help build the graph
		DirectShowLib.ICaptureGraphBuilder2 icgb2 = new DirectShowLib.CaptureGraphBuilder2() as DirectShowLib.ICaptureGraphBuilder2;

		try
		{
			// Link the ICaptureGraphBuilder2 to the IFilterGraph2
			hr = icgb2.SetFiltergraph(m_graphBuilder);
			DsError.ThrowExceptionForHR( hr );

#if DEBUG
			// Allows you to view the graph with GraphEdit File/Connect
			m_DsRot = new DsROTEntry(m_graphBuilder);
#endif

			// Add the filters necessary to render the file.  This function will
			// work with a number of different file types.
			IBaseFilter	sourceFilter = null;
			hr = m_graphBuilder.AddSourceFilter(FileName, FileName, out sourceFilter);
			DsError.ThrowExceptionForHR( hr );

			// Get the SampleGrabber interface
			m_sampGrabber = (DirectShowLib.ISampleGrabber) new DirectShowLib.SampleGrabber();
			DirectShowLib.IBaseFilter baseGrabFlt = (DirectShowLib.IBaseFilter)	m_sampGrabber;

			// Configure the Sample Grabber
			ConfigureSampleGrabber(m_sampGrabber);

			// Add it to the filter
			hr = m_graphBuilder.AddFilter( baseGrabFlt, "Ds.NET Grabber" );
			DsError.ThrowExceptionForHR( hr );

			// System.Windows.Forms.MessageBox.Show(Width.ToString(), Height.ToString(), System.Windows.Forms.MessageBoxButtons.OK);

			// A Null Renderer does not display the video
			// But it allows the Sample Grabber to run
			// And it will keep proper playback timing
			// Unless specified otherwise.
			DirectShowLib.NullRenderer nullRenderer = new DirectShowLib.NullRenderer();

			m_graphBuilder.AddFilter((DirectShowLib.IBaseFilter) nullRenderer, "Null Renderer");

			// Connect the pieces together, use the default renderer
			hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, (DirectShowLib.IBaseFilter) nullRenderer);
			DsError.ThrowExceptionForHR( hr );

			// Now that the graph is built, read the dimensions of the bitmaps we'll be getting
			SaveSizeInfo(m_sampGrabber);

			// Configure the Video Window
			//DirectShowLib.IVideoWindow videoWindow = m_graphBuilder as DirectShowLib.IVideoWindow;
			//ConfigureVideoWindow(videoWindow, hWin);

			// Grab some other interfaces
			m_mediaEvent = m_graphBuilder as DirectShowLib.IMediaEvent;
			m_mediaCtrl = m_graphBuilder as DirectShowLib.IMediaControl;
		}
		finally
		{
			if (icgb2 != null)
			{
				Marshal.ReleaseComObject(icgb2);
				icgb2 = null;
			}
		}
#if DEBUG
		// Double check to make sure we aren't releasing something
		// important.
		GC.Collect();
		GC.WaitForPendingFinalizers();
#endif
	}
Пример #7
0
    /// <summary> build the capture graph for grabber. </summary>
    private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight, Control hControl)
    {
        int hr;

        DirectShowLib.ISampleGrabber        sampGrabber = null;
        DirectShowLib.IBaseFilter           capFilter   = null;
        DirectShowLib.ICaptureGraphBuilder2 capGraph    = null;

        // Get the graphbuilder object
        m_graphBuilder = (DirectShowLib.IFilterGraph2) new FilterGraph();
        m_mediaCtrl    = m_graphBuilder as DirectShowLib.IMediaControl;
        try
        {
            // Get the ICaptureGraphBuilder2
            capGraph = (DirectShowLib.ICaptureGraphBuilder2) new DirectShowLib.CaptureGraphBuilder2();

            // Get the SampleGrabber interface
            sampGrabber = (DirectShowLib.ISampleGrabber) new DirectShowLib.SampleGrabber();

            // Start building the graph
            hr = capGraph.SetFiltergraph(m_graphBuilder);
            DsError.ThrowExceptionForHR(hr);

            // Add the video device
            hr = m_graphBuilder.AddSourceFilterForMoniker(dev.Mon, null, "Video input", out capFilter);
            DsError.ThrowExceptionForHR(hr);

            DirectShowLib.IBaseFilter baseGrabFlt = (DirectShowLib.IBaseFilter)sampGrabber;
            ConfigureSampleGrabber(sampGrabber);

            // Add the frame grabber to the graph
            hr = m_graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber");
            DsError.ThrowExceptionForHR(hr);

            // If any of the default config items are set
            if (iFrameRate + iHeight + iWidth > 0)
            {
                SetConfigParms(capGraph, capFilter, iFrameRate, iWidth, iHeight);
            }

            // ------------------------------------

            if (false)
            {
                if (false)
                {
                    hr = capGraph.RenderStream(DirectShowLib.PinCategory.Preview, DirectShowLib.MediaType.Video, capFilter, null, baseGrabFlt);
                    DsError.ThrowExceptionForHR(hr);
                }
                theCompressor = CreateFilter(FilterCategory.VideoCompressorCategory, "Microsoft MPEG-4 Video Codec V2");

                // Add the Video compressor filter to the graph
                if (theCompressor != null)
                {
                    hr = m_graphBuilder.AddFilter(theCompressor, "Compressor Filter");
                    DsError.ThrowExceptionForHR(hr);
                }

                // Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink
                DirectShowLib.IBaseFilter     mux;
                DirectShowLib.IFileSinkFilter sink;
                hr = capGraph.SetOutputFileName(DirectShowLib.MediaSubType.Avi, "C:\\Test.avi", out mux, out sink);
                DsError.ThrowExceptionForHR(hr);

                hr = capGraph.RenderStream(DirectShowLib.PinCategory.Capture, DirectShowLib.MediaType.Video, capFilter, theCompressor, mux);
                DsError.ThrowExceptionForHR(hr);

                Marshal.ReleaseComObject(mux);
                Marshal.ReleaseComObject(sink);

                hr = capGraph.RenderStream(DirectShowLib.PinCategory.Preview, DirectShowLib.MediaType.Video, capFilter, null, null);
                DsError.ThrowExceptionForHR(hr);

                //ShowVideoWindow(hControl);
            }
            else
            {
                hr = capGraph.RenderStream(DirectShowLib.PinCategory.Capture, DirectShowLib.MediaType.Video, capFilter, null, baseGrabFlt);
                DsError.ThrowExceptionForHR(hr);

                //hr = capGraph.RenderStream(DirectShowLib.PinCategory.Preview, DirectShowLib.MediaType.Video, capFilter, null, null);
                //DsError.ThrowExceptionForHR(hr);

                //ShowVideoWindow(hControl);
            }

            // --------------------------------------

            //hr = capGraph.RenderStream( DirectShowLib.PinCategory.Capture, DirectShowLib.MediaType.Video, capFilter, null, baseGrabFlt );
            //DsError.ThrowExceptionForHR( hr );

            SaveSizeInfo(sampGrabber);
        }
        finally
        {
            if (capFilter != null)
            {
                Marshal.ReleaseComObject(capFilter);
                capFilter = null;
            }
            if (sampGrabber != null)
            {
                Marshal.ReleaseComObject(sampGrabber);
                sampGrabber = null;
            }
            if (capGraph != null)
            {
                Marshal.ReleaseComObject(capGraph);
                capGraph = null;
            }
        }
    }
Пример #8
0
        private void SetupGraph(Control hWin, string filename)
        {
            pGraphBuilder   = (DirectShowLib.IGraphBuilder) new FilterGraph();
            pMediaControl   = (DirectShowLib.IMediaControl)pGraphBuilder;
            pVideoWindow    = (DirectShowLib.IVideoWindow)pGraphBuilder;
            pVideoFrameStep = (DirectShowLib.IVideoFrameStep)pGraphBuilder; // video frame...
            pMediaPosition  = (DirectShowLib.IMediaPosition)pGraphBuilder;
            //DirectShowLib.IBaseFilter pBaseFilter = (DirectShowLib.IBaseFilter)pGraphBuilder;

            //pMediaSeeking = (DirectShowLib.IMediaSeeking)pGraphBuilder;
            //pMediaSeeking.SetPositions(5000, AMSeekingSeekingFlags.AbsolutePositioning, 6000, AMSeekingSeekingFlags.AbsolutePositioning);

            //test
            DirectShowLib.ICaptureGraphBuilder2  pCaptureGraphBuilder2;
            DirectShowLib.IBaseFilter            pRenderer;
            DirectShowLib.IVMRFilterConfig9      pIVMRFilterConfig9;
            DirectShowLib.IVMRWindowlessControl9 pVMRWC9;

            pCaptureGraphBuilder2 = (DirectShowLib.ICaptureGraphBuilder2) new CaptureGraphBuilder2();
            pCaptureGraphBuilder2.SetFiltergraph(pGraphBuilder);     // CaptureGraph를  GraphBuilder에 붙인다.

            //pGraphBuilder.AddFilter(pMediaControl "SDZ 375 Source");  // GraphBuilder에 영상장치필터를 추가한다.
            pRenderer          = (DirectShowLib.IBaseFilter) new DirectShowLib.VideoMixingRenderer9(); // 믹서 필터를 생성 한다.
            pIVMRFilterConfig9 = (DirectShowLib.IVMRFilterConfig9)pRenderer;                           // 믹서 필터의 속성을 설정한다.
            pIVMRFilterConfig9.SetRenderingMode(VMR9Mode.Windowless);
            pIVMRFilterConfig9.SetNumberOfStreams(2);

            pVMRWC9 = (DirectShowLib.IVMRWindowlessControl9)pRenderer;              // 오버레이 평면의 속성을 설정한다.
            pVMRWC9.SetVideoClippingWindow(hWin.Handle);
            pVMRWC9.SetBorderColor(0);
            pVMRWC9.SetVideoPosition(null, hWin.ClientRectangle);
            pGraphBuilder.AddFilter(pRenderer, "Video Mixing Renderer");                               // GraphBuilder에 믹스 필터를 추가한다.
            pCaptureGraphBuilder2.RenderStream(null, MediaType.Video, pGraphBuilder, null, pRenderer); // 영상표시를 위한 필터를 설정한다.
            ///test

            //sampleGrabber
            AMMediaType am_media_type = new AMMediaType();

            pSampleGrabber           = (DirectShowLib.ISampleGrabber) new SampleGrabber();
            pSampleGrabberFilter     = (DirectShowLib.IBaseFilter)pSampleGrabber;
            am_media_type.majorType  = MediaType.Video;
            am_media_type.subType    = MediaSubType.RGB24;
            am_media_type.formatType = FormatType.VideoInfo;
            pSampleGrabber.SetMediaType(am_media_type);
            //Graph에 sampleGrabber filter를 추가
            pGraphBuilder.AddFilter(pSampleGrabberFilter, "Sample Grabber");

            pMediaControl.RenderFile(filename);

            pVideoWindow.put_Owner(hWin.Handle);
            pVideoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipSiblings);
            Rectangle rect = hWin.ClientRectangle;

            pVideoWindow.SetWindowPosition(0, 0, rect.Right, rect.Bottom);

            //sampleGrabber2
            pSampleGrabber.GetConnectedMediaType(am_media_type);
            DirectShowLib.VideoInfoHeader pVideoInfoHeader = (DirectShowLib.VideoInfoHeader)Marshal.PtrToStructure(am_media_type.formatPtr, typeof(VideoInfoHeader));
            String str = string.Format("size = {0} x {1}", pVideoInfoHeader.BmiHeader.Width, pVideoInfoHeader.BmiHeader.Height);

            Video_Width   = pVideoInfoHeader.BmiHeader.Width;
            Video_Height  = pVideoInfoHeader.BmiHeader.Height;
            str          += string.Format("sample size = {0}", am_media_type.sampleSize);
            textBox1.Text = str;
            DsUtils.FreeAMMediaType(am_media_type);
            //SetBufferSamples를 실행하지 않으면 버퍼로부터 데이터를 얻을 수 없다.
            //불필요하게 부하를 주고싶지 않은경우 false, 데이터를 얻고싶으면 true
            pSampleGrabber.SetBufferSamples(true);

            //play time
            pMediaPosition = (DirectShowLib.IMediaPosition)pGraphBuilder;
            double Length;

            pMediaPosition.get_Duration(out Length);
            String str2 = string.Format("play time: {0}", Length);

            textBox1.Text = str2;
            pMediaPosition.put_CurrentPosition(5.0); //set current Position



            //2017.05.08
            DirectShowLib.IVMRWindowlessControl9 windowlessCtrl = (DirectShowLib.IVMRWindowlessControl9)pRenderer;
            windowlessCtrl.SetVideoClippingWindow(hWin.Handle);
            IntPtr lpDib;

            windowlessCtrl.GetCurrentImage(out lpDib);
            BitmapInfoHeader head;

            head = (BitmapInfoHeader)Marshal.PtrToStructure(lpDib, typeof(BitmapInfoHeader));
            int         width       = head.Width;
            int         height      = head.Height;
            int         stride      = width * (head.BitCount / 8);
            PixelFormat pixelFormat = PixelFormat.Format24bppRgb;

            switch (head.BitCount)
            {
            case 24: pixelFormat = PixelFormat.Format24bppRgb; break;

            case 32: pixelFormat = PixelFormat.Format32bppRgb; break;

            case 48: pixelFormat = PixelFormat.Format48bppRgb; break;

            default: throw new Exception("Unknown BitCount");
            }

            Bitmap Cap = new Bitmap(width, height, stride, pixelFormat, lpDib);

            Cap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            pictureBox1.Image = Cap;
        }
Пример #9
0
    // Build the capture graph for grabber and renderer.</summary>
    // (Control to show video in, Filename to play)
    private void SetupGraph(string FileName)
    {
        int hr;

        // Get the graphbuilder object
        m_graphBuilder = new DirectShowLib.FilterGraph() as DirectShowLib.IFilterGraph2;

        // Get a ICaptureGraphBuilder2 to help build the graph
        DirectShowLib.ICaptureGraphBuilder2 icgb2 = new DirectShowLib.CaptureGraphBuilder2() as DirectShowLib.ICaptureGraphBuilder2;

        try
        {
            // Link the ICaptureGraphBuilder2 to the IFilterGraph2
            hr = icgb2.SetFiltergraph(m_graphBuilder);
            DsError.ThrowExceptionForHR(hr);

#if DEBUG
            // Allows you to view the graph with GraphEdit File/Connect
            m_DsRot = new DsROTEntry(m_graphBuilder);
#endif

            // Add the filters necessary to render the file.  This function will
            // work with a number of different file types.
            IBaseFilter sourceFilter = null;
            hr = m_graphBuilder.AddSourceFilter(FileName, FileName, out sourceFilter);
            DsError.ThrowExceptionForHR(hr);

            // Get the SampleGrabber interface
            m_sampGrabber = (DirectShowLib.ISampleGrabber) new DirectShowLib.SampleGrabber();
            DirectShowLib.IBaseFilter baseGrabFlt = (DirectShowLib.IBaseFilter)m_sampGrabber;

            // Configure the Sample Grabber
            ConfigureSampleGrabber(m_sampGrabber);

            // Add it to the filter
            hr = m_graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber");
            DsError.ThrowExceptionForHR(hr);

            // System.Windows.Forms.MessageBox.Show(Width.ToString(), Height.ToString(), System.Windows.Forms.MessageBoxButtons.OK);

            // A Null Renderer does not display the video
            // But it allows the Sample Grabber to run
            // And it will keep proper playback timing
            // Unless specified otherwise.
            DirectShowLib.NullRenderer nullRenderer = new DirectShowLib.NullRenderer();

            m_graphBuilder.AddFilter((DirectShowLib.IBaseFilter)nullRenderer, "Null Renderer");

            // Connect the pieces together, use the default renderer
            hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, (DirectShowLib.IBaseFilter)nullRenderer);
            DsError.ThrowExceptionForHR(hr);

            // Now that the graph is built, read the dimensions of the bitmaps we'll be getting
            SaveSizeInfo(m_sampGrabber);

            // Configure the Video Window
            //DirectShowLib.IVideoWindow videoWindow = m_graphBuilder as DirectShowLib.IVideoWindow;
            //ConfigureVideoWindow(videoWindow, hWin);

            // Grab some other interfaces
            m_mediaEvent = m_graphBuilder as DirectShowLib.IMediaEvent;
            m_mediaCtrl  = m_graphBuilder as DirectShowLib.IMediaControl;
        }
        finally
        {
            if (icgb2 != null)
            {
                Marshal.ReleaseComObject(icgb2);
                icgb2 = null;
            }
        }
#if DEBUG
        // Double check to make sure we aren't releasing something
        // important.
        GC.Collect();
        GC.WaitForPendingFinalizers();
#endif
    }