Example #1
0
        public static void AddTransportStreamFiltersToGraph(IFilterGraph2 graphBuilder, out IBaseFilter bdaTIF, out IBaseFilter bdaSecTab)
        {
            int hr = 0;

            DsDevice[] devices;

            bdaTIF    = null;
            bdaSecTab = null;

            devices = DsDevice.GetDevicesOfCat(FilterCategory.BDATransportInformationRenderersCategory);
            for (int i = 0; i < devices.Length; i++)
            {
                if (devices[i].Name.Equals("BDA MPEG2 Transport Information Filter"))
                {
                    hr = graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out bdaTIF);
                    DsError.ThrowExceptionForHR(hr);
                    continue;
                }

                if (devices[i].Name.Equals("MPEG-2 Sections and Tables"))
                {
                    hr = graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out bdaSecTab);
                    DsError.ThrowExceptionForHR(hr);
                    continue;
                }
            }
        }
Example #2
0
        public static void AddNetworkFiltersToGraph(IFilterGraph2 graphBuilder, out IBaseFilter bdaMPE, out IBaseFilter bdaIPSink)
        {
            int hr = 0;

            DsDevice[] devices;

            bdaMPE    = null;
            bdaIPSink = null;

            devices = DsDevice.GetDevicesOfCat(FilterCategory.BDAReceiverComponentsCategory);
            foreach (DsDevice device in devices)
            {
                if (device.Name.IndexOf("BDA MPE") != -1)
                {
                    hr = graphBuilder.AddSourceFilterForMoniker(device.Mon, null, "BDA MPE Filter", out bdaMPE);
                    DsError.ThrowExceptionForHR(hr);
                    break;
                }
            }

            devices = DsDevice.GetDevicesOfCat(FilterCategory.BDARenderingFiltersCategory);
            foreach (DsDevice device in devices)
            {
                if (device.Name.Equals("BDA IP Sink"))
                {
                    hr = graphBuilder.AddSourceFilterForMoniker(device.Mon, null, "BDA IP Sink", out bdaIPSink);
                    DsError.ThrowExceptionForHR(hr);
                    break;
                }
            }
        }
Example #3
0
        private IBaseFilter BuildPreviewOnlyCaptureGraph(DsDevice dev, VideoFormatHelper.SupportedVideoFormat selectedFormat, ref float iFrameRate, ref int iWidth, ref int iHeight)
        {
            // Capture Source (Capture/Video) --> (Input) Sample Grabber (Output) --> (In) Null Renderer

            IBaseFilter nullRenderer = null;

            try
            {
                IBaseFilter capFilter;

                // Add the video device
                int hr = filterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
                DsError.ThrowExceptionForHR(hr);

                if (capFilter != null)
                {
                    SetConfigParms(capBuilder, capFilter, selectedFormat, ref iFrameRate, ref iWidth, ref iHeight);
                }

                IBaseFilter baseGrabFlt = (IBaseFilter)samplGrabber;
                ConfigureSampleGrabber(samplGrabber);

                hr = filterGraph.AddFilter(baseGrabFlt, "ASCOM Video Grabber");
                DsError.ThrowExceptionForHR(hr);

                // Connect the video device output to the sample grabber
                IPin videoCaptureOutputPin = FindPin(capFilter, PinDirection.Output, MediaType.Video, PinCategory.Capture, "Capture");
                IPin grabberInputPin       = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);
                hr = filterGraph.Connect(videoCaptureOutputPin, grabberInputPin);
                DsError.ThrowExceptionForHR(hr);
                Marshal.ReleaseComObject(videoCaptureOutputPin);
                Marshal.ReleaseComObject(grabberInputPin);

                // Add the frame grabber to the graph
                nullRenderer = (IBaseFilter) new NullRenderer();
                hr           = filterGraph.AddFilter(nullRenderer, "ASCOM Video Null Renderer");
                DsError.ThrowExceptionForHR(hr);

                // Connect the sample grabber to the null renderer (so frame samples will be coming through)
                IPin grabberOutputPin = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);
                IPin renderedInputPin = DsFindPin.ByDirection(nullRenderer, PinDirection.Input, 0);
                hr = filterGraph.Connect(grabberOutputPin, renderedInputPin);
                DsError.ThrowExceptionForHR(hr);
                Marshal.ReleaseComObject(grabberOutputPin);
                Marshal.ReleaseComObject(renderedInputPin);

                return(capFilter);
            }
            finally
            {
                if (nullRenderer != null)
                {
                    Marshal.ReleaseComObject(nullRenderer);
                }
            }
        }
Example #4
0
        private void BuildGraph()
        {
            int hr = 0;

            DsDevice [] devs;

            graphBuilder = (IFilterGraph2) new FilterGraph();
            rot          = new DsROTEntry(graphBuilder);

            vmr9 = (IBaseFilter) new VideoMixingRenderer9();
            hr   = graphBuilder.AddFilter(vmr9, "VMR9");

            devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            DsDevice dev = devs[0];

            hr = graphBuilder.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out filter);
            DsError.ThrowExceptionForHR(hr);

            captureGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
            hr           = captureGraph.SetFiltergraph(graphBuilder);
            DsError.ThrowExceptionForHR(hr);

            hr = captureGraph.RenderStream(PinCategory.Capture, MediaType.Video, filter, null, vmr9);
            DsError.ThrowExceptionForHR(hr);

            object tmp;

            hr = captureGraph.FindInterface(null, null, filter, typeof(IAMTVTuner).GUID, out tmp);
            DsError.ThrowExceptionForHR(hr);
            tuner = (IAMTVTuner)tmp;

            mediaControl = (IMediaControl)graphBuilder;
            mediaControl.Run();
        }
        void BuildGraph()
        {
            int                   hr;
            IBaseFilter           ppbf, ppFilter;
            ICaptureGraphBuilder2 icgb2;

            DsDevice [] devs;

            IGraphBuilder graphBuilder = new FilterGraph() as IGraphBuilder;

            icgb2 = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
            icgb2.SetFiltergraph(graphBuilder);

            DsROTEntry    ds   = new DsROTEntry(graphBuilder);
            IFilterGraph2 ifg2 = graphBuilder as IFilterGraph2;

            devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            DsDevice dev = devs[0];

            hr = ifg2.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out ppFilter);
            DsError.ThrowExceptionForHR(hr);

            // Use the ICaptureGraphBuilder2 to add the avi mux
            hr = icgb2.SetOutputFileName(MediaSubType.Avi, FileName, out ppbf, out m_ppsink);
            DsError.ThrowExceptionForHR(hr);

            hr = icgb2.RenderStream(PinCategory.Capture, MediaType.Video, ppFilter, null, ppbf);
            DsError.ThrowExceptionForHR(hr);
        }
        void BuildGraph()
        {
            int         hr;
            IBaseFilter ppFilter;

            DsDevice []   devs;
            IGraphBuilder graphBuilder = new FilterGraph() as IGraphBuilder;

            m_ROT = new DsROTEntry(graphBuilder);
            IFilterGraph2 ifg2 = graphBuilder as IFilterGraph2;

            devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            DsDevice dev = devs[0];

            hr = ifg2.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out ppFilter);
            DsError.ThrowExceptionForHR(hr);

            m_idf = ppFilter as IAMDroppedFrames;

            IPin IPinOut = DsFindPin.ByDirection(ppFilter, PinDirection.Output, 0);

            hr = ifg2.Render(IPinOut);
            DsError.ThrowExceptionForHR(hr);

            m_imc = graphBuilder as IMediaControl;
            hr    = m_imc.Run();
            DsError.ThrowExceptionForHR(hr);
        }
Example #7
0
        private void Configure()
        {
            int         hr;
            IBaseFilter pFilter;
            IBaseFilter pRender = (IBaseFilter) new VideoRendererDefault();

            ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

            m_FilterGraph = (IFilterGraph2) new FilterGraph();
            DsROTEntry ds = new DsROTEntry(m_FilterGraph);

            hr = icgb.SetFiltergraph(m_FilterGraph);
            DsError.ThrowExceptionForHR(hr);

            DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            hr = m_FilterGraph.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out pFilter);
            DsError.ThrowExceptionForHR(hr);

            hr = m_FilterGraph.AddFilter(pRender, "renderererer");
            DsError.ThrowExceptionForHR(hr);

            hr = icgb.RenderStream(null, null, pFilter, null, pRender);
            DsError.ThrowExceptionForHR(hr);

            m_fmf1 = (IAMFilterMiscFlags)pRender;
            m_fmf2 = (IAMFilterMiscFlags)pFilter;

            Marshal.ReleaseComObject(icgb);
        }
        private void Configure()
        {
            // In order to lock a profile, you have to have at least one stream
            // connected to the sink. I connect a video thru the DVVideoEnc into
            // the StreamBufferSink.
            int hr;

            m_pRender = (IBaseFilter) new VideoRendererDefault();

            ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

            m_FilterGraph = (IFilterGraph2) new FilterGraph();
            DsROTEntry ds = new DsROTEntry(m_FilterGraph);

            hr = icgb.SetFiltergraph(m_FilterGraph);
            DsError.ThrowExceptionForHR(hr);

            DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            hr = m_FilterGraph.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out m_pFilter);
            DsError.ThrowExceptionForHR(hr);

            hr = m_FilterGraph.AddFilter(m_pRender, "renderererer");
            DsError.ThrowExceptionForHR(hr);

            hr = icgb.RenderStream(null, null, m_pFilter, null, m_pRender);
            DsError.ThrowExceptionForHR(hr);

            m_fc = (IFilterChain)m_FilterGraph;

            Marshal.ReleaseComObject(icgb);
        }
        void BuildGraph()
        {
            int         hr;
            IBaseFilter ppFilter;

            DsDevice []   devs;
            IGraphBuilder graphBuilder = new FilterGraph() as IGraphBuilder;

            m_ROT = new DsROTEntry(graphBuilder);
            IFilterGraph2 ifg2 = graphBuilder as IFilterGraph2;

            devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            DsDevice dev = devs[0];

            hr = ifg2.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out ppFilter);
            DsError.ThrowExceptionForHR(hr);

            ICaptureGraphBuilder2 captureGraphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;

            hr = captureGraphBuilder.SetFiltergraph(graphBuilder);

            object o;

            hr = captureGraphBuilder.FindInterface(null, null, ppFilter, typeof(IAMStreamConfig).GUID, out o);
            DsError.ThrowExceptionForHR(hr);

            m_asc = o as IAMStreamConfig;

            //m_imc = graphBuilder as IMediaControl;
            //hr = m_imc.Run();
            //DsError.ThrowExceptionForHR(hr);
        }
Example #10
0
        /// <summary>
        /// Adds video source filter to the filter graph.
        /// </summary>
        private void AddFilter_Source()
        {
            CaptureFilter = null;
            int hr = FilterGraph.AddSourceFilterForMoniker(_CameraMoniker, null, "Source Filter", out CaptureFilter);

            DsError.ThrowExceptionForHR(hr);

            // Pins used in graph
            IPin pinSourceCapture = null;

            try
            {
                pinSourceCapture = DsFindPin.ByDirection(CaptureFilter, PinDirection.Output, 0);
                SetSourceParams(pinSourceCapture, _FrameSize);
            }
            catch
            {
                throw;
            }
            finally
            {
                SafeReleaseComObject(pinSourceCapture);
            }
            return;
        }
        static void Main()
        {
            using (System.Threading.Mutex mutex = new System.Threading.Mutex(false, "Global\\" + appGuid))
            {
                if (!mutex.WaitOne(0, false))
                {
                    return;
                }

                DsDevice[] capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);

                foreach (var device in capDevices)
                {
                    if (device.DevicePath == @"@device:pnp:\\?\pci#ven_1131&dev_7160
                           &subsys_12abf50a&rev_03#6&37bccbbe&0&000800e1#{65e8773d-8f56
                           -11d0-a3b9-00a0c9223196}\{6f814be9-9af6-43cf
                           -9249-c0340100021c}")
                    {
                        IFilterGraph2 m_FilterGraph = (IFilterGraph2) new FilterGraph();

                        IBaseFilter           capFilter = null;
                        ICaptureGraphBuilder2 capGraph  = null;

                        capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

                        int hr;

                        hr = capGraph.SetFiltergraph(m_FilterGraph);
                        hr = m_FilterGraph.AddSourceFilterForMoniker(device.Mon, null, device.Name, out capFilter);

                        IAMAnalogVideoDecoder videoDec = capFilter as IAMAnalogVideoDecoder;

                        bool signalDetected = false;

                        hr = videoDec.get_HorizontalLocked(out signalDetected);

                        if (signalDetected == true)
                        {
                            System.Diagnostics.Process.Start(
                                @"C:\Users\PC\Documents\HIDDEN_FOLDER\WirecastLaunch.wcst");

                            return;
                        }
                        else
                        {
                            // Poll for 'signal' change
                        }

                        break;
                    }
                }

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
        /// <summary>
        /// Initializes the devices and prepares for capture
        /// </summary>
        public void Initialize()
        {
            _filterGraph = new FilterGraph() as IFilterGraph2;
            _mediaCtrl   = _filterGraph as IMediaControl;

            ISampleGrabber        sampGrabber = null;
            IBaseFilter           capFilter   = null;
            ICaptureGraphBuilder2 capGraph    = null;
            int status;

            try
            {
                capGraph    = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;
                sampGrabber = new SampleGrabber() as ISampleGrabber;

                status = capGraph.SetFiltergraph(_filterGraph);
                DsError.ThrowExceptionForHR(status);

                //Adds the video device
                status = _filterGraph.AddSourceFilterForMoniker(_device.DsDevice.Mon, null, "Video input", out capFilter);
                DsError.ThrowExceptionForHR(status);

                IBaseFilter baseGrabFilter = sampGrabber as IBaseFilter;
                ConfigureSampleGrabber(sampGrabber);

                // Add the frame grabber to the graph
                status = _filterGraph.AddFilter(baseGrabFilter, ".net grabber");
                DsError.ThrowExceptionForHR(status);

                //TODO: Set Framerate, height and width here

                status = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, null, baseGrabFilter);
                DsError.ThrowExceptionForHR(status);

                SaveSizeInfo(sampGrabber);

                _bitmapMemory = Marshal.AllocCoTaskMem(_stride * _videoHeight);
            }
            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;
                }
            }
        }
Example #13
0
        /// <summary> build the capture graph. </summary>
        ///
        //publicにすればShowCapPinDialogで参照して使えるかも
        private void SetupGraph(DsDevice dev, string szOutputFileName)
        {
            int hr;

            IBaseFilter capFilter = null;
            IBaseFilter asfWriter = null;

            //ICaptureGraphBuilder2 capGraph = null;
            //ビデオキャプチャ&編集用のメソッドを備えたキャプチャグラフビルダ

            // Get the graphbuilder object
            m_FilterGraph = (IFilterGraph2) new FilterGraph();

#if DEBUG
            m_rot = new DsROTEntry(m_FilterGraph);
#endif

            try
            {
                // Get the ICaptureGraphBuilder2
                capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

                // Start building the graph
                hr = capGraph.SetFiltergraph(m_FilterGraph);
                Marshal.ThrowExceptionForHR(hr);

                // Add the capture device to the graph
                hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
                Marshal.ThrowExceptionForHR(hr);

                asfWriter = ConfigAsf(capGraph, szOutputFileName);

                hr = capGraph.RenderStream(null, null, capFilter, null, asfWriter);
                Marshal.ThrowExceptionForHR(hr);

                m_mediaCtrl = m_FilterGraph as IMediaControl;
            }
            finally
            {
                if (capFilter != null)
                {
                    Marshal.ReleaseComObject(capFilter);
                    capFilter = null;
                }
                if (asfWriter != null)
                {
                    Marshal.ReleaseComObject(asfWriter);
                    asfWriter = null;
                }
                if (capGraph != null)
                {
                    Marshal.ReleaseComObject(capGraph);
                    capGraph = null;
                }
            }
        }
        private void Configure()
        {
            // In order to lock a profile, you have to have at least one stream
            // connected to the sink. I connect a video thru the DVVideoEnc into
            // the StreamBufferSink.
            const string FileName = "delme.out";

            int         hr;
            IBaseFilter pFilter;
            object      o;

            ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
            StreamBufferSink      sbk  = new StreamBufferSink();

            m_FilterGraph = (IFilterGraph2) new FilterGraph();
            DsROTEntry ds = new DsROTEntry(m_FilterGraph);

            hr = icgb.SetFiltergraph(m_FilterGraph);
            DsError.ThrowExceptionForHR(hr);

            hr = m_FilterGraph.AddFilter((IBaseFilter)sbk, "StreamBufferSink");
            DsError.ThrowExceptionForHR(hr);

            DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            hr = m_FilterGraph.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out pFilter);
            DsError.ThrowExceptionForHR(hr);

            DVVideoEnc  dve         = new DVVideoEnc();
            IBaseFilter ibfVideoEnc = (IBaseFilter)dve;

            hr = m_FilterGraph.AddFilter(ibfVideoEnc, "dvenc");
            DsError.ThrowExceptionForHR(hr);

            hr = icgb.RenderStream(null, null, pFilter, ibfVideoEnc, (IBaseFilter)sbk);
            DsError.ThrowExceptionForHR(hr);

            Marshal.ReleaseComObject(pFilter);
            Marshal.ReleaseComObject(icgb);

            IStreamBufferSink isbc = (IStreamBufferSink)sbk;

            hr = isbc.LockProfile("delme.prf");
            DsError.ThrowExceptionForHR(hr);

            File.Delete(FileName);

            hr = isbc.CreateRecorder("delme.out", RecordingType.Content, out o);
            DsError.ThrowExceptionForHR(hr);

            // Make sure we really got a recorder object
            IStreamBufferRecordingAttribute sbra = (IStreamBufferRecordingAttribute)o;

            hr = sbra.EnumAttributes(out m_sbra);
            DsError.ThrowExceptionForHR(hr);
        }
Example #15
0
        //DirectShow整個 Rendering Pipe 被稱為 Graph
        //觀念類似於RF電路圖,要串起整個電路才能正確render video stream
        //https://www.twblogs.net/a/5d04e5f9bd9eee487be9b034
        public void SetupDirectShowGraph(DsDevice dev, Control render_target)
        {
            IBaseFilter cap_filter = null;

            GraphBuilder = new FilterGraph() as IFilterGraph2;
            //將Capture Device封裝成Filter,塞進整個電路
            GraphBuilder.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out cap_filter);
            VmrRenderer = (IBaseFilter) new VideoMixingRenderer9();
            SetupVideoStream(GraphBuilder, cap_filter, VmrRenderer, render_target);
            CurrentDevice = dev;
        }
Example #16
0
        //DirectShow整個 Rendering Pipe 被稱為 Graph
        //觀念類似於RF電路圖,要串起整個電路才能正確render video stream
        //https://www.twblogs.net/a/5d04e5f9bd9eee487be9b034
        public void Setup(DsDevice dev, IntPtr render_handle, Rectangle render_rect)
        {
            IBaseFilter cap_filter = null;

            GraphBuilder = new FilterGraph() as IFilterGraph2;
            //將Capture Device封裝成Filter,塞進整個電路
            GraphBuilder.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out cap_filter);
            VmrRenderer = (IBaseFilter) new VideoMixingRenderer9();
            SetupVideoStream(GraphBuilder, cap_filter, VmrRenderer, render_handle, render_rect);
            CurrentDevice = dev;
        }
Example #17
0
        private void Configure()
        {
            // In order to lock a profile, you have to have at least one stream
            // connected to the sink. I connect a video thru the DVVideoEnc into
            // the StreamBufferSink.
            int hr;
            int v;
            int oldv;

            IBaseFilter pFilter;
            IBaseFilter pRender = (IBaseFilter) new VideoRendererDefault();

            ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

            m_FilterGraph = (IFilterGraph2) new FilterGraph();
            DsROTEntry    ds  = new DsROTEntry(m_FilterGraph);
            IGraphVersion igv = (IGraphVersion)m_FilterGraph;

            hr = igv.QueryVersion(out v);
            DsError.ThrowExceptionForHR(hr);
            Debug.Assert(v == 0, "Version1");
            oldv = v;

            hr = icgb.SetFiltergraph(m_FilterGraph);
            DsError.ThrowExceptionForHR(hr);

            DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            hr = m_FilterGraph.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out pFilter);
            DsError.ThrowExceptionForHR(hr);

            hr = igv.QueryVersion(out v);
            DsError.ThrowExceptionForHR(hr);
            Debug.Assert(v > oldv, "Version2");
            oldv = v;

            hr = m_FilterGraph.AddFilter(pRender, "renderererer");
            DsError.ThrowExceptionForHR(hr);

            hr = igv.QueryVersion(out v);
            DsError.ThrowExceptionForHR(hr);
            Debug.Assert(v > oldv, "Version2");
            oldv = v;

            hr = icgb.RenderStream(null, null, pFilter, null, pRender);
            DsError.ThrowExceptionForHR(hr);

            hr = igv.QueryVersion(out v);
            DsError.ThrowExceptionForHR(hr);
            Debug.Assert(v > oldv, "Version4");
            oldv = v;

            Marshal.ReleaseComObject(icgb);
        }
        private void CMB_videosources_SelectedIndexChanged(object sender, EventArgs e)
        {
            int                   hr;
            int                   count;
            int                   size;
            object                o;
            IBaseFilter           capFilter = null;
            ICaptureGraphBuilder2 capGraph  = null;
            AMMediaType           media     = null;
            VideoInfoHeader       v;
            VideoStreamConfigCaps c;
            List <GCSBitmapInfo>  modes = new List <GCSBitmapInfo>();

            // Get the ICaptureGraphBuilder2
            capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
            IFilterGraph2 m_FilterGraph = (IFilterGraph2) new FilterGraph();

            DsDevice[] capDevices;
            capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);

            // Add the video device
            hr = m_FilterGraph.AddSourceFilterForMoniker(capDevices[CMB_videosources.SelectedIndex].Mon, null, "Video input", out capFilter);
            DsError.ThrowExceptionForHR(hr);

            // Find the stream config interface
            hr = capGraph.FindInterface(PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMStreamConfig).GUID, out o);
            DsError.ThrowExceptionForHR(hr);

            IAMStreamConfig videoStreamConfig = o as IAMStreamConfig;

            if (videoStreamConfig == null)
            {
                throw new Exception("Failed to get IAMStreamConfig");
            }

            hr = videoStreamConfig.GetNumberOfCapabilities(out count, out size);
            DsError.ThrowExceptionForHR(hr);
            IntPtr TaskMemPointer = Marshal.AllocCoTaskMem(size);

            for (int i = 0; i < count; i++)
            {
                IntPtr ptr = IntPtr.Zero;

                hr = videoStreamConfig.GetStreamCaps(i, out media, TaskMemPointer);
                v  = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader));
                c  = (VideoStreamConfigCaps)Marshal.PtrToStructure(TaskMemPointer, typeof(VideoStreamConfigCaps));
                modes.Add(new GCSBitmapInfo(v.BmiHeader.Width, v.BmiHeader.Height, c.MaxFrameInterval, c.VideoStandard.ToString(), media));
            }
            Marshal.FreeCoTaskMem(TaskMemPointer);
            DsUtils.FreeAMMediaType(media);

            CMB_videoresolutions.DataSource = modes;
        }
        private void BuildGraph()
        {
            int hr = 0;

            graphBuilder = (IFilterGraph2) new FilterGraph();
            rot          = new DsROTEntry(graphBuilder);

            // Assume that the first device in this category IS a BDA device...
            DsDevice[] devices = DsDevice.GetDevicesOfCat(FilterCategory.BDASourceFiltersCategory);

            hr = graphBuilder.AddSourceFilterForMoniker(devices[0].Mon, null, devices[0].Name, out bdaTuner);
            DsError.ThrowExceptionForHR(hr);
        }
        void BuildGraph()
        {
            int         hr;
            IBaseFilter ppFilter;

            DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            DsDevice    dev  = devs[0];

            IGraphBuilder graphBuilder = new FilterGraph() as IGraphBuilder;
            DsROTEntry    ds           = new DsROTEntry(graphBuilder);
            IFilterGraph2 ifg2         = graphBuilder as IFilterGraph2;

            hr = ifg2.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out ppFilter);
            DsError.ThrowExceptionForHR(hr);

            m_ps = ppFilter as IKsPropertySet;
        }
        /// <summary>
        /// Creates the tuner filter instance
        /// </summary>
        /// <param name="graph">The stored graph</param>
        /// <param name="graphBuilder">The graphbuilder</param>
        /// <returns>true, if the graph building was successful</returns>
        public bool CreateFilterInstance(Graph graph, IFilterGraph2 graphBuilder)
        {
            Log.Log.WriteFile("analog: AddTvTunerFilter {0}", _tunerDevice.Name);
            if (DevicesInUse.Instance.IsUsed(_tunerDevice))
            {
                return(false);
            }
            IBaseFilter tmp;
            int         hr;

            try
            {
                hr = graphBuilder.AddSourceFilterForMoniker(_tunerDevice.Mon, null, _tunerDevice.Name, out tmp);
            }
            catch (Exception)
            {
                Log.Log.WriteFile("analog: cannot add filter to graph");
                return(false);
            }
            if (hr != 0)
            {
                Log.Log.Error("analog: AddTvTunerFilter failed:0x{0:X}", hr);
                throw new TvException("Unable to add tvtuner to graph");
            }
            _filterTvTuner = tmp;
            DevicesInUse.Instance.Add(_tunerDevice);
            _tuner = _filterTvTuner as IAMTVTuner;
            if (string.IsNullOrEmpty(graph.Tuner.Name) || !_tunerDevice.Name.Equals(
                    graph.Tuner.Name))
            {
                Log.Log.WriteFile("analog: Detecting capabilities of the tuner");
                graph.Tuner.Name = _tunerDevice.Name;
                int index;
                _audioPin = FilterGraphTools.FindMediaPin(_filterTvTuner, MediaType.AnalogAudio, MediaSubType.Null,
                                                          PinDirection.Output, out index);
                graph.Tuner.AudioPin = index;
                return(CheckCapabilities(graph));
            }
            Log.Log.WriteFile("analog: Using stored capabilities of the tuner");
            _audioPin        = DsFindPin.ByDirection(_filterTvTuner, PinDirection.Output, graph.Tuner.AudioPin);
            _supportsFMRadio = (graph.Tuner.RadioMode & RadioMode.FM) != 0;
            _supportsAMRadio = (graph.Tuner.RadioMode & RadioMode.AM) != 0;
            return(true);
        }
        private void Config()
        {
            int hr = 0;

            graphBuilder = (IFilterGraph2) new FilterGraph();
            rot          = new DsROTEntry(graphBuilder);

            DsDevice[]  devices;
            IBaseFilter filter;
            IPin        pin;

            devices = DsDevice.GetDevicesOfCat(FilterCategory.BDASourceFiltersCategory);
//      devices = DsDevice.GetDevicesOfCat(FilterCategory.BDARenderingFiltersCategory);
//      devices = DsDevice.GetDevicesOfCat(FilterCategory.BDAReceiverComponentsCategory);
//      devices = DsDevice.GetDevicesOfCat(FilterCategory.BDANetworkProvidersCategory);

            foreach (DsDevice device in devices)
            {
                hr = graphBuilder.AddSourceFilterForMoniker(device.Mon, null, device.Name, out filter);
                DsError.ThrowExceptionForHR(hr);

                pin = DsFindPin.ByDirection(filter, PinDirection.Input, 0);
                if (pin is IBDA_PinControl)
                {
                    pinControl = (IBDA_PinControl)pin;
                    return;
                }
                Marshal.ReleaseComObject(pin);

                pin = DsFindPin.ByDirection(filter, PinDirection.Output, 0);
                if (pin is IBDA_PinControl)
                {
                    pinControl = (IBDA_PinControl)pin;
                    return;
                }
                Marshal.ReleaseComObject(pin);

                hr = graphBuilder.RemoveFilter(filter);
                DsError.ThrowExceptionForHR(hr);

                Marshal.ReleaseComObject(filter);
            }
        }
        private void SetupGraph(DsDevice dev, int RequestedHeight, int RequestedWidth, short iBPP)
        {
            int hr;

            m_FilterGraph = new FilterGraph() as IFilterGraph2;
            try
            {
                hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
                DsError.ThrowExceptionForHR(hr);
                m_pinCapture = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
                m_pinStill   = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0);
                SetupCapturePin(RequestedHeight, RequestedWidth, iBPP);
                SetupStillPin(RequestedHeight, RequestedWidth, iBPP);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Setup Graph Error: " + ex.Message);
            }
        }
Example #24
0
        private void Configure()
        {
            // In order to lock a profile, you have to have at least one stream
            // connected to the sink. I connect a video thru the DVVideoEnc into
            // the StreamBufferSink.
            int         hr;
            IBaseFilter pFilter;

            ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
            StreamBufferSink      sbk  = new StreamBufferSink();

            m_FilterGraph = (IFilterGraph2) new FilterGraph();
            DsROTEntry ds = new DsROTEntry(m_FilterGraph);

            hr = icgb.SetFiltergraph(m_FilterGraph);
            DsError.ThrowExceptionForHR(hr);

            hr = m_FilterGraph.AddFilter((IBaseFilter)sbk, "StreamBufferSink");
            DsError.ThrowExceptionForHR(hr);

            DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            hr = m_FilterGraph.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out pFilter);
            DsError.ThrowExceptionForHR(hr);

            DVVideoEnc  dve         = new DVVideoEnc();
            IBaseFilter ibfVideoEnc = (IBaseFilter)dve;

            hr = m_FilterGraph.AddFilter(ibfVideoEnc, "dvenc");
            DsError.ThrowExceptionForHR(hr);

            hr = icgb.RenderStream(null, null, pFilter, ibfVideoEnc, (IBaseFilter)sbk);
            DsError.ThrowExceptionForHR(hr);

            m_isbc = (IStreamBufferSink)sbk;
            hr     = m_isbc.LockProfile(null);
            DsError.ThrowExceptionForHR(hr);

            Marshal.ReleaseComObject(pFilter);
            Marshal.ReleaseComObject(icgb);
        }
        void BuildGraph()
        {
            int         hr;
            IBaseFilter ppFilter;

            DsDevice []   devs;
            IGraphBuilder graphBuilder = new FilterGraph() as IGraphBuilder;

            m_ROT = new DsROTEntry(graphBuilder);
            IFilterGraph2 ifg2 = graphBuilder as IFilterGraph2;

            devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            DsDevice dev = devs[0];

            hr = ifg2.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out ppFilter);
            DsError.ThrowExceptionForHR(hr);

            m_ivc = ppFilter as IAMVideoControl;

            m_IPinOut = DsFindPin.ByDirection(ppFilter, PinDirection.Output, 0);

            hr = ifg2.Render(m_IPinOut);
            DsError.ThrowExceptionForHR(hr);

            ICaptureGraphBuilder2 captureGraphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;

            hr = captureGraphBuilder.SetFiltergraph(graphBuilder);

            object o;

            hr = captureGraphBuilder.FindInterface(null, null, ppFilter, typeof(IAMAnalogVideoDecoder).GUID, out o);
            DsError.ThrowExceptionForHR(hr);

            m_avd = o as IAMAnalogVideoDecoder;

            m_imc = graphBuilder as IMediaControl;
            hr    = m_imc.Run();
            DsError.ThrowExceptionForHR(hr);
        }
Example #26
0
        void BuildGraph()
        {
            int         hr;
            IBaseFilter ppFilter;

            DsDevice []   devs;
            IGraphBuilder graphBuilder = new FilterGraph() as IGraphBuilder;

            m_ROT = new DsROTEntry(graphBuilder);
            IFilterGraph2 ifg2 = graphBuilder as IFilterGraph2;

            devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            DsDevice dev = devs[0];

            hr = ifg2.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out ppFilter);
            DsError.ThrowExceptionForHR(hr);

            m_imc = graphBuilder as IMediaControl;
            hr    = m_imc.Run();
            DsError.ThrowExceptionForHR(hr);

            hr = ppFilter.GetSyncSource(out m_irc);
            DsError.ThrowExceptionForHR(hr);
        }
Example #27
0
        public void CreateGraph()
        {
            try
            {
                int result = 0;

                // フィルタグラフマネージャ作成
                graphBuilder = new FilterGraph() as IFilterGraph2;

                // キャプチャグラフビルダ作成
                captureGraphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;

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

                // ソースフィルタ作成
                // キャプチャデバイスをソースフィルタに対応付ける
                captureFilter = null;
                result = graphBuilder.AddSourceFilterForMoniker(
                    _capDevice.Mon, null, _capDevice.Name, out captureFilter);
                DsError.ThrowExceptionForHR(result);

                // サンプルグラバ作成
                sampleGrabber = new SampleGrabber() as ISampleGrabber;

                // フィルタと関連付ける
                IBaseFilter grabFilter = sampleGrabber as IBaseFilter;

                // キャプチャするオーディオのフォーマットを設定
                AMMediaType amMediaType = new AMMediaType();
                amMediaType.majorType = MediaType.Audio;
                amMediaType.subType = MediaSubType.PCM;
                amMediaType.formatPtr = IntPtr.Zero;
                result = sampleGrabber.SetMediaType(amMediaType);
                DsError.ThrowExceptionForHR(result);
                DsUtils.FreeAMMediaType(amMediaType);

                // callback 登録
                sampleGrabber.SetOneShot(false);
                DsError.ThrowExceptionForHR(result);

                result = sampleGrabber.SetBufferSamples(true);
                DsError.ThrowExceptionForHR(result);

                // キャプチャするフォーマットを取得
                object o;
                result = captureGraphBuilder.FindInterface(
                    DsGuid.FromGuid(PinCategory.Capture),
                    DsGuid.FromGuid(MediaType.Audio),
                    captureFilter,
                    typeof(IAMStreamConfig).GUID, out o);
                DsError.ThrowExceptionForHR(result);
                IAMStreamConfig config = o as IAMStreamConfig;
                AMMediaType media;
                result = config.GetFormat(out media);
                DsError.ThrowExceptionForHR(result);

                WaveFormatEx wf = new WaveFormatEx();
                Marshal.PtrToStructure(media.formatPtr, wf);

                CaptureOption opt = new CaptureOption(wf);
                _sampler = new DSAudioSampler(opt);

                DsUtils.FreeAMMediaType(media);
                Marshal.ReleaseComObject(config);

                result = sampleGrabber.SetCallback(_sampler, 1);
                DsError.ThrowExceptionForHR(result);

                //grabFilter(変換フィルタ)をgraphBuilder(フィルタグラフマネージャ)に追加.
                result = graphBuilder.AddFilter(grabFilter, "Audio Grab Filter");
                DsError.ThrowExceptionForHR(result);

                //キャプチャフィルタをサンプルグラバーフィルタに接続する
                result = captureGraphBuilder.RenderStream(
                    DsGuid.FromGuid(PinCategory.Capture),
                    DsGuid.FromGuid(MediaType.Audio),
                    captureFilter, null, grabFilter);
                DsError.ThrowExceptionForHR(result);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }
Example #28
0
 protected void AddAudioDecoderFilters(IFilterGraph2 graphBuilder)
 {
     int hr = 0;
     if (this.AudioDecoderDevice != null)
         hr = graphBuilder.AddSourceFilterForMoniker(this.AudioDecoderDevice.Mon, null, this.AudioDecoderDevice.Name, out this.audioDecoderFilter);
     DsError.ThrowExceptionForHR(hr);
 }
Example #29
0
 /// <summary>
 /// Creates the filter by trying to detect it
 /// </summary>
 /// <param name="crossbar">The crossbar componen</param>
 /// <param name="tuner">The tuner component</param>
 /// <param name="graph">The stored graph</param>
 /// <param name="graphBuilder">The graphBuilder</param>
 /// <returns>true, if the graph building was successful</returns>
 private bool CreateAutomaticFilterInstance(Graph graph, Tuner tuner, Crossbar crossbar, IFilterGraph2 graphBuilder)
 {
     //get all tv audio tuner devices on this system
     DsDevice[] devices = null;
     try
     {
         devices = DsDevice.GetDevicesOfCat(FilterCategory.AMKSTVAudio);
         devices = DeviceSorter.Sort(devices, tuner.TunerName, crossbar.CrossBarName);
     }
     catch (Exception)
     {
         Log.Log.WriteFile("analog: AddTvAudioFilter no tv audio devices found - Trying TvTuner filter");
     }
     if (devices != null && devices.Length > 0)
     {
         // try each tv audio tuner
         for (int i = 0; i < devices.Length; i++)
         {
             IBaseFilter tmp;
             Log.Log.WriteFile("analog: AddTvAudioFilter try:{0} {1}", devices[i].Name, i);
             //if tv audio tuner is currently in use we can skip it
             if (DevicesInUse.Instance.IsUsed(devices[i]))
             {
                 continue;
             }
             int hr;
             try
             {
                 //add tv audio tuner to graph
                 hr = graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out tmp);
             }
             catch (Exception)
             {
                 Log.Log.WriteFile("analog: cannot add filter to graph");
                 continue;
             }
             if (hr != 0)
             {
                 //failed to add tv audio tuner to graph, continue with the next one
                 if (tmp != null)
                 {
                     graphBuilder.RemoveFilter(tmp);
                     Release.ComObject("tvAudioFilter filter", tmp);
                 }
                 continue;
             }
             // try connecting the tv tuner-> tv audio tuner
             if (FilterGraphTools.ConnectPin(graphBuilder, tuner.AudioPin, tmp, 0))
             {
                 // Got it !
                 // Connect tv audio tuner to the crossbar
                 IPin pin = DsFindPin.ByDirection(tmp, PinDirection.Output, 0);
                 hr = graphBuilder.Connect(pin, crossbar.AudioTunerIn);
                 if (hr < 0)
                 {
                     //failed
                     graphBuilder.RemoveFilter(tmp);
                     Release.ComObject("audiotuner pinin", pin);
                     Release.ComObject("audiotuner filter", tmp);
                 }
                 else
                 {
                     //succeeded. we're done
                     Log.Log.WriteFile("analog: AddTvAudioFilter succeeded:{0}", devices[i].Name);
                     Release.ComObject("audiotuner pinin", pin);
                     _filterTvAudioTuner = tmp;
                     _audioDevice        = devices[i];
                     DevicesInUse.Instance.Add(_audioDevice);
                     _tvAudioTunerInterface = tuner.Filter as IAMTVAudio;
                     break;
                 }
             }
             else
             {
                 // cannot connect tv tuner-> tv audio tuner, try next one...
                 graphBuilder.RemoveFilter(tmp);
                 Release.ComObject("audiotuner filter", tmp);
             }
         }
     }
     if (_filterTvAudioTuner == null)
     {
         Log.Log.WriteFile("analog: AddTvAudioFilter no tv audio devices found - Trying TvTuner filter");
         int hr = graphBuilder.Connect(tuner.AudioPin, crossbar.AudioTunerIn);
         if (hr != 0)
         {
             Log.Log.Error("analog: unable to add TvAudioTuner to graph - even TvTuner as TvAudio fails");
             mode = TvAudioVariant.Unavailable;
         }
         else
         {
             Log.Log.WriteFile("analog: AddTvAudioFilter connected TvTuner with Crossbar directly succeeded!");
             mode = TvAudioVariant.TvTunerConnection;
             _tvAudioTunerInterface = tuner.Filter as IAMTVAudio;
             if (_tvAudioTunerInterface != null)
             {
                 Log.Log.WriteFile("analog: AddTvAudioFilter succeeded - TvTuner is also TvAudio");
                 _filterTvAudioTuner = tuner.Filter;
                 mode = TvAudioVariant.TvTuner;
             }
         }
         graph.TvAudio.Mode = mode;
     }
     else
     {
         mode = TvAudioVariant.Normal;
         graph.TvAudio.Name = _audioDevice.Name;
     }
     if (mode != TvAudioVariant.Unavailable && mode != TvAudioVariant.TvTunerConnection &&
         _tvAudioTunerInterface != null)
     {
         CheckCapabilities(graph);
     }
     return(true);
 }
Example #30
0
        /// <summary> build the capture graph. </summary>
        private void SetupGraph(DsDevice dev, string szOutputFileName)
        {
            int hr;

            IBaseFilter capFilter = null;
            IBaseFilter asfWriter = null;
            ICaptureGraphBuilder2 capGraph = null;

            // Get the graphbuilder object
            m_FilterGraph = (IFilterGraph2)new FilterGraph();

            #if DEBUG
            m_rot = new DsROTEntry( m_FilterGraph );
            #endif

            try
            {
                // Get the ICaptureGraphBuilder2
                capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

                // Start building the graph
                hr = capGraph.SetFiltergraph( m_FilterGraph );
                Marshal.ThrowExceptionForHR( hr );

                // Add the capture device to the graph
                hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
                Marshal.ThrowExceptionForHR( hr );

                asfWriter = ConfigAsf(capGraph, szOutputFileName);

                hr = capGraph.RenderStream(null, null, capFilter, null, asfWriter);
                Marshal.ThrowExceptionForHR( hr );

                m_mediaCtrl = m_FilterGraph as IMediaControl;
            }
            finally
            {
                if (capFilter != null)
                {
                    Marshal.ReleaseComObject(capFilter);
                    capFilter = null;
                }
                if (asfWriter != null)
                {
                    Marshal.ReleaseComObject(asfWriter);
                    asfWriter = null;
                }
                if (capGraph != null)
                {
                    Marshal.ReleaseComObject(capGraph);
                    capGraph = null;
                }
            }
        }
        private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight)
        {
            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            ICaptureGraphBuilder2 capGraph = null;

            graphBuilder = (IFilterGraph2)new FilterGraph();
            mediaCtrl = graphBuilder as IMediaControl;
            try
            {
                capGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
                sampGrabber = (ISampleGrabber)new SampleGrabber();
                var hr = capGraph.SetFiltergraph(graphBuilder);
                DsError.ThrowExceptionForHR(hr);
                hr = graphBuilder.AddSourceFilterForMoniker(dev.Moniker, null, "Video Capture Device", out capFilter);
                DsError.ThrowExceptionForHR(hr);
                var baseGrabFlt = (IBaseFilter)sampGrabber;
                ConfigureSampleGrabber(sampGrabber);
                hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                DsError.ThrowExceptionForHR(hr);
                if (iFrameRate + iHeight + iWidth > 0)
                    SetConfigParms(capGraph, capFilter, iFrameRate, iWidth, iHeight);

                hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, null, baseGrabFlt);
                DsError.ThrowExceptionForHR(hr);
                SaveSizeInfo(sampGrabber);
            }
            finally
            {
                if (capFilter != null)
                    Marshal.ReleaseComObject(capFilter);

                if (sampGrabber != null)
                    Marshal.ReleaseComObject(sampGrabber);

                if (capGraph != null)
                    Marshal.ReleaseComObject(capGraph);
            }
        }
Example #32
0
    /// <summary>
    /// Adds the multiplexer filter to the graph.
    /// several posibilities
    ///  1. no tv multiplexer needed
    ///  2. tv multiplexer filter which is connected to a single encoder filter
    ///  3. tv multiplexer filter which is connected to two encoder filter (audio/video)
    ///  4. tv multiplexer filter which is connected to the capture filter
    /// at the end this method the graph looks like this:
    /// 
    ///  option 2: single encoder filter
    ///    [                ]----->[                ]      [             ]
    ///    [ capture filter ]      [ encoder filter ]----->[ multiplexer ]
    ///    [                ]----->[                ]      [             ]
    ///
    ///
    ///  option 3: dual encoder filters
    ///    [                ]----->[   video        ]    
    ///    [ capture filter ]      [ encoder filter ]------>[             ]
    ///    [                ]      [                ]       [             ]
    ///    [                ]                               [ multiplexer ]
    ///    [                ]----->[   audio        ]------>[             ]
    ///                            [ encoder filter ]      
    ///                            [                ]
    ///
    ///  option 4: no encoder filter
    ///    [                ]----->[             ]
    ///    [ capture filter ]      [ multiplexer ]
    ///    [                ]----->[             ]
    /// </summary>
    /// <param name="matchPinNames">if set to <c>true</c> the pin names of the multiplexer filter should match the pin names of the encoder filter.</param>
    /// <param name="_graphBuilder">GraphBuilder</param>
    /// <param name="_tuner">Tuner</param>
    /// <param name="_tvAudio">TvAudio</param>
    /// <param name="_crossbar">Crossbar</param>
    /// <param name="_capture">Capture</param>
    /// <returns>true if encoder filters are added, otherwise false</returns>
    private bool AddTvMultiPlexer(bool matchPinNames, IFilterGraph2 _graphBuilder, Tuner _tuner, TvAudio _tvAudio,
                                  Crossbar _crossbar, Capture _capture)
    {
      //Log.Log.WriteFile("analog: AddTvMultiPlexer");
      DsDevice[] devicesHW;
      DsDevice[] devicesSW;
      DsDevice[] devices;
      //get a list of all multiplexers available on this system
      try
      {
        devicesHW = DsDevice.GetDevicesOfCat(FilterCategory.WDMStreamingMultiplexerDevices);
        devicesHW = DeviceSorter.Sort(devicesHW, _tuner.TunerName, _tvAudio.TvAudioName, _crossbar.CrossBarName,
                                      _capture.VideoCaptureName, _capture.AudioCaptureName, _videoEncoderDevice,
                                      _audioEncoderDevice, _multiplexerDevice);
        // also add the SoftWare Multiplexers in case no compatible HardWare multiplexer is found (NVTV cards)
        devicesSW = _tuner.IsNvidiaCard()
                      ? DsDevice.GetDevicesOfCat(FilterCategory.MediaMultiplexerCategory)
                      : new DsDevice[0];

        devices = new DsDevice[devicesHW.Length + devicesSW.Length];
        int nr = 0;
        for (int i = 0; i < devicesHW.Length; ++i)
          devices[nr++] = devicesHW[i];
        for (int i = 0; i < devicesSW.Length; ++i)
          devices[nr++] = devicesSW[i];
      }
      catch (Exception ex)
      {
        Log.Log.WriteFile("analog: AddTvMultiPlexer no multiplexer devices found (Exception) " + ex.Message);
        return false;
      }
      if (devices.Length == 0)
      {
        Log.Log.WriteFile("analog: AddTvMultiPlexer no multiplexer devices found");
        return false;
      }
      //for each multiplexer
      for (int i = 0; i < devices.Length; i++)
      {
        IBaseFilter tmp;
        Log.Log.WriteFile("analog: AddTvMultiPlexer try:{0} {1}", devices[i].Name, i);
        // if multiplexer is in use, we can skip it
        if (DevicesInUse.Instance.IsUsed(devices[i]))
          continue;
        int hr;
        try
        {
          //add multiplexer to graph
          hr = _graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out tmp);
        }
        catch (Exception)
        {
          Log.Log.WriteFile("analog: cannot add filter to graph");
          continue;
        }
        if (hr != 0)
        {
          //failed to add it to graph, continue with the next multiplexer
          if (tmp != null)
          {
            _graphBuilder.RemoveFilter(tmp);
            Release.ComObject("multiplexer filter", tmp);
          }
          continue;
        }
        // try to connect the multiplexer to encoders/capture devices
        if (ConnectMultiplexer(tmp, matchPinNames, _graphBuilder, _tuner, _capture))
        {
          // succeeded, we're done
          _filterMultiplexer = tmp;
          _multiplexerDevice = devices[i];
          DevicesInUse.Instance.Add(_multiplexerDevice);
          Log.Log.WriteFile("analog: AddTvMultiPlexer succeeded");
          break;
        }
        // unable to connect it, remove the filter and continue with the next one
        _graphBuilder.RemoveFilter(tmp);
        Release.ComObject("multiplexer filter", tmp);
      }
      if (_filterMultiplexer == null)
      {
        Log.Log.WriteFile("analog: no TvMultiPlexer found");
        return false;
      }
      return true;
    }
Example #33
0
    /// <summary>
    /// Creates the filter by trying to detect it
    /// </summary>
    /// <param name="tuner">The tuner component</param>
    /// <param name="graph">The stored graph</param>
    /// <param name="graphBuilder">The graphBuilder</param>
    /// <returns>true, if the graph building was successful</returns>
    private bool CreateAutomaticFilterInstance(Graph graph, IFilterGraph2 graphBuilder, Tuner tuner)
    {
      _audioTunerIn = null;
      DsDevice[] devices;
      //get list of all crossbar devices installed on this system
      try
      {
        devices = DsDevice.GetDevicesOfCat(FilterCategory.AMKSCrossbar);
        devices = DeviceSorter.Sort(devices, graph.Tuner.Name);
      }
      catch (Exception)
      {
        Log.Log.WriteFile("analog: AddCrossBarFilter no crossbar devices found");
        return false;
      }
      if (devices == null || devices.Length == 0)
      {
        Log.Log.WriteFile("analog: AddCrossBarFilter no crossbar devices found");
        return false;
      }
      //try each crossbar
      for (int i = 0; i < devices.Length; i++)
      {
        IBaseFilter tmp;
        Log.Log.WriteFile("analog: AddCrossBarFilter try:{0} {1}", devices[i].Name, i);
        //if crossbar is already in use then we can skip it
        if (DevicesInUse.Instance.IsUsed(devices[i]))
          continue;
        int hr;
        try
        {
          //add the crossbar to the graph
          hr = graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out tmp);
        }
        catch (Exception)
        {
          Log.Log.WriteFile("analog: cannot add filter to graph");
          continue;
        }
        if (hr != 0)
        {
          //failed. try next crossbar
          if (tmp != null)
          {
            graphBuilder.RemoveFilter(tmp);
            Release.ComObject("CrossBarFilter", tmp);
          }
          continue;
        }
        _crossBarFilter = (IAMCrossbar)tmp;
        CheckCapabilities();
        if (_videoOutPinIndex == -1)
        {
          Log.Log.WriteFile("analog: AddCrossbarFilter no video output found");
          graphBuilder.RemoveFilter(tmp);
          _crossBarFilter = null;
          Release.ComObject("CrossBarFilter", tmp);
          continue;
        }

        // Check that the crossbar has a tuner video input pin.
        IPin pinIn = null;
        if (_videoPinMap.ContainsKey(AnalogChannel.VideoInputType.Tuner))
        {
          pinIn = DsFindPin.ByDirection(tmp, PinDirection.Input, _videoPinMap[AnalogChannel.VideoInputType.Tuner]);
        }
        if (pinIn == null)
        {
          // no pin found, continue with next crossbar
          Log.Log.WriteFile("analog: AddCrossBarFilter no video tuner input pin detected");
          if (tmp != null)
          {
            graphBuilder.RemoveFilter(tmp);
            _crossBarFilter = null;
            Release.ComObject("CrossBarFilter", tmp);
          }
          continue;
        }
        //connect tv tuner->crossbar
        int tempVideoPinIndex;
        if (FilterGraphTools.ConnectFilter(graphBuilder, tuner.Filter, pinIn, out tempVideoPinIndex))
        {
          // Got it, we're done
          _filterCrossBar = tmp;
          _crossBarDevice = devices[i];
          DevicesInUse.Instance.Add(_crossBarDevice);
          if (_audioTunerIn == null)
          {
            _audioTunerIn = DsFindPin.ByDirection(_filterCrossBar, PinDirection.Input,
                                                  _audioPinMap[AnalogChannel.AudioInputType.Tuner]);
          }
          Release.ComObject("crossbar videotuner pin", pinIn);
          _videoOut = DsFindPin.ByDirection(_filterCrossBar, PinDirection.Output, _videoOutPinIndex);
          if (_audioOutPinIndex != -1)
          {
            _audioOut = DsFindPin.ByDirection(_filterCrossBar, PinDirection.Output, _audioOutPinIndex);
          }
          Log.Log.WriteFile("analog: AddCrossBarFilter succeeded");
          graph.Crossbar.AudioOut = _audioOutPinIndex;
          graph.Crossbar.AudioPinMap = _audioPinMap;
          graph.Crossbar.Name = _crossBarDevice.Name;
          graph.Crossbar.VideoOut = _videoOutPinIndex;
          graph.Crossbar.VideoPinMap = _videoPinMap;
          graph.Crossbar.VideoPinRelatedAudioMap = _videoPinRelatedAudioMap;
          graph.Tuner.VideoPin = tempVideoPinIndex;
          break;
        }
        // cannot connect tv tuner to crossbar, try next crossbar device
        graphBuilder.RemoveFilter(tmp);
        Release.ComObject("crossbar videotuner pin", pinIn);
        Release.ComObject("crossbar filter", tmp);
      }
      return _filterCrossBar != null;
    }
        /// <summary> build the capture graph for grabber. </summary>
        private void SetupGraph(DsDevice dev, int iSampleRate, int iChannels)
        {
            int hr;

            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            ICaptureGraphBuilder2 capGraph = null;
            IBaseFilter baseGrabFlt = null;
            IBaseFilter nullrenderer = null;
            IMediaFilter mediaFilt = m_FilterGraph as IMediaFilter;

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

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

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

                // Add the audio device
                hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, "Audio input", out capFilter);
                DsError.ThrowExceptionForHR(hr);

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

                // Get the SampleGrabber interface
                sampGrabber = new SampleGrabber() as ISampleGrabber;
                baseGrabFlt = sampGrabber as IBaseFilter;

                ConfigureSampleGrabber(sampGrabber);

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

                // ---------------------------------
                // Connect the file filter to the sample grabber

                // Hopefully this will be the audio pin, we could check by reading it's mediatype
                IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0);

                // Get the input pin from the sample grabber
                IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

                hr = m_FilterGraph.Connect(iPinOut, iPinIn);
                DsError.ThrowExceptionForHR(hr);

                // Add the null renderer to the graph
                nullrenderer = new NullRenderer() as IBaseFilter;
                hr = m_FilterGraph.AddFilter(nullrenderer, "Null renderer");
                DsError.ThrowExceptionForHR(hr);

                // ---------------------------------
                // Connect the sample grabber to the null renderer
                iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);
                iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0);

                hr = m_FilterGraph.Connect(iPinOut, iPinIn);
                DsError.ThrowExceptionForHR(hr);

                // Read and cache the resulting settings
                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;
                }
            }
        }
Example #35
0
    /// <summary>
    /// Adds one or 2 encoder filters to the graph
    ///  several posibilities
    ///  1. no encoder filter needed
    ///  2. single encoder filter with seperate audio/video inputs and 1 (mpeg-2) output
    ///  3. single encoder filter with a mpeg2 program stream input (I2S)
    ///  4. two encoder filters. one for audio and one for video
    ///
    ///  At the end of this method the graph looks like:
    ///
    ///  option 2: one encoder filter, with 2 inputs
    ///    [                ]----->[                ]
    ///    [ capture filter ]      [ encoder filter ]
    ///    [                ]----->[                ]
    ///
    ///
    ///  option 3: one encoder filter, with 1 input
    ///    [                ]      [                ]
    ///    [ capture filter ]----->[ encoder filter ]
    ///    [                ]      [                ]
    ///
    ///
    ///  option 4: 2 encoder filters one for audio and one for video
    ///    [                ]----->[   video        ]
    ///    [ capture filter ]      [ encoder filter ]
    ///    [                ]      [                ]
    ///    [                ]   
    ///    [                ]----->[   audio        ]
    ///                            [ encoder filter ]
    ///                            [                ]
    ///
    /// </summary>
    /// <param name="matchPinNames">if set to <c>true</c> the pin names of the encoder filter should match the pin names of the capture filter.</param>
    /// <param name="mpeg2ProgramFilter">if set to <c>true</c> than only encoders with an mpeg2 program output pins are accepted</param>
    /// <param name="_graphBuilder">GraphBuilder</param>
    /// <param name="_tuner">Tuner</param>
    /// <param name="_tvAudio">TvAudio</param>
    /// <param name="_crossbar">Crossbar</param>
    /// <param name="_capture">Capture</param>
    /// <returns>true if encoder filters are added, otherwise false</returns>
    private bool AddTvEncoderFilter(bool matchPinNames, bool mpeg2ProgramFilter, IFilterGraph2 _graphBuilder,
                                    Tuner _tuner, TvAudio _tvAudio, Crossbar _crossbar, Capture _capture)
    {
      Log.Log.WriteFile("analog: AddTvEncoderFilter - MatchPinNames: {0} - MPEG2ProgramFilter: {1}", matchPinNames,
                        mpeg2ProgramFilter);
      bool finished = false;
      DsDevice[] devices;
      // first get all encoder filters available on this system
      try
      {
        devices = DsDevice.GetDevicesOfCat(FilterCategory.WDMStreamingEncoderDevices);
        devices = DeviceSorter.Sort(devices, _tuner.TunerName, _tvAudio.TvAudioName, _crossbar.CrossBarName,
                                    _capture.VideoCaptureName, _capture.AudioCaptureName, _videoEncoderDevice,
                                    _audioEncoderDevice, _multiplexerDevice);
      }
      catch (Exception)
      {
        Log.Log.WriteFile("analog: AddTvEncoderFilter no encoder devices found (Exception)");
        return false;
      }
      if (devices == null)
      {
        Log.Log.WriteFile("analog: AddTvEncoderFilter no encoder devices found (devices == null)");
        return false;
      }
      if (devices.Length == 0)
      {
        Log.Log.WriteFile("analog: AddTvEncoderFilter no encoder devices found");
        return false;
      }
      //for each encoder
      Log.Log.WriteFile("analog: AddTvEncoderFilter found:{0} encoders", devices.Length);
      for (int i = 0; i < devices.Length; i++)
      {
        IBaseFilter tmp;
        //if encoder is in use, we can skip it
        if (DevicesInUse.Instance.IsUsed(devices[i]))
        {
          Log.Log.WriteFile("analog:  skip :{0} (inuse)", devices[i].Name);
          continue;
        }
        Log.Log.WriteFile("analog:  try encoder:{0} {1}", devices[i].Name, i);
        int hr;
        try
        {
          //add encoder filter to graph
          hr = _graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out tmp);
        }
        catch (Exception)
        {
          Log.Log.WriteFile("analog: cannot add filter {0} to graph", devices[i].Name);
          continue;
        }
        if (hr != 0)
        {
          //failed to add filter to graph, continue with the next one
          if (tmp != null)
          {
            _graphBuilder.RemoveFilter(tmp);
            Release.ComObject("TvEncoderFilter", tmp);
          }
          continue;
        }
        if (tmp == null)
          continue;
        // Encoder has been added to the graph
        // Now some cards have 2 encoder types, one for mpeg-2 transport stream and one for
        // mpeg-2 program stream. We dont want the mpeg-2 transport stream !
        // So first we check the output pins...
        // and dont accept filters which have a mpeg-ts output pin..
        // get the output pin
        bool isTsFilter = mpeg2ProgramFilter;
        IPin pinOut = DsFindPin.ByDirection(tmp, PinDirection.Output, 0);
        if (pinOut != null)
        {
          //check which media types it support
          IEnumMediaTypes enumMediaTypes;
          pinOut.EnumMediaTypes(out enumMediaTypes);
          if (enumMediaTypes != null)
          {
            int fetched;
            AMMediaType[] mediaTypes = new AMMediaType[20];
            enumMediaTypes.Next(20, mediaTypes, out fetched);
            if (fetched > 0)
            {
              for (int media = 0; media < fetched; ++media)
              {
                //check if media is mpeg-2 transport
                if (mediaTypes[media].majorType == MediaType.Stream &&
                    mediaTypes[media].subType == MediaSubType.Mpeg2Transport)
                {
                  isTsFilter = true;
                }
                //check if media is mpeg-2 program
                if (mediaTypes[media].majorType == MediaType.Stream &&
                    mediaTypes[media].subType == MediaSubType.Mpeg2Program)
                {
                  isTsFilter = false;
                  break;
                }

                // NVTV dual tuner needs this one to make it work so dont skip it
                if (mediaTypes[media].majorType == MediaType.Video &&
                    mediaTypes[media].subType == new Guid("be626472-fe7c-4a21-9f0b-d8f18b5ab441")) /*MediaSubType.?? */
                {
                  isTsFilter = false;
                  break;
                }
              }
            }
          }
          Release.ComObject("pinout", pinOut);
        }
        //if encoder has mpeg-2 ts output pin, then we skip it and continue with the next one
        if (isTsFilter)
        {
          Log.Log.WriteFile("analog:  filter {0} does not have mpeg-2 ps output or is a mpeg-2 ts filters",
                            devices[i].Name);
          _graphBuilder.RemoveFilter(tmp);
          Release.ComObject("TvEncoderFilter", tmp);
          continue;
        }
        // get the input pins of the encoder (can be 1 or 2 inputs)
        IPin pin1 = DsFindPin.ByDirection(tmp, PinDirection.Input, 0);
        IPin pin2 = DsFindPin.ByDirection(tmp, PinDirection.Input, 1);
        if (pin1 != null)
          Log.Log.WriteFile("analog: encoder in-pin1:{0}", FilterGraphTools.LogPinInfo(pin1));
        if (pin2 != null)
          Log.Log.WriteFile("analog: encoder in-pin2:{0}", FilterGraphTools.LogPinInfo(pin2));
        // if the encoder has 2 input pins then this means it has seperate inputs for audio and video
        if (pin1 != null && pin2 != null)
        {
          // try to connect the capture device -> encoder filters..
          if (ConnectEncoderFilter(tmp, true, true, matchPinNames, _graphBuilder, _capture))
          {
            //succeeded, encoder has been added and we are done
            _filterVideoEncoder = tmp;
            _videoEncoderDevice = devices[i];
            DevicesInUse.Instance.Add(_videoEncoderDevice);
            Log.Log.WriteFile("analog: AddTvEncoderFilter succeeded (encoder with 2 inputs)");
            //            success = true;
            finished = true;
            tmp = null;
          }
        }
        else if (pin1 != null)
        {
          //encoder filter only has 1 input pin.
          //First we get the media type of this pin to determine if its audio of video
          IEnumMediaTypes enumMedia;
          AMMediaType[] media = new AMMediaType[20];
          int fetched;
          pin1.EnumMediaTypes(out enumMedia);
          enumMedia.Next(1, media, out fetched);
          if (fetched == 1)
          {
            //media type found
            Log.Log.WriteFile("analog: AddTvEncoderFilter encoder output major:{0} sub:{1}", media[0].majorType,
                              media[0].subType);
            //is it audio?
            if (media[0].majorType == MediaType.Audio)
            {
              //yes, pin is audio
              //then connect the encoder to the audio output pin of the capture filter
              if (ConnectEncoderFilter(tmp, false, true, matchPinNames, _graphBuilder, _capture))
              {
                //this worked. but we're not done yet. We probably need to add a video encoder also
                _filterAudioEncoder = tmp;
                _audioEncoderDevice = devices[i];
                DevicesInUse.Instance.Add(_audioEncoderDevice);
                //                success = true;
                Log.Log.WriteFile("analog: AddTvEncoderFilter succeeded (audio encoder)");
                // if video encoder was already added, then we're done.
                if (_filterVideoEncoder != null)
                  finished = true;
                tmp = null;
              }
            }
            else
            {
              //pin is video
              //then connect the encoder to the video output pin of the capture filter
              if (ConnectEncoderFilter(tmp, true, false, matchPinNames, _graphBuilder, _capture))
              {
                //this worked. but we're not done yet. We probably need to add a audio encoder also
                _filterVideoEncoder = tmp;
                _videoEncoderDevice = devices[i];
                DevicesInUse.Instance.Add(_videoEncoderDevice);
                //                success = true;
                Log.Log.WriteFile("analog: AddTvEncoderFilter succeeded (video encoder)");
                // if audio encoder was already added, then we're done.
                if (_filterAudioEncoder != null)
                  finished = true;
                tmp = null;
              }
            }
            DsUtils.FreeAMMediaType(media[0]);
          }
          else
          {
            // filter does not report any media type (which is strange)
            // we must do something, so we treat it as a video input pin
            Log.Log.WriteFile("analog: AddTvEncoderFilter no media types for pin1"); //??
            if (ConnectEncoderFilter(tmp, true, false, matchPinNames, _graphBuilder, _capture))
            {
              _filterVideoEncoder = tmp;
              _videoEncoderDevice = devices[i];
              DevicesInUse.Instance.Add(_videoEncoderDevice);
              //              success = true;
              Log.Log.WriteFile("analog: AddTvEncoderFilter succeeded");
              finished = true;
              tmp = null;
            }
          }
        }
        else
        {
          Log.Log.WriteFile("analog: AddTvEncoderFilter no pin1");
        }
        if (pin1 != null)
          Release.ComObject("encoder pin0", pin1);
        if (pin2 != null)
          Release.ComObject("encoder pin1", pin2);
        if (tmp != null)
        {
          _graphBuilder.RemoveFilter(tmp);
          Release.ComObject("encoder filter", tmp);
        }
        if (finished)
        {
          Log.Log.WriteFile("analog: AddTvEncoderFilter succeeded 3");
          return true;
        }
      } //for (int i = 0; i < devices.Length; i++)
      Log.Log.WriteFile("analog: AddTvEncoderFilter no encoder found");
      return false;
    }
Example #36
0
 /// <summary>
 /// Creates the filter based on the configuration file
 /// </summary>
 /// <param name="tuner">The tuner component</param>
 /// <param name="graph">The stored graph</param>
 /// <param name="graphBuilder">The graphBuilder</param>
 /// <returns>true, if the graph building was successful</returns>
 private bool CreateConfigurationBasedFilterInstance(Graph graph, IFilterGraph2 graphBuilder, Tuner tuner)
 {
   string deviceName = graph.Crossbar.Name;
   _audioTunerIn = null;
   DsDevice[] devices;
   //get list of all crossbar devices installed on this system
   try
   {
     devices = DsDevice.GetDevicesOfCat(FilterCategory.AMKSCrossbar);
     devices = DeviceSorter.Sort(devices, graph.Tuner.Name);
   }
   catch (Exception)
   {
     Log.Log.WriteFile("analog: AddCrossBarFilter no crossbar devices found");
     return false;
   }
   if (devices == null || devices.Length == 0)
   {
     Log.Log.WriteFile("analog: AddCrossBarFilter no crossbar devices found");
     return false;
   }
   //try each crossbar
   for (int i = 0; i < devices.Length; i++)
   {
     IBaseFilter tmp;
     //if crossbar is already in use then we can skip it
     if (DevicesInUse.Instance.IsUsed(devices[i]))
       continue;
     if (!deviceName.Equals(devices[i].Name))
       continue;
     Log.Log.WriteFile("analog: AddCrossBarFilter use:{0} {1}", devices[i].Name, i);
     int hr;
     try
     {
       //add the crossbar to the graph
       hr = graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out tmp);
     }
     catch (Exception)
     {
       Log.Log.WriteFile("analog: cannot add filter to graph");
       continue;
     }
     if (hr != 0)
     {
       //failed. try next crossbar
       if (tmp != null)
       {
         graphBuilder.RemoveFilter(tmp);
         Release.ComObject("CrossBarFilter", tmp);
       }
       continue;
     }
     _crossBarFilter = (IAMCrossbar)tmp;
     _videoPinMap = graph.Crossbar.VideoPinMap;
     _audioPinMap = graph.Crossbar.AudioPinMap;
     _videoPinRelatedAudioMap = graph.Crossbar.VideoPinRelatedAudioMap;
     _videoOutPinIndex = graph.Crossbar.VideoOut;
     _audioOutPinIndex = graph.Crossbar.AudioOut;
     if (_videoOutPinIndex == -1)
     {
       Log.Log.WriteFile("analog: AddCrossbarFilter no video output found");
       graphBuilder.RemoveFilter(tmp);
       _crossBarFilter = null;
       Release.ComObject("CrossBarFilter", tmp);
       continue;
     }
     //connect tv tuner->crossbar
     IPin tunerOut = DsFindPin.ByDirection(tuner.Filter, PinDirection.Output,
                                           graph.Tuner.VideoPin);
     if (tunerOut != null && _videoPinMap.ContainsKey(AnalogChannel.VideoInputType.Tuner) &&
         FilterGraphTools.ConnectPin(graphBuilder, tunerOut, tmp, _videoPinMap[AnalogChannel.VideoInputType.Tuner]))
     {
       // Got it, we're done
       _filterCrossBar = tmp;
       _crossBarDevice = devices[i];
       DevicesInUse.Instance.Add(_crossBarDevice);
       if (_audioTunerIn == null)
       {
         _audioTunerIn = DsFindPin.ByDirection(_filterCrossBar, PinDirection.Input,
                                               _audioPinMap[AnalogChannel.AudioInputType.Tuner]);
       }
       Release.ComObject("tuner video out", tunerOut);
       _videoOut = DsFindPin.ByDirection(_filterCrossBar, PinDirection.Output, _videoOutPinIndex);
       if (_audioOutPinIndex != -1)
       {
         _audioOut = DsFindPin.ByDirection(_filterCrossBar, PinDirection.Output, _audioOutPinIndex);
       }
       Log.Log.WriteFile("analog: AddCrossBarFilter succeeded");
       break;
     }
     // cannot connect tv tuner to crossbar, try next crossbar device
     if (tmp != null)
     {
       graphBuilder.RemoveFilter(tmp);
       Release.ComObject("crossbarFilter filter", tmp);
     }
     if (tunerOut != null)
     {
       Release.ComObject("tuner video out", tunerOut);
     }
   }
   return _filterCrossBar != null;
 }
        public void PrepareCapture(int i_width, int i_height,float i_frame_rate)
        {
            const int BBP = 32;
            //既にキャプチャ中なら諦める。
            if (this.m_graphi_active)
            {
                throw new Exception();
            }
            //現在確保中のグラフインスタンスを全て削除
            CleanupGraphiObjects();

            int hr;
            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            IPin pSampleIn = null;

            //グラフビルダを作る。
            this.m_FilterGraph = new FilterGraph() as IFilterGraph2;

            try
            {
                //フィルタグラフにキャプチャを追加して、capFilterにピンを受け取る。
                hr = m_FilterGraph.AddSourceFilterForMoniker(this.m_dev.Mon, null, this.m_dev.Name, out capFilter);
                DsError.ThrowExceptionForHR(hr);

                //stillピンを探す
                m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0);
                //stillピンが無ければPreviewを探す。
                if (m_pinStill == null)
                {
                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0);
                }
                // Still haven't found one.  Need to put a splitter in so we have
                // one stream to capture the bitmap from, and one to display.  Ok, we
                // don't *have* to do it that way, but we are going to anyway.
                if (m_pinStill == null)
                {
                    // There is no still pin
                    m_VidControl = null;
                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);

                }else{
                    // Get a control pointer (used in Click())
                    m_VidControl = capFilter as IAMVideoControl;

                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
                }

                if (i_height + i_width + BBP > 0)
                {
                    SetConfigParms(m_pinStill, i_width, i_height,i_frame_rate, BBP);
                }

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

                //sampGrabberの設定
                IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
                ConfigureSampleGrabber(sampGrabber);

                pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

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

                if (m_VidControl == null)
                {
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);

                }else{
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);
                }
                hr = sampGrabber.GetConnectedMediaType(this._capture_mediatype);
                DsError.ThrowExceptionForHR(hr);
                //ビデオフォーマット等の更新
                upateVideoInfo(sampGrabber);
            }
            finally
            {
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                    sampGrabber = null;
                }
                if (pSampleIn != null)
                {
                    Marshal.ReleaseComObject(pSampleIn);
                    pSampleIn = null;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void InitCaptureGraph()
        {
            mGraphBuilder = (IFilterGraph2)new FilterGraph();
            mMediaControl = (IMediaControl)mGraphBuilder;

            ISampleGrabber sampleGrabber = null;
            IBaseFilter captureFilter = null;
            ICaptureGraphBuilder2 captureGraph = null;
            try
            {
                captureGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
                sampleGrabber = (ISampleGrabber)new SampleGrabber();

                int hr = captureGraph.SetFiltergraph(mGraphBuilder);
                DsError.ThrowExceptionForHR(hr);

                hr = mGraphBuilder.AddSourceFilterForMoniker(mDevice.Mon, null, "Video input", out captureFilter);
                DsError.ThrowExceptionForHR(hr);

                IBaseFilter baseGrabberFilter = (IBaseFilter)sampleGrabber;
                ConfigureSampleGrabber(sampleGrabber);

                hr = mGraphBuilder.AddFilter(baseGrabberFilter, "Ds.NET Grabber");
                DsError.ThrowExceptionForHR(hr);

                if (mFrameRate + mHeight + mWidth > 0)
                {
                    InitConfigParams(captureGraph, captureFilter);
                }

                hr = captureGraph.RenderStream(PinCategory.Capture, MediaType.Video, captureFilter, null, baseGrabberFilter);
                DsError.ThrowExceptionForHR(hr);

                SaveSizeInfo(sampleGrabber);
            }
            finally
            {
                if (captureFilter != null)
                {
                    Marshal.ReleaseComObject(captureFilter);
                    captureFilter = null;
                }
                if (sampleGrabber != null)
                {
                    Marshal.ReleaseComObject(sampleGrabber);
                    sampleGrabber = null;
                }
                if (captureGraph != null)
                {
                    Marshal.ReleaseComObject(captureGraph);
                    captureGraph = null;
                }
            }
        }
Example #39
0
 /// <summary>
 /// Creates the tuner filter instance
 /// </summary>
 /// <param name="graph">The stored graph</param>
 /// <param name="graphBuilder">The graphbuilder</param>
 /// <returns>true, if the graph building was successful</returns>
 public bool CreateFilterInstance(Graph graph, IFilterGraph2 graphBuilder)
 {
   Log.Log.WriteFile("analog: AddTvTunerFilter {0}", _tunerDevice.Name);
   if (DevicesInUse.Instance.IsUsed(_tunerDevice))
     return false;
   IBaseFilter tmp;
   int hr;
   try
   {
     hr = graphBuilder.AddSourceFilterForMoniker(_tunerDevice.Mon, null, _tunerDevice.Name, out tmp);
   }
   catch (Exception)
   {
     Log.Log.WriteFile("analog: cannot add filter to graph");
     return false;
   }
   if (hr != 0)
   {
     Log.Log.Error("analog: AddTvTunerFilter failed:0x{0:X}", hr);
     throw new TvException("Unable to add tvtuner to graph");
   }
   _filterTvTuner = tmp;
   DevicesInUse.Instance.Add(_tunerDevice);
   _tuner = _filterTvTuner as IAMTVTuner;
   if (string.IsNullOrEmpty(graph.Tuner.Name) || !_tunerDevice.Name.Equals(
     graph.Tuner.Name))
   {
     Log.Log.WriteFile("analog: Detecting capabilities of the tuner");
     graph.Tuner.Name = _tunerDevice.Name;
     int index;
     _audioPin = FilterGraphTools.FindMediaPin(_filterTvTuner, MediaType.AnalogAudio, MediaSubType.Null,
                                               PinDirection.Output, out index);
     graph.Tuner.AudioPin = index;
     return CheckCapabilities(graph);
   }
   Log.Log.WriteFile("analog: Using stored capabilities of the tuner");
   _audioPin = DsFindPin.ByDirection(_filterTvTuner, PinDirection.Output, graph.Tuner.AudioPin);
   _supportsFMRadio = (graph.Tuner.RadioMode & RadioMode.FM) != 0;
   _supportsAMRadio = (graph.Tuner.RadioMode & RadioMode.AM) != 0;
   return true;
 }
Example #40
0
 protected void AddBDAVideoDecoderFilters(IFilterGraph2 graphBuilder)
 {
     int hr = 0;
     if (this.H264DecoderDevice != null)
         hr = graphBuilder.AddSourceFilterForMoniker(this.H264DecoderDevice.Mon, null, this.H264DecoderDevice.Name, out this.videoH264DecoderFilter);
     else if (this.Mpeg2DecoderDevice != null)
         hr = graphBuilder.AddSourceFilterForMoniker(this.Mpeg2DecoderDevice.Mon, null, this.Mpeg2DecoderDevice.Name, out this.videoMpeg2DecoderFilter);
     DsError.ThrowExceptionForHR(hr);
 }
Example #41
0
        /// <summary> build the capture graph for grabber. </summary>
        private void SetupGraph(DsDevice dev, int iWidth, int iHeight, short iBPP)
        {
            ISampleGrabber sampGrabber = null;
            IPin pCaptureOut = null;
            IPin pRenderIn = null;

            // Get the graphbuilder object
            m_FilterGraph = (IFilterGraph2)new FilterGraph();

            try
            {

                // add the video input device
                IBaseFilter capFilter;
                int hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
                DsError.ThrowExceptionForHR(hr);

                //if (iHeight + iWidth + iBPP > 0)
                //{
                //SetConfigParms(pRaw, iWidth, iHeight, iBPP);
                //}
                pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);

                // Get the SampleGrabber interface
                sampGrabber = new SampleGrabber() as ISampleGrabber;
                // Configure the sample grabber
                IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
                ConfigureSampleGrabber(sampGrabber);

                // Add the sample grabber to the graph
                hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ares Video Grabber");
                DsError.ThrowExceptionForHR(hr);

                pRenderIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

                // Connect the capture pin to the renderer
                hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn);
                DsError.ThrowExceptionForHR(hr);

                // Learn the video properties
                SaveSizeInfo(sampGrabber);

                // Start the graph
                IMediaControl mediaCtrl = (IMediaControl)m_FilterGraph;
                hr = mediaCtrl.Run();
                DsError.ThrowExceptionForHR(hr);
            }
            finally
            {
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                }
                if (pCaptureOut != null)
                {
                    Marshal.ReleaseComObject(pCaptureOut);
                }
                if (pRenderIn != null)
                {
                    Marshal.ReleaseComObject(pRenderIn);
                }
            }
        }
Example #42
0
 private void handleInternalNetworkProviderFilter(DsDevice[] devices, IFilterGraph2 graphBuilder,
                                                  Guid networkProviderClsId, DsROTEntry rotEntry)
 {
   IDvbNetworkProvider interfaceNetworkProvider;
   TuningType tuningTypes;
   for (int i = 0; i < devices.Length; i++)
   {
     bool isCablePreferred = false;
     string name = devices[i].Name ?? "unknown";
     name = name.ToLowerInvariant();
     Log.Log.WriteFile("Found card:{0}", name);
     //silicondust work-around for dvb type detection issue. generic provider would always use dvb-t
     if (name.Contains("silicondust hdhomerun tuner"))
     {
       isCablePreferred = CheckHDHomerunCablePrefered(name);
       Log.Log.WriteFile("silicondust hdhomerun detected - prefer cable mode: {0}", isCablePreferred);
     }
     IBaseFilter tmp;
     graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, name, out tmp);
     //Use the Microsoft Network Provider method first but only if available
     IBaseFilter networkDVB = FilterGraphTools.AddFilterFromClsid(graphBuilder, networkProviderClsId,
                                                                  "MediaPortal Network Provider");
     interfaceNetworkProvider = (IDvbNetworkProvider)networkDVB;
     string hash = GetHash(devices[i].DevicePath);
     interfaceNetworkProvider.ConfigureLogging(GetFileName(devices[i].DevicePath), hash, LogLevelOption.Debug);
     if (ConnectFilter(graphBuilder, networkDVB, tmp))
     {
       Log.Log.WriteFile("Detected DVB card:{0}- Hash: {1}", name, hash);
       interfaceNetworkProvider.GetAvailableTuningTypes(out tuningTypes);
       Log.Log.WriteFile("TuningTypes: " + tuningTypes);
       // determine the DVB card supported GUIDs here!
       if ((tuningTypes & TuningType.DvbT) != 0 && !isCablePreferred)
       {
         Log.Log.WriteFile("Detected DVB-T* card:{0}", name);
         TvCardDVBT dvbtCard = new TvCardDVBT(_epgEvents, devices[i]);
         _cards.Add(dvbtCard);
       }
       if ((tuningTypes & TuningType.DvbS) != 0 && !isCablePreferred)
       {
         Log.Log.WriteFile("Detected DVB-S* card:{0}", name);
         TvCardDVBS dvbsCard = new TvCardDVBS(_epgEvents, devices[i]);
         _cards.Add(dvbsCard);
       }
       if ((tuningTypes & TuningType.DvbC) != 0)
       {
         Log.Log.WriteFile("Detected DVB-C* card:{0}", name);
         TvCardDVBC dvbcCard = new TvCardDVBC(_epgEvents, devices[i]);
         _cards.Add(dvbcCard);
       }
       if ((tuningTypes & TuningType.Atsc) != 0 && !isCablePreferred)
       {
         Log.Log.WriteFile("Detected ATSC* card:{0}", name);
         TvCardATSC dvbsCard = new TvCardATSC(_epgEvents, devices[i]);
         _cards.Add(dvbsCard);
       }
     }
     graphBuilder.RemoveFilter(tmp);
     Release.ComObject("tmp filter", tmp);
     graphBuilder.RemoveFilter(networkDVB);
     Release.ComObject("ms provider", networkDVB);
   }
   FilterGraphTools.RemoveAllFilters(graphBuilder);
   rotEntry.Dispose();
   Release.ComObject("graph builder", graphBuilder);
 }
Example #43
0
        /// <summary> build the capture graph for grabber. </summary>
        private void SetupGraph(DsDevice dev, AMMediaType media)
        {
            int hr;

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

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

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

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

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

                // add video crossbar
                // thanks to Andrew Fernie - this is to get tv tuner cards working
                IAMCrossbar crossbar = null;
                object o;

                hr = capGraph.FindInterface(PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMCrossbar).GUID, out o);
                if (hr >= 0)
                {
                    crossbar = (IAMCrossbar)o;
                    int oPin, iPin;
                    int ovLink, ivLink;
                    ovLink = ivLink = 0;

                    crossbar.get_PinCounts(out oPin, out iPin);
                    int pIdxRel;
                    PhysicalConnectorType tp;
                    for (int i = 0; i < iPin; i++)
                    {
                        crossbar.get_CrossbarPinInfo(true, i, out pIdxRel, out tp);
                        if (tp == PhysicalConnectorType.Video_Composite) ivLink = i;
                    }

                    for (int i = 0; i < oPin; i++)
                    {
                        crossbar.get_CrossbarPinInfo(false, i, out pIdxRel, out tp);
                        if (tp == PhysicalConnectorType.Video_VideoDecoder) ovLink = i;
                    }

                    try
                    {
                        crossbar.Route(ovLink, ivLink);
                        o = null;
                    }

                    catch
                    {
                        throw new Exception("Failed to get IAMCrossbar");
                    }
                }

                //add AVI Decompressor
                IBaseFilter pAVIDecompressor = (IBaseFilter)new AVIDec();
                hr = m_FilterGraph.AddFilter(pAVIDecompressor, "AVI Decompressor");
                DsError.ThrowExceptionForHR(hr);

                //
                IBaseFilter baseGrabFlt = (IBaseFilter)	sampGrabber;
                ConfigureSampleGrabber(sampGrabber);

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

                SetConfigParms(capGraph, capFilter, media);

                hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, pAVIDecompressor, baseGrabFlt);
                if (hr < 0)
                {
                    hr = capGraph.RenderStream(PinCategory.Capture, 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;
                }
            }
        }
Example #44
0
        public static void AddBDATunerAndDemodulatorToGraph(IFilterGraph2 graphBuilder, IBaseFilter networkProvider, out IBaseFilter tuner, out IBaseFilter capture)
        {
            int hr = 0;

            DsDevice[]            devices;
            ICaptureGraphBuilder2 capBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

            capBuilder.SetFiltergraph(graphBuilder);

            try
            {
                tuner   = null;
                capture = null;

                devices = DsDevice.GetDevicesOfCat(FilterCategory.BDASourceFiltersCategory);

                for (int i = 0; i < devices.Length; i++)
                {
                    IBaseFilter tmp;

                    hr = graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out tmp);
                    DsError.ThrowExceptionForHR(hr);

                    hr = capBuilder.RenderStream(null, null, networkProvider, null, tmp);
                    if (hr == 0)
                    {
                        tuner = tmp;
                        break;
                    }
                    else
                    {
                        hr = graphBuilder.RemoveFilter(tmp);
                        Marshal.ReleaseComObject(tmp);
                    }
                }

                devices = DsDevice.GetDevicesOfCat(FilterCategory.BDAReceiverComponentsCategory);

                for (int i = 0; i < devices.Length; i++)
                {
                    IBaseFilter tmp;

                    hr = graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out tmp);
                    DsError.ThrowExceptionForHR(hr);

                    hr = capBuilder.RenderStream(null, null, tuner, null, tmp);
                    if (hr == 0)
                    {
                        capture = tmp;
                        break;
                    }
                    else
                    {
                        hr = graphBuilder.RemoveFilter(tmp);
                        Marshal.ReleaseComObject(tmp);
                    }
                }
            }
            finally
            {
                Marshal.ReleaseComObject(capBuilder);
            }
        }
Example #45
0
 /// <summary>
 /// Creates the filter by trying to detect it
 /// </summary>
 /// <param name="crossbar">The crossbar componen</param>
 /// <param name="tuner">The tuner component</param>
 /// <param name="graph">The stored graph</param>
 /// <param name="graphBuilder">The graphBuilder</param>
 /// <returns>true, if the graph building was successful</returns>
 private bool CreateAutomaticFilterInstance(Graph graph, Tuner tuner, Crossbar crossbar, IFilterGraph2 graphBuilder)
 {
   //get all tv audio tuner devices on this system
   DsDevice[] devices = null;
   try
   {
     devices = DsDevice.GetDevicesOfCat(FilterCategory.AMKSTVAudio);
     devices = DeviceSorter.Sort(devices, tuner.TunerName, crossbar.CrossBarName);
   }
   catch (Exception)
   {
     Log.Log.WriteFile("analog: AddTvAudioFilter no tv audio devices found - Trying TvTuner filter");
   }
   if (devices != null && devices.Length > 0)
   {
     // try each tv audio tuner
     for (int i = 0; i < devices.Length; i++)
     {
       IBaseFilter tmp;
       Log.Log.WriteFile("analog: AddTvAudioFilter try:{0} {1}", devices[i].Name, i);
       //if tv audio tuner is currently in use we can skip it
       if (DevicesInUse.Instance.IsUsed(devices[i]))
         continue;
       int hr;
       try
       {
         //add tv audio tuner to graph
         hr = graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out tmp);
       }
       catch (Exception)
       {
         Log.Log.WriteFile("analog: cannot add filter to graph");
         continue;
       }
       if (hr != 0)
       {
         //failed to add tv audio tuner to graph, continue with the next one
         if (tmp != null)
         {
           graphBuilder.RemoveFilter(tmp);
           Release.ComObject("tvAudioFilter filter", tmp);
         }
         continue;
       }
       // try connecting the tv tuner-> tv audio tuner
       if (FilterGraphTools.ConnectPin(graphBuilder, tuner.AudioPin, tmp, 0))
       {
         // Got it !
         // Connect tv audio tuner to the crossbar
         IPin pin = DsFindPin.ByDirection(tmp, PinDirection.Output, 0);
         hr = graphBuilder.Connect(pin, crossbar.AudioTunerIn);
         if (hr < 0)
         {
           //failed
           graphBuilder.RemoveFilter(tmp);
           Release.ComObject("audiotuner pinin", pin);
           Release.ComObject("audiotuner filter", tmp);
         }
         else
         {
           //succeeded. we're done
           Log.Log.WriteFile("analog: AddTvAudioFilter succeeded:{0}", devices[i].Name);
           Release.ComObject("audiotuner pinin", pin);
           _filterTvAudioTuner = tmp;
           _audioDevice = devices[i];
           DevicesInUse.Instance.Add(_audioDevice);
           _tvAudioTunerInterface = tuner.Filter as IAMTVAudio;
           break;
         }
       }
       else
       {
         // cannot connect tv tuner-> tv audio tuner, try next one...
         graphBuilder.RemoveFilter(tmp);
         Release.ComObject("audiotuner filter", tmp);
       }
     }
   }
   if (_filterTvAudioTuner == null)
   {
     Log.Log.WriteFile("analog: AddTvAudioFilter no tv audio devices found - Trying TvTuner filter");
     int hr = graphBuilder.Connect(tuner.AudioPin, crossbar.AudioTunerIn);
     if (hr != 0)
     {
       Log.Log.Error("analog: unable to add TvAudioTuner to graph - even TvTuner as TvAudio fails");
       mode = TvAudioVariant.Unavailable;
     }
     else
     {
       Log.Log.WriteFile("analog: AddTvAudioFilter connected TvTuner with Crossbar directly succeeded!");
       mode = TvAudioVariant.TvTunerConnection;
       _tvAudioTunerInterface = tuner.Filter as IAMTVAudio;
       if (_tvAudioTunerInterface != null)
       {
         Log.Log.WriteFile("analog: AddTvAudioFilter succeeded - TvTuner is also TvAudio");
         _filterTvAudioTuner = tuner.Filter;
         mode = TvAudioVariant.TvTuner;
       }
     }
     graph.TvAudio.Mode = mode;
   }
   else
   {
     mode = TvAudioVariant.Normal;
     graph.TvAudio.Name = _audioDevice.Name;
   }
   if (mode != TvAudioVariant.Unavailable && mode != TvAudioVariant.TvTunerConnection &&
       _tvAudioTunerInterface != null)
   {
     CheckCapabilities(graph);
   }
   return true;
 }
Example #46
0
        /// <summary> build the capture graph for grabber. </summary>
        private void SetupGraph(DsDevice dev, AMMediaType media)
        {
            int hr;

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

            // Get the graphbuilder object
            m_FilterGraph = (IFilterGraph2) new FilterGraph();
            m_mediaCtrl = m_FilterGraph as IMediaControl;
            try
            {
                // Get the ICaptureGraphBuilder2
                capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
                /*
                // check for crossbar
                var capDevices2 = DsDevice.GetDevicesOfCat(FilterCategory.AMKSCrossbar);
                if (capDevices2.Length > 0)
                {

                    IBaseFilter cross;
                    hr = m_FilterGraph.AddSourceFilterForMoniker(capDevices2[0].Mon, null, "crossbar", out cross);
                    ISpecifyPropertyPages pProp = cross as ISpecifyPropertyPages;

                    //Get the name of the filter from the FilterInfo struct
                    FilterInfo filterInfo;
                    hr = cross.QueryFilterInfo(out filterInfo);
                    DsError.ThrowExceptionForHR(hr);

                    // Get the propertypages from the property bag
                    DsCAUUID caGUID;
                    hr = pProp.GetPages(out caGUID);
                    DsError.ThrowExceptionForHR(hr);

                    //Create and display the OlePropertyFrame
                    object oDevice = (object)cross;
                    hr = NativeMethods.OleCreatePropertyFrame(IntPtr.Zero, 0, 0, filterInfo.achName, 1, ref oDevice, caGUID.cElems, caGUID.pElems, 0, 0, IntPtr.Zero);
                    DsError.ThrowExceptionForHR(hr);

                    Marshal.ReleaseComObject(oDevice);

                    //IAMCrossbar crossbar2 = cross as IAMCrossbar;
                    //int inputPinCount, outputPinCount;
                    //crossbar2.get_PinCounts(out inputPinCount, out outputPinCount);
                    //crossbar2.Route(0, (int)PhysicalConnectorType.Video_Composite);
                    cross = null;
                }*/

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

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

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

                // add video crossbar
                // thanks to Andrew Fernie - this is to get tv tuner cards working
                IAMCrossbar crossbar = null;
                object o;

                hr = capGraph.FindInterface(PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMCrossbar).GUID, out o);
                if (hr >= 0)
                {
                    crossbar = (IAMCrossbar)o;
                    int oPin, iPin;
                    int ovLink, ivLink;
                    ovLink = ivLink = 0;

                    crossbar.get_PinCounts(out oPin, out iPin);
                    int pIdxRel;
                    PhysicalConnectorType tp;
                    for (int i = 0; i < iPin; i++)
                    {
                        crossbar.get_CrossbarPinInfo(true, i, out pIdxRel, out tp);
                        if (tp == PhysicalConnectorType.Video_Composite) ivLink = i;
                    }

                    for (int i = 0; i < oPin; i++)
                    {
                        crossbar.get_CrossbarPinInfo(false, i, out pIdxRel, out tp);
                        if (tp == PhysicalConnectorType.Video_VideoDecoder) ovLink = i;
                    }

                    try
                    {
                        crossbar.Route(ovLink, ivLink);
                        o = null;
                    }

                    catch
                    {
                        throw new Exception("Failed to get IAMCrossbar");
                    }
                }

                //add AVI Decompressor
                IBaseFilter pAVIDecompressor = (IBaseFilter)new AVIDec();
                hr = m_FilterGraph.AddFilter(pAVIDecompressor, "AVI Decompressor");
                DsError.ThrowExceptionForHR(hr);

                //
                IBaseFilter baseGrabFlt = (IBaseFilter)	sampGrabber;
                ConfigureSampleGrabber(sampGrabber);

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

                SetConfigParms(capGraph, capFilter, media);

                hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, pAVIDecompressor, baseGrabFlt);
                if (hr < 0)
                {
                    hr = capGraph.RenderStream(PinCategory.Capture, 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;
                }
            }
        }
        /// <summary>
        /// Initializes the devices and prepares for capture
        /// </summary>
        public void Initialize()
        {
            _filterGraph = new FilterGraph() as IFilterGraph2;
            _mediaCtrl = _filterGraph as IMediaControl;

            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            ICaptureGraphBuilder2 capGraph = null;
            int status;

            try
            {
                capGraph = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;
                sampGrabber = new SampleGrabber() as ISampleGrabber;

                status = capGraph.SetFiltergraph(_filterGraph);
                DsError.ThrowExceptionForHR(status);

                //Adds the video device
                status = _filterGraph.AddSourceFilterForMoniker(_device.DsDevice.Mon, null, "Video input", out capFilter);
                DsError.ThrowExceptionForHR(status);

                IBaseFilter baseGrabFilter = sampGrabber as IBaseFilter;
                ConfigureSampleGrabber(sampGrabber);

                // Add the frame grabber to the graph
                status = _filterGraph.AddFilter(baseGrabFilter, ".net grabber");
                DsError.ThrowExceptionForHR(status);

                //TODO: Set Framerate, height and width here

                status = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, null, baseGrabFilter);
                DsError.ThrowExceptionForHR(status);

                SaveSizeInfo(sampGrabber);

                _bitmapMemory = Marshal.AllocCoTaskMem(_stride * _videoHeight);
            }
            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;
                }
            }
        }
        /// <summary> build the capture graph for grabber. </summary>
        private void SetupGraph(DsDevice dev, int iWidth, int iHeight, short iBPP, Control hControl)
        {
            int hr;

            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            IPin pCaptureOut = null;
            IPin pSampleIn = null;
            IPin pRenderIn = null;

            // Get the graphbuilder object
            m_FilterGraph = new FilterGraph() as IFilterGraph2;

            try
            {
            #if DEBUG
                m_rot = new DsROTEntry(m_FilterGraph);
            #endif
                // add the video input device
                hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
                DsError.ThrowExceptionForHR(hr);

                // Find the still pin
                m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0);

                // Didn't find one.  Is there a preview pin?
                if (m_pinStill == null)
                {
                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0);
                }

                // Still haven't found one.  Need to put a splitter in so we have
                // one stream to capture the bitmap from, and one to display.  Ok, we
                // don't *have* to do it that way, but we are going to anyway.
                if (m_pinStill == null)
                {
                    IPin pRaw = null;
                    IPin pSmart = null;

                    // There is no still pin
                    m_VidControl = null;

                    // Add a splitter
                    IBaseFilter iSmartTee = (IBaseFilter)new SmartTee();

                    try
                    {
                        hr = m_FilterGraph.AddFilter(iSmartTee, "SmartTee");
                        DsError.ThrowExceptionForHR(hr);

                        // Find the find the capture pin from the video device and the
                        // input pin for the splitter, and connnect them
                        pRaw = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
                        pSmart = DsFindPin.ByDirection(iSmartTee, PinDirection.Input, 0);

                        hr = m_FilterGraph.Connect(pRaw, pSmart);
                        DsError.ThrowExceptionForHR(hr);

                        // Now set the capture and still pins (from the splitter)
                        m_pinStill = DsFindPin.ByName(iSmartTee, "Preview");
                        pCaptureOut = DsFindPin.ByName(iSmartTee, "Capture");

                        // If any of the default config items are set, perform the config
                        // on the actual video device (rather than the splitter)
                        if (iHeight + iWidth + iBPP > 0)
                        {
                            SetConfigParms(pRaw, iWidth, iHeight, iBPP);
                        }
                    }
                    finally
                    {
                        if (pRaw != null)
                        {
                            Marshal.ReleaseComObject(pRaw);
                        }
                        if (pRaw != pSmart)
                        {
                            Marshal.ReleaseComObject(pSmart);
                        }
                        if (pRaw != iSmartTee)
                        {
                            Marshal.ReleaseComObject(iSmartTee);
                        }
                    }
                }
                else
                {
                    // Get a control pointer (used in Click())
                    m_VidControl = capFilter as IAMVideoControl;

                    pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);

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

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

                // Configure the sample grabber
                IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
                ConfigureSampleGrabber(sampGrabber);
                pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

                // Get the default video renderer
                IBaseFilter pRenderer = new VideoRendererDefault() as IBaseFilter;
                hr = m_FilterGraph.AddFilter(pRenderer, "Renderer");
                DsError.ThrowExceptionForHR(hr);

                pRenderIn = DsFindPin.ByDirection(pRenderer, PinDirection.Input, 0);

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

                if (m_VidControl == null)
                {
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);

                    // Connect the capture pin to the renderer
                    hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn);
                    DsError.ThrowExceptionForHR(hr);
                }
                else
                {
                    // Connect the capture pin to the renderer
                    hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn);
                    DsError.ThrowExceptionForHR(hr);

                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);
                }

                // Learn the video properties
                SaveSizeInfo(sampGrabber);
                ConfigVideoWindow(hControl);

                // Start the graph
                IMediaControl mediaCtrl = m_FilterGraph as IMediaControl;
                hr = mediaCtrl.Run();
                DsError.ThrowExceptionForHR(hr);
            }
            finally
            {
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                    sampGrabber = null;
                }
                if (pCaptureOut != null)
                {
                    Marshal.ReleaseComObject(pCaptureOut);
                    pCaptureOut = null;
                }
                if (pRenderIn != null)
                {
                    Marshal.ReleaseComObject(pRenderIn);
                    pRenderIn = null;
                }
                if (pSampleIn != null)
                {
                    Marshal.ReleaseComObject(pSampleIn);
                    pSampleIn = null;
                }
            }
        }
Example #49
0
        /// <summary> build the capture graph for grabber. </summary>
        private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight, string FileName)
        {
            int hr;

            ISampleGrabber sampGrabber = null;
            IBaseFilter baseGrabFlt = null;
            IBaseFilter capFilter = null;
            IBaseFilter muxFilter = null;
            IFileSinkFilter fileWriterFilter = null;
            ICaptureGraphBuilder2 capGraph = null;

            // Get the graphbuilder object
            m_FilterGraph = new FilterGraph() as IFilterGraph2;
            m_mediaCtrl = m_FilterGraph as IMediaControl;

            #if DEBUG
            m_rot = new DsROTEntry(m_FilterGraph);
            #endif
            try
            {
                // Get the ICaptureGraphBuilder2
                capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

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

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

                // Add the video device
                hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
                DsError.ThrowExceptionForHR( hr );

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

                // Add the frame grabber to the graph
                hr = m_FilterGraph.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);
                }

                // Create a filter for the output avi file
                hr = capGraph.SetOutputFileName(MediaSubType.Avi, FileName, out muxFilter, out fileWriterFilter);
                DsError.ThrowExceptionForHR( hr );

                // Connect everything together
                hr = capGraph.RenderStream( PinCategory.Capture, MediaType.Video, capFilter, baseGrabFlt,  muxFilter);
                DsError.ThrowExceptionForHR( hr );

                // Now that sizes are fixed, store the sizes
                SaveSizeInfo(sampGrabber);
            }
            finally
            {
                if (fileWriterFilter != null)
                {
                    Marshal.ReleaseComObject(fileWriterFilter);
                    fileWriterFilter = null;
                }
                if (muxFilter != null)
                {
                    Marshal.ReleaseComObject(muxFilter);
                    muxFilter = null;
                }
                if (capFilter != null)
                {
                    Marshal.ReleaseComObject(capFilter);
                    capFilter = null;
                }
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                    sampGrabber = null;
                }
            }
        }
Example #50
0
        /// <summary> build the capture graph for grabber. </summary>
        private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight)
        {
            int hr;

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

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

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

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

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

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

                // Add the frame grabber to the graph
                hr = m_FilterGraph.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);
                }

                hr = capGraph.RenderStream( PinCategory.Capture, 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;
                }
            }
        }
Example #51
0
        private void BuildGraph(Control hControl)
        {
            int hr;
            DsDevice [] devs;
            IBaseFilter ibfSource = null;
            IBaseFilter dmoFilter = null;
            IBaseFilter ibfRender = null;
            IDMOWrapperFilter dmoWrapperFilter = null;

            ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();

            graphBuilder = (IFilterGraph2) new FilterGraph();
            #if DEBUG
            m_rot = new DsROTEntry(graphBuilder);
            #endif

            hr = icgb.SetFiltergraph(graphBuilder);
            DsError.ThrowExceptionForHR(hr);

            devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);

            if (devs.Length < 1)
            {
                throw new Exception("This sample requires a capture device.  If you don't " +
                    "have a capture device, change the BuildGraph routine to use AddSourceFilter " +
                    "and use a file.");
            }

            // Add a source filter
            hr = graphBuilder.AddSourceFilterForMoniker(devs[0].Mon, null, "Video Input Device", out ibfSource);
            DsError.ThrowExceptionForHR(hr);

            // Add a DMO Wrapper Filter
            dmoFilter = (IBaseFilter) new DMOWrapperFilter();
            dmoWrapperFilter = (IDMOWrapperFilter) dmoFilter;

            // Since I know the guid of the DMO I am looking for, I *could* do this.
            //hr = dmoWrapperFilter.Init(new Guid("{7EF28FD7-E88F-45bb-9CDD-8A62956F2D75}"), DMOCatergory.VideoEffect);
            //DMOError.ThrowExceptionForHR(hr);

            // But it is more useful to show how to scan for the DMO
            Guid g = FindGuid("DmoFlip", DMOCategory.VideoEffect);

            hr = dmoWrapperFilter.Init(g, DMOCategory.VideoEffect);
            DMOError.ThrowExceptionForHR(hr);

            SetDMOParams(dmoFilter);

            // Add it to the Graph
            hr = graphBuilder.AddFilter(dmoFilter, "DMO Filter");
            DsError.ThrowExceptionForHR(hr);

            ibfRender = (IBaseFilter)new VideoRenderer();
            hr = graphBuilder.AddFilter(ibfRender, "renderer");
            DsError.ThrowExceptionForHR(hr);

            hr = icgb.RenderStream(null, null, ibfSource, dmoFilter, ibfRender);
            DsError.ThrowExceptionForHR(hr);

            ConfigVideo(graphBuilder as IVideoWindow, hControl);

            Marshal.ReleaseComObject(ibfSource);
            Marshal.ReleaseComObject(ibfRender);
        }
Example #52
0
        public void PrepareCapture(int i_width, int i_height, float i_frame_rate)
        {
            const int BBP = 32;

            //既にキャプチャ中なら諦める。
            if (this.m_graphi_active)
            {
                throw new Exception();
            }
            //現在確保中のグラフインスタンスを全て削除
            CleanupGraphiObjects();

            int            hr;
            ISampleGrabber sampGrabber = null;
            IBaseFilter    capFilter   = null;
            IPin           pSampleIn   = null;

            //グラフビルダを作る。
            this.m_FilterGraph = new FilterGraph() as IFilterGraph2;

            try
            {
                //フィルタグラフにキャプチャを追加して、capFilterにピンを受け取る。
                hr = m_FilterGraph.AddSourceFilterForMoniker(this.m_dev.Mon, null, this.m_dev.Name, out capFilter);
                DsError.ThrowExceptionForHR(hr);

                //stillピンを探す
                m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0);
                //stillピンが無ければPreviewを探す。
                if (m_pinStill == null)
                {
                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0);
                }
                // Still haven't found one.  Need to put a splitter in so we have
                // one stream to capture the bitmap from, and one to display.  Ok, we
                // don't *have* to do it that way, but we are going to anyway.
                if (m_pinStill == null)
                {
                    // There is no still pin
                    m_VidControl = null;
                    m_pinStill   = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
                }
                else
                {
                    // Get a control pointer (used in Click())
                    m_VidControl = capFilter as IAMVideoControl;

                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
                }

                if (i_height + i_width + BBP > 0)
                {
                    SetConfigParms(m_pinStill, i_width, i_height, i_frame_rate, BBP);
                }

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

                //sampGrabberの設定
                IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
                ConfigureSampleGrabber(sampGrabber);

                pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

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

                if (m_VidControl == null)
                {
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);
                }
                else
                {
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);
                }
                hr = sampGrabber.GetConnectedMediaType(this._capture_mediatype);
                DsError.ThrowExceptionForHR(hr);
                //ビデオフォーマット等の更新
                upateVideoInfo(sampGrabber);
            }
            finally
            {
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                    sampGrabber = null;
                }
                if (pSampleIn != null)
                {
                    Marshal.ReleaseComObject(pSampleIn);
                    pSampleIn = null;
                }
            }
        }
Example #53
0
        /// <summary>
        /// Returns the <see cref="CameraInfo"/> for the given <see cref="DsDevice"/>.
        /// </summary>
        /// <param name="dev">A <see cref="DsDevice"/> to parse name and capabilities for.</param>
        /// <returns>The <see cref="CameraInfo"/> for the given device.</returns>
        private CameraInfo Caps(DsDevice dev)
        {
            var camerainfo = new CameraInfo();

            try
            {
                // Get the graphbuilder object
                m_graphBuilder = (IFilterGraph2) new FilterGraph();

                // Get the ICaptureGraphBuilder2
                capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

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

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

                hr = capGraph.SetFiltergraph(m_graphBuilder);
                DsError.ThrowExceptionForHR(hr);

                hr = m_graphBuilder.AddFilter(capFilter, "Ds.NET Video Capture Device");
                DsError.ThrowExceptionForHR(hr);

                object o = null;
                DsGuid cat = PinCategory.Capture;
                DsGuid type = MediaType.Interleaved;
                DsGuid iid = typeof (IAMStreamConfig).GUID;

                // Check if Video capture filter is in use
                hr = capGraph.RenderStream(cat, MediaType.Video, capFilter, null, null);
                if (hr != 0)
                {
                    return null;
                }

                //hr = capGraph.FindInterface(PinCategory.Capture, MediaType.Interleaved, capFilter, typeof(IAMStreamConfig).GUID, out o);
                //if (hr != 0)
                //{
                hr = capGraph.FindInterface(PinCategory.Capture, MediaType.Video, capFilter,
                                            typeof (IAMStreamConfig).GUID, out o);
                DsError.ThrowExceptionForHR(hr);
                //}

                var videoStreamConfig = o as IAMStreamConfig;

                int iCount = 0;
                int iSize = 0;

                try
                {
                    if (videoStreamConfig != null) videoStreamConfig.GetNumberOfCapabilities(out iCount, out iSize);
                }
                catch (Exception ex)
                {
                    //ErrorLogger.ProcessException(ex, false);
                    return null;
                }

                pscc = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof (VideoStreamConfigCaps)));

                camerainfo.Name = dev.Name;
                camerainfo.DirectshowDevice = dev;

                for (int i = 0; i < iCount; i++)
                {
                    VideoStreamConfigCaps scc;

                    try
                    {
                        AMMediaType curMedType;
                        if (videoStreamConfig != null) hr = videoStreamConfig.GetStreamCaps(i, out curMedType, pscc);
                        Marshal.ThrowExceptionForHR(hr);
                        scc = (VideoStreamConfigCaps) Marshal.PtrToStructure(pscc, typeof (VideoStreamConfigCaps));

                        var CSF = new CamSizeFPS();
                        CSF.FPS = (int) (10000000/scc.MinFrameInterval);
                        CSF.Height = scc.InputSize.Height;
                        CSF.Width = scc.InputSize.Width;

                        if (!InSizeFpsList(camerainfo.SupportedSizesAndFPS, CSF))
                            if (ParametersOK(CSF))
                                camerainfo.SupportedSizesAndFPS.Add(CSF);
                    }
                    catch (Exception ex)
                    {
                        //ErrorLogger.ProcessException(ex, false);
                    }
                }
            }
            finally
            {
            }

            return camerainfo;
        }
Example #54
0
        /// <summary> build the capture graph for grabber. </summary>
        private void SetupGraph(DsDevice dev, int iWidth, int iHeight, short iBPP, Control hControl)
        {
            int hr;

            ISampleGrabber sampGrabber = null;
            IBaseFilter    capFilter   = null;
            IPin           pCaptureOut = null;
            IPin           pSampleIn   = null;
            IPin           pRenderIn   = null;

            // Get the graphbuilder object
            m_FilterGraph = new FilterGraph() as IFilterGraph2;

            try
            {
#if DEBUG
                m_rot = new DsROTEntry(m_FilterGraph);
#endif
                // add the video input device
                hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
                DsError.ThrowExceptionForHR(hr);

                // Find the still pin
                m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0);

                // Didn't find one.  Is there a preview pin?
                if (m_pinStill == null)
                {
                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0);
                }

                // Still haven't found one.  Need to put a splitter in so we have
                // one stream to capture the bitmap from, and one to display.  Ok, we
                // don't *have* to do it that way, but we are going to anyway.
                if (m_pinStill == null)
                {
                    IPin pRaw   = null;
                    IPin pSmart = null;

                    // There is no still pin
                    m_VidControl = null;

                    // Add a splitter
                    IBaseFilter iSmartTee = (IBaseFilter) new SmartTee();

                    try
                    {
                        hr = m_FilterGraph.AddFilter(iSmartTee, "SmartTee");
                        DsError.ThrowExceptionForHR(hr);

                        // Find the find the capture pin from the video device and the
                        // input pin for the splitter, and connnect them
                        pRaw   = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
                        pSmart = DsFindPin.ByDirection(iSmartTee, PinDirection.Input, 0);

                        hr = m_FilterGraph.Connect(pRaw, pSmart);
                        DsError.ThrowExceptionForHR(hr);

                        // Now set the capture and still pins (from the splitter)
                        m_pinStill  = DsFindPin.ByName(iSmartTee, "Preview");
                        pCaptureOut = DsFindPin.ByName(iSmartTee, "Capture");

                        // If any of the default config items are set, perform the config
                        // on the actual video device (rather than the splitter)
                        if (iHeight + iWidth + iBPP > 0)
                        {
                            SetConfigParms(pRaw, iWidth, iHeight, iBPP);
                        }
                    }
                    finally
                    {
                        if (pRaw != null)
                        {
                            Marshal.ReleaseComObject(pRaw);
                        }
                        if (pRaw != pSmart)
                        {
                            Marshal.ReleaseComObject(pSmart);
                        }
                        if (pRaw != iSmartTee)
                        {
                            Marshal.ReleaseComObject(iSmartTee);
                        }
                    }
                }
                else
                {
                    // Get a control pointer (used in Click())
                    m_VidControl = capFilter as IAMVideoControl;

                    pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);

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

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

                // Configure the sample grabber
                IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
                ConfigureSampleGrabber(sampGrabber);
                pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

                // Get the default video renderer
                IBaseFilter pRenderer = new VideoRendererDefault() as IBaseFilter;
                hr = m_FilterGraph.AddFilter(pRenderer, "Renderer");
                DsError.ThrowExceptionForHR(hr);

                pRenderIn = DsFindPin.ByDirection(pRenderer, PinDirection.Input, 0);

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

                if (m_VidControl == null)
                {
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);

                    // Connect the capture pin to the renderer
                    hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn);
                    DsError.ThrowExceptionForHR(hr);
                }
                else
                {
                    // Connect the capture pin to the renderer
                    hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn);
                    DsError.ThrowExceptionForHR(hr);

                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);
                }

                // Learn the video properties
                SaveSizeInfo(sampGrabber);
                ConfigVideoWindow(hControl);

                // Start the graph
                IMediaControl mediaCtrl = m_FilterGraph as IMediaControl;
                hr = mediaCtrl.Run();
                DsError.ThrowExceptionForHR(hr);
            }
            finally
            {
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                    sampGrabber = null;
                }
                if (pCaptureOut != null)
                {
                    Marshal.ReleaseComObject(pCaptureOut);
                    pCaptureOut = null;
                }
                if (pRenderIn != null)
                {
                    Marshal.ReleaseComObject(pRenderIn);
                    pRenderIn = null;
                }
                if (pSampleIn != null)
                {
                    Marshal.ReleaseComObject(pSampleIn);
                    pSampleIn = null;
                }
            }
        }
Example #55
0
    /// <summary>
    /// Adds the video compressor.
    /// </summary>
    /// <param name="_graphBuilder">GraphBuilder</param>
    /// <returns></returns>
    private bool AddVideoCompressor(IFilterGraph2 _graphBuilder)
    {
      Log.Log.WriteFile("analog: AddVideoCompressor");
      DsDevice[] devices1 = DsDevice.GetDevicesOfCat(FilterCategory.VideoCompressorCategory);
      DsDevice[] devices2 = DsDevice.GetDevicesOfCat(FilterCategory.LegacyAmFilterCategory);
      IList<SoftwareEncoder> videoEncoders = _layer.GetSofwareEncodersVideo();
      DsDevice[] videoDevices = new DsDevice[videoEncoders.Count];
      for (int x = 0; x < videoEncoders.Count; ++x)
      {
        videoDevices[x] = null;
      }
      for (int i = 0; i < devices1.Length; i++)
      {
        for (int x = 0; x < videoEncoders.Count; ++x)
        {
          if (videoEncoders[x].Name == devices1[i].Name)
          {
            videoDevices[x] = devices1[i];
            break;
          }
        }
      }
      for (int i = 0; i < devices2.Length; i++)
      {
        for (int x = 0; x < videoEncoders.Count; ++x)
        {
          if (videoEncoders[x].Name == devices2[i].Name)
          {
            videoDevices[x] = devices2[i];
            break;
          }
        }
      }
      //for each compressor
      Log.Log.WriteFile("analog: AddVideoCompressor found:{0} compressor", videoDevices.Length);
      for (int i = 0; i < videoDevices.Length; i++)
      {
        IBaseFilter tmp;
        if (videoDevices[i] == null || !EncodersInUse.Instance.Add(videoDevices[i], videoEncoders[i]))
        {
          continue;
        }

        Log.Log.WriteFile("analog:  try compressor:{0}", videoDevices[i].Name);
        int hr;
        try
        {
          //add compressor filter to graph
          hr = _graphBuilder.AddSourceFilterForMoniker(videoDevices[i].Mon, null, videoDevices[i].Name, out tmp);
        }
        catch (Exception)
        {
          Log.Log.WriteFile("analog: cannot add compressor to graph");
          EncodersInUse.Instance.Remove(videoDevices[i]);
          continue;
        }
        if (hr != 0)
        {
          //failed to add filter to graph, continue with the next one
          if (tmp != null)
          {
            _graphBuilder.RemoveFilter(tmp);
            Release.ComObject("videocompressor", tmp);
          }
          EncodersInUse.Instance.Remove(videoDevices[i]);
          continue;
        }
        if (tmp == null)
        {
          EncodersInUse.Instance.Remove(videoDevices[i]);
          continue;
        }

        // check if this compressor filter has an mpeg audio output pin
        Log.Log.WriteFile("analog:  connect video pin->video compressor");
        IPin pinVideo = DsFindPin.ByDirection(tmp, PinDirection.Input, 0);
        // we found a nice compressor, lets try to connect the analog video pin to the compressor
        hr = _graphBuilder.Connect(_pinAnalogVideo, pinVideo);
        if (hr != 0)
        {
          Log.Log.WriteFile("analog: failed to connect video pin->video compressor");
          //unable to connec the pin, remove it and continue with next compressor
          _graphBuilder.RemoveFilter(tmp);
          Release.ComObject("videocompressor", tmp);
          EncodersInUse.Instance.Remove(videoDevices[i]);
          continue;
        }

        //succeeded.
        _videoCompressorDevice = videoDevices[i];
        _filterVideoCompressor = tmp;
        return true;
      }
      return false;
    }