private void BuildGraph(string fileName) { int hr = 0; try { graphBuilder = (IFilterGraph2)new FilterGraph(); mediaControl = (IMediaControl)graphBuilder; mediaSeeking = (IMediaSeeking)graphBuilder; mediaPosition = (IMediaPosition)graphBuilder; vmr9 = (IBaseFilter)new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); } catch (Exception e) { CloseInterfaces(); MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void BuildGraph(string fileName) { int hr = 0; try { graphBuilder = (IFilterGraph2) new FilterGraph(); mediaControl = (IMediaControl)graphBuilder; mediaSeeking = (IMediaSeeking)graphBuilder; mediaPosition = (IMediaPosition)graphBuilder; vmr9 = (IBaseFilter) new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); } catch (Exception e) { CloseInterfaces(); MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void BuildGraph() { int hr = 0; graphBuilder = (IFilterGraph2) new FilterGraph(); rot = new DsROTEntry(graphBuilder); vmr9 = (IBaseFilter) new VideoMixingRenderer9(); IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9)vmr9; hr = filterConfig.SetNumberOfStreams(2); DsError.ThrowExceptionForHR(hr); // Put the VMR9 in Renderless mode hr = filterConfig.SetRenderingMode(VMR9Mode.Renderless); DsError.ThrowExceptionForHR(hr); surfaceAllocatorNotify = (IVMRSurfaceAllocatorNotify9)vmr9; // Advise to VMR9 of our custom Allocator / Presenter hr = surfaceAllocatorNotify.AdviseSurfaceAllocator(cookie, this); DsError.ThrowExceptionForHR(hr); // Advise our custom Allocator / Presenter of the VMR9 hr = this.AdviseNotify(surfaceAllocatorNotify); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.AddFilter(vmr9, "VMR9"); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(@"..\..\..\Resources\foo.avi", null); DsError.ThrowExceptionForHR(hr); }
public void ConnectAStreamToVMR() { int hr = 0; hr = graphBuilder.RenderFile(@"..\..\..\Resources\foo.avi", null); DsError.ThrowExceptionForHR(hr); }
private void BuildGraph(string fileName) { int hr = 0; try { graphBuilder = (IFilterGraph2) new FilterGraph(); mediaControl = (IMediaControl)graphBuilder; vmr9 = (IBaseFilter) new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); mixerBitmap = (IVMRMixerBitmap9)vmr9; menuMixer.Enabled = true; mixerEnabled = true; usingGDI = false; UpdateMixerMenu(); SetMixerSettings(); } catch (Exception e) { CloseInterfaces(); MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void BuildGraph() { int hr = 0; graphBuilder = (IFilterGraph2) new FilterGraph(); vmr = (IBaseFilter) new VideoMixingRenderer(); hr = graphBuilder.AddFilter(vmr, "VMR"); DsError.ThrowExceptionForHR(hr); hr = (vmr as IVMRFilterConfig).SetNumberOfStreams(1); DsError.ThrowExceptionForHR(hr); IPin pinIn = DsFindPin.ByDirection(vmr, PinDirection.Input, 0); streamControl = (IVMRVideoStreamControl)pinIn; hr = graphBuilder.RenderFile(@"..\..\..\Resources\foo.avi", null); DsError.ThrowExceptionForHR(hr); rot = new DsROTEntry(graphBuilder); // Run the graph to really connect VMR pins. // This sample doesn't work if the graph has not been run at least one time hr = (graphBuilder as IMediaControl).Run(); DsError.ThrowExceptionForHR(hr); }
public void InitVMR9(string filename) { int hr = 0; // Create a DirectShow FilterGraph graphBuilder = (IFilterGraph2) new FilterGraph(); // Add it in ROT for debug purpose rot = new DsROTEntry(graphBuilder); // Add a notification handler (see WndProc) hr = (graphBuilder as IMediaEventEx).SetNotifyWindow(this.Handle, WM_GRAPHNOTIFY, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); // Create a VMR9 object vmr9 = (IBaseFilter) new VideoMixingRenderer9(); IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9)vmr9; // We want the Renderless mode! hr = filterConfig.SetRenderingMode(VMR9Mode.Renderless); DsError.ThrowExceptionForHR(hr); // One stream is enough for this sample hr = filterConfig.SetNumberOfStreams(1); DsError.ThrowExceptionForHR(hr); // Create the Allocator / Presenter object allocator = new Allocator(device); IVMRSurfaceAllocatorNotify9 vmrSurfAllocNotify = (IVMRSurfaceAllocatorNotify9)vmr9; // Notify the VMR9 filter about our allocator hr = vmrSurfAllocNotify.AdviseSurfaceAllocator(IntPtr.Zero, allocator); DsError.ThrowExceptionForHR(hr); // Notify our allocator about the VMR9 filter hr = allocator.AdviseNotify(vmrSurfAllocNotify); DsError.ThrowExceptionForHR(hr); IVMRMixerControl9 mixerControl = (IVMRMixerControl9)vmr9; // Select the mixer mode : YUV or RGB hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetYUV | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering); //hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetRGB | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering); DsError.ThrowExceptionForHR(hr); // Add the filter to the graph hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); // Render the file hr = graphBuilder.RenderFile(filename, null); DsError.ThrowExceptionForHR(hr); // Run the graph hr = (graphBuilder as IMediaControl).Run(); DsError.ThrowExceptionForHR(hr); }
private void TestSetGet() { int hr; hr = m_ifg.RenderFile("foo.avi", null); DsError.ThrowExceptionForHR(hr); Debug.Assert(foo.bCreated, "Hit Created"); Debug.Assert(foo.bSelected, "Hit Selected"); }
private void Configure() { int hr; m_FilterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry rot = new DsROTEntry(m_FilterGraph); hr = m_FilterGraph.RenderFile("foo.avi", null); DsError.ThrowExceptionForHR(hr); m_mes = (IMediaEventSink)m_FilterGraph; }
public void BuildGraph() { int hr = 0; graphBuilder = (IFilterGraph2) new FilterGraph(); rot = new DsROTEntry(graphBuilder); vmr = (IBaseFilter) new VideoMixingRenderer(); IVMRFilterConfig filterConfig = (IVMRFilterConfig)vmr; hr = filterConfig.SetNumberOfStreams(2); DsError.ThrowExceptionForHR(hr); // Set the custom compositor hr = filterConfig.SetImageCompositor(this); DsError.ThrowExceptionForHR(hr); IVMRMixerControl mixerControl = (IVMRMixerControl)vmr; // In COLORREF, colors are coded in ABGR format hr = mixerControl.SetBackgroundClr(backgroundColorABGR); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.AddFilter(vmr, "VMR"); DsError.ThrowExceptionForHR(hr); // The 2 VMR pins must be connected... hr = graphBuilder.RenderFile(@"..\..\..\Resources\foo.avi", null); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(@"..\..\..\Resources\foo.avi", null); DsError.ThrowExceptionForHR(hr); hr = (graphBuilder as IMediaControl).Run(); DsError.ThrowExceptionForHR(hr); }
private void Config() { int hr; IBaseFilter pFilter; IFilterGraph2 m_FilterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry rot = new DsROTEntry(m_FilterGraph); hr = m_FilterGraph.RenderFile(@"p.wmv", null); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.FindFilterByName("p.wmv", out pFilter); m_es = pFilter as IAMExtendedSeeking; }
private void BuildGraph() { int hr = 0; graphBuilder = (IFilterGraph2) new FilterGraph(); vmr9 = (IBaseFilter) new VideoMixingRenderer9(); hr = graphBuilder.AddFilter(vmr9, "VMR9"); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(@"..\..\..\Resources\foo.avi", null); DsError.ThrowExceptionForHR(hr); mixerBitmap = (IVMRMixerBitmap9)vmr9; }
private static void playAudioFile(object fileName) { try { IFilterGraph2 filterGraph = (IFilterGraph2) new FilterGraph(); if (0 == filterGraph.RenderFile(fileName as string, IntPtr.Zero)) { IMediaControl mediaControl = (IMediaControl)filterGraph; mediaControl.Run(); IMediaEvent mediaEvent = (IMediaEvent)filterGraph; int eventCode; mediaEvent.WaitForCompletion(6000, out eventCode); } } catch (Exception) { } }
private void BuildGraph(string fileName) { int hr = 0; try { graphBuilder = (IFilterGraph2) new FilterGraph(); mediaControl = (IMediaControl)graphBuilder; ISampleGrabber sampGrabber = null; IBaseFilter baseGrabFlt = null; // Get the SampleGrabber interface sampGrabber = (ISampleGrabber) new SampleGrabber(); vmr9 = (IBaseFilter) new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); mediaPosition = (IMediaPosition)graphBuilder; m_MediaEvent = graphBuilder as IMediaEvent; hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); } catch (Exception e) { CloseInterfaces(); MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Configure() { int hr; IBaseFilter pFilter; m_FilterGraph = (IFilterGraph2) new FilterGraph(); hr = m_FilterGraph.RenderFile(@"France-Info.asx", null); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.FindFilterByName("XML Playlist", out pFilter); DsError.ThrowExceptionForHR(hr); hr = ((IMediaControl)m_FilterGraph).Run(); DsError.ThrowExceptionForHR(hr); m_imc2 = (IAMMediaContent2)pFilter; }
private void BuildGraph() { int hr = 0; graphBuilder = (IFilterGraph2) new FilterGraph(); vmr = (IBaseFilter) new VideoMixingRenderer(); hr = graphBuilder.AddFilter(vmr, "VMR"); DsError.ThrowExceptionForHR(hr); // To enable the VMR7's mixer and compositor hr = (vmr as IVMRFilterConfig).SetNumberOfStreams(1); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(@"..\..\..\Resources\foo.avi", null); DsError.ThrowExceptionForHR(hr); mixerBitmap = (IVMRMixerBitmap)vmr; }
private void Configure() { int hr; m_FilterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry rot = new DsROTEntry(m_FilterGraph); IBaseFilter ibf = (IBaseFilter) new OverlayMixer(); hr = m_FilterGraph.AddFilter((IBaseFilter)ibf, "asdf"); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.RenderFile("foo.avi", null); DsError.ThrowExceptionForHR(hr); IPin iPin = DsFindPin.ByDirection(ibf, PinDirection.Input, 0); m_imcPrimary = (IMixerPinConfig)iPin; Marshal.ReleaseComObject(ibf); }
public void BuildGraph() { int hr = 0; graphBuilder = (IFilterGraph2) new FilterGraph(); rot = new DsROTEntry(graphBuilder); vmr = (IBaseFilter) new VideoMixingRenderer(); IVMRFilterConfig filterConfig = (IVMRFilterConfig)vmr; hr = filterConfig.SetNumberOfStreams(2); DsError.ThrowExceptionForHR(hr); // Put the VMR7 in Renderless mode hr = filterConfig.SetRenderingMode(VMRMode.Renderless); DsError.ThrowExceptionForHR(hr); surfaceAllocatorNotify = (IVMRSurfaceAllocatorNotify)vmr; hr = surfaceAllocatorNotify.AdviseSurfaceAllocator(cookie, this); DsError.ThrowExceptionForHR(hr); defaultAllocatorPresenter = (IVMRSurfaceAllocator)Activator.CreateInstance(Type.GetTypeFromCLSID(VMRClsId.AllocPresenter)); form = new Form(); form.Show(); hr = (defaultAllocatorPresenter as IVMRWindowlessControl).SetVideoClippingWindow(form.Handle); DsError.ThrowExceptionForHR(hr); hr = this.AdviseNotify(surfaceAllocatorNotify); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.AddFilter(vmr, "VMR"); hr = graphBuilder.RenderFile(@"..\..\..\Resources\foo.avi", null); DsError.ThrowExceptionForHR(hr); }
public bool BuildGraph(IntPtr windowHandle, string fileName, bool isVideo) { currentSpeed = NormalSpeed; int hr = 0; try { graphBuilder = (IFilterGraph2)new FilterGraph(); mediaControl = (IMediaControl)graphBuilder; mediaEvent = (IMediaEventEx)graphBuilder; winHandle = windowHandle; mediaEvent.SetNotifyWindow(winHandle, VideoForm.WM_DSEvent, winHandle); if (isVideo) { if (IsEvrPlay) { ConfigureEVR(); } else { ConfigureVMR(); } } hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); } catch (Exception e) { MessageBox.Show(fileName + "\n" + e.Message); return false; } return true; }
private void BuildGraph(string fileName) { int hr = 0; try { graphBuilder = (IFilterGraph2)new FilterGraph(); mediaControl = (IMediaControl)graphBuilder; ISampleGrabber sampGrabber = null; IBaseFilter baseGrabFlt = null; // Get the SampleGrabber interface sampGrabber = (ISampleGrabber)new SampleGrabber(); vmr9 = (IBaseFilter)new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); mediaPosition = (IMediaPosition)graphBuilder; m_MediaEvent = graphBuilder as IMediaEvent; hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); } catch (Exception e) { CloseInterfaces(); MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void BuildGraph(string fileName) { int hr = 0; try { graphBuilder = (IFilterGraph2) new FilterGraph(); mediaControl = (IMediaControl) graphBuilder; vmr9 = (IBaseFilter) new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(fileName, null); DsError.ThrowExceptionForHR(hr); mixerBitmap = (IVMRMixerBitmap9) vmr9; menuMixer.Enabled = true; mixerEnabled = true; usingGDI = false; UpdateMixerMenu(); SetMixerSettings(); } catch(Exception e) { CloseInterfaces(); MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Open a new video feed (either web-cam or video file). /// </summary> /// <param name="filter">Specifies the web-cam filter to use, or <i>null</i> when opening a video file.</param> /// <param name="pb">Specifies the output window, or <i>null</i> when running headless and only receiving snapshots.</param> /// <param name="strFile">Specifies the video file to use, or <i>null</i> when opening a web-cam feed.</param> /// <param name="vidCap">Optionally specifies the video capabilities to use, or <i>null</i> to ignore and use the default video capabilities.</param> /// <returns>The duration (if any) is returned, or 0.</returns> /// <remarks>To get the video capabilities see the GetVideoCapatiblities method.</remarks> public long Open(Filter filter, PictureBox pb, string strFile, VideoCapability vidCap = null) { int hr; if (filter != null && strFile != null) { throw new ArgumentException("Both the filter and file are non NULL - only one of these can be used at a time; The filter is used with the web-cam and the file is used with a video file."); } m_selectedFilter = filter; m_graphBuilder = (IFilterGraph2)Activator.CreateInstance(Type.GetTypeFromCLSID(Clsid.FilterGraph, true)); // When using a web-cam, create the moniker for the filter and add the filter to the graph. if (strFile == null) { IMoniker moniker = m_selectedFilter.CreateMoniker(); m_graphBuilder.AddSourceFilterForMoniker(moniker, null, m_selectedFilter.Name, out m_camFilter); Marshal.ReleaseComObject(moniker); m_camControl = m_camFilter as IAMCameraControl; // Create the capture builder used to build the web-cam filter graph. m_captureGraphBuilder = (ICaptureGraphBuilder2)Activator.CreateInstance(Type.GetTypeFromCLSID(Clsid.CaptureGraphBuilder2, true)); hr = m_captureGraphBuilder.SetFiltergraph(m_graphBuilder as IGraphBuilder); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Add the web-cam filter to the graph. hr = m_graphBuilder.AddFilter(m_camFilter, m_selectedFilter.Name); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Set the desired video capabilities. if (vidCap != null) { setVideoCapabilities(m_captureGraphBuilder, m_camFilter, vidCap); } } else { // Build the graph with the video file. hr = m_graphBuilder.RenderFile(strFile, null); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } m_mediaSeek = m_graphBuilder as IMediaSeeking; if (pb != null) { m_videoFrameStep = m_graphBuilder as IVideoFrameStep; } } // Create the sample grabber used to get snapshots. m_sampleGrabber = (ISampleGrabber)Activator.CreateInstance(Type.GetTypeFromCLSID(Clsid.SampleGrabber, true)); m_baseGrabFilter = m_sampleGrabber as IBaseFilter; m_mediaControl = m_graphBuilder as IMediaControl; // When using a target window, get the video window used with the target output window if (pb != null) { m_mediaEventEx = m_graphBuilder as IMediaEventEx; m_videoWindow = m_graphBuilder as IVideoWindow; } // Otherwise create the null renderer for no video output is needed (only snapshots). else { m_nullRenderer = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(Clsid.NullRenderer, true)); } // Add the sample grabber to the filter graph. hr = m_graphBuilder.AddFilter(m_baseGrabFilter, "Ds.Lib Grabber"); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Turn off the sample grabber buffers. hr = m_sampleGrabber.SetBufferSamples(false); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Turn off the sample grabber one-shot. hr = m_sampleGrabber.SetOneShot(false); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Turn ON the sample grabber callback where video data is to be received. hr = m_sampleGrabber.SetCallback(this, 1); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Set the media format used by the sample grabber. AMMediaType media = new AMMediaType(); media.majorType = MediaType.Video; media.subType = MediaSubType.RGB24; media.formatType = FormatType.VideoInfo; hr = m_sampleGrabber.SetMediaType(media); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Connect the WebCam Filters and Frame Grabber. if (m_selectedFilter != null) { Guid cat; Guid med; cat = PinCategory.Preview; med = MediaType.Video; hr = m_captureGraphBuilder.RenderStream(ref cat, ref med, m_camFilter, null, null); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } cat = PinCategory.Capture; med = MediaType.Video; hr = m_captureGraphBuilder.RenderStream(ref cat, ref med, m_camFilter, null, m_baseGrabFilter); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } } // Connect the Frame Grabber and (optionally the Null Renderer) else { // Get the video decoder and its pins. m_videoFilter = Utility.GetFilter(m_graphBuilder as IGraphBuilder, "Video Decoder", false); IPin pOutput; hr = Utility.GetPin(m_videoFilter, PinDirection.Output, out pOutput); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } IPin pInput; hr = pOutput.ConnectedTo(out pInput); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } PinInfo pinInfo; hr = pInput.QueryPinInfo(out pinInfo); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Get the sample grabber pins. IPin pGrabInput; hr = Utility.GetPin(m_baseGrabFilter, PinDirection.Input, out pGrabInput); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } IPin pGrabOutput; hr = Utility.GetPin(m_baseGrabFilter, PinDirection.Output, out pGrabOutput); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Disconnect the source filter output and the input it is connected to. hr = pOutput.Disconnect(); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } hr = pInput.Disconnect(); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Connect the source output to the Grabber input. hr = m_graphBuilder.Connect(pOutput, pGrabInput); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // When rendering video output, connect the Grabber output to the original downstream input that the source was connected to. if (m_nullRenderer == null) { hr = m_graphBuilder.Connect(pGrabOutput, pInput); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } } Marshal.ReleaseComObject(pOutput); Marshal.ReleaseComObject(pInput); Marshal.ReleaseComObject(pGrabInput); Marshal.ReleaseComObject(pGrabOutput); } // Remove sound filters. IBaseFilter soundFilter = Utility.GetFilter(m_graphBuilder as IGraphBuilder, "Audio Decoder", false); if (soundFilter != null) { hr = m_graphBuilder.RemoveFilter(soundFilter); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } Marshal.ReleaseComObject(soundFilter); } soundFilter = Utility.GetFilter(m_graphBuilder as IGraphBuilder, "Sound", false); if (soundFilter != null) { hr = m_graphBuilder.RemoveFilter(soundFilter); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } Marshal.ReleaseComObject(soundFilter); } // When using a headless (no video rendering) setup, connect the null renderer to the Sample Grabber. if (m_nullRenderer != null) { // Add the null renderer. hr = m_graphBuilder.AddFilter(m_nullRenderer, "Null Renderer"); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Get the sample grabber output pin. IPin pGrabOutput; hr = Utility.GetPin(m_baseGrabFilter, PinDirection.Output, out pGrabOutput); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Get the null renderer input pin. IPin pInput; hr = Utility.GetPin(m_nullRenderer, PinDirection.Input, out pInput); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Disconnect the sample grabber pin. hr = pGrabOutput.Disconnect(); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Connect the Grabber output to the null renderer. hr = m_graphBuilder.Connect(pGrabOutput, pInput); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } Marshal.ReleaseComObject(pInput); Marshal.ReleaseComObject(pGrabOutput); // Remove the Video Renderer for it is no longer needed. IBaseFilter ivideorender = Utility.GetFilter(m_graphBuilder as IGraphBuilder, "Video Renderer"); if (ivideorender != null) { m_graphBuilder.RemoveFilter(ivideorender); Marshal.ReleaseComObject(ivideorender); } } // Get the sample grabber media settings and video header. media = new AMMediaType(); hr = m_sampleGrabber.GetConnectedMediaType(media); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } if ((media.formatType != FormatType.VideoInfo && media.formatType != FormatType.WaveEx && media.formatType != FormatType.MpegVideo) || media.formatPtr == IntPtr.Zero) { throw new Exception("Media grabber format is unknown."); } // Get the video header with frame sizing information. m_videoInfoHeader = Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader)) as VideoInfoHeader; Marshal.FreeCoTaskMem(media.formatPtr); media.formatPtr = IntPtr.Zero; // If we are rendering video output, setup the video window (which requires a message pump). if (m_videoWindow != null) { // setup the video window hr = m_videoWindow.put_Owner(pb.Handle); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } hr = m_videoWindow.put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // resize the window hr = m_videoWindow.SetWindowPosition(0, 0, pb.Width, pb.Height); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } hr = m_videoWindow.put_Visible(DsHlp.OATRUE); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Subscribe to the picturebox size changed event. pb.SizeChanged += Pb_SizeChanged; } // start the capturing hr = m_mediaControl.Run(); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // When using a video file, immediately stop at the start. if (strFile != null) { hr = m_mediaControl.Pause(); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } } // When using a media file, we need to save the video file's duration. if (m_mediaSeek != null) { hr = m_mediaSeek.GetDuration(out m_lDuration); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } } m_bConnected = true; return(m_lDuration); }
/// <summary>Load a video/audio file and prepare to play it</summary> public void LoadFile(string file) { try { if (m_filter_graph != null) { throw new Exception("Reusing this Video object is not allowed"); } m_file = file; m_filter_graph = new FilterGraph() as IFilterGraph2; if (m_filter_graph == null) { throw new Exception("failed to create direct show filter graph"); } // Have the filter graph construct the appropriate graph automatically DsError.ThrowExceptionForHR(m_filter_graph.RenderFile(file, null)); #if DEBUG // Allows you to view the graph with GraphEdit File/Connect m_ds_rot = new DsROTEntry(m_filter_graph); #endif // Grab some other interfaces m_media_event = m_filter_graph as IMediaEvent; m_media_ctrl = m_filter_graph as IMediaControl; m_media_position = m_filter_graph as IMediaPosition; if (m_media_event == null) { throw new Exception("failed to obtain a direct show IMediaEvent interface"); } if (m_media_ctrl == null) { throw new Exception("failed to obtain a direct show IMediaControl interface"); } if (m_media_position == null) { throw new Exception("failed to obtain a direct show IMediaPosition interface"); } // Grab optional interfaces m_video_window = m_filter_graph as IVideoWindow; m_basic_video = m_filter_graph as IBasicVideo; m_basic_audio = m_filter_graph as IBasicAudio; // If this is an audio-only clip, get_Visible() won't work. Also, if this // video is encoded with an unsupported codec, we won't see any video, // although the audio will work if it is of a supported format. if (m_video_window != null) { const int E_NOINTERFACE = unchecked ((int)0x80004002); // Use put Visible since we want to hide the video window // till we're ready to show it anyway int hr = m_video_window.put_Visible(OABool.False); if (hr == E_NOINTERFACE) { m_video_window = null; } else { DsError.ThrowExceptionForHR(hr); } } // Get the event handle the graph will use to signal when events occur, wrap it in a ManualResetEvent IntPtr media_event; DsError.ThrowExceptionForHR(m_media_event.GetEventHandle(out media_event)); m_event = new ManualResetEvent(false) { SafeWaitHandle = new SafeWaitHandle(media_event, false) }; // Create a new thread to wait for events m_media_event_thread = new Thread(MediaEventWait) { Name = "Media Event Thread" }; m_media_event_thread.Start(); if (FileLoaded != null) { FileLoaded(this, file); } } catch { Dispose(); throw; } #if DEBUG GC.Collect(); // Double check to make sure we aren't releasing something important. GC.WaitForPendingFinalizers(); #endif }
public void InitVMR9(string filename) { int hr = 0; // Create a DirectShow FilterGraph graphBuilder = (IFilterGraph2) new FilterGraph(); // Add it in ROT for debug purpose rot = new DsROTEntry(graphBuilder); // Add a notification handler (see WndProc) hr = (graphBuilder as IMediaEventEx).SetNotifyWindow(this.Handle, WM_GRAPHNOTIFY, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); // Create a VMR9 object vmr9 = (IBaseFilter) new VideoMixingRenderer9(); IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9) vmr9; // We want the Renderless mode! hr = filterConfig.SetRenderingMode(VMR9Mode.Renderless); DsError.ThrowExceptionForHR(hr); // Create the Allocator / Presenter object allocator = new Allocator(device); IVMRSurfaceAllocatorNotify9 vmrSurfAllocNotify = (IVMRSurfaceAllocatorNotify9) vmr9; // Notify the VMR9 filter about our allocator hr = vmrSurfAllocNotify.AdviseSurfaceAllocator(IntPtr.Zero, allocator); DsError.ThrowExceptionForHR(hr); // Notify our allocator about the VMR9 filter hr = allocator.AdviseNotify(vmrSurfAllocNotify); DsError.ThrowExceptionForHR(hr); if (config.IsUsingMixing) { // One stream is enough for this sample // This line also load the mixing componant hr = filterConfig.SetNumberOfStreams(1); DsError.ThrowExceptionForHR(hr); IVMRMixerControl9 mixerControl = (IVMRMixerControl9) vmr9; // Select the mixer mode : YUV or RGB if (config.UseYUVMixing) { hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetYUV | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering); } else { hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetRGB | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering); } DsError.ThrowExceptionForHR(hr); Debug.WriteLine("Using VMR9 mixing mode"); } // Add the filter to the graph hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); // Render the file hr = graphBuilder.RenderFile(filename, null); DsError.ThrowExceptionForHR(hr); // Run the graph hr = (graphBuilder as IMediaControl).Run(); DsError.ThrowExceptionForHR(hr); }
public bool Initialize(string n, string p, string t) { int hr = 0; path = p; name = n; textureName = t; if (textureName == null) { textureName = Manager.TextureName(this); } log.InfoFormat("DirectShowCodec[{0}] Initialize: {1} ({2}), texture '{3}'", ID(), name, path, textureName); // check for DirectX-ness if (!(Root.Instance.RenderSystem is Axiom.RenderSystems.DirectX9.D3D9RenderSystem)) { throw new Exception("DirectShow movie codec is DirectX only"); } if ((Root.Instance.RenderSystem as D3D9RenderSystem).PrimaryWindow == null) { throw new Exception("Initializing before primary window has been created"); } D3DRenderWindow rw = (Root.Instance.RenderSystem as D3D9RenderSystem).PrimaryWindow; // Create a DirectShow FilterGraph graphBuilder = (IFilterGraph2)new FilterGraph(); // Add it in ROT for debug purpose rot = new DsROTEntry(graphBuilder); // Create a VMR9 object vmr9 = (IBaseFilter)new VideoMixingRenderer9(); IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9)vmr9; // We want the Renderless mode! hr = filterConfig.SetRenderingMode(VMR9Mode.Renderless); DsError.ThrowExceptionForHR(hr); // One stream is enough for this sample hr = filterConfig.SetNumberOfStreams(1); DsError.ThrowExceptionForHR(hr); // Create the Allocator / Presenter object Device device = (Device)rw.GetCustomAttribute("D3DDEVICE"); allocator = new DirectShowAllocator(device, ID(), TextureName()); IVMRSurfaceAllocatorNotify9 vmrSurfAllocNotify = (IVMRSurfaceAllocatorNotify9)vmr9; // Notify the VMR9 filter about our allocator hr = vmrSurfAllocNotify.AdviseSurfaceAllocator(IntPtr.Zero, allocator); DsError.ThrowExceptionForHR(hr); // Notify our allocator about the VMR9 filter hr = allocator.AdviseNotify(vmrSurfAllocNotify); DsError.ThrowExceptionForHR(hr); IVMRMixerControl9 mixerControl = (IVMRMixerControl9)vmr9; // Select the mixer mode : YUV or RGB hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetYUV | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering); DsError.ThrowExceptionForHR(hr); // Add the VMR-9 filter to the graph hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9"); DsError.ThrowExceptionForHR(hr); bool shouldStream = false; string url = path; #if false int maxRedirects = DirectShowWebReader.MAX_REDIRECTS; DirectShowWebReader reader = null; while (maxRedirects != 0) { if (url.StartsWith("http://") || url.StartsWith("https://")) { try { // Start reading the data reader = new DirectShowWebReader(); hr = reader.Load(url); DsError.ThrowExceptionForHR(hr); shouldStream = true; break; } catch (DirectShowWebReader.RedirectException e) { log.Info("Redirect to (" + e.url + ")"); url = e.url; maxRedirects--; reader = null; } } else { shouldStream = false; reader = null; break; } } if (maxRedirects == 0) { log.Warn("Stopped redirection after " + DirectShowWebReader.MAX_REDIRECTS + " attempts."); } #else shouldStream = DirectShowCodec.ShouldStream(url); #endif if(shouldStream) { #if false // Add our source filter to the graph DirectShowStreamer strm = new DirectShowStreamer(reader); hr = graphBuilder.AddFilter(strm, DirectShowStreamer.FILTER_NAME); DsError.ThrowExceptionForHR(hr); streamer = strm; streamer.SetType(reader.MajorType, reader.MinorType); // Render the file hr = graphBuilder.Render(strm.GetOutputPin()); DsError.ThrowExceptionForHR(hr); #else WMAsfReader asf = new WMAsfReader(); IBaseFilter ibf = (asf as IBaseFilter); IFileSourceFilter ifs = (asf as IFileSourceFilter); hr = ifs.Load(url, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); // XXXMLM - how to set buffer time? // IWMStreamConfig2 on the output pins? hr = graphBuilder.AddFilter(ibf, "WM ASF Reader"); DsError.ThrowExceptionForHR(hr); IEnumPins ie; IPin[] pins = new IPin[1]; hr = ibf.EnumPins(out ie); int fetched = 0; while (hr == 0) { hr = ie.Next(1, pins, out fetched); if (fetched != 0) { hr = graphBuilder.Render(pins[0]); DsError.ThrowExceptionForHR(hr); } } #endif } else { // Render the file hr = graphBuilder.RenderFile(url, null); DsError.ThrowExceptionForHR(hr); } #if true // Run the graph hr = (graphBuilder as IMediaControl).Run(); DsError.ThrowExceptionForHR(hr); // Wait for ready hr = (graphBuilder as IMediaControl).Pause(); DsError.ThrowExceptionForHR(hr); ps = PLAY_STATE.BUFFERING; #endif return true; }