Ejemplo n.º 1
0
        public bool AddVideoFilters(IGraphBuilder graphBuilder, ICaptureGraphBuilder2 captureGraphBuilder)
        {
            bool ret = SetDevice();

            if (!ret)
            {
                return(false);
            }

            //サンプルグラバ(videoSampleGrabber)を作成
            mVideoSampleGrabber = GraphFactory.MakeSampleGrabber();

            //フィルタと関連付ける.
            mVideoGrabFilter = (IBaseFilter)mVideoSampleGrabber;

            //キャプチャするビデオデータのフォーマットを設定.
            AMMediaType amMediaType = new AMMediaType();

            amMediaType.majorType  = MediaType.Video;
            amMediaType.subType    = MediaSubType.RGB24;
            amMediaType.formatType = FormatType.VideoInfo;

            int result;

            result = mVideoSampleGrabber.SetMediaType(amMediaType);
            if (result < 0)
            {
                Marshal.ThrowExceptionForHR(result);
            }

            // 映像のcaptureFilter(ソースフィルタ)をgraphBuilder(フィルタグラフマネージャ)に追加.
            result = graphBuilder.AddFilter(mVideoCaptureFilter, "Video Capture Device");
            if (result < 0)
            {
                Marshal.ThrowExceptionForHR(result);
            }

            //videoGrabFilter(変換フィルタ)をgraphBuilder(フィルタグラフマネージャ)に追加.
            result = graphBuilder.AddFilter(mVideoGrabFilter, "Video Grab Filter");
            //result = graphBuilder.AddFilter(videoGrabFilter, "Frame Grab Filter");
            if (result < 0)
            {
                Marshal.ThrowExceptionForHR(result);
            }

            // 映像のキャプチャフィルタをサンプルグラバーフィルタに接続する.
            result = captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, mVideoCaptureFilter, null, mVideoGrabFilter);
            if (result < 0)
            {
                Marshal.ThrowExceptionForHR(result);
            }

            // 映像のキャプチャフィルタをデフォルトのレンダラフィルタ(ディスプレイ上に出力)に接続する.(プレビュー)
            result = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, mVideoCaptureFilter, null, null);
            if (result < 0)
            {
                Marshal.ThrowExceptionForHR(result);
            }

            // フレームキャプチャの設定が完了したかを確認する.
            amMediaType = new AMMediaType();
            result      = mVideoSampleGrabber.GetConnectedMediaType(amMediaType);
            if (result < 0)
            {
                Marshal.ThrowExceptionForHR(result);
            }
            if ((amMediaType.formatType != FormatType.VideoInfo) || (amMediaType.formatPtr == IntPtr.Zero))
            {
                throw new NotSupportedException("エラー:キャプチャできない映像メディアフォーマットです.Error: This video media format cannnot be caputered.");
            }

            // キャプチャするビデオデータのフォーマットから,videoInfoHeaderを作成する.
            mVideoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(amMediaType.formatPtr, typeof(VideoInfoHeader));
            Marshal.FreeCoTaskMem(amMediaType.formatPtr);
            amMediaType.formatPtr = IntPtr.Zero;

            // フィルタ内を通るサンプルをバッファにコピーしないように指定する.
            result = mVideoSampleGrabber.SetBufferSamples(false);
            // サンプルを一つ(1フレーム)受け取ったらフィルタを停止するように指定する.
            if (result == 0)
            {
                result = mVideoSampleGrabber.SetOneShot(false);
            }
            // コールバック関数の利用を停止する.
            if (result == 0)
            {
                result = mVideoSampleGrabber.SetCallback(null, 0);
            }
            if (result < 0)
            {
                Marshal.ThrowExceptionForHR(result);
            }

            return(true);
        }