Ejemplo n.º 1
0
        //COMやインタフェースの解放を行う
        public void CloseInterfaces()
        {
            try
            {
                if (mediaControl != null)
                {
                    mediaControl.Stop();
                    Thread.Sleep(100);
                    mediaControl = null;
                }

                if (mediaEvent != null)
                {
                    mediaEvent.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero);
                    mediaEvent = null;
                }

                if (videoWindow != null)
                {
                    videoWindow.put_Visible(OABool.False);
                    videoWindow.put_Owner(IntPtr.Zero);
                    videoWindow = null;
                }

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

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

                if (mVideoChecker != null)
                {
                    mVideoChecker.CloseInterfaces();
                    mVideoChecker = null;
                }
                //if (mAudioChecker != null)
                //{
                //	mAudioChecker.CloseInterfaces();
                //	mAudioChecker = null;
                //}
            }
            catch (Exception e)
            {
                SMMMessageBox.Show("エラー:インタフェースの終了に失敗しました。Error: An error ocouured when closing video interface. " + e.ToString(), SMMMessageBoxIcon.Error);
            }
        }
Ejemplo n.º 2
0
        private bool directShowInitialize()
        {
            try
            {
                mVideoChecker = new VideoChecker(this);
                //mAudioChecker = new AudioChecker(this);

                //mVideoChecker.Test();

                // 3. フィルタグラフマネージャを作成し,各種操作を行うためのインタフェースを取得する.

                //graphBuilderを作成.
                graphBuilder = GraphFactory.MakeGraphBuilder();

                //各種インタフェースを取得.
                mediaControl = (IMediaControl)graphBuilder;
                videoWindow  = (IVideoWindow)graphBuilder;
                mediaEvent   = (IMediaEventEx)graphBuilder;


                // 4. キャプチャグラフビルダと,サンプルグラバフィルタ(個々のビデオデータを取得するフィルタ)を作成する.

                //キャプチャグラフビルダ(captureGraphBuilder)を作成.
                captureGraphBuilder = GraphFactory.MakeCaptureGraphBuilder();

                // 5. 基本となるフィルタグラフマネージャ(graphBuilder)にキャプチャグラフビルダと各フィルタを追加する.

                //captureGraphBuilder(キャプチャグラフビルダ)をgraphBuilder(フィルタグラフマネージャ)に追加.
                result = captureGraphBuilder.SetFiltergraph(graphBuilder);
                if (result < 0)
                {
                    Marshal.ThrowExceptionForHR(result);
                }


                // 1. デバイスを取得し、2. キャプチャデバイスをソースフィルタに対応づける.
                // 6. 各フィルタの接続を行い,入力画像のキャプチャとプレビューの準備を行う.
                bool ret = mVideoChecker.AddVideoFilters(graphBuilder, captureGraphBuilder);
                if (!ret)
                {
                    //SMMMessageBox.Show("エラー:映像入力デバイスが見つかりませんでした。\nプログラムを終了します。Error: Any video devices are not found.\n Closing this application.", SMMMessageBoxIcon.Error);
                    return(false);
                }

                //ret = mAudioChecker.AddAudioFilters(graphBuilder, captureGraphBuilder);
                //if (!ret) {
                //	SMMMessageBox.Show("エラー:音声入力デバイスが見つかりませんでした。\nプログラムを終了します。Error: Any audio devices are not found.\n Closing this application.", SMMMessageBoxIcon.Error);
                //	return false;
                //}

                // 7. プレビュー映像(レンダラフィルタの出力)の出力場所を設定する.

                //プレビュー映像を表示するパネルを指定.
                result = videoWindow.put_Owner(this.panel1.Handle);
                if (result < 0)
                {
                    Marshal.ThrowExceptionForHR(result);
                }
                //ビデオ表示領域のスタイルを指定.
                result = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren);
                if (result < 0)
                {
                    Marshal.ThrowExceptionForHR(result);
                }
                //ビデオパネルのサイズを変更する.
                Rectangle rect = this.panel1.ClientRectangle;
                videoWindow.SetWindowPosition(0, 0, rect.Right, rect.Bottom);

                //レンダラフィルタの出力を可視化する.
                result = videoWindow.put_Visible(OABool.True);
                if (result < 0)
                {
                    Marshal.ThrowExceptionForHR(result);
                }



                // 8. DirectShowイベントを,Windowsメッセージを通して通知するための設定を行う.

                // mediaEvent(DirectShowイベント)をWM_GRAPHNOTIFY(Windowsメッセージ)に対応付ける.
                result = mediaEvent.SetNotifyWindow(this.Handle, WM_GRAPHNOTIFY, IntPtr.Zero);
                if (result < 0)
                {
                    Marshal.ThrowExceptionForHR(result);
                }



                // 9. プレビューを開始する.

                result = mediaControl.Run();
                if (result < 0)
                {
                    Marshal.ThrowExceptionForHR(result);
                }
            } catch (Exception e)
            {
                SMMMessageBox.Show(e.Message, SMMMessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }