StopCapture()
        {
            int hr;

            // Are we capturing?
            if (m_Capturing)
            {
                // disconnect segments
                hr = m_pBridge.BridgeGraphs(null, null);
                DsError.ThrowExceptionForHR(hr);

                // stop capture graph
                IMediaControl pMC = (IMediaControl)m_pCaptureGraph;

                hr = pMC.Stop();
                DsError.ThrowExceptionForHR(hr);

                // disable capture stream (to save resources)
                IAMStreamControl pSC = (IAMStreamControl)m_pCapOutput;

                pSC.StartAt(NEVER, 0); // Ignore any error

                m_Capturing = false;
            }
        }
        StartCapture()
        {
            int hr;

            if (m_DeviceSelected)
            {
                if (m_strFile != null)
                {
                    // re-enable capture stream
                    IAMStreamControl pSC = (IAMStreamControl)m_pCapOutput;

                    // immediately!
                    pSC.StartAt(null, 0); // Ignore any error

                    // start capture graph
                    IMediaControl pMC = (IMediaControl)m_pCaptureGraph;
                    hr = pMC.Run();
                    DsError.ThrowExceptionForHR(hr);

                    hr = m_pBridge.BridgeGraphs(m_pSourceGraphSinkFilter, m_pCaptureGraphSourceFilter);
                    DsError.ThrowExceptionForHR(hr);

                    // If we make it here, we are capturing
                    m_Capturing = true;
                }
                else
                {
                    throw new Exception("File name not specified");
                }
            }
            else
            {
                throw new Exception("Device not selected");
            }
        }
Example #3
0
        private void Configure()
        {
            int hr;

            m_FilterGraph = (IFilterGraph2) new FilterGraph();

            DsROTEntry ds = new DsROTEntry(m_FilterGraph);

            ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

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

            DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            Guid        iid  = typeof(IBaseFilter).GUID;
            object      o;

            devs[0].Mon.BindToObject(null, null, ref iid, out o);

            IBaseFilter dev = (IBaseFilter)o;

            hr = m_FilterGraph.AddFilter(dev, "device");
            DsError.ThrowExceptionForHR(hr);

            IBaseFilter mux = (IBaseFilter) new AviDest();

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

            hr = icgb.RenderStream(null, null, dev, null, mux);
            DsError.ThrowExceptionForHR(hr);

            IPin pPin = DsFindPin.ByDirection(mux, PinDirection.Input, 0);

            m_sc = (IAMStreamControl)pPin;

            Marshal.ReleaseComObject(icgb);
            Marshal.ReleaseComObject(mux);
            Marshal.ReleaseComObject(dev);
        }
        SelectDevice(DsDevice dev, IntPtr hwnd)
        {
            int                   hr;
            IBaseFilter           pfDevice = null;
            ICaptureGraphBuilder2 pBuilder = null;

            // release any leftovers
            ReleaseSelectMembers();

            try
            {
                // create source graph and add sink filter
                m_pSourceGraph = (IGraphBuilder) new FilterGraph();
                m_rot1         = new DsROTEntry(m_pSourceGraph);

                m_pBridge = (IGMFBridgeController) new GMFBridgeController();

                // init to video-only, in discard mode (ie when source graph
                // is running but not connected, buffers are discarded at the bridge)
                hr = m_pBridge.AddStream(true, eFormatType.MuxInputs, true);
                DsError.ThrowExceptionForHR(hr);

                // Add the requested device
                hr = ((IFilterGraph2)m_pSourceGraph).AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out pfDevice);
                DsError.ThrowExceptionForHR(hr);

                // Add the sink filter to the source graph
                hr = m_pBridge.InsertSinkFilter(m_pSourceGraph, out m_pSourceGraphSinkFilter);
                DsError.ThrowExceptionForHR(hr);

                // use capture graph builder to render preview
                pBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

                // Init the CaptureGraphBuilder2
                hr = pBuilder.SetFiltergraph(m_pSourceGraph);
                DsError.ThrowExceptionForHR(hr);

                // Connect the filters together to allow preview
                hr = pBuilder.RenderStream(PinCategory.Preview, MediaType.Video, pfDevice, null, null);
                DsError.ThrowExceptionForHR(hr);

                // connect capture output to the pseudo-sink filter,
                // where it will be discarded until required
                hr = pBuilder.RenderStream(PinCategory.Capture, MediaType.Video, pfDevice, null, m_pSourceGraphSinkFilter);
                DsError.ThrowExceptionForHR(hr);

                // turn off capture stream if possible except when capturing
                hr = pBuilder.FindPin(pfDevice, PinDirection.Output, PinCategory.Capture, MediaType.Video, false, 0, out m_pCapOutput);
                if (hr >= 0)
                {
                    IAMStreamControl pSC = (IAMStreamControl)m_pCapOutput;
                    pSC.StartAt(NEVER, 0);  // Ignore any error
                }

                ConfigureVideo(hwnd);

                IMediaControl pMC = (IMediaControl)m_pSourceGraph;

                hr = pMC.Run();
                DsError.ThrowExceptionForHR(hr);

                // If we made it here, the device is selected
                m_DeviceSelected = true;
            }
            catch
            {
                ReleaseSelectMembers();
                throw;
            }
            finally
            {
                if (pBuilder != null)
                {
                    Marshal.ReleaseComObject(pBuilder);
                }

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