Ejemplo n.º 1
0
        public PreferencesForm()
        {
            InitializeComponent();

            // 現環境のデバイスリスト取得
            string[] videoDeviceNameList = GraphFactory.GetDeviceNameList(FilterCategory.VideoInputDevice);
            //string[] audioDeviceNameList = GraphFactory.GetDeviceNameList(FilterCategory.AudioInputDevice);

            ComboBox_VideoDeviceName.Items.Clear();
            if (videoDeviceNameList != null && videoDeviceNameList.Length > 0)
            {
                // デバイスリストをコンボボックスにセット
                ComboBox_VideoDeviceName.Items.AddRange(videoDeviceNameList);
            }
            //ComboBox_AudioDeviceName.Items.Clear();
            //if (audioDeviceNameList != null && audioDeviceNameList.Length > 0)
            //{
            //	// デバイスリストをコンボボックスにセット
            //	ComboBox_AudioDeviceName.Items.AddRange(audioDeviceNameList);
            //}

            // 設定ファイルの内容を画面に反映
            Preferences option = Preferences.Instance;

            if (!string.IsNullOrEmpty(option.mVideoDeviceName))
            {
                foreach (string deviceName in ComboBox_VideoDeviceName.Items)
                {
                    string defaultDeviceName = option.mVideoDeviceName;
                    if (deviceName == defaultDeviceName)
                    {
                        ComboBox_VideoDeviceName.SelectedItem = deviceName;
                        break;
                    }
                }
            }
            //if (!string.IsNullOrEmpty(option.mAudioDeviceName))
            //{
            //	foreach (string deviceName in ComboBox_AudioDeviceName.Items)
            //	{
            //		string defaultDeviceName = option.mAudioDeviceName;
            //		if (deviceName == defaultDeviceName)
            //		{
            //			ComboBox_AudioDeviceName.SelectedItem = deviceName;
            //			break;
            //		}
            //	}
            //}
        }
Ejemplo n.º 2
0
        private bool SetDevice()
        {
            DsDevice audioDevice = GraphFactory.GetDevice(FilterCategory.AudioInputDevice, GraphFactory.DEVICE_TYPE.AUDIO);

            if (audioDevice == null)
            {
                SMMMessageBox.Show("エラー:音声デバイスが選択されませんでした。Error: Audio device is not selected.", SMMMessageBoxIcon.Warning);
                return(false);
            }

            // 音声のキャプチャデバイス(device)とソースフィルタ(captureFilter)を対応付ける.
            mAudioCaptureFilter = GraphFactory.GetCaptureFilter(audioDevice);

            return(true);
        }
Ejemplo n.º 3
0
        private bool SetDevice()
        {
            // 1. デバイスを取得
            DsDevice videoDevice = GraphFactory.GetDevice(FilterCategory.VideoInputDevice, GraphFactory.DEVICE_TYPE.VIDEO);

            if (videoDevice == null)
            {
                SMMMessageBox.Show("エラー:映像デバイスが選択されませんでした。Error: Video device is not selected.", SMMMessageBoxIcon.Warning);
                return(false);
            }

            // 2. キャプチャデバイスをソースフィルタに対応づける.

            // 映像のキャプチャデバイス(device)とソースフィルタ(captureFilter)を対応付ける.
            mVideoCaptureFilter = GraphFactory.GetCaptureFilter(videoDevice);

            return(true);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }