public VideoInfoHeader StartProvider(bool preview, ISampleGrabberCB samplegrabber, IntPtr owner) { int hr; VideoInfoHeader rv; try { if (!DsUtils.IsCorrectDirectXVersion()) { throw new Exception("At least DirectX 8.1 is required!"); } if (!DsDev.GetDevicesOfCat(FilterCategory.VideoInputDevice, out capDevices)) { throw new Exception("Could not retrieve the list of capture devices!"); } if (capDevices.Count == 0) { throw new Exception("Capture Sample Provider is not applicable!"); } DsDevice device = null; if (capDevices.Count == 1) { device = capDevices[0] as DsDevice; } else { DeviceSelector selector = new DeviceSelector(capDevices); selector.ShowDialog(); device = selector.SelectedDevice; } if (device == null) { throw new Exception("No video capture device could be selected!"); } CreateCaptureDevice(device.Mon); GetInterfaces(); rv = SetupGraph(preview, samplegrabber); SetupVideoWindow(owner); hr = mediaCtrl.Run(); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } } catch (Exception ee) { throw new Exception("Could not start video stream\r\n" + ee.Message); } return(rv); }
public VideoInfoHeader StartProvider(bool preview, ISampleGrabberCB samplegrabber, IntPtr owner) { this.preview = preview; this.sampleGrabber = samplegrabber; inputMediaDialog.ShowDialog(); SetupVideoWindow(owner); return(videoInfoHeader); }
/// <summary> /// /// </summary> /// <param name="device"></param> /// <param name="nWidth"></param> /// <param name="nHeight"></param> /// <param name="nFPS"></param> /// <param name="bpp"></param> /// <param name="grabberCallback"></param> /// <param name="subType"></param> /// <returns></returns> public int Open(DsDevice device, int nWidth, int nHeight, int nFPS, int bpp, ISampleGrabberCB grabberCallback = null, Guid subType = new Guid()) { camWidth = nWidth; camHeight = nHeight; camFPS = nFPS; camBpp = bpp; camStride = nWidth * bpp / 8; return(CaptureVideo(device, nFPS, nWidth, nHeight, grabberCallback, subType));; }
protected void RenderWindowHelper(ICaptureGraphBuilder2 graphBuilder, ISampleGrabberCB callback, string typeName, IPin pin, IBaseFilter renderer) { int hr; // Add the renderer to the graph hr = Graph.AddFilter(renderer, typeName + " Renderer"); _cleanup.Add(renderer); DESError.ThrowExceptionForHR(hr); // Do everything else RenderHelper(graphBuilder, callback, typeName, pin, null, renderer); }
protected void RenderHelper(ICaptureGraphBuilder2 graphBuilder, ISampleGrabberCB callback, string typeName, IPin pin, IBaseFilter compressor, IBaseFilter destination) { if (graphBuilder == null) throw new ArgumentNullException(GraphBuilderParameterName); if (pin == null) throw new ArgumentNullException(PinParameterName); if (destination == null) throw new ArgumentNullException(DestinationParameterName); int hr; IBaseFilter ibfSampleGrabber = null; try { // If no callback was provided, don't create a samplegrabber if (callback != null) { var isg = (ISampleGrabber) new SampleGrabber(); ibfSampleGrabber = (IBaseFilter) isg; _cleanup.Add(ibfSampleGrabber); hr = isg.SetCallback(callback, 1); DESError.ThrowExceptionForHR(hr); hr = Graph.AddFilter(ibfSampleGrabber, typeName + " sample grabber"); DESError.ThrowExceptionForHR(hr); } // If a compressor was provided, add it to the graph and connect it up if (compressor != null) { // Connect the pin. hr = Graph.AddFilter(compressor, typeName + " Compressor"); DESError.ThrowExceptionForHR(hr); FilterGraphTools.ConnectFilters(Graph, pin, ibfSampleGrabber, true); FilterGraphTools.ConnectFilters(Graph, ibfSampleGrabber, compressor, true); FilterGraphTools.ConnectFilters(Graph, compressor, destination, true); } else { // Just connect the SampleGrabber (if any) hr = graphBuilder.RenderStream(null, null, pin, ibfSampleGrabber, destination); DESError.ThrowExceptionForHR(hr); } } finally { if (ibfSampleGrabber != null) { Marshal.ReleaseComObject(ibfSampleGrabber); } } }
public void SetUpForTs(ISampleGrabberCB grabber, int methodToCall) { FilterGraphTools.DisconnectPins(mpeg2Demux); //FilterGraphTools.DisconnectPins(demodulator); FilterGraphTools.DisconnectPins(audioRenderer); FilterGraphTools.DisconnectPins(videoRenderer); //graphBuilder.RemoveFilter(audioRenderer); //graphBuilder.RemoveFilter(videoRenderer); sampleGrabber = (ISampleGrabber) new SampleGrabber(); AMMediaType media = new AMMediaType(); media.majorType = MediaType.Stream; media.subType = MediaSubType.Mpeg2Transport; media.formatType = FormatType.MpegStreams; sampleGrabber.SetOneShot(false); sampleGrabber.SetBufferSamples(true); int hr = sampleGrabber.SetMediaType(media); DsError.ThrowExceptionForHR(hr); graphBuilder.AddFilter((IBaseFilter)sampleGrabber, "Sample Grabber"); nullRenderer = (IBaseFilter) new NullRenderer(); graphBuilder.AddFilter(nullRenderer, "NULL Renderer"); IPin pinIn = DsFindPin.ByName((IBaseFilter)sampleGrabber, "Input"); IPin pinOut = DsFindPin.ByDirection(capture, PinDirection.Output, 0); IEnumMediaTypes eMedia; pinOut.EnumMediaTypes(out eMedia); AMMediaType[] mediaTypes = new AMMediaType[1]; eMedia.Next(mediaTypes.Length, mediaTypes, IntPtr.Zero); hr = sampleGrabber.SetMediaType(mediaTypes[0]); DsError.ThrowExceptionForHR(hr); pinOut.Disconnect(); PinInfo info; pinOut.QueryPinInfo(out info); hr = graphBuilder.ConnectDirect(pinOut, pinIn, mediaTypes[0]); //hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = DsFindPin.ByName(nullRenderer, "In"); pinOut = DsFindPin.ByName((IBaseFilter)sampleGrabber, "Output"); hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); sampleGrabber.SetCallback(grabber, methodToCall); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = null; }
//Step in to capture the handler then proceed to call the ISampleGrabber.SetCallbcak public int SetCallback(ISampleGrabberCB pCallback, int WhichMethodToCallback) { m_callBackHandle = (CamCaptureGrabberCallBack)pCallback; return((this as ISampleGrabber).SetCallback((ISampleGrabberCB)pCallback, WhichMethodToCallback)); }
/// <summary> /// /// </summary> /// <param name="device"></param> /// <param name="iFrameRate"></param> /// <param name="iWidth"></param> /// <param name="iHeight"></param> /// <param name="grabberCallback"></param> /// <param name="subType"></param> /// <returns></returns> public int CaptureVideo(DsDevice device, int iFrameRate, int iWidth, int iHeight, ISampleGrabberCB grabberCallback, Guid subType) { int hr = 0; IBaseFilter sourceFilter = null; IBaseFilter renderFilter = null; try { // Get DirectShow interfaces hr = OpenInterfaces(); // Attach the filter graph to the capture graph hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder); DsError.ThrowExceptionForHR(hr); // Use the system device enumerator and class enumerator to find // a video capture/preview device, such as a desktop USB video camera. sourceFilter = SelectCaptureDevice(device); // Add Capture filter to graph. hr = this.graphBuilder.AddFilter(sourceFilter, "DirectShowCam"); DsError.ThrowExceptionForHR(hr); // Configure preview settings. SetConfigParams(this.captureGraphBuilder, sourceFilter, iFrameRate, iWidth, iHeight, subType); // Initialize SampleGrabber. sampleGrabber = new SampleGrabber() as ISampleGrabber; // Configure SampleGrabber. Add preview callback. ConfigureSampleGrabber(sampleGrabber, subType, grabberCallback); // Add SampleGrabber to graph. hr = this.graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "Frame Callback"); DsError.ThrowExceptionForHR(hr); // Add the Null Render to the filter graph renderFilter = new NullRenderer() as IBaseFilter; hr = this.graphBuilder.AddFilter(renderFilter, "NullRenderer"); // Render the preview hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, (sampleGrabber as IBaseFilter), renderFilter); //hr = this.captureGraphBuilder.RenderStream( PinCategory.Preview, MediaType.Video, sourceFilter, (sampleGrabber as IBaseFilter), null ); DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampleGrabber); // Add our graph to the running object table, which will allow // the GraphEdit application to "spy" on our graph rot = new DsROTEntry(this.graphBuilder); // Start previewing video data //hr = this.mediaControl.Run(); //DsError.ThrowExceptionForHR( hr ); } catch { ////MessageBox.Show( "An unrecoverable error has occurred." ); } finally { if (sourceFilter != null) { Marshal.ReleaseComObject(sourceFilter); sourceFilter = null; } if (sampleGrabber != null) { Marshal.ReleaseComObject(sampleGrabber); sampleGrabber = null; } } return(hr); }
/// <summary> /// /// </summary> /// <param name="sampleGrabber"></param> /// <param name="subType"></param> /// <param name="grabberCallback"></param> private void ConfigureSampleGrabber(ISampleGrabber sampleGrabber, Guid subType, ISampleGrabberCB grabberCallback) { AMMediaType media; int hr; // Set the media type to Video and format as subtype guid media = new AMMediaType(); media.majorType = MediaType.Video; // WORKAROUND !!! In case of normal USB Web camera, it should be set RGB24 with parameter null. if (subType != MediaSubType.Null) { media.subType = subType; } else { media.subType = MediaSubType.RGB24; } media.formatType = FormatType.VideoInfo; hr = sampleGrabber.SetMediaType(media); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(media); media = null; // +EDDY_TEST ///sampleGrabber.SetBufferSamples( true ); ///sampleGrabber.SetOneShot( true ); // -EDDY_TEST hr = sampleGrabber.SetCallback(grabberCallback, 1); // BufferCallback //hr = sampleGrabber.SetCallback( grabberCallback, 0 ); // SampleCallback DsError.ThrowExceptionForHR(hr); }
public void SetUpForTs(ISampleGrabberCB grabber, int methodToCall) { FilterGraphTools.DisconnectPins(mpeg2Demux); //FilterGraphTools.DisconnectPins(demodulator); FilterGraphTools.DisconnectPins(audioRenderer); FilterGraphTools.DisconnectPins(videoRenderer); //graphBuilder.RemoveFilter(audioRenderer); //graphBuilder.RemoveFilter(videoRenderer); sampleGrabber = (ISampleGrabber)new SampleGrabber(); AMMediaType media = new AMMediaType(); media.majorType = MediaType.Stream; media.subType = MediaSubType.Mpeg2Transport; media.formatType = FormatType.MpegStreams; sampleGrabber.SetOneShot(false); sampleGrabber.SetBufferSamples(true); int hr = sampleGrabber.SetMediaType(media); DsError.ThrowExceptionForHR(hr); graphBuilder.AddFilter((IBaseFilter)sampleGrabber, "Sample Grabber"); nullRenderer = (IBaseFilter)new NullRenderer(); graphBuilder.AddFilter(nullRenderer, "NULL Renderer"); IPin pinIn = DsFindPin.ByName((IBaseFilter)sampleGrabber, "Input"); IPin pinOut = DsFindPin.ByDirection(capture, PinDirection.Output, 0); IEnumMediaTypes eMedia; pinOut.EnumMediaTypes(out eMedia); AMMediaType[] mediaTypes = new AMMediaType[1]; eMedia.Next(mediaTypes.Length, mediaTypes, IntPtr.Zero); hr = sampleGrabber.SetMediaType(mediaTypes[0]); DsError.ThrowExceptionForHR(hr); pinOut.Disconnect(); PinInfo info; pinOut.QueryPinInfo(out info); hr = graphBuilder.ConnectDirect(pinOut, pinIn, mediaTypes[0]); //hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = DsFindPin.ByName(nullRenderer, "In"); pinOut = DsFindPin.ByName((IBaseFilter)sampleGrabber, "Output"); hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); sampleGrabber.SetCallback(grabber, methodToCall); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = null; }
protected void RenderHelper(ICaptureGraphBuilder2 graphBuilder, ISampleGrabberCB callback, string typeName, IPin pin, IBaseFilter compressor, IBaseFilter destination) { if (graphBuilder == null) { throw new ArgumentNullException(GraphBuilderParameterName); } if (pin == null) { throw new ArgumentNullException(PinParameterName); } if (destination == null) { throw new ArgumentNullException(DestinationParameterName); } int hr; IBaseFilter ibfSampleGrabber = null; try { // If no callback was provided, don't create a samplegrabber if (callback != null) { var isg = (ISampleGrabber) new SampleGrabber(); ibfSampleGrabber = (IBaseFilter)isg; _cleanup.Add(ibfSampleGrabber); hr = isg.SetCallback(callback, 1); DESError.ThrowExceptionForHR(hr); hr = Graph.AddFilter(ibfSampleGrabber, typeName + " sample grabber"); DESError.ThrowExceptionForHR(hr); } // If a compressor was provided, add it to the graph and connect it up if (compressor != null) { // Connect the pin. hr = Graph.AddFilter(compressor, typeName + " Compressor"); DESError.ThrowExceptionForHR(hr); FilterGraphTools.ConnectFilters(Graph, pin, ibfSampleGrabber, true); FilterGraphTools.ConnectFilters(Graph, ibfSampleGrabber, compressor, true); FilterGraphTools.ConnectFilters(Graph, compressor, destination, true); } else { // Just connect the SampleGrabber (if any) hr = graphBuilder.RenderStream(null, null, pin, ibfSampleGrabber, destination); DESError.ThrowExceptionForHR(hr); } } finally { if (ibfSampleGrabber != null) { Marshal.ReleaseComObject(ibfSampleGrabber); } } }
/// <summary> build the capture graph for grabber. </summary> /* * Ha nem akar magától felépülni, szükség lehet egy manuális beszúrásra. * Ez pl a gyártónak jó, hogy ne menjen a cucc, csak az õ programjával. LoL * Ekkor minden kamerához ki kell választani a megfelelõ, rejtett splittert, * és utána újra próbálkozni. Ekkor már menni fog. Érdekes "védelem". */ VideoInfoHeader SetupGraph(bool preview, ISampleGrabberCB samplegrabber) { int hr; try { hr = capGraph.SetFiltergraph(graphBuilder); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } hr = graphBuilder.AddFilter(capFilter, "Ds.NET Video Capture Device"); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } //DsUtils.ShowCapPinDialog( capGraph, capFilter, IntPtr.Zero ); AMMediaType media = new AMMediaType(); media.majorType = MediaType.Video; media.subType = MediaSubType.RGB24; media.formatType = FormatType.VideoInfo; // ??? hr = sampGrabber.SetMediaType(media); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } Guid cat; Guid med; if (preview) { cat = PinCategory.Preview; med = MediaType.Video; hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, null); // preview if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } } cat = PinCategory.Capture; med = MediaType.Video; hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, baseGrabFlt); // baseGrabFlt // Ha itt száll el gáz van!!! if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } media = new AMMediaType(); hr = sampGrabber.GetConnectedMediaType(media); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader)); Marshal.FreeCoTaskMem(media.formatPtr); media.formatPtr = IntPtr.Zero; //Modified according to the platform SDK, to capture the buffer hr = sampGrabber.SetBufferSamples(false); if (hr == 0) { hr = sampGrabber.SetOneShot(false); } if (hr == 0) { hr = sampGrabber.SetCallback(samplegrabber, 1); } if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } //this is needed for external processing return(videoInfoHeader); } catch (Exception ee) { throw new Exception("Could not setup graph\r\n" + ee.Message); } }