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"); } }
private void TestStart() { int hr; AMStreamInfo pInfo; hr = m_sc.StartAt(123, 456); DsError.ThrowExceptionForHR(hr); hr = m_sc.GetInfo(out pInfo); Debug.Assert(pInfo.tStart == 123 && pInfo.dwStartCookie == 456, "start"); }
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); } } }