private void Setup() { int hr; IBaseFilter ibf; IFilterGraph2 ifg = new FilterGraph() as IFilterGraph2; m_imc = ifg as IMediaControl; DsROTEntry rot = new DsROTEntry(ifg); DsDevice[] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); hr = ifg.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out ibf); ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; hr = icgb2.SetFiltergraph(ifg); DsError.ThrowExceptionForHR(hr); hr = icgb2.RenderStream(null, null, ibf, null, null); DsError.ThrowExceptionForHR(hr); IBaseFilter pFilter; hr = ifg.FindFilterByName("Video Renderer", out pFilter); m_qc = pFilter as IQualityControl; rot.Dispose(); }
private void TestProf() { int hr; ISBE2MediaTypeProfile pProfile; hr = m_ISBE2Crossbar.GetInitialProfile(out pProfile); DsError.ThrowExceptionForHR(hr); Debug.Assert(pProfile != null); int i = 0; IPin[] op = null; hr = m_ISBE2Crossbar.SetOutputProfile(pProfile, ref i, op); DsError.ThrowExceptionForHR(hr); Debug.Assert(i == 2); op = new IPin[i]; hr = m_ISBE2Crossbar.SetOutputProfile(pProfile, ref i, op); DsError.ThrowExceptionForHR(hr); Debug.Assert(op[0] != null && op[1] != null); ICaptureGraphBuilder2 cgb = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; hr = cgb.SetFiltergraph(m_fg as IGraphBuilder); hr = cgb.RenderStream(null, MediaType.Video, op[0], null, null); }
private void TestEm() { int hr; AllocatorProperties prop = new AllocatorProperties(); Guid grf = typeof(IBaseFilter).GUID; prop.cbAlign = 1; prop.cbBuffer = 3000000; prop.cbPrefix = 0; prop.cBuffers = 12; hr = m_ibn.SuggestAllocatorProperties(prop); DsError.ThrowExceptionForHR(hr); IGraphBuilder ifg = new FilterGraph() as IGraphBuilder; DsROTEntry rot = new DsROTEntry(ifg); hr = ifg.AddFilter(m_ibf, "Device"); DsError.ThrowExceptionForHR(hr); ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; hr = icgb2.SetFiltergraph(ifg); DsError.ThrowExceptionForHR(hr); hr = icgb2.RenderStream(null, null, m_ibf, null, null); DsError.ThrowExceptionForHR(hr); // Returns E_FAIL for all my devices, so I wrote my own filter // that implements it for a test. Note: You CANNOT use "out" here. hr = m_ibn.GetAllocatorProperties(prop); //DsError.ThrowExceptionForHR(hr); rot.Dispose(); }
private void Config2() { Guid g = typeof(IBaseFilter).GUID; DsDevice[] devs; IBaseFilter ibf, ibf2; object o; int hr; IFilterGraph2 ifg = new FilterGraph() as IFilterGraph2; ibf = m_rs as IBaseFilter; hr = ifg.AddFilter(ibf, "resizer"); DsError.ThrowExceptionForHR(hr); devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); devs[1].Mon.BindToObject(null, null, ref g, out o); ibf2 = o as IBaseFilter; hr = ifg.AddFilter(ibf2, "camera"); DsError.ThrowExceptionForHR(hr); ICaptureGraphBuilder2 cgb = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; hr = cgb.SetFiltergraph(ifg); DsError.ThrowExceptionForHR(hr); hr = cgb.RenderStream(null, null, ibf2, null, ibf); 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); 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); }
private void Config() { int hr; IFilterGraph gb = (IFilterGraph)new FilterGraph(); int iDeviceIndex = 0; ICaptureGraphBuilder2 cgb = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; IMediaControl m_imc1 = gb as IMediaControl; hr = cgb.SetFiltergraph((IGraphBuilder)gb); DsDevice[] dev = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); IBaseFilter theDevice; hr = ((IFilterGraph2)gb).AddSourceFilterForMoniker(dev[iDeviceIndex].Mon, null, dev[iDeviceIndex].Name, out theDevice); IStreamBufferSink sbs = new SBE2Sink() as IStreamBufferSink; hr = gb.AddFilter((IBaseFilter)sbs, "StreamBufferSink"); DsError.ThrowExceptionForHR(hr); hr = cgb.RenderStream(null, null, theDevice, null, sbs as IBaseFilter); DsError.ThrowExceptionForHR(hr); hr = sbs.LockProfile(Environment.ExpandEnvironmentVariables(FILENAME)); DsError.ThrowExceptionForHR(hr); SetupGraph2(); Marshal.ReleaseComObject(cgb); Marshal.ReleaseComObject(theDevice); m_imc1.Run(); System.Threading.Thread.Sleep(1000); m_imc2.Run(); System.Threading.Thread.Sleep(1000); }
private void Setup() { int hr; IFilterGraph2 ifg = new FilterGraph() as IFilterGraph2; m_imc = ifg as IMediaControl; DsROTEntry rot = new DsROTEntry(ifg); IBaseFilter pFilter; DsDevice[] devs = DsDevice.GetDevicesOfCat(FilterCategory.AudioInputDevice); hr = ifg.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out pFilter); DsError.ThrowExceptionForHR(hr); ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; hr = icgb2.SetFiltergraph(ifg); DsError.ThrowExceptionForHR(hr); //IPin pPin = DsFindPin.ByDirection((IBaseFilter)o, PinDirection.Output, 0); hr = icgb2.RenderStream(null, MediaType.Audio, pFilter, null, null); DsError.ThrowExceptionForHR(hr); IBaseFilter pAudio; hr = ifg.FindFilterByName("Audio Renderer", out pAudio); m_ibn = pAudio as IAMAudioRendererStats; }
/// <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; } } }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(Control hWin, string FileName) { int hr; m_FilterGraph = new FilterGraph() as IFilterGraph2; ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber) new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null); DsError.ThrowExceptionForHR(hr); // Configure the Video Window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } }
public FrameGrabber(DsDevice camDevice) { IFilterGraph2 filterGraph; ICaptureGraphBuilder2 graphBuilder; IBaseFilter camBase, nullRenderer; ISampleGrabber sampleGrabber; filterGraph = new FilterGraph() as IFilterGraph2; mediaCtrl = filterGraph as IMediaControl; graphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; HRCheck(graphBuilder.SetFiltergraph(filterGraph)); // Add camera HRCheck(filterGraph.AddSourceFilterForMoniker( camDevice.Mon, null, camDevice.Name, out camBase)); // Add sample grabber sampleGrabber = new SampleGrabber() as ISampleGrabber; var mType = new AMMediaType() { majorType = MediaType.Video, subType = MediaSubType.RGB24, formatType = FormatType.VideoInfo }; HRCheck(sampleGrabber.SetMediaType(mType)); DsUtils.FreeAMMediaType(mType); HRCheck(sampleGrabber.SetCallback(this, 1)); HRCheck(filterGraph.AddFilter(sampleGrabber as IBaseFilter, "CamGrabber")); // Add null renderer nullRenderer = new NullRenderer() as IBaseFilter; HRCheck(filterGraph.AddFilter(nullRenderer, "Null renderer")); // Render the webcam through the grabber and the renderer HRCheck(graphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, camBase, sampleGrabber as IBaseFilter, nullRenderer)); // Get resulting picture size mType = new AMMediaType(); HRCheck(sampleGrabber.GetConnectedMediaType(mType)); if (mType.formatType != FormatType.VideoInfo || mType.formatPtr == IntPtr.Zero) { throw new NotSupportedException("Unknown grabber media format"); } var videoInfoHeader = Marshal.PtrToStructure(mType.formatPtr, typeof(VideoInfoHeader)) as VideoInfoHeader; width = videoInfoHeader.BmiHeader.Width; height = videoInfoHeader.BmiHeader.Height; Console.WriteLine("{0} x {1}", width, height); stride = width * (videoInfoHeader.BmiHeader.BitCount / 8); DsUtils.FreeAMMediaType(mType); HRCheck(mediaCtrl.Run()); }
/// <summary>Set up the filter graph for grabbing snapshots</summary> public void EnableGrabbing() { ICaptureGraphBuilder2 icgb2 = null; try { // Get a ICaptureGraphBuilder2 to help build the graph // Link the ICaptureGraphBuilder2 to the IFilterGraph2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; if (icgb2 == null) { throw new Exception("failed to create direct show CaptureGraphBuilder2"); } DsError.ThrowExceptionForHR(icgb2.SetFiltergraph(m_filter_graph)); // Get the SampleGrabber interface m_samp_grabber = (ISampleGrabber) new SampleGrabber(); { // Set the media type to Video/RBG24 AMMediaType media = new AMMediaType { majorType = MediaType.Video, subType = MediaSubType.RGB24, formatType = FormatType.VideoInfo }; try { DsError.ThrowExceptionForHR(m_samp_grabber.SetMediaType(media)); } finally { DsUtils.FreeAMMediaType(media); } } // Configure the sample grabber DsError.ThrowExceptionForHR(m_samp_grabber.SetBufferSamples(true)); // Add the sample graber to the filter graph IBaseFilter grab_filter = (IBaseFilter)m_samp_grabber; DsError.ThrowExceptionForHR(m_filter_graph.AddFilter(grab_filter, "DS.NET Grabber")); } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); } } }
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); }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(string FileName) { int hr; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); // Add the filters necessary to render the file. This function will // work with a number of different file types. IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber)new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // Add the null renderer to the graph IBaseFilter nullrenderer = new NullRenderer() as IBaseFilter; hr = m_FilterGraph.AddFilter(nullrenderer, "Null renderer"); DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, nullrenderer); DsError.ThrowExceptionForHR(hr); // Now that the graph is built, read the dimensions of the bitmaps we'll be getting SaveSizeInfo(m_sampGrabber); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } #if DEBUG // Double check to make sure we aren't releasing something // important. GC.Collect(); GC.WaitForPendingFinalizers(); #endif }
protected override void SetupGraph() { this.HasBeenSetup = true; // Get the graphbuilder object this.FilterGraph = new FilterGraph() as IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 if (icgb2 != null) { int hr = icgb2.SetFiltergraph(this.FilterGraph); DsError.ThrowExceptionForHR(hr); // Add the filters necessary to render the file. This function will // work with a number of different file types. IBaseFilter sourceFilter; hr = this.FilterGraph.AddSourceFilter(this.fileName, this.fileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // VIDEO // Get the SampleGrabber interface this.SampGrabber = (ISampleGrabber) new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)this.SampGrabber; // Configure the Sample Grabber this.ConfigureSampleGrabber(this.SampGrabber); // Add it to the filter hr = this.FilterGraph.AddFilter(baseGrabFlt, "Ds.NET GrabberV"); DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null); DsError.ThrowExceptionForHR(hr); // Connect audio hr = icgb2.RenderStream(null, MediaType.Audio, sourceFilter, null, null); if (hr != -2147467259 || hr == 0) { // don't throw for videos without audio track DsError.ThrowExceptionForHR(hr); } } IVideoWindow window = (IVideoWindow)this.FilterGraph; if (window != null) { window.put_AutoShow(OABool.False); } // Now that the graph is built, read the dimensions of the bitmaps we'll be getting this.SaveSizeInfo(this.SampGrabber); // Grab some other interfaces this.MediaCtrl = this.FilterGraph as IMediaControl; this.SampGrabber.SetBufferSamples(true); this.SampGrabber.SetOneShot(false); this.MediaEvent = this.FilterGraph as IMediaEvent; this.audio = this.FilterGraph as IBasicAudio; this.MediaPosition = this.FilterGraph as IMediaPosition; this.Volume = this.volume; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); } } }
public void SetupGraph() { GraphBuilder = (IGraphBuilder)new FilterGraph(); // Get ICaptureGraphBuilder2 to build the graph CaptureGraphBuilder2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { ReturnHR = CaptureGraphBuilder2.SetFiltergraph(GraphBuilder); IBaseFilter source_filter = null; ReturnHR = GraphBuilder.AddSourceFilter(InputFile, "Source Filter", out source_filter); IBaseFilter wmv_video_decoder_dmo = (IBaseFilter)new DMOWrapperFilter(); IDMOWrapperFilter dmo_wrapper_filter_v = (IDMOWrapperFilter)wmv_video_decoder_dmo; ReturnHR = dmo_wrapper_filter_v.Init(new Guid("{82D353DF-90BD-4382-8BC2-3F6192B76E34}"), DMOCategory.VideoDecoder); ReturnHR = GraphBuilder.AddFilter(wmv_video_decoder_dmo, "Wmv Video Decoder DMO"); IBaseFilter wmv_audio_decoder_dmo = (IBaseFilter)new DMOWrapperFilter(); IDMOWrapperFilter dmo_wrapper_filter_a = (IDMOWrapperFilter)wmv_audio_decoder_dmo; ReturnHR = dmo_wrapper_filter_a.Init(new Guid("{2EEB4ADF-4578-4D10-BCA7-BB955F56320A}"), DMOCategory.AudioDecoder); ReturnHR = GraphBuilder.AddFilter(wmv_audio_decoder_dmo, "Wmv Audio Decoder DMO"); IBaseFilter vp8_encoder = (IBaseFilter)new VP8Encoder(); ReturnHR = GraphBuilder.AddFilter(vp8_encoder, "VP8 Encoder"); IVP8Encoder vp8_encoder_interface = (IVP8Encoder)vp8_encoder; if (target_bitrate != 0) vp8_encoder_interface.SetTargetBitrate(target_bitrate); IBaseFilter webm_muxer = (IBaseFilter)new WebMMuxer(); ReturnHR = GraphBuilder.AddFilter(webm_muxer, "WebM Muxer"); IBaseFilter vorbis_encoder = (IBaseFilter)new VorbisEncoder(); ReturnHR = GraphBuilder.AddFilter(vorbis_encoder, "Vorbis Encoder"); IBaseFilter file_writer = (IBaseFilter)new FileWriter(); ReturnHR = GraphBuilder.AddFilter(file_writer, "file writer"); IFileSinkFilter filewriter_sink = file_writer as IFileSinkFilter; ReturnHR = filewriter_sink.SetFileName(OutputFile, null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("Raw Video 1", source_filter), FindPin("in0", wmv_video_decoder_dmo), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("Raw Audio 0", source_filter), FindPin("in0", wmv_audio_decoder_dmo), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("out0", wmv_audio_decoder_dmo), FindPin("PCM In", vorbis_encoder), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("out0", wmv_video_decoder_dmo), FindPin("YUV", vp8_encoder), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("Vorbis Out", vorbis_encoder), FindPin("audio", webm_muxer), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("VP80", vp8_encoder), FindPin("video", webm_muxer), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("outpin", webm_muxer), FindPin("in", file_writer), null); } catch (Exception) { LogError("Failed to initialize FilterGraph."); } finally { if (CaptureGraphBuilder2 != null) { Marshal.ReleaseComObject(CaptureGraphBuilder2); CaptureGraphBuilder2 = null; } } }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(Control hWin, string FileName) { int hr; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); #if DEBUG // Allows you to view the graph with GraphEdit File/Connect m_DsRot = new DsROTEntry(m_FilterGraph); #endif // Add the filters necessary to render the file. This function will // work with a number of different file types. IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber) new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null); DsError.ThrowExceptionForHR(hr); // Now that the graph is built, read the dimensions of the bitmaps we'll be getting SaveSizeInfo(m_sampGrabber); // Configure the Video Window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } #if DEBUG // Double check to make sure we aren't releasing something // important. GC.Collect(); GC.WaitForPendingFinalizers(); #endif }
/// <summary> /// Build the filter graph /// </summary> /// <param name="hWin">Window to draw into</param> private void SetupGraph(Control hWin) { // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph var captureGraphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 var hr = captureGraphBuilder.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); #if DEBUG // Allows you to view the graph with GraphEdit File/Connect m_DsRot = new DsROTEntry(m_FilterGraph); #endif // Our data source var source = new GenericSampleSourceFilter() as IBaseFilter; try { // Get the pin from the filter so we can configure it var ipin = DsFindPin.ByDirection(source, PinDirection.Output, 0); // x264vfw // {D76E2820-1563-11CF-AC98-00AA004C0FA9} // MediaType_Video // Pins: input(0), output(0) // DirectVobSub // {93A22E7A-5091-45EF-BA61-6DA26156A5D0} // Pins: // Input - Name: Video, Id: In // Input - Name: Input, id: Input, // majortype: MEDIATYPE_Subtitle {E487EB08-6B26-4BE9-9DD3-993434D313FD} // subtype: MEDIASUBTYPE_ASS {326444F7-686F-47FF-A4B2-C8C96307B4C2} // formattype: FORMAT_SubtitleInfo {A33D2F7D-96BC-4337-B23B-A8B9FBC295E9} // Output - Name: Output, Id: Out // Interfaces: IAMStreamSelect, IBaseFilter, IMediaFilter, IPersist, ISpecifyPropertyPages, IUnknown // IMemInputPin // {56A8689D-0AD4-11CE-B03A-0020AF0BA770} try { // Configure the pin using the provided BitmapInfo //this.ConfigurePusher(ipin as IGenericSampleConfig); var genericSampleConfig = ipin as IGenericSampleConfig; m_ImageHandler.SetMediaType(genericSampleConfig); // Specify the callback routine to call with each sample hr = genericSampleConfig.SetBitmapCB(m_ImageHandler); DsError.ThrowExceptionForHR(hr); } finally { Marshal.ReleaseComObject(ipin); } // Add the filter to the graph hr = m_FilterGraph.AddFilter(source, "GenericSampleSourceFilter"); Marshal.ThrowExceptionForHR(hr); // IFileSourceFilter // {56A868A6-0AD4-11CE-B03A-0020AF0BA770} // Connect the filters together, use the default renderer hr = captureGraphBuilder.RenderStream(null, null, source, null, null); // DsError.ThrowExceptionForHR( hr ); // Ignore any error. Blindly assume there is no video hr = captureGraphBuilder.RenderStream(null, MediaType.Audio, source, null, null); // DsError.ThrowExceptionForHR( hr ); // Ignore any error. Blindly assume there is no audio } finally { Marshal.ReleaseComObject(source); } // Configure the Video Window var videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaCtrl = m_FilterGraph as IMediaControl; } finally { Marshal.ReleaseComObject(captureGraphBuilder); } }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(Control hWin, string FileName) { int hr; IBaseFilter ibfRenderer = null; IPin iPinInFilter = null; IPin iPinOutFilter = null; IPin iPinInDest = null; m_FilterGraph = new FilterGraph() as IFilterGraph2; ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, "Ds.NET FileFilter", out sourceFilter); DsError.ThrowExceptionForHR(hr); // Hopefully this will be the video pin IPin iPinOutSource = DsFindPin.ByDirection(sourceFilter, PinDirection.Output, 0); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber)new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); iPinInFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); iPinOutFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); // Add it to the filter hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.Connect(iPinOutSource, iPinInFilter); DsError.ThrowExceptionForHR(hr); // Get the default video renderer ibfRenderer = (IBaseFilter)new VideoRendererDefault(); // Add it to the graph hr = m_FilterGraph.AddFilter(ibfRenderer, "Ds.NET VideoRendererDefault"); DsError.ThrowExceptionForHR(hr); iPinInDest = DsFindPin.ByDirection(ibfRenderer, PinDirection.Input, 0); // Connect the graph. Many other filters automatically get added here hr = m_FilterGraph.Connect(iPinOutFilter, iPinInDest); DsError.ThrowExceptionForHR(hr); // Configure the Video Window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; m_mediaPosition = m_FilterGraph as IMediaPosition; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } }
public void SetupGraph() { GraphBuilder = (IGraphBuilder) new FilterGraph(); // Get ICaptureGraphBuilder2 to build the graph CaptureGraphBuilder2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { ReturnHR = CaptureGraphBuilder2.SetFiltergraph(GraphBuilder); IBaseFilter source_filter = null; ReturnHR = GraphBuilder.AddSourceFilter(InputFile, "Source Filter", out source_filter); IBaseFilter wmv_video_decoder_dmo = (IBaseFilter) new DMOWrapperFilter(); IDMOWrapperFilter dmo_wrapper_filter_v = (IDMOWrapperFilter)wmv_video_decoder_dmo; ReturnHR = dmo_wrapper_filter_v.Init(new Guid("{82D353DF-90BD-4382-8BC2-3F6192B76E34}"), DMOCategory.VideoDecoder); ReturnHR = GraphBuilder.AddFilter(wmv_video_decoder_dmo, "Wmv Video Decoder DMO"); IBaseFilter wmv_audio_decoder_dmo = (IBaseFilter) new DMOWrapperFilter(); IDMOWrapperFilter dmo_wrapper_filter_a = (IDMOWrapperFilter)wmv_audio_decoder_dmo; ReturnHR = dmo_wrapper_filter_a.Init(new Guid("{2EEB4ADF-4578-4D10-BCA7-BB955F56320A}"), DMOCategory.AudioDecoder); ReturnHR = GraphBuilder.AddFilter(wmv_audio_decoder_dmo, "Wmv Audio Decoder DMO"); IBaseFilter vp8_encoder = (IBaseFilter) new VP8Encoder(); ReturnHR = GraphBuilder.AddFilter(vp8_encoder, "VP8 Encoder"); IVP8Encoder vp8_encoder_interface = (IVP8Encoder)vp8_encoder; if (target_bitrate != 0) { vp8_encoder_interface.SetTargetBitrate(target_bitrate); } IBaseFilter webm_muxer = (IBaseFilter) new WebMMuxer(); ReturnHR = GraphBuilder.AddFilter(webm_muxer, "WebM Muxer"); IBaseFilter vorbis_encoder = (IBaseFilter) new VorbisEncoder(); ReturnHR = GraphBuilder.AddFilter(vorbis_encoder, "Vorbis Encoder"); IBaseFilter file_writer = (IBaseFilter) new FileWriter(); ReturnHR = GraphBuilder.AddFilter(file_writer, "file writer"); IFileSinkFilter filewriter_sink = file_writer as IFileSinkFilter; ReturnHR = filewriter_sink.SetFileName(OutputFile, null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("Raw Video 1", source_filter), FindPin("in0", wmv_video_decoder_dmo), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("Raw Audio 0", source_filter), FindPin("in0", wmv_audio_decoder_dmo), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("out0", wmv_audio_decoder_dmo), FindPin("PCM In", vorbis_encoder), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("out0", wmv_video_decoder_dmo), FindPin("YUV", vp8_encoder), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("Vorbis Out", vorbis_encoder), FindPin("audio", webm_muxer), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("VP80", vp8_encoder), FindPin("video", webm_muxer), null); ReturnHR = GraphBuilder.ConnectDirect(FindPin("outpin", webm_muxer), FindPin("in", file_writer), null); } catch (Exception) { LogError("Failed to initialize FilterGraph."); } finally { if (CaptureGraphBuilder2 != null) { Marshal.ReleaseComObject(CaptureGraphBuilder2); CaptureGraphBuilder2 = null; } } }
protected override void SetupGraph() { this.HasBeenSetup = true; // Get the graphbuilder object this.FilterGraph = new FilterGraph() as IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 if (icgb2 != null) { int hr = icgb2.SetFiltergraph(this.FilterGraph); DsError.ThrowExceptionForHR(hr); // Add the filters necessary to render the file. This function will // work with a number of different file types. IBaseFilter sourceFilter; hr = this.FilterGraph.AddSourceFilter(this.fileName, this.fileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // VIDEO // Get the SampleGrabber interface this.SampGrabber = (ISampleGrabber)new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter)this.SampGrabber; // Configure the Sample Grabber this.ConfigureSampleGrabber(this.SampGrabber); // Add it to the filter hr = this.FilterGraph.AddFilter(baseGrabFlt, "Ds.NET GrabberV"); DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null); DsError.ThrowExceptionForHR(hr); // Connect audio hr = icgb2.RenderStream(null, MediaType.Audio, sourceFilter, null, null); if (hr != -2147467259 || hr == 0) { // don't throw for videos without audio track DsError.ThrowExceptionForHR(hr); } } IVideoWindow window = (IVideoWindow)this.FilterGraph; if (window != null) { window.put_AutoShow(OABool.False); } // Now that the graph is built, read the dimensions of the bitmaps we'll be getting this.SaveSizeInfo(this.SampGrabber); // Grab some other interfaces this.MediaCtrl = this.FilterGraph as IMediaControl; this.SampGrabber.SetBufferSamples(true); this.SampGrabber.SetOneShot(false); this.MediaEvent = this.FilterGraph as IMediaEvent; this.audio = this.FilterGraph as IBasicAudio; this.MediaPosition = this.FilterGraph as IMediaPosition; this.Volume = this.volume; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); } } }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph(Control hWin, string FileName) { int hr; m_FilterGraph = new FilterGraph() as IFilterGraph2; ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try { hr = icgb2.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); IBaseFilter sourceFilter = null; hr = m_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR( hr ); // Get the SampleGrabber interface m_sampGrabber = (ISampleGrabber) new SampleGrabber(); IBaseFilter baseGrabFlt = (IBaseFilter) m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_FilterGraph.AddFilter( baseGrabFlt, "Ds.NET Grabber" ); DsError.ThrowExceptionForHR( hr ); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null); DsError.ThrowExceptionForHR( hr ); // Configure the Video Window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; } finally { if (icgb2 != null) { Marshal.ReleaseComObject(icgb2); icgb2 = null; } } }