/// <summary> /// Builds the graph. /// </summary> public override void BuildGraph() { try { if (_graphState != GraphState.Idle) { Log.Log.Error("dvbt:Graph already built"); throw new TvException("Graph already build"); } Log.Log.WriteFile("dvbt:BuildGraph"); _graphBuilder = (IFilterGraph2)new FilterGraph(); _capBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); _capBuilder.SetFiltergraph(_graphBuilder); _rotEntry = new DsROTEntry(_graphBuilder); AddNetworkProviderFilter(typeof (DVBTNetworkProvider).GUID); AddTsWriterFilterToGraph(); if (!useInternalNetworkProvider) { CreateTuningSpace(); AddMpeg2DemuxerToGraph(); } AddAndConnectBDABoardFilters(_device); string graphName = _device.Name + " - DVBT Graph.grf"; FilterGraphTools.SaveGraphFile(_graphBuilder, graphName); GetTunerSignalStatistics(); _graphState = GraphState.Created; } catch (Exception ex) { Log.Log.Write(ex); Dispose(); _graphState = GraphState.Idle; throw new TvExceptionGraphBuildingFailed("Graph building failed", ex); } }
private void BuildGraph(string filename) { int hr = 0; try { graphBuilder = (IFilterGraph2)new FilterGraph(); mediaControl = (IMediaControl)graphBuilder; rot = new DsROTEntry(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); RunGraph(); } 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> /// This constructor internally build a DirectShow graph using the given file name parameter. /// </summary> /// <param name="filename">A media file.</param> /// <remarks>This constructor use the BlackListManager class to bane the use of the ffdshow Audio and Video decoders during the Intelligent Connect graph building.</remarks> public SimplePlayer(string filename) { if (string.IsNullOrEmpty(filename)) throw new ArgumentNullException("filename"); if (!File.Exists(filename)) throw new FileNotFoundException(); this.graphBuilder = (IFilterGraph2)new FilterGraph(); #if DEBUG this.rot = new DsROTEntry(this.graphBuilder); #endif this.blackListManager = new BlackListManager(this.graphBuilder); // blacklist the ffdshow Audio Decoder filter this.blackListManager.AddBlackListedFilter(new Guid("0F40E1E5-4F79-4988-B1A9-CC98794E6B55")); // blacklist the ffdshow Video Decoder filter this.blackListManager.AddBlackListedFilter(new Guid("04FE9017-F873-410E-871E-AB91661A4EF7")); int hr = this.graphBuilder.RenderFile(filename, null); DsError.ThrowExceptionForHR(hr); this.mediaControl = (IMediaControl)this.graphBuilder; this.mediaEvent = (IMediaEvent)this.graphBuilder; }
//public override void BuildGraphWithNoRenderer() //{ // this.graphBuilder = (IFilterGraph2)new FilterGraph(); // rot = new DsROTEntry(this.graphBuilder); // OnGraphStarted(); //} public override void BuildGraph() { this.graphBuilder = (IFilterGraph2)new FilterGraph(); rot = new DsROTEntry(this.graphBuilder); AddRenderers(); ConfigureVMR9InWindowlessMode(); this.hostingControl.CurrentGraphBuilder = this; OnGraphStarted(); }
public void BuildGraph(ITuningSpace tuningSpace) { this.graphBuilder = (IFilterGraph2) new FilterGraph(); rot = new DsROTEntry(this.graphBuilder); // Method names should be self explanatory AddNetworkProviderFilter(tuningSpace); AddMPEG2DemuxFilter(); AddAndConnectBDABoardFilters(); AddTransportStreamFiltersToGraph(); AddRenderers(); ConfigureVMR9InWindowlessMode(); ConnectFilters(); }
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { base.Dispose( disposing ); if (m_rot != null) { m_rot.Dispose(); m_rot = null; } if (graphBuilder != null) { (graphBuilder as IMediaControl).Stop(); Marshal.ReleaseComObject(graphBuilder); graphBuilder = null; } }
/// <summary> /// Build graph /// </summary> public override void BuildGraph() { try { if (_graphState != GraphState.Idle) { throw new TvException("Graph already build"); } Log.Log.WriteFile("BuildGraph"); _graphBuilder = (IFilterGraph2)new FilterGraph(); _capBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); _capBuilder.SetFiltergraph(_graphBuilder); _rotEntry = new DsROTEntry(_graphBuilder); _infTeeMain = (IBaseFilter)new InfTee(); int hr = _graphBuilder.AddFilter(_infTeeMain, "Inf Tee"); if (hr != 0) { Log.Log.Error("dvbip:Add main InfTee returns:0x{0:X}", hr); throw new TvException("Unable to add mainInfTee"); } AddTsWriterFilterToGraph(); AddStreamSourceFilter(_defaultUrl); IBaseFilter lastFilter = _filterStreamSource; AddMdPlugs(ref lastFilter); if (!ConnectTsWriter(lastFilter)) { throw new TvExceptionGraphBuildingFailed("Graph building failed"); } _conditionalAccess = new ConditionalAccess(_filterStreamSource, _filterTsWriter, null, this); _graphState = GraphState.Created; } catch (Exception ex) { Log.Log.Write(ex); Dispose(); _graphState = GraphState.Idle; throw ex; } }
/// <summary> /// Build the graph. /// </summary> public override void BuildGraph() { try { if (_graphState != GraphState.Idle) { Log.Log.Info("PBDA CC: device already initialised"); return; } Log.Log.Info("PBDA CC: build graph"); _graphBuilder = (IFilterGraph2)new FilterGraph(); _capBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); _capBuilder.SetFiltergraph(_graphBuilder); _rotEntry = new DsROTEntry(_graphBuilder); AddNetworkProviderFilter(typeof (ATSCNetworkProvider).GUID); AddTsWriterFilterToGraph(); if (!useInternalNetworkProvider) { AddMpeg2DemuxerToGraph(); } IBaseFilter lastFilter; AddAndConnectBDABoardFilters(_device, out lastFilter); _bdaCa = _filterTuner as IBDA_ConditionalAccess; if (_bdaCa == null) { throw new TvExceptionGraphBuildingFailed("PBDA CC: tuner filter does not implement required interface"); } AddPbdaFilter(ref lastFilter); CompleteGraph(ref lastFilter); bool connected = ConnectTsWriter(_filterTuner); Log.Log.Debug("PBDA CC: connect OOB pin result = {0}", connected); CheckCableCardInfo(); string graphName = _device.Name + " - NA Cable Graph.grf"; FilterGraphTools.SaveGraphFile(_graphBuilder, graphName); GetTunerSignalStatistics(); _graphState = GraphState.Created; } catch (Exception) { Dispose(); throw; } }
/// <summary> create the used COM components and get the interfaces. </summary> protected virtual bool GetDVDInterfaces(string path) { int hr; //Type comtype = null; object comobj = null; _freeNavigator = true; _dvdInfo = null; _dvdCtrl = null; bool useAC3Filter = false; string dvdNavigator = ""; string aspectRatioMode = ""; string displayMode = ""; _videoPref = DvdPreferredDisplayMode.DisplayContentDefault; using (MediaPortal.Profile.Settings xmlreader = new MPSettings()) { dvdNavigator = xmlreader.GetValueAsString("dvdplayer", "navigator", "DVD Navigator"); aspectRatioMode = xmlreader.GetValueAsString("dvdplayer", "armode", "").ToLower(); dvdNavigator = "dslibdvdnav"; if (aspectRatioMode == "crop") { arMode = AspectRatioMode.Crop; } if (aspectRatioMode == "letterbox") { arMode = AspectRatioMode.LetterBox; } if (aspectRatioMode == "stretch") { arMode = AspectRatioMode.Stretched; } //if ( aspectRatioMode == "stretch" ) arMode = AspectRatioMode.zoom14to9; if (aspectRatioMode == "follow stream") { arMode = AspectRatioMode.StretchedAsPrimary; } useAC3Filter = xmlreader.GetValueAsBool("dvdplayer", "ac3", false); displayMode = xmlreader.GetValueAsString("dvdplayer", "displaymode", "").ToLower(); if (displayMode == "default") { _videoPref = DvdPreferredDisplayMode.DisplayContentDefault; } if (displayMode == "16:9") { _videoPref = DvdPreferredDisplayMode.Display16x9; } if (displayMode == "4:3 pan scan") { _videoPref = DvdPreferredDisplayMode.Display4x3PanScanPreferred; } if (displayMode == "4:3 letterbox") { _videoPref = DvdPreferredDisplayMode.Display4x3LetterBoxPreferred; } } try { _dvdGraph = (IDvdGraphBuilder)new DvdGraphBuilder(); hr = _dvdGraph.GetFiltergraph(out _graphBuilder); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } _rotEntry = new DsROTEntry((IFilterGraph)_graphBuilder); _vmr9Filter = (IBaseFilter)new VideoMixingRenderer9(); IVMRFilterConfig9 config = _vmr9Filter as IVMRFilterConfig9; hr = config.SetNumberOfStreams(1); hr = config.SetRenderingMode(VMR9Mode.Windowless); windowlessCtrl = (IVMRWindowlessControl9)_vmr9Filter; windowlessCtrl.SetVideoClippingWindow(this.panVideoWin.Handle); // config.SetRenderingPrefs(VMR9RenderPrefs. _graphBuilder.AddFilter(_vmr9Filter, "Video Mixing Renderer 9"); // _vmr7 = new VMR7Util(); // _vmr7.AddVMR7(_graphBuilder); try { _dvdbasefilter = DirectShowUtil.AddFilterToGraph(_graphBuilder, dvdNavigator); if (_dvdbasefilter != null) { IDvdControl2 cntl = (IDvdControl2)_dvdbasefilter; if (cntl != null) { _dvdInfo = (IDvdInfo2)cntl; _dvdCtrl = (IDvdControl2)cntl; if (path != null) { if (path.Length != 0) { cntl.SetDVDDirectory(path); } } _dvdCtrl.SetOption(DvdOptionFlag.HMSFTimeCodeEvents, true); // use new HMSF timecode format _dvdCtrl.SetOption(DvdOptionFlag.ResetOnStop, false); AddPreferedCodecs(_graphBuilder); DirectShowUtil.RenderOutputPins(_graphBuilder, _dvdbasefilter); // _videoWin = _graphBuilder as IVideoWindow; _freeNavigator = false; } //DirectShowUtil.ReleaseComObject( _dvdbasefilter); _dvdbasefilter = null; } } catch (Exception ex) { string strEx = ex.Message; } Guid riid; if (_dvdInfo == null) { riid = typeof(IDvdInfo2).GUID; hr = _dvdGraph.GetDvdInterface(riid, out comobj); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } _dvdInfo = (IDvdInfo2)comobj; comobj = null; } if (_dvdCtrl == null) { riid = typeof(IDvdControl2).GUID; hr = _dvdGraph.GetDvdInterface(riid, out comobj); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } _dvdCtrl = (IDvdControl2)comobj; comobj = null; } _mediaCtrl = (IMediaControl)_graphBuilder; _mediaEvt = (IMediaEventEx)_graphBuilder; _basicAudio = _graphBuilder as IBasicAudio; _mediaPos = (IMediaPosition)_graphBuilder; _mediaSeek = (IMediaSeeking)_graphBuilder; _mediaStep = (IVideoFrameStep)_graphBuilder; _basicVideo = _graphBuilder as IBasicVideo2; _videoWin = _graphBuilder as IVideoWindow; // disable Closed Captions! IBaseFilter baseFilter; _graphBuilder.FindFilterByName("Line 21 Decoder", out baseFilter); if (baseFilter == null) { _graphBuilder.FindFilterByName("Line21 Decoder", out baseFilter); } if (baseFilter != null) { _line21Decoder = (IAMLine21Decoder)baseFilter; if (_line21Decoder != null) { AMLine21CCState state = AMLine21CCState.Off; hr = _line21Decoder.SetServiceState(state); if (hr == 0) { logger.Info("DVDPlayer:Closed Captions disabled"); } else { logger.Info("DVDPlayer:failed 2 disable Closed Captions"); } } } /* // get video window if (_videoWin==null) { riid = typeof( IVideoWindow ).GUID; hr = _dvdGraph.GetDvdInterface( ref riid, out comobj ); if( hr < 0 ) Marshal.ThrowExceptionForHR( hr ); _videoWin = (IVideoWindow) comobj; comobj = null; } */ // GetFrameStepInterface(); DirectShowUtil.SetARMode(_graphBuilder, arMode); DirectShowUtil.EnableDeInterlace(_graphBuilder); //m_ovMgr = new OVTOOLLib.OvMgrClass(); //m_ovMgr.SetGraph(_graphBuilder); return true; } catch (Exception) { //MessageBox.Show( this, "Could not get interfaces\r\n" + ee.Message, "DVDPlayer.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop ); CloseDVDInterfaces(); return false; } finally { if (comobj != null) { DirectShowUtil.ReleaseComObject(comobj); } comobj = null; } }
public void CaptureVideo() { int hr = 0; IBaseFilter sourceFilter = null; try { // Get DirectShow interfaces GetInterfaces(); // 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 = FindCaptureDevice(); // Add Capture filter to our graph. hr = this.graphBuilder.AddFilter(sourceFilter, "Video Capture"); DsError.ThrowExceptionForHR(hr); // Render the preview pin on the video capture filter // Use this instead of this.graphBuilder.RenderFile hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, null, null); DsError.ThrowExceptionForHR(hr); // Now that the filter has been added to the graph and we have // rendered its stream, we can release this reference to the filter. Marshal.ReleaseComObject(sourceFilter); // Set video window style and position SetupVideoWindow(); // 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); // Remember current state this.currentState = PlayState.Running; } catch { MessageBox.Show("An unrecoverable error has occurred."); } }
public bool Transcode(TranscodeInfo info, MediaPortal.Core.Transcoding.VideoFormat format, MediaPortal.Core.Transcoding.Quality quality, Standard standard) { if (!Supports(format)) { return(false); } string ext = System.IO.Path.GetExtension(info.file); if (ext.ToLower() != ".dvr-ms" && ext.ToLower() != ".sbe") { return(false); } //Type comtype = null; //object comobj = null; try { Log.Info("DVR2MPG: create graph"); graphBuilder = (IGraphBuilder) new FilterGraph(); _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder); Log.Info("DVR2MPG: add streambuffersource"); bufferSource = (IStreamBufferSource) new StreamBufferSource(); IBaseFilter filter = (IBaseFilter)bufferSource; graphBuilder.AddFilter(filter, "SBE SOURCE"); Log.Info("DVR2MPG: load file:{0}", info.file); IFileSourceFilter fileSource = (IFileSourceFilter)bufferSource; int hr = fileSource.Load(info.file, null); Log.Info("DVR2MPG: Add Cyberlink MPEG2 multiplexer to graph"); string monikerPowerDvdMuxer = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{7F2BBEAF-E11C-4D39-90E8-938FB5A86045}"; powerDvdMuxer = Marshal.BindToMoniker(monikerPowerDvdMuxer) as IBaseFilter; if (powerDvdMuxer == null) { Log.Warn("DVR2MPG: FAILED:Unable to create Cyberlink MPEG Muxer (PowerDVD)"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(powerDvdMuxer, "PDR MPEG Muxer"); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:Add Cyberlink MPEG Muxer to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } //add filewriter Log.Info("DVR2MPG: Add FileWriter to graph"); string monikerFileWrite = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{3E8868CB-5FE8-402C-AA90-CB1AC6AE3240}"; IBaseFilter fileWriterbase = Marshal.BindToMoniker(monikerFileWrite) as IBaseFilter; if (fileWriterbase == null) { Log.Warn("DVR2MPG: FAILED:Unable to create FileWriter"); Cleanup(); return(false); } fileWriterFilter = fileWriterbase as IFileSinkFilter; if (fileWriterFilter == null) { Log.Warn("DVR2MPG: FAILED:Add unable to get IFileSinkFilter for filewriter"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(fileWriterbase, "FileWriter"); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:Add FileWriter to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } //connect output #0 of streambuffer source->powerdvd audio in //connect output #1 of streambuffer source->powerdvd video in Log.Info("DVR2MPG: connect streambuffer->multiplexer"); IPin pinOut0, pinOut1; IPin pinIn0, pinIn1; pinOut0 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 0); pinOut1 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 1); pinIn0 = DsFindPin.ByDirection(powerDvdMuxer, PinDirection.Input, 0); pinIn1 = DsFindPin.ByDirection(powerDvdMuxer, PinDirection.Input, 1); if (pinOut0 == null || pinOut1 == null || pinIn0 == null || pinIn1 == null) { Log.Warn("DVR2MPG: FAILED:unable to get pins of muxer&source"); Cleanup(); return(false); } bool usingAc3 = false; AMMediaType amAudio = new AMMediaType(); amAudio.majorType = MediaType.Audio; amAudio.subType = MediaSubType.Mpeg2Audio; hr = pinOut0.Connect(pinIn1, amAudio); if (hr != 0) { amAudio.subType = MediaSubType.DolbyAC3; hr = pinOut0.Connect(pinIn1, amAudio); usingAc3 = true; } if (hr != 0) { Log.Warn("DVR2MPG: FAILED: unable to connect audio pins: 0x{0:X}", hr); Cleanup(); return(false); } if (usingAc3) { Log.Info("DVR2MPG: using AC3 audio"); } else { Log.Info("DVR2MPG: using MPEG audio"); } AMMediaType amVideo = new AMMediaType(); amVideo.majorType = MediaType.Video; amVideo.subType = MediaSubType.Mpeg2Video; hr = pinOut1.Connect(pinIn0, amVideo); if (hr != 0) { Log.Warn("DVR2MPG: FAILED: unable to connect video pins: 0x{0:X}", hr); Cleanup(); return(false); } //connect output of powerdvd muxer->input of filewriter Log.Info("DVR2MPG: connect multiplexer->filewriter"); IPin pinOut, pinIn; pinOut = DsFindPin.ByDirection(powerDvdMuxer, PinDirection.Output, 0); if (pinOut == null) { Log.Warn("DVR2MPG: FAILED:cannot get output pin of Cyberlink MPEG muxer :0x{0:X}", hr); Cleanup(); return(false); } pinIn = DsFindPin.ByDirection(fileWriterbase, PinDirection.Input, 0); if (pinIn == null) { Log.Warn("DVR2MPG: FAILED:cannot get input pin of Filewriter :0x{0:X}", hr); Cleanup(); return(false); } AMMediaType mt = new AMMediaType(); hr = pinOut.Connect(pinIn, mt); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:connect muxer->filewriter :0x{0:X}", hr); Cleanup(); return(false); } //set output filename string outputFileName = System.IO.Path.ChangeExtension(info.file, ".mpg"); Log.Info("DVR2MPG: set output file to :{0}", outputFileName); mt.majorType = MediaType.Stream; mt.subType = MediaSubTypeEx.MPEG2; hr = fileWriterFilter.SetFileName(outputFileName, mt); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:unable to set filename for filewriter :0x{0:X}", hr); Cleanup(); return(false); } mediaControl = graphBuilder as IMediaControl; mediaSeeking = graphBuilder as IMediaSeeking; mediaEvt = graphBuilder as IMediaEventEx; Log.Info("DVR2MPG: start transcoding"); hr = mediaControl.Run(); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:unable to start graph :0x{0:X}", hr); Cleanup(); return(false); } } catch (Exception ex) { Log.Error("DVR2MPG: Unable create graph: {0}", ex.Message); Cleanup(); return(false); } return(true); }
// 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_graphBuilder = new DirectShowLib.FilterGraph() as DirectShowLib.IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph DirectShowLib.ICaptureGraphBuilder2 icgb2 = new DirectShowLib.CaptureGraphBuilder2() as DirectShowLib.ICaptureGraphBuilder2; try { // Link the ICaptureGraphBuilder2 to the IFilterGraph2 hr = icgb2.SetFiltergraph(m_graphBuilder); DsError.ThrowExceptionForHR(hr); #if DEBUG // Allows you to view the graph with GraphEdit File/Connect m_DsRot = new DsROTEntry(m_graphBuilder); #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_graphBuilder.AddSourceFilter(FileName, FileName, out sourceFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface m_sampGrabber = (DirectShowLib.ISampleGrabber) new DirectShowLib.SampleGrabber(); DirectShowLib.IBaseFilter baseGrabFlt = (DirectShowLib.IBaseFilter)m_sampGrabber; // Configure the Sample Grabber ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter hr = m_graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // System.Windows.Forms.MessageBox.Show(Width.ToString(), Height.ToString(), System.Windows.Forms.MessageBoxButtons.OK); // A Null Renderer does not display the video // But it allows the Sample Grabber to run // And it will keep proper playback timing // Unless specified otherwise. DirectShowLib.NullRenderer nullRenderer = new DirectShowLib.NullRenderer(); m_graphBuilder.AddFilter((DirectShowLib.IBaseFilter)nullRenderer, "Null Renderer"); // Connect the pieces together, use the default renderer hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, (DirectShowLib.IBaseFilter)nullRenderer); 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 //DirectShowLib.IVideoWindow videoWindow = m_graphBuilder as DirectShowLib.IVideoWindow; //ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_graphBuilder as DirectShowLib.IMediaEvent; m_mediaCtrl = m_graphBuilder as DirectShowLib.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 }
private bool oldOpenSource() { /* Make sure we clean up any remaining mess */ FreeResources(); string fileSource = FileSource; if (string.IsNullOrEmpty(fileSource)) { return(false); } try { /* Creates the GraphBuilder COM object */ m_graph = new FilterGraphNoThread() as IGraphBuilder; if (m_graph == null) { throw new Exception("Could not create a graph"); } try { /* Add our prefered audio renderer */ InsertAudioRenderer(AudioRenderer); } catch { // No Audio device found Trace.TraceError("No Audio Device found!"); } IBaseFilter renderer = CreateVideoRenderer(VideoRenderer, m_graph, 2); var filterGraph = m_graph as IFilterGraph2; if (filterGraph == null) { throw new Exception("Could not QueryInterface for the IFilterGraph2"); } IBaseFilter sourceFilter; /* Have DirectShow find the correct source filter for the Uri */ int hr = filterGraph.AddSourceFilter(fileSource, fileSource, out sourceFilter); DsError.ThrowExceptionForHR(hr); /* We will want to enum all the pins on the source filter */ IEnumPins pinEnum; hr = sourceFilter.EnumPins(out pinEnum); DsError.ThrowExceptionForHR(hr); IntPtr fetched = IntPtr.Zero; IPin[] pins = { null }; /* Counter for how many pins successfully rendered */ int pinsRendered = 0; if (VideoRenderer == VideoRendererType.VideoMixingRenderer9) { var mixer = renderer as IVMRMixerControl9; if (mixer != null) { VMR9MixerPrefs dwPrefs; mixer.GetMixingPrefs(out dwPrefs); dwPrefs &= ~VMR9MixerPrefs.RenderTargetMask; dwPrefs |= VMR9MixerPrefs.RenderTargetRGB; //mixer.SetMixingPrefs(dwPrefs); } } /* Loop over each pin of the source filter */ while (pinEnum.Next(pins.Length, pins, fetched) == 0) { if (filterGraph.RenderEx(pins[0], AMRenderExFlags.RenderToExistingRenderers, IntPtr.Zero) >= 0) { pinsRendered++; } Marshal.ReleaseComObject(pins[0]); } Marshal.ReleaseComObject(pinEnum); Marshal.ReleaseComObject(sourceFilter); if (pinsRendered == 0) { throw new Exception("Could not render any streams from the source Uri"); } #if DEBUG /* Adds the GB to the ROT so we can view * it in graphedit */ m_dsRotEntry = new DsROTEntry(m_graph); #endif /* Configure the graph in the base class */ SetupFilterGraph(m_graph); HasVideo = true; /* Sets the NaturalVideoWidth/Height */ //SetNativePixelSizes(renderer); } catch (Exception ex) { /* This exection will happen usually if the media does * not exist or could not open due to not having the * proper filters installed */ FreeResources(); /* Fire our failed event */ InvokeMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); return(false); } InvokeMediaOpened(); return(true); }
/// <summary> /// If the url to be played can be buffered before starting playback, this function /// starts building a graph by adding the preferred video and audio render to it. /// This needs to be called on the MpMain Thread. /// </summary> /// <returns>true, if the url can be buffered (a graph was started), false if it can't be and null if an error occured building the graph</returns> public bool? PrepareGraph() { string sourceFilterName = GetSourceFilterName(m_strCurrentFile); if (!string.IsNullOrEmpty(sourceFilterName)) { graphBuilder = (IGraphBuilder)new FilterGraph(); _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder); basicVideo = graphBuilder as IBasicVideo2; Vmr9 = new VMR9Util(); Vmr9.AddVMR9(graphBuilder); Vmr9.Enable(false); // set VMR9 back to NOT Active -> otherwise GUI is not refreshed while graph is building GUIGraphicsContext.Vmr9Active = false; // add the audio renderer using (Settings settings = new MPSettings()) { string audiorenderer = settings.GetValueAsString("movieplayer", "audiorenderer", "Default DirectSound Device"); DirectShowUtil.AddAudioRendererToGraph(graphBuilder, audiorenderer, false); } // set fields for playback mediaCtrl = (IMediaControl)graphBuilder; mediaEvt = (IMediaEventEx)graphBuilder; mediaSeek = (IMediaSeeking)graphBuilder; mediaPos = (IMediaPosition)graphBuilder; basicAudio = (IBasicAudio)graphBuilder; videoWin = (IVideoWindow)graphBuilder; // add the source filter IBaseFilter sourceFilter = null; try { if (sourceFilterName == OnlineVideos.MPUrlSourceFilter.Downloader.FilterName) { sourceFilter = FilterFromFile.LoadFilterFromDll("MPUrlSourceSplitter\\MPUrlSourceSplitter.ax", new Guid(OnlineVideos.MPUrlSourceFilter.Downloader.FilterCLSID), true); if (sourceFilter != null) Marshal.ThrowExceptionForHR(graphBuilder.AddFilter(sourceFilter, OnlineVideos.MPUrlSourceFilter.Downloader.FilterName)); } if (sourceFilter == null) { sourceFilter = DirectShowUtil.AddFilterToGraph(graphBuilder, sourceFilterName); } } catch (Exception ex) { Log.Instance.Warn("Error adding '{0}' filter to graph: {1}", sourceFilterName, ex.Message); return null; } finally { if (sourceFilter != null) DirectShowUtil.ReleaseComObject(sourceFilter, 2000); } return true; } else { return false; } }
/// <summary> /// Initialize the graph /// </summary> public void InitGraph() { if (theDevice == null) return; //Create the Graph graphBuilder = (IGraphBuilder) new FilterGraph(); //Create the Capture Graph Builder ICaptureGraphBuilder2 captureGraphBuilder = null; captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); //Create the media control for controlling the graph mediaControl = (IMediaControl) this.graphBuilder; // Attach the filter graph to the capture graph int hr = captureGraphBuilder.SetFiltergraph(this.graphBuilder); DsError.ThrowExceptionForHR(hr); //Add the Video input device to the graph hr = graphBuilder.AddFilter(theDevice, "source filter"); DsError.ThrowExceptionForHR(hr); //Add the Video compressor filter to the graph hr = graphBuilder.AddFilter(theCompressor, "compressor filter"); DsError.ThrowExceptionForHR(hr); //Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink IBaseFilter mux; IFileSinkFilter sink; hr = captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, textBox1.Text, out mux, out sink); DsError.ThrowExceptionForHR(hr); //Render any preview pin of the device hr = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, theDevice, null, null); DsError.ThrowExceptionForHR(hr); //Connect the device and compressor to the mux to render the capture part of the graph hr = captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, theDevice, theCompressor, mux); DsError.ThrowExceptionForHR(hr); #if DEBUG m_rot = new DsROTEntry(graphBuilder); #endif //get the video window from the graph IVideoWindow videoWindow = null; videoWindow = (IVideoWindow) graphBuilder; //Set the owener of the videoWindow to an IntPtr of some sort (the Handle of any control - could be a form / button etc.) hr = videoWindow.put_Owner(panel1.Handle); DsError.ThrowExceptionForHR(hr); //Set the style of the video window hr = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren); DsError.ThrowExceptionForHR(hr); // Position video window in client rect of main application window hr = videoWindow.SetWindowPosition(0,0, panel1.Width, panel1.Height); DsError.ThrowExceptionForHR(hr); // Make the video window visible hr = videoWindow.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(mux); Marshal.ReleaseComObject(sink); Marshal.ReleaseComObject(captureGraphBuilder); }
private void Configure() { // In order to lock a profile, you have to have at least one stream // connected to the sink. I connect a video thru the DVVideoEnc into // the StreamBufferSink. int hr; IBaseFilter pFilter; ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); StreamBufferSink sbk = new StreamBufferSink(); m_FilterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry ds = new DsROTEntry(m_FilterGraph); hr = icgb.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.AddFilter((IBaseFilter)sbk, "StreamBufferSink"); DsError.ThrowExceptionForHR(hr); DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); hr = m_FilterGraph.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out pFilter); DsError.ThrowExceptionForHR(hr); DVVideoEnc dve = new DVVideoEnc(); IBaseFilter ibfVideoEnc = (IBaseFilter)dve; hr = m_FilterGraph.AddFilter(ibfVideoEnc, "dvenc"); DsError.ThrowExceptionForHR(hr); hr = icgb.RenderStream(null, null, pFilter, ibfVideoEnc, (IBaseFilter)sbk); DsError.ThrowExceptionForHR(hr); IStreamBufferSink isbc = (IStreamBufferSink)sbk; hr = isbc.LockProfile(null); DsError.ThrowExceptionForHR(hr); IPin i = DsFindPin.ByDirection((IBaseFilter)sbk, PinDirection.Input, 0); ((IMediaControl)m_FilterGraph).Run(); // -------------------------- IBaseFilter streamBuffer = null; m_FilterGraph2 = (IFilterGraph2) new FilterGraph(); DsROTEntry ds2 = new DsROTEntry(m_FilterGraph2); streamBuffer = (IBaseFilter) new StreamBufferSource(); hr = m_FilterGraph2.AddFilter(streamBuffer, "Stream buffer sink"); DsError.ThrowExceptionForHR(hr); IStreamBufferSource sbsrc = (IStreamBufferSource)streamBuffer; hr = sbsrc.SetStreamSink(isbc); DsError.ThrowExceptionForHR(hr); IPin i2 = DsFindPin.ByDirection((IBaseFilter)streamBuffer, PinDirection.Output, 0); ((IMediaControl)m_FilterGraph2).Run(); m_sbms = (IStreamBufferMediaSeeking2)sbsrc; // -------------------------- Marshal.ReleaseComObject(pFilter); Marshal.ReleaseComObject(icgb); }
/// <summary> /// build the capture graph for grabber. /// </summary> /// <param name="strCapture">The STR capture.</param> /// <param name="strCompressor">The STR compressor.</param> /// <param name="strFileName">Name of the STR file.</param> /// <param name="iFrameRate">The i frame rate.</param> /// <param name="iWidth">Width of the i.</param> /// <param name="iHeight">Height of the i.</param> /// <param name="owner">The owner.</param> /// <param name="record">if set to <c>true</c> [record].</param> private void SetupGraph(string strCapture, string strCompressor, string strFileName, int iFrameRate, int iWidth, int iHeight, IntPtr owner, bool record) { ICaptureGraphBuilder2 captureGraphBuilder = null; ISampleGrabber sampGrabber = null; IBaseFilter theIPinTee = null; IBaseFilter mux = null; IFileSinkFilter sink = null; IBaseFilter captureDevice = null; IBaseFilter captureCompressor = null; IBaseFilter theRenderer = null; int hr = 0; try { //Create the filter for the selected video input captureDevice = CreateFilter(FilterCategory.VideoInputDevice, strCapture); //Create the filter for the selected video compressor captureCompressor = CreateFilter(FilterCategory.VideoCompressorCategory, strCompressor); //Create the Graph _graphBuilder = (IGraphBuilder) new FilterGraph(); //Create the Capture Graph Builder captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); // Attach the filter graph to the capture graph hr = captureGraphBuilder.SetFiltergraph(this._graphBuilder); checkHR(hr, "Error attaching filter graph to capture graph"); //Add the Video input device to the graph hr = _graphBuilder.AddFilter(captureDevice, "QUAVS input filter"); checkHR(hr, "Error attaching video input"); //setup cature device SetConfigParms(captureGraphBuilder, captureDevice, iFrameRate, iWidth, iHeight); //Add a sample grabber sampGrabber = (ISampleGrabber) new SampleGrabber(); ConfigureSampleGrabber(sampGrabber); hr = _graphBuilder.AddFilter((IBaseFilter)sampGrabber, "QUAVS SampleGrabber"); checkHR(hr, "Error adding sample grabber"); //connect capture device to SampleGrabber hr = _graphBuilder.Connect(GetPin(captureDevice, "Capture"), GetPin((IBaseFilter)sampGrabber, "Input")); checkHR(hr, "Error attaching sample grabber to capture pin"); //Add Ininite Pin Tee theIPinTee = (IBaseFilter) new InfTee(); hr = _graphBuilder.AddFilter(theIPinTee, "QUAVS Pin Tee"); checkHR(hr, "Error adding infinite tee pin"); //connect capture SampleGrabber to IPinTee hr = _graphBuilder.Connect(GetPin((IBaseFilter)sampGrabber, "Output"), GetPin(theIPinTee, "Input")); checkHR(hr, "Error adding SampleGrabber"); if (record) { //Add the Video compressor filter to the graph hr = _graphBuilder.AddFilter(captureCompressor, "QUAVS compressor filter"); checkHR(hr, "Error adding compressor filter"); //connect capture IPinTee output1 to compressor hr = _graphBuilder.Connect(GetPin(theIPinTee, "Output1"), GetPin(captureCompressor, "Input")); checkHR(hr, "Error adding TO DO"); //Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink hr = captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, strFileName, out mux, out sink); checkHR(hr, "Error adding mux filter or setting output file name"); //connect compressor to mux output hr = _graphBuilder.Connect(GetPin(captureCompressor, "Output"), GetPin(mux, "Input 01")); checkHR(hr, "Error connecting the compressor to mux"); // Get the default video renderer theRenderer = new VideoRendererDefault() as IBaseFilter; hr = _graphBuilder.AddFilter(theRenderer, "Renderer"); checkHR(hr, "Error adding screen renderer"); //connect capture TO DO hr = _graphBuilder.Connect(GetPin(theIPinTee, "Output2"), GetPin(theRenderer, "VMR Input0")); checkHR(hr, "Error connecting screen renderer"); } else { // Get the default video renderer theRenderer = new VideoRendererDefault() as IBaseFilter; hr = _graphBuilder.AddFilter(theRenderer, "Renderer"); checkHR(hr, "Error adding screen renderer"); //connect capture TO DO hr = _graphBuilder.Connect(GetPin(theIPinTee, "Output1"), GetPin(theRenderer, "VMR Input0")); checkHR(hr, "Error connecting screen renderer"); } SaveSizeInfo(sampGrabber); #if DEBUG _rot = new DsROTEntry(_graphBuilder); #endif if (owner != IntPtr.Zero) { //get the video window from the graph IVideoWindow videoWindow = null; videoWindow = (IVideoWindow)_graphBuilder; //Set the owener of the videoWindow to an IntPtr of some sort (the Handle of any control - could be a form / button etc.) hr = videoWindow.put_Owner(owner); DsError.ThrowExceptionForHR(hr); //Set the style of the video window hr = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren); DsError.ThrowExceptionForHR(hr); hr = videoWindow.SetWindowPosition(0, 0, iWidth, iHeight); DsError.ThrowExceptionForHR(hr); // Make the video window visible hr = videoWindow.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); } //Create the media control for controlling the graph _mediaCtrl = (IMediaControl)this._graphBuilder; } catch (Exception e) { TraceException.WriteLine(e); //Trace.WriteLine(e.Message); CloseInterfaces(); } finally { if (sink != null) { Marshal.ReleaseComObject(sink); sink = null; } if (mux != null) { Marshal.ReleaseComObject(mux); mux = null; } if (captureGraphBuilder != null) { Marshal.ReleaseComObject(captureGraphBuilder); captureGraphBuilder = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (theIPinTee != null) { Marshal.ReleaseComObject(theIPinTee); theIPinTee = null; } if (captureDevice != null) { Marshal.ReleaseComObject(captureDevice); captureDevice = null; } if (captureCompressor != null) { Marshal.ReleaseComObject(captureCompressor); captureCompressor = null; } if (theRenderer != null) { Marshal.ReleaseComObject(theRenderer); theRenderer = null; } } }
public override bool Play(string strFile) { updateTimer = DateTime.Now; m_speedRate = 10000; m_bVisible = false; m_iVolume = 100; _state = PlayState.Init; m_strCurrentFile = strFile; m_bFullScreen = true; m_ar = GUIGraphicsContext.ARType; VideoRendererStatistics.VideoState = VideoRendererStatistics.State.VideoPresent; _updateNeeded = true; Log.Info("RTSPPlayer:play {0}", strFile); //lock ( typeof(VideoPlayerVMR7) ) { CloseInterfaces(); m_bStarted = false; if (!GetInterfaces()) { m_strCurrentFile = ""; CloseInterfaces(); return(false); } int hr = mediaEvt.SetNotifyWindow(GUIGraphicsContext.ActiveForm, WM_GRAPHNOTIFY, IntPtr.Zero); if (hr < 0) { Error.SetError("Unable to play movie", "Can not set notifications"); m_strCurrentFile = ""; CloseInterfaces(); return(false); } DirectShowUtil.SetARMode(graphBuilder, AspectRatioMode.Stretched); _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder); // DsUtils.DumpFilters(graphBuilder); hr = _mediaCtrl.Run(); if (hr < 0) { Error.SetError("Unable to play movie", "Unable to start movie"); m_strCurrentFile = ""; CloseInterfaces(); return(false); } GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_PLAYBACK_STARTED, 0, 0, 0, 0, 0, null); msg.Label = strFile; GUIWindowManager.SendThreadMessage(msg); _state = PlayState.Playing; //Brutus GUIGraphicsContext.IsFullScreenVideo=true; m_iPositionX = GUIGraphicsContext.VideoWindow.X; m_iPositionY = GUIGraphicsContext.VideoWindow.Y; m_iWidth = GUIGraphicsContext.VideoWindow.Width; m_iHeight = GUIGraphicsContext.VideoWindow.Height; m_ar = GUIGraphicsContext.ARType; _updateNeeded = true; SetVideoWindow(); mediaPos.get_Duration(out _duration); Log.Info("RTSPPlayer:Duration:{0}", _duration); if (_mediaType == g_Player.MediaType.TV) { //if (_duration < 1) _duration = 1; //SeekAbsolute(_duration - 1); } else { //SeekAbsolute(0); } OnInitialized(); } // Wait for a while to wait VMR9 to get ready. // Implemented due to problems starting to play before VMR9 was ready resulting in black screen. Thread.Sleep(200); return(true); }
private void Cleanup() { if (graphBuilder == null) { return; } int hr; Log.Info("RTSPPlayer:cleanup DShow graph"); try { if (_mediaCtrl != null) { int counter = 0; FilterState state; hr = _mediaCtrl.Stop(); hr = _mediaCtrl.GetState(10, out state); while (state != FilterState.Stopped || GUIGraphicsContext.InVmr9Render) { Thread.Sleep(100); hr = _mediaCtrl.GetState(10, out state); counter++; if (counter >= 30) { if (state != FilterState.Stopped) { Log.Error("RTSPPlayer: graph still running"); } if (GUIGraphicsContext.InVmr9Render) { Log.Error("RTSPPlayer: in renderer"); } break; } } _mediaCtrl = null; } if (Vmr9 != null) { Vmr9.Enable(false); } if (mediaEvt != null) { hr = mediaEvt.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero); mediaEvt = null; } videoWin = graphBuilder as IVideoWindow; if (videoWin != null) { hr = videoWin.put_Visible(OABool.False); hr = videoWin.put_Owner(IntPtr.Zero); videoWin = null; } _mediaSeeking = null; mediaPos = null; basicAudio = null; basicVideo = null; videoWin = null; SubEngine.GetInstance().FreeSubtitles(); if (graphBuilder != null) { DirectShowUtil.RemoveFilters(graphBuilder); if (_rotEntry != null) { _rotEntry.SafeDispose(); _rotEntry = null; } DirectShowUtil.ReleaseComObject(graphBuilder); graphBuilder = null; } if (Vmr9 != null) { Vmr9.SafeDispose(); Vmr9 = null; } GUIGraphicsContext.form.Invalidate(true); _state = PlayState.Init; if (_mpegDemux != null) { Log.Info("cleanup mpegdemux"); while ((hr = DirectShowUtil.ReleaseComObject(_mpegDemux)) > 0) { ; } _mpegDemux = null; } if (_rtspSource != null) { Log.Info("cleanup _rtspSource"); while ((hr = DirectShowUtil.ReleaseComObject(_rtspSource)) > 0) { ; } _rtspSource = null; } if (_subtitleFilter != null) { while ((hr = DirectShowUtil.ReleaseComObject(_subtitleFilter)) > 0) { ; } _subtitleFilter = null; if (this.dvbSubRenderer != null) { this.dvbSubRenderer.SetPlayer(null); } this.dvbSubRenderer = null; } if (vobSub != null) { Log.Info("cleanup vobSub"); while ((hr = DirectShowUtil.ReleaseComObject(vobSub)) > 0) { ; } vobSub = null; } } catch (Exception ex) { Log.Error("RTSPPlayer: Exception while cleanuping DShow graph - {0} {1}", ex.Message, ex.StackTrace); } //switch back to directx windowed mode Log.Info("RTSPPlayer: Disabling DX9 exclusive mode"); GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_SWITCH_FULL_WINDOWED, 0, 0, 0, 0, 0, null); GUIWindowManager.SendMessage(msg); }
/// <summary> /// Builds the DVD DirectShow graph /// </summary> private void BuildGraph() { try { FreeResources(); int hr; /* Create our new graph */ m_graph = (IGraphBuilder) new FilterGraphNoThread(); #if DEBUG m_rot = new DsROTEntry(m_graph); #endif /* We are going to use the VMR9 for now. The EVR does not * seem to work with the interactive menus yet. It should * play Dvds fine otherwise */ var rendererType = VideoRendererType.VideoMixingRenderer9; /* Creates and initializes a new renderer ready to render to WPF */ m_renderer = CreateVideoRenderer(rendererType, m_graph, 2); /* Do some VMR9 specific stuff */ if (rendererType == VideoRendererType.VideoMixingRenderer9) { var mixer = m_renderer as IVMRMixerControl9; if (mixer != null) { VMR9MixerPrefs dwPrefs; mixer.GetMixingPrefs(out dwPrefs); dwPrefs &= ~VMR9MixerPrefs.RenderTargetMask; dwPrefs |= VMR9MixerPrefs.RenderTargetYUV; /* Enable this line to prefer YUV */ //hr = mixer.SetMixingPrefs(dwPrefs); } } /* Create a new DVD Navigator. */ var dvdNav = (IBaseFilter) new DVDNavigator(); /* The DVDControl2 interface lets us control DVD features */ m_dvdControl = dvdNav as IDvdControl2; if (m_dvdControl == null) { throw new WPFMediaKitException("Could not QueryInterface the IDvdControl2 interface"); } /* QueryInterface the DVDInfo2 */ m_dvdInfo = dvdNav as IDvdInfo2; /* If a Dvd directory has been set then use it, if not, let DShow find the Dvd */ if (!string.IsNullOrEmpty(DvdDirectory)) { hr = m_dvdControl.SetDVDDirectory(DvdDirectory); DsError.ThrowExceptionForHR(hr); } /* This gives us the DVD time in Hours-Minutes-Seconds-Frame time format, and other options */ hr = m_dvdControl.SetOption(DvdOptionFlag.HMSFTimeCodeEvents, true); DsError.ThrowExceptionForHR(hr); /* If the graph stops, resume at the same point */ m_dvdControl.SetOption(DvdOptionFlag.ResetOnStop, false); hr = m_graph.AddFilter(dvdNav, "DVD Navigator"); DsError.ThrowExceptionForHR(hr); IPin dvdVideoPin = null; IPin dvdAudioPin = null; IPin dvdSubPicturePin = null; IPin dvdNavPin; int i = 0; /* Loop all the output pins on the DVD Navigator, trying to find which pins are which. * We could more easily find the pins by name, but this is more fun...and more flexible * if we ever want to use a 3rd party DVD navigator that used different pin names */ while ((dvdNavPin = DsFindPin.ByDirection(dvdNav, PinDirection.Output, i)) != null) { var mediaTypes = new AMMediaType[1]; IntPtr pFetched = IntPtr.Zero; IEnumMediaTypes mediaTypeEnum; dvdNavPin.EnumMediaTypes(out mediaTypeEnum); /* Loop over each of the mediaTypes of each pin */ while (mediaTypeEnum.Next(1, mediaTypes, pFetched) == 0) { AMMediaType mediaType = mediaTypes[0]; /* This will be the video stream pin */ if (mediaType.subType == MediaSubType.Mpeg2Video) { /* Keep the ref and we'll work with it later */ dvdVideoPin = dvdNavPin; break; } /* This will be the audio stream pin */ if (mediaType.subType == MediaSubType.DolbyAC3 || mediaType.subType == MediaSubType.Mpeg2Audio) { /* Keep the ref and we'll work with it later */ dvdAudioPin = dvdNavPin; break; } /* This is the Dvd sub picture pin. This generally * shows overlays for Dvd menus and sometimes closed captions */ if (mediaType.subType == DVD_SUBPICTURE_TYPE) { /* Keep the ref and we'll work with it later */ dvdSubPicturePin = dvdNavPin; break; } } mediaTypeEnum.Reset(); Marshal.ReleaseComObject(mediaTypeEnum); i++; } /* This is the windowed renderer. This is *NEEDED* in order * for interactive menus to work with the other VMR9 in renderless mode */ var dummyRenderer = (IBaseFilter) new VideoMixingRenderer9(); var dummyRendererConfig = (IVMRFilterConfig9)dummyRenderer; /* In order for this interactive menu trick to work, the VMR9 * must be set to Windowed. We will make sure the window is hidden later on */ hr = dummyRendererConfig.SetRenderingMode(VMR9Mode.Windowed); DsError.ThrowExceptionForHR(hr); hr = dummyRendererConfig.SetNumberOfStreams(1); DsError.ThrowExceptionForHR(hr); hr = m_graph.AddFilter(dummyRenderer, "Dummy Windowed"); DsError.ThrowExceptionForHR(hr); if (dvdAudioPin != null) { /* This should render out to the default audio device. We * could modify this code here to go out any audio * device, such as SPDIF or another sound card */ hr = m_graph.Render(dvdAudioPin); DsError.ThrowExceptionForHR(hr); } /* Get the first input pin on our dummy renderer */ m_dummyRendererPin = DsFindPin.ByConnectionStatus(dummyRenderer, /* Filter to search */ PinConnectedStatus.Unconnected, 0); /* Get an available pin on our real renderer */ IPin rendererPin = DsFindPin.ByConnectionStatus(m_renderer, /* Filter to search */ PinConnectedStatus.Unconnected, 0); /* Pin index */ /* Connect the pin to the renderer */ hr = m_graph.Connect(dvdVideoPin, rendererPin); DsError.ThrowExceptionForHR(hr); /* Get the next available pin on our real renderer */ rendererPin = DsFindPin.ByConnectionStatus(m_renderer, /* Filter to search */ PinConnectedStatus.Unconnected, 0); /* Pin index */ /* Render the sub picture, which will connect * the DVD navigator to the codec, not the renderer */ hr = m_graph.Render(dvdSubPicturePin); DsError.ThrowExceptionForHR(hr); /* These are the subtypes most likely to be our dvd subpicture */ var preferedSubpictureTypes = new[] { MediaSubType.ARGB4444, MediaSubType.AI44, MediaSubType.AYUV, MediaSubType.ARGB32 }; IPin dvdSubPicturePinOut = null; /* Find what should be the subpicture pin out */ foreach (var guidType in preferedSubpictureTypes) { dvdSubPicturePinOut = FindPinInGraphByMediaType(guidType, /* GUID of the media type being searched for */ PinDirection.Output, m_graph); /* Our current graph */ if (dvdSubPicturePinOut != null) { break; } } if (dvdSubPicturePinOut == null) { throw new WPFMediaKitException("Could not find the sub picture pin out"); } /* Here we connec thte Dvd sub picture pin to the video renderer. * This enables the overlays on Dvd menus and some closed * captions to be rendered. */ hr = m_graph.Connect(dvdSubPicturePinOut, rendererPin); DsError.ThrowExceptionForHR(hr); /* Search for the Line21 out in the graph */ IPin line21Out = FindPinInGraphByMediaType(MediaType.AuxLine21Data, PinDirection.Output, m_graph); if (line21Out == null) { throw new WPFMediaKitException("Could not find the Line21 pin out"); } /* We connect our line21Out out in to the dummy renderer * this is what ultimatly makes interactive DVDs work with * VMR9 in renderless (for WPF) */ hr = m_graph.Connect(line21Out, m_dummyRendererPin); DsError.ThrowExceptionForHR(hr); /* This is the dummy renderers Win32 window. */ m_dummyRenderWindow = dummyRenderer as IVideoWindow; if (m_dummyRenderWindow == null) { throw new WPFMediaKitException("Could not QueryInterface for IVideoWindow"); } ConfigureDummyWindow(); /* Setup our base classes with this filter graph */ SetupFilterGraph(m_graph); /* Sets the NaturalVideoWidth/Height */ SetNativePixelSizes(m_renderer); } catch (Exception ex) { FreeResources(); InvokeMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); return; } InvokeMediaOpened(); }
private void Cleanup() { if (graphBuilder == null) { return; } int hr; Log.Info("RTSPPlayer:cleanup DShow graph"); try { if (VMR9Util.g_vmr9 != null) { VMR9Util.g_vmr9.Vmr9MediaCtrl(_mediaCtrl); VMR9Util.g_vmr9.Enable(false); } if (mediaEvt != null) { hr = mediaEvt.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero); } videoWin = graphBuilder as IVideoWindow; if (videoWin != null && GUIGraphicsContext.VideoRenderer != GUIGraphicsContext.VideoRendererType.madVR) { videoWin.put_Owner(IntPtr.Zero); videoWin.put_Visible(OABool.False); } _mediaCtrl = null; mediaEvt = null; _mediaSeeking = null; mediaPos = null; basicAudio = null; basicVideo = null; videoWin = null; SubEngine.GetInstance().FreeSubtitles(); if (graphBuilder != null) { DirectShowUtil.RemoveFilters(graphBuilder); if (_rotEntry != null) { _rotEntry.SafeDispose(); _rotEntry = null; } DirectShowUtil.FinalReleaseComObject(graphBuilder); graphBuilder = null; } if (VMR9Util.g_vmr9 != null) { VMR9Util.g_vmr9.SafeDispose(); VMR9Util.g_vmr9 = null; } GUIGraphicsContext.form.Invalidate(true); _state = PlayState.Init; if (_mpegDemux != null) { Log.Info("cleanup mpegdemux"); DirectShowUtil.FinalReleaseComObject(_mpegDemux); _mpegDemux = null; } if (_rtspSource != null) { Log.Info("cleanup _rtspSource"); DirectShowUtil.FinalReleaseComObject(_rtspSource); _rtspSource = null; } if (_subtitleFilter != null) { DirectShowUtil.FinalReleaseComObject(_subtitleFilter); _subtitleFilter = null; if (this.dvbSubRenderer != null) { this.dvbSubRenderer.SetPlayer(null); } this.dvbSubRenderer = null; } if (vobSub != null) { Log.Info("cleanup vobSub"); DirectShowUtil.FinalReleaseComObject(vobSub); vobSub = null; } } catch (Exception ex) { Log.Error("RTSPPlayer: Exception while cleanuping DShow graph - {0} {1}", ex.Message, ex.StackTrace); } //switch back to directx windowed mode Log.Info("RTSPPlayer: Disabling DX9 exclusive mode"); GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_SWITCH_FULL_WINDOWED, 0, 0, 0, 0, 0, null); GUIWindowManager.SendMessage(msg); }
private void CloseInterfaces() { int hr; if (m_adecOut != null) { CleanUpInterface(m_adecOut); m_adecOut = null; } if (_defaultAudioRenderer != null) { m_graph.RemoveFilter(_defaultAudioRenderer as DirectShowLib.IBaseFilter); CleanUpInterface(_defaultAudioRenderer); _defaultAudioRenderer = null; } if (_reclockAudioRenderer != null) { m_graph.RemoveFilter(_reclockAudioRenderer as DirectShowLib.IBaseFilter); CleanUpInterface(_reclockAudioRenderer); _reclockAudioRenderer = null; } if (_wasapiAR != null) { m_graph.RemoveFilter(_wasapiAR as DirectShowLib.IBaseFilter); CleanUpInterface(_wasapiAR); _wasapiAR = null; } if (_lavaudio != null) { m_graph.RemoveFilter(_lavaudio as DirectShowLib.IBaseFilter); CleanUpInterface(_lavaudio); _lavaudio = null; } if (_xyVsFilter != null) { m_graph.RemoveFilter(_xyVsFilter as DirectShowLib.IBaseFilter); CleanUpInterface(_xyVsFilter); _xyVsFilter = null; } if (_xySubFilter != null) { m_graph.RemoveFilter(_xySubFilter as DirectShowLib.IBaseFilter); CleanUpInterface(_xySubFilter); _xySubFilter = null; } if (_lavvideo != null) { m_graph.RemoveFilter(_lavvideo as DirectShowLib.IBaseFilter); CleanUpInterface(_lavvideo); _lavvideo = null; } if (_madvr != null) { m_graph.RemoveFilter(_madvr as DirectShowLib.IBaseFilter); CleanUpInterface(_madvr); _madvr = null; } if (_videoWindow != null) { // Relinquish ownership (IMPORTANT!) after hiding video window hr = _videoWindow.put_Visible(OABool.False); hr = _videoWindow.put_Owner(IntPtr.Zero); } if (_mediaEventEx != null) { hr = _mediaEventEx.SetNotifyWindow(IntPtr.Zero, 0, IntPtr.Zero); //Marshal.ReleaseComObject(_mediaEventEx); //_mediaEventEx = null; } //if (_dvdNav != null) //{ // Marshal.ReleaseComObject(_dvdNav); // _dvdNav = null; //} /* //this will double release the source filter if (dvdInfo != null) { Marshal.ReleaseComObject(dvdInfo); dvdInfo = null; } if (_mDvdControl != null) { Marshal.ReleaseComObject(_mDvdControl); } */ _mDvdControl = null; CleanUpInterface(_mPDisplay); _mPDisplay = null; CleanUpInterface(_sourceFilter); _sourceFilter = null; CleanUpInterface(_mPEvr); _mPEvr = null; CleanUpInterface(m_filterGraph); m_filterGraph = null; m_filterGraph = null; _mediaEventEx = null; _mediaSeeking = null; _mediaPosition = null; _mediaControl = null; _basicAudio = null; _basicVideo = null; m_graph = null; _videoWindow = null; _filterGraph = null; if (m_dsRot != null) m_dsRot.Dispose(); m_dsRot = null; _mSeekCaps = 0; _streams = null; GC.Collect(); }
public bool Transcode(TranscodeInfo info, VideoFormat format, Quality quality, Standard standard) { try { if (!Supports(format)) { return(false); } string ext = System.IO.Path.GetExtension(info.file); if (ext.ToLower() != ".ts" && ext.ToLower() != ".mpg") { Log.Info("TSReader2WMV: wrong file format"); return(false); } Log.Info("TSReader2WMV: create graph"); graphBuilder = (IGraphBuilder) new FilterGraph(); _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder); Log.Info("TSReader2WMV: add filesource"); TsReader reader = new TsReader(); tsreaderSource = (IBaseFilter)reader; //ITSReader ireader = (ITSReader)reader; //ireader.SetTsReaderCallback(this); //ireader.SetRequestAudioChangeCallback(this); IBaseFilter filter = (IBaseFilter)tsreaderSource; graphBuilder.AddFilter(filter, "TSReader Source"); IFileSourceFilter fileSource = (IFileSourceFilter)tsreaderSource; Log.Info("TSReader2WMV: load file:{0}", info.file); int hr = fileSource.Load(info.file, null); //add audio/video codecs string strVideoCodec = ""; string strH264VideoCodec = ""; string strAudioCodec = ""; string strAACAudioCodec = ""; using (MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.MPSettings()) { strVideoCodec = xmlreader.GetValueAsString("mytv", "videocodec", ""); strAudioCodec = xmlreader.GetValueAsString("mytv", "audiocodec", ""); strAACAudioCodec = xmlreader.GetValueAsString("mytv", "aacaudiocodec", ""); strH264VideoCodec = xmlreader.GetValueAsString("mytv", "h264videocodec", ""); } //Find the type of decoder required for the output video & audio pins on TSReader. Log.Info("TSReader2WMV: find tsreader compatible audio/video decoders"); IPin pinOut0, pinOut1; IPin pinIn0, pinIn1; pinOut0 = DsFindPin.ByDirection((IBaseFilter)tsreaderSource, PinDirection.Output, 0); //audio pinOut1 = DsFindPin.ByDirection((IBaseFilter)tsreaderSource, PinDirection.Output, 1); //video if (pinOut0 == null || pinOut1 == null) { Log.Error("TSReader2WMV: FAILED: unable to get output pins of tsreader"); Cleanup(); return(false); } bool usingAAC = false; IEnumMediaTypes enumMediaTypes; hr = pinOut0.EnumMediaTypes(out enumMediaTypes); while (true) { AMMediaType[] mediaTypes = new AMMediaType[1]; int typesFetched; hr = enumMediaTypes.Next(1, mediaTypes, out typesFetched); if (hr != 0 || typesFetched == 0) { break; } if (mediaTypes[0].majorType == MediaType.Audio && mediaTypes[0].subType == MediaSubType.LATMAAC) { Log.Info("TSReader2WMV: found LATM AAC audio out pin on tsreader"); usingAAC = true; } } bool usingH264 = false; hr = pinOut1.EnumMediaTypes(out enumMediaTypes); while (true) { AMMediaType[] mediaTypes = new AMMediaType[1]; int typesFetched; hr = enumMediaTypes.Next(1, mediaTypes, out typesFetched); if (hr != 0 || typesFetched == 0) { break; } if (mediaTypes[0].majorType == MediaType.Video && mediaTypes[0].subType == AVC1) { Log.Info("TSReader2WMV: found H.264 video out pin on tsreader"); usingH264 = true; } } //Add the type of decoder required for the output video & audio pins on TSReader. Log.Info("TSReader2WMV: add audio/video decoders to graph"); if (usingH264 == false) { Log.Info("TSReader2WMV: add mpeg2 video decoder:{0}", strVideoCodec); VideoCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strVideoCodec); if (VideoCodec == null) { Log.Error("TSReader2WMV: unable to add mpeg2 video decoder"); Cleanup(); return(false); } } else { Log.Info("TSReader2WMV: add h264 video codec:{0}", strH264VideoCodec); VideoCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strH264VideoCodec); if (VideoCodec == null) { Log.Error("TSReader2WMV: FAILED:unable to add h264 video codec"); Cleanup(); return(false); } } if (usingAAC == false) { Log.Info("TSReader2WMV: add mpeg2 audio codec:{0}", strAudioCodec); AudioCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strAudioCodec); if (AudioCodec == null) { Log.Error("TSReader2WMV: FAILED:unable to add mpeg2 audio codec"); Cleanup(); return(false); } } else { Log.Info("TSReader2WMV: add aac audio codec:{0}", strAACAudioCodec); AudioCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strAACAudioCodec); if (AudioCodec == null) { Log.Error("TSReader2WMV: FAILED:unable to add aac audio codec"); Cleanup(); return(false); } } Log.Info("TSReader2WMV: connect tsreader->audio/video decoders"); //connect output #0 (audio) of tsreader->audio decoder input pin 0 //connect output #1 (video) of tsreader->video decoder input pin 0 pinIn0 = DsFindPin.ByDirection(AudioCodec, PinDirection.Input, 0); //audio pinIn1 = DsFindPin.ByDirection(VideoCodec, PinDirection.Input, 0); //video if (pinIn0 == null || pinIn1 == null) { Log.Error("TSReader2WMV: FAILED: unable to get pins of video/audio codecs"); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut0, pinIn0); if (hr != 0) { Log.Error("TSReader2WMV: FAILED: unable to connect audio pins :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut1, pinIn1); if (hr != 0) { Log.Error("TSReader2WMV: FAILED: unable to connect video pins :0x{0:X}", hr); Cleanup(); return(false); } string outputFilename = System.IO.Path.ChangeExtension(info.file, ".wmv"); if (!AddWmAsfWriter(outputFilename, quality, standard)) { return(false); } Log.Info("TSReader2WMV: start pre-run"); mediaControl = graphBuilder as IMediaControl; mediaSeeking = tsreaderSource as IMediaSeeking; mediaEvt = graphBuilder as IMediaEventEx; mediaPos = graphBuilder as IMediaPosition; //get file duration long lTime = 5 * 60 * 60; lTime *= 10000000; long pStop = 0; hr = mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop), AMSeekingSeekingFlags.NoPositioning); if (hr == 0) { long lStreamPos; mediaSeeking.GetCurrentPosition(out lStreamPos); // stream position m_dDuration = lStreamPos; lTime = 0; mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop), AMSeekingSeekingFlags.NoPositioning); } double duration = m_dDuration / 10000000d; Log.Info("TSReader2WMV: movie duration:{0}", Util.Utils.SecondsToHMSString((int)duration)); hr = mediaControl.Run(); if (hr != 0) { Log.Error("TSReader2WMV: FAILED: unable to start graph :0x{0:X}", hr); Cleanup(); return(false); } int maxCount = 20; while (true) { long lCurrent; mediaSeeking.GetCurrentPosition(out lCurrent); double dpos = (double)lCurrent; dpos /= 10000000d; System.Threading.Thread.Sleep(100); if (dpos >= 2.0d) { break; } maxCount--; if (maxCount <= 0) { break; } } Log.Info("TSReader2WMV: pre-run done"); Log.Info("TSReader2WMV: Get duration of movie"); mediaControl.Stop(); FilterState state; mediaControl.GetState(500, out state); GC.Collect(); GC.Collect(); GC.Collect(); GC.WaitForPendingFinalizers(); Log.Info("TSReader2WMV: reconnect mpeg2 video codec->ASF WM Writer"); graphBuilder.RemoveFilter(fileWriterbase); if (!AddWmAsfWriter(outputFilename, quality, standard)) { return(false); } Log.Info("TSReader2WMV: Start transcoding"); hr = mediaControl.Run(); if (hr != 0) { Log.Error("TSReader2WMV:FAILED:unable to start graph :0x{0:X}", hr); Cleanup(); return(false); } } catch (Exception e) { // TODO: Handle exceptions. Log.Error("unable to transcode file:{0} message:{1}", info.file, e.Message); return(false); } return(true); }
/// <summary> do cleanup and release DirectShow. </summary> protected void CloseInterfaces() { if (_graphBuilder == null) { return; } int hr; Log.Debug("BDPlayer: Cleanup DShow graph {0}", GUIGraphicsContext.InVmr9Render); try { BDOSDRenderer.Release(); if (_mediaCtrl != null) { int counter = 0; FilterState state; hr = _mediaCtrl.Stop(); hr = _mediaCtrl.GetState(10, out state); while (state != FilterState.Stopped || GUIGraphicsContext.InVmr9Render) { Thread.Sleep(100); hr = _mediaCtrl.GetState(10, out state); counter++; if (counter >= 30) { if (state != FilterState.Stopped) Log.Error("BDPlayer: graph still running"); if (GUIGraphicsContext.InVmr9Render) Log.Error("BDPlayer: in renderer"); break; } } _mediaCtrl = null; } if (_vmr9 != null) { _vmr9.Enable(false); } if (_mediaEvt != null) { hr = _mediaEvt.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero); _mediaEvt = null; } _videoWin = _graphBuilder as IVideoWindow; if (_videoWin != null) { hr = _videoWin.put_Visible(OABool.False); hr = _videoWin.put_Owner(IntPtr.Zero); _videoWin = null; } _mediaSeeking = null; _basicAudio = null; _basicVideo = null; _ireader = null; #region Cleanup Sebastiii if (VideoCodec != null) { DirectShowUtil.ReleaseComObject(VideoCodec, 5000); VideoCodec = null; Log.Info("BDPlayer: Cleanup VideoCodec"); } if (AudioCodec != null) { DirectShowUtil.ReleaseComObject(AudioCodec, 5000); AudioCodec = null; Log.Info("BDPlayer: Cleanup AudioCodec"); } if (_audioRendererFilter != null) { while (DirectShowUtil.ReleaseComObject(_audioRendererFilter) > 0) ; _audioRendererFilter = null; Log.Info("BDPlayer: Cleanup AudioRenderer"); } //Test to ReleaseComObject from PostProcessFilter list objects. if (PostProcessFilterVideo.Count > 0) { foreach (var ppFilter in PostProcessFilterVideo) { if (ppFilter.Value != null) DirectShowUtil.ReleaseComObject(ppFilter.Value, 5000); } PostProcessFilterVideo.Clear(); Log.Info("BDPlayer: Cleanup PostProcessVideo"); } //Test to ReleaseComObject from PostProcessFilter list objects. if (PostProcessFilterAudio.Count > 0) { foreach (var ppFilter in PostProcessFilterAudio) { if (ppFilter.Value != null) DirectShowUtil.ReleaseComObject(ppFilter.Value, 5000); } PostProcessFilterAudio.Clear(); Log.Info("BDPlayer: Cleanup PostProcessAudio"); } #endregion if (_interfaceBDReader != null) { DirectShowUtil.ReleaseComObject(_interfaceBDReader, 5000); _interfaceBDReader = null; } if (_graphBuilder != null) { DirectShowUtil.RemoveFilters(_graphBuilder); if (_rotEntry != null) { _rotEntry.SafeDispose(); _rotEntry = null; } while ((hr = DirectShowUtil.ReleaseComObject(_graphBuilder)) > 0) ; _graphBuilder = null; } if (_dvbSubRenderer != null) { _dvbSubRenderer.SetPlayer(null); _dvbSubRenderer = null; } if (_vmr9 != null) { _vmr9.SafeDispose(); _vmr9 = null; } GUIGraphicsContext.form.Invalidate(true); _state = PlayState.Init; } catch (Exception ex) { Log.Error("BDPlayer: Exception while cleaning DShow graph - {0} {1}", ex.Message, ex.StackTrace); } //switch back to directx windowed mode ExclusiveMode(false); }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, int iFrameRate, int iWidth, int iHeight, string FileName) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter baseGrabFlt = null; IBaseFilter capFilter = null; IBaseFilter muxFilter = null; IFileSinkFilter fileWriterFilter = null; ICaptureGraphBuilder2 capGraph = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; m_mediaCtrl = m_FilterGraph as IMediaControl; #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif try { // Get the ICaptureGraphBuilder2 capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); // Get the SampleGrabber interface sampGrabber = (ISampleGrabber) new SampleGrabber(); // Start building the graph hr = capGraph.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); // Add the video device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // If any of the default config items are set if (iFrameRate + iHeight + iWidth > 0) { SetConfigParms(capGraph, capFilter, iFrameRate, iWidth, iHeight); } // Create a filter for the output avi file hr = capGraph.SetOutputFileName(MediaSubType.Avi, FileName, out muxFilter, out fileWriterFilter); DsError.ThrowExceptionForHR(hr); // Connect everything together hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, baseGrabFlt, muxFilter); DsError.ThrowExceptionForHR(hr); // Now that sizes are fixed, store the sizes SaveSizeInfo(sampGrabber); } finally { if (fileWriterFilter != null) { Marshal.ReleaseComObject(fileWriterFilter); fileWriterFilter = null; } if (muxFilter != null) { Marshal.ReleaseComObject(muxFilter); muxFilter = null; } if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } } }
private void Cleanup() { Log.Info("DVR2MPG: cleanup"); if (_rotEntry != null) { _rotEntry.SafeDispose(); } _rotEntry = null; if (mediaControl != null) { mediaControl.Stop(); mediaControl = null; } mediaSeeking = null; mediaEvt = null; mediaControl = null; if (powerDvdMuxer != null) DirectShowUtil.ReleaseComObject(powerDvdMuxer); powerDvdMuxer = null; if (fileWriterFilter != null) DirectShowUtil.ReleaseComObject(fileWriterFilter); fileWriterFilter = null; if (bufferSource != null) DirectShowUtil.ReleaseComObject(bufferSource); bufferSource = null; DirectShowUtil.RemoveFilters(graphBuilder); if (graphBuilder != null) DirectShowUtil.ReleaseComObject(graphBuilder); graphBuilder = null; }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(string FileName) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter baseGrabFlt = null; IBaseFilter capFilter = null; IBaseFilter nullrenderer = null; // Get the graphbuilder object m_graphBuilder = new FilterGraph() as IFilterGraph2; m_mediaCtrl = m_graphBuilder as IMediaControl; m_MediaEvent = m_graphBuilder as IMediaEvent; m_MediaSeeking = m_graphBuilder as IMediaSeeking; IMediaFilter mediaFilt = m_graphBuilder as IMediaFilter; try { #if DEBUG m_rot = new DsROTEntry(m_graphBuilder); #endif // Add the video source hr = m_graphBuilder.AddSourceFilter(FileName, "Ds.NET FileFilter", out capFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // --------------------------------- // Connect the file filter to the sample grabber // Hopefully this will be the video pin, we could check by reading it's mediatype IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); // Get the input pin from the sample grabber IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); hr = m_graphBuilder.Connect(iPinOut, iPinIn); DsError.ThrowExceptionForHR(hr); // Add the null renderer to the graph nullrenderer = new NullRenderer() as IBaseFilter; hr = m_graphBuilder.AddFilter(nullrenderer, "Null renderer"); DsError.ThrowExceptionForHR(hr); // --------------------------------- // Connect the sample grabber to the null renderer iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0); hr = m_graphBuilder.Connect(iPinOut, iPinIn); DsError.ThrowExceptionForHR(hr); // Turn off the clock. This causes the frames to be sent // thru the graph as fast as possible hr = mediaFilt.SetSyncSource(null); DsError.ThrowExceptionForHR(hr); // Read and cache the image sizes SaveSizeInfo(sampGrabber); } finally { if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (nullrenderer != null) { Marshal.ReleaseComObject(nullrenderer); nullrenderer = null; } } }
private void PlayMovieInWindow(string filename) { int hr = 0; if (filename == string.Empty) return; this.graphBuilder = (IGraphBuilder) new FilterGraph(); // Have the graph builder construct its the appropriate graph automatically hr = this.graphBuilder.RenderFile(filename, null); DsError.ThrowExceptionForHR(hr); // QueryInterface for DirectShow interfaces this.mediaControl = (IMediaControl) this.graphBuilder; this.mediaEventEx = (IMediaEventEx) this.graphBuilder; this.mediaSeeking = (IMediaSeeking) this.graphBuilder; this.mediaPosition = (IMediaPosition) this.graphBuilder; // Query for video interfaces, which may not be relevant for audio files this.videoWindow = this.graphBuilder as IVideoWindow; this.basicVideo = this.graphBuilder as IBasicVideo; // Query for audio interfaces, which may not be relevant for video-only files this.basicAudio = this.graphBuilder as IBasicAudio; // Is this an audio-only file (no video component)? CheckVisibility(); // Have the graph signal event via window callbacks for performance hr = this.mediaEventEx.SetNotifyWindow(this.Handle, WMGraphNotify, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); if (!this.isAudioOnly) { // Setup the video window hr = this.videoWindow.put_Owner(this.Handle); DsError.ThrowExceptionForHR(hr); hr = this.videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipSiblings | WindowStyle.ClipChildren); DsError.ThrowExceptionForHR(hr); hr = InitVideoWindow(1, 1); DsError.ThrowExceptionForHR(hr); GetFrameStepInterface(); } else { // Initialize the default player size and enable playback menu items hr = InitPlayerWindow(); DsError.ThrowExceptionForHR(hr); EnablePlaybackMenu(true, MediaType.Audio); } // Complete window initialization CheckSizeMenu(menuFileSizeNormal); this.isFullScreen = false; this.currentPlaybackRate = 1.0; UpdateMainTitle(); #if DEBUG rot = new DsROTEntry(this.graphBuilder); #endif this.Focus(); // Run the graph to play the media file hr = this.mediaControl.Run(); DsError.ThrowExceptionForHR(hr); this.currentState=PlayState.Running; }
/// <summary> do cleanup and release DirectShow. </summary> private void CloseInterfaces() { if (graphBuilder == null) { return; } Logger.Instance.Debug("AirplayerAudioPlayer: Close interfaces"); int hr; try { if (mediaCtrl != null) { int counter = 0; FilterState state; hr = mediaCtrl.Stop(); hr = mediaCtrl.GetState(10, out state); while (state != FilterState.Stopped || GUIGraphicsContext.InVmr9Render) { System.Threading.Thread.Sleep(100); hr = mediaCtrl.GetState(10, out state); counter++; if (counter >= 30) { if (state != FilterState.Stopped) { Logger.Instance.Debug("ShairportPlayer: graph still running"); } if (GUIGraphicsContext.InVmr9Render) { Logger.Instance.Debug("ShairportPlayer: in renderer"); } break; } } mediaCtrl = null; } m_state = PlayState.Init; if (mediaEvt != null) { hr = mediaEvt.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero); mediaEvt = null; } mediaSeek = null; mediaPos = null; basicAudio = null; if (graphBuilder != null) { if (_rotEntry != null) { _rotEntry.Dispose(); _rotEntry = null; } Marshal.ReleaseComObject(graphBuilder); graphBuilder = null; } m_state = PlayState.Init; Logger.Instance.Debug("AirplayerAudioPlayer: Interfaces closed"); } catch (Exception ex) { Logger.Instance.Debug("AirplayerAudioPlayer: Exception closing interfaces '{0}'\r\n{1}", ex.Message, ex.StackTrace); } }
// 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 }
private void BuildGraph() { int hr = 0; graphBuilder = (IFilterGraph2) new FilterGraph(); rot = new DsROTEntry(graphBuilder); ICaptureGraphBuilder2 capBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); capBuilder.SetFiltergraph(graphBuilder); // Get the BDA network provider specific for this given network type networkProvider = BDAUtils.GetNetworkProvider(networkType); hr = graphBuilder.AddFilter(networkProvider, "BDA Network Provider"); DsError.ThrowExceptionForHR(hr); tuner = (ITuner)networkProvider; // Get a tuning space for this network type tuningSpace = BDAUtils.GetTuningSpace(networkType); hr = tuner.put_TuningSpace(tuningSpace); DsError.ThrowExceptionForHR(hr); // Create a tune request from this tuning space tuneRequest = BDAUtils.CreateTuneRequest(tuningSpace); // Is it okay ? hr = tuner.Validate(tuneRequest); if (hr == 0) { // Set it hr = tuner.put_TuneRequest(tuneRequest); DsError.ThrowExceptionForHR(hr); // found a BDA Tuner and a BDA Capture that can connect to this network provider BDAUtils.AddBDATunerAndDemodulatorToGraph(graphBuilder, networkProvider, out bdaTuner, out bdaCapture); if ((bdaTuner != null) && (bdaCapture != null)) { // Create and add the mpeg2 demux mpeg2Demux = (IBaseFilter) new MPEG2Demultiplexer(); hr = graphBuilder.AddFilter(mpeg2Demux, "MPEG2 Demultiplexer"); DsError.ThrowExceptionForHR(hr); // connect it to the BDA Capture hr = capBuilder.RenderStream(null, null, bdaCapture, null, mpeg2Demux); DsError.ThrowExceptionForHR(hr); // Add the two mpeg2 transport stream helper filters BDAUtils.AddTransportStreamFiltersToGraph(graphBuilder, out bdaTIF, out bdaSecTab); if ((bdaTIF != null) && (bdaSecTab != null)) { // Render all the output pins of the demux (missing filters are added) for (int i = 0; i < 5; i++) { IPin pin = DsFindPin.ByDirection(mpeg2Demux, PinDirection.Output, i); hr = graphBuilder.Render(pin); Marshal.ReleaseComObject(pin); } mpeg2Data = (IMpeg2Data)bdaSecTab; } } } }
// Build the capture graph for grabber and renderer.</summary> // (Control to show video in, Filename to play) private void SetupGraph2(Control hWin, string FileName, ref bool RateOk) { 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 m_videoWindow = m_FilterGraph as IVideoWindow; ConfigureVideoWindow(m_videoWindow, hWin); // Grab some other interfaces m_mediaEvent = m_FilterGraph as IMediaEvent; m_mediaCtrl = m_FilterGraph as IMediaControl; m_mediaSeeking = m_FilterGraph as IMediaSeeking; try { if (RateOk) { IBaseFilter pAudioRenderer = (IBaseFilter) new DSoundRender(); hr = m_FilterGraph.AddFilter(pAudioRenderer, "Audio Renderer"); DsError.ThrowExceptionForHR(hr); hr = icgb2.RenderStream(null, MediaType.Audio, sourceFilter, null, pAudioRenderer); DsError.ThrowExceptionForHR(hr); m_basicAudio = m_FilterGraph as IBasicAudio; } } catch { } } 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> create the used COM components and get the interfaces. </summary> protected override bool GetInterfaces(string path) { int hr; object comobj = null; _freeNavigator = true; _dvdInfo = null; _dvdCtrl = null; _videoWin = null; string dvdDNavigator = "DVD Navigator"; string aspectRatio = ""; string displayMode = ""; bool turnoffDXVA = false; bool showClosedCaptions = false; int codecValue = 0; string codecType = ""; using (Settings xmlreader = new MPSettings()) { showClosedCaptions = xmlreader.GetValueAsBool("dvdplayer", "showclosedcaptions", false); dvdDNavigator = xmlreader.GetValueAsString("dvdplayer", "navigator", "DVD Navigator"); if (dvdDNavigator.ToLower().Contains("cyberlink dvd navigator")) { _cyberlinkDVDNavigator = true; } aspectRatio = xmlreader.GetValueAsString("dvdplayer", "armode", "").ToLower(); if (aspectRatio == "crop") { arMode = AspectRatioMode.Crop; } else if (aspectRatio == "letterbox") { arMode = AspectRatioMode.LetterBox; } else if (aspectRatio == "stretch") { arMode = AspectRatioMode.Stretched; } else if (aspectRatio == "follow stream") { arMode = AspectRatioMode.StretchedAsPrimary; } displayMode = xmlreader.GetValueAsString("dvdplayer", "displaymode", "").ToLower(); if (displayMode == "default") { _videoPref = DvdPreferredDisplayMode.DisplayContentDefault; } else if (displayMode == "16:9") { _videoPref = DvdPreferredDisplayMode.Display16x9; } else if (displayMode == "4:3 pan scan") { _videoPref = DvdPreferredDisplayMode.Display4x3PanScanPreferred; } else if (displayMode == "4:3 letterbox") { _videoPref = DvdPreferredDisplayMode.Display4x3LetterBoxPreferred; } turnoffDXVA = xmlreader.GetValueAsBool("dvdplayer", "turnoffdxva", true); Log.Info("DVDPlayer9:Turn off DXVA value = {0}", turnoffDXVA); if (turnoffDXVA == true) { codecType = xmlreader.GetValueAsString("dvdplayer", "videocodec", ""); Log.Info("DVDPlayer9:Video Decoder = {0}", codecType); if (codecType == "InterVideo Video Decoder") { codecValue = xmlreader.GetValueAsInt("videocodec", "intervideo", 1); if (codecValue == 1) { Log.Info("DVDPlayer9:Turning InterVideo DXVA off"); using ( RegistryKey subkey = Registry.CurrentUser.CreateSubKey(@"Software\InterVideo\Common\VideoDec\MediaPortal")) { subkey.SetValue("DXVA", 0); } } if (codecValue == 0) { Log.Info("DVDPlayer9:InterVideo DXVA already off"); } } if (codecType.StartsWith("CyberLink Video/SP Decoder")) { codecValue = xmlreader.GetValueAsInt("videocodec", "cyberlink", 1); if (codecValue == 1) { Log.Info("DVDPlayer9:Turning CyberLink DXVA off"); using ( RegistryKey subkey = Registry.CurrentUser.CreateSubKey(@"Software\Cyberlink\Common\CLVSD\MediaPortal")) { subkey.SetValue("UIUseHVA", 0); } } if (codecValue == 0) { Log.Info("DVDPlayer9:CyberLink DXVA already off"); } } if (codecType == "NVIDIA Video Decoder") { codecValue = xmlreader.GetValueAsInt("videocodec", "nvidia", 1); if (codecValue == 1) { Log.Info("DVDPlayer9:Turning NVIDIA DXVA off"); using ( RegistryKey subkey = Registry.LocalMachine.CreateSubKey(@"Software\NVIDIA Corporation\Filters\Video")) { subkey.SetValue("EnableDXVA", 0); } } if (codecValue == 0) { Log.Info("DVDPlayer9:NVIDIA DXVA already off"); } } } } Log.Info("DVDPlayer9:Enabling DX9 exclusive mode"); GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_SWITCH_FULL_WINDOWED, 0, 0, 0, 1, 0, null); GUIWindowManager.SendMessage(msg); try { _dvdGraph = (IDvdGraphBuilder) new DvdGraphBuilder(); hr = _dvdGraph.GetFiltergraph(out _graphBuilder); DsError.ThrowExceptionForHR(hr); _rotEntry = new DsROTEntry((IFilterGraph)_graphBuilder); _vmr9 = new VMR9Util(); _vmr9.AddVMR9(_graphBuilder); _vmr9.Enable(false); try { Log.Info("DVDPlayer9:Add {0}", dvdDNavigator); _dvdbasefilter = DirectShowUtil.AddFilterToGraph(_graphBuilder, dvdDNavigator); if (_dvdbasefilter != null) { AddPreferedCodecs(_graphBuilder); _dvdCtrl = (IDvdControl2)_dvdbasefilter; if (_dvdCtrl != null) { _dvdInfo = (IDvdInfo2)_dvdbasefilter; if (!String.IsNullOrEmpty(path)) { hr = _dvdCtrl.SetDVDDirectory(path); DsError.ThrowExceptionForHR(hr); } _dvdCtrl.SetOption(DvdOptionFlag.HMSFTimeCodeEvents, true); // use new HMSF timecode format _dvdCtrl.SetOption(DvdOptionFlag.ResetOnStop, false); DirectShowUtil.RenderGraphBuilderOutputPins(_graphBuilder, _dvdbasefilter); _freeNavigator = false; } } } catch (Exception ex) { Log.Error("DVDPlayer9:Add {0} as navigator failed: {1}", dvdDNavigator, ex.Message); } if (_dvdInfo == null) { Log.Info("Dvdplayer9:Volume rendered, get interfaces"); hr = _dvdGraph.GetDvdInterface(typeof(IDvdInfo2).GUID, out comobj); DsError.ThrowExceptionForHR(hr); _dvdInfo = (IDvdInfo2)comobj; comobj = null; } if (_dvdCtrl == null) { Log.Info("Dvdplayer9:Get IDvdControl2"); hr = _dvdGraph.GetDvdInterface(typeof(IDvdControl2).GUID, out comobj); DsError.ThrowExceptionForHR(hr); _dvdCtrl = (IDvdControl2)comobj; comobj = null; if (_dvdCtrl != null) { Log.Info("Dvdplayer9:Get IDvdControl2"); } else { Log.Error("Dvdplayer9:Failed to get IDvdControl2"); } } // disable Closed Captions! IBaseFilter basefilter; _graphBuilder.FindFilterByName("Line 21 Decoder", out basefilter); if (basefilter == null) { _graphBuilder.FindFilterByName("Line21 Decoder", out basefilter); } if (basefilter == null) { _graphBuilder.FindFilterByName("Line 21 Decoder 2", out basefilter); } if (basefilter != null) { Log.Info("Dvdplayer9: Line21 Decoder (Closed Captions), in use: {0}", showClosedCaptions); _line21Decoder = (IAMLine21Decoder)basefilter; if (_line21Decoder != null) { AMLine21CCState state = showClosedCaptions ? AMLine21CCState.On : AMLine21CCState.Off; hr = _line21Decoder.SetServiceState(state); if (hr == 0) { Log.Info("DVDPlayer9: Closed Captions state change successful"); } else { Log.Info("DVDPlayer9: Failed to change Closed Captions state"); } } } if (!_vmr9.IsVMR9Connected) { Log.Info("DVDPlayer9:Failed vmr9 not connected"); _mediaCtrl = null; Cleanup(); return(false); } #region PostProcessingEngine Detection IPostProcessingEngine postengine = PostProcessingEngine.GetInstance(true); if (!postengine.LoadPostProcessing(_graphBuilder)) { PostProcessingEngine.engine = new PostProcessingEngine.DummyEngine(); } #endregion _mediaCtrl = (IMediaControl)_graphBuilder; _mediaEvt = (IMediaEventEx)_graphBuilder; _basicAudio = (IBasicAudio)_graphBuilder; _mediaPos = (IMediaPosition)_graphBuilder; _basicVideo = (IBasicVideo2)_graphBuilder; _videoWidth = _vmr9.VideoWidth; _videoHeight = _vmr9.VideoHeight; DirectShowUtil.SetARMode(_graphBuilder, arMode); _vmr9.SetDeinterlaceMode(); _vmr9.Enable(true); Log.Info("Dvdplayer9:Graph created"); _started = true; return(true); } catch (Exception ex) { Log.Error("DvdPlayer9:Exception while creating DShow graph {0} {1}", ex.Message, ex.StackTrace); CloseInterfaces(); return(false); } }
private void BuildGraph(Control hControl) { int hr; DsDevice [] devs; IBaseFilter ibfSource = null; IBaseFilter dmoFilter = null; IBaseFilter ibfRender = null; IDMOWrapperFilter dmoWrapperFilter = null; ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); graphBuilder = (IFilterGraph2) new FilterGraph(); #if DEBUG m_rot = new DsROTEntry(graphBuilder); #endif hr = icgb.SetFiltergraph(graphBuilder); DsError.ThrowExceptionForHR(hr); devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); if (devs.Length < 1) { throw new Exception("This sample requires a capture device. If you don't " + "have a capture device, change the BuildGraph routine to use AddSourceFilter " + "and use a file."); } // Add a source filter hr = graphBuilder.AddSourceFilterForMoniker(devs[0].Mon, null, "Video Input Device", out ibfSource); DsError.ThrowExceptionForHR(hr); // Add a DMO Wrapper Filter dmoFilter = (IBaseFilter) new DMOWrapperFilter(); dmoWrapperFilter = (IDMOWrapperFilter)dmoFilter; // Since I know the guid of the DMO I am looking for, I *could* do this. //hr = dmoWrapperFilter.Init(new Guid("{7EF28FD7-E88F-45bb-9CDD-8A62956F2D75}"), DMOCatergory.VideoEffect); //DMOError.ThrowExceptionForHR(hr); // But it is more useful to show how to scan for the DMO Guid g = FindGuid("DmoFlip", DMOCategory.VideoEffect); hr = dmoWrapperFilter.Init(g, DMOCategory.VideoEffect); DMOError.ThrowExceptionForHR(hr); SetDMOParams(dmoFilter); // Add it to the Graph hr = graphBuilder.AddFilter(dmoFilter, "DMO Filter"); DsError.ThrowExceptionForHR(hr); ibfRender = (IBaseFilter) new VideoRenderer(); hr = graphBuilder.AddFilter(ibfRender, "renderer"); DsError.ThrowExceptionForHR(hr); hr = icgb.RenderStream(null, null, ibfSource, dmoFilter, ibfRender); DsError.ThrowExceptionForHR(hr); ConfigVideo(graphBuilder as IVideoWindow, hControl); Marshal.ReleaseComObject(ibfSource); Marshal.ReleaseComObject(ibfRender); }
/// <summary> /// Initialize the graph /// </summary> public void InitGraph() { if (theDevice == null) { return; } object o; //Create the Graph graphBuilder = (IGraphBuilder) new FilterGraph(); #if DEBUG m_rot = new DsROTEntry(graphBuilder); #endif //Create the Capture Graph Builder ICaptureGraphBuilder2 captureGraphBuilder = null; captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); //Create the media control for controlling the graph mediaControl = (IMediaControl)this.graphBuilder; // Attach the filter graph to the capture graph int hr = captureGraphBuilder.SetFiltergraph(this.graphBuilder); DsError.ThrowExceptionForHR(hr); //Add the Video input device to the graph hr = graphBuilder.AddFilter(theDevice, "source filter"); DsError.ThrowExceptionForHR(hr); //Add the Video compressor filter to the graph if (theCompressor != null) { hr = graphBuilder.AddFilter(theCompressor, "compressor filter"); DsError.ThrowExceptionForHR(hr); } //Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink IBaseFilter mux; IFileSinkFilter sink; hr = captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, textBox1.Text, out mux, out sink); DsError.ThrowExceptionForHR(hr); //Connect the device and compressor to the mux to render the capture part of the graph hr = captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, theDevice, theCompressor, mux); DsError.ThrowExceptionForHR(hr); //Render any preview pin of the device hr = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, theDevice, null, null); DsError.ThrowExceptionForHR(hr); tuner = null; crossbar = null; hr = captureGraphBuilder.FindInterface(null, null, theDevice, typeof(IAMTVTuner).GUID, out o); if (hr >= 0) { tuner = (IAMTVTuner)o; o = null; hr = captureGraphBuilder.FindInterface(null, null, theDevice, typeof(IAMCrossbar).GUID, out o); if (hr >= 0) { crossbar = (IBaseFilter)o; o = null; } } //get the video window from the graph IVideoWindow videoWindow = null; videoWindow = (IVideoWindow)graphBuilder; //Set the owener of the videoWindow to an IntPtr of some sort (the Handle of any control - could be a form / button etc.) hr = videoWindow.put_Owner(panel1.Handle); DsError.ThrowExceptionForHR(hr); //Set the style of the video window hr = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren); DsError.ThrowExceptionForHR(hr); // Position video window in client rect of main application window hr = videoWindow.SetWindowPosition(0, 0, panel1.Width, panel1.Height); DsError.ThrowExceptionForHR(hr); // Make the video window visible hr = videoWindow.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(mux); Marshal.ReleaseComObject(sink); Marshal.ReleaseComObject(captureGraphBuilder); }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(string FileName, Control hWin) { int hr; IBaseFilter ibfRenderer = null; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin iPinInFilter = null; IPin iPinOutFilter = null; IPin iPinInDest = null; IBasicAudio basicAudio = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif try { // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; // Add the video source hr = m_FilterGraph.AddSourceFilter(FileName, "Ds.NET FileFilter", out capFilter); DsError.ThrowExceptionForHR(hr); // Hopefully this will be the video pin IPin iPinOutSource = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); iPinInFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); iPinOutFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); // Add the frame grabber to the graph 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); SaveSizeInfo(sampGrabber); // Set the output window IVideoWindow videoWindow = m_FilterGraph as IVideoWindow; hr = videoWindow.put_Owner(hWin.Handle); DsError.ThrowExceptionForHR(hr); hr = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren | WindowStyle.ClipSiblings); DsError.ThrowExceptionForHR(hr); hr = videoWindow.put_Visible(OABool.True); DsError.ThrowExceptionForHR(hr); //TODO : Need a better way to hide the video in the parent Window Rectangle rc = hWin.ClientRectangle; if (mParentWindowDisplay) hr = videoWindow.SetWindowPosition(0, 0, rc.Right, rc.Bottom); else hr = videoWindow.SetWindowPosition(0, 0, 0, 0); DsError.ThrowExceptionForHR(hr); IGraphBuilder graphBuilder = m_FilterGraph as IGraphBuilder; ICaptureGraphBuilder2 captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); // Attach the filter graph to the capture graph hr = captureGraphBuilder.SetFiltergraph(graphBuilder); DsError.ThrowExceptionForHR(hr); hr = captureGraphBuilder.RenderStream(null, MediaType.Audio, capFilter, null, null); } finally { if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (ibfRenderer != null) { Marshal.ReleaseComObject(ibfRenderer); ibfRenderer = null; } if (iPinInFilter != null) { Marshal.ReleaseComObject(iPinInFilter); iPinInFilter = null; } if (iPinOutFilter != null) { Marshal.ReleaseComObject(iPinOutFilter); iPinOutFilter = null; } if (iPinInDest != null) { Marshal.ReleaseComObject(iPinInDest); iPinInDest = null; } } }
SetNextFilename(string pFile) { int hr; ICaptureGraphBuilder2 pBuilder = null; IBaseFilter pfMux = null; IFileSinkFilter pSink = null; if (pFile != null) { if (m_DeviceSelected) { ReleaseFilenameMembers(); m_pCaptureGraph = (IGraphBuilder) new FilterGraph(); try { m_rot2 = new DsROTEntry(m_pCaptureGraph); // Use the bridge to add the sourcefilter to the graph hr = m_pBridge.InsertSourceFilter(m_pSourceGraphSinkFilter, m_pCaptureGraph, out m_pCaptureGraphSourceFilter); DsError.ThrowExceptionForHR(hr); // use capture graph builder to create mux/writer stage pBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); hr = pBuilder.SetFiltergraph(m_pCaptureGraph); DsError.ThrowExceptionForHR(hr); // create the mux/writer hr = pBuilder.SetOutputFileName(MediaSubType.Avi, pFile, out pfMux, out pSink); DsError.ThrowExceptionForHR(hr); // render source output to mux hr = pBuilder.RenderStream(null, null, m_pCaptureGraphSourceFilter, null, pfMux); DsError.ThrowExceptionForHR(hr); // Store the file name for later use m_strFile = pFile; } catch { ReleaseFilenameMembers(); } finally { if (pBuilder != null) { Marshal.ReleaseComObject(pBuilder); } if (pfMux != null) { Marshal.ReleaseComObject(pfMux); } if (pSink != null) { Marshal.ReleaseComObject(pSink); } } } else { throw new Exception("Device not selected"); } } else { throw new Exception("Invalid parameter"); } }
public void CloseInterfaces() { // Stop previewing data if (this.mediaControl != null) this.mediaControl.StopWhenReady(); this.currentState = PlayState.Stopped; // Stop receiving events if (this.mediaEventEx != null) this.mediaEventEx.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero); // Relinquish ownership (IMPORTANT!) of the video window. // Failing to call put_Owner can lead to assert failures within // the video renderer, as it still assumes that it has a valid // parent window. if(this.videoWindow != null) { this.videoWindow.put_Visible(OABool.False); this.videoWindow.put_Owner(IntPtr.Zero); } // Remove filter graph from the running object table if (rot != null) { rot.Dispose(); rot = null; } // Release DirectShow interfaces Marshal.ReleaseComObject(this.mediaControl); this.mediaControl = null; Marshal.ReleaseComObject(this.mediaEventEx); this.mediaEventEx = null; Marshal.ReleaseComObject(this.videoWindow); this.videoWindow = null; Marshal.ReleaseComObject(this.graphBuilder); this.graphBuilder = null; Marshal.ReleaseComObject(this.captureGraphBuilder); this.captureGraphBuilder = null; }
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); } } }
/// <summary> do cleanup and release DirectShow. </summary> protected virtual void CloseDVDInterfaces() { if (_graphBuilder == null) { return; } int hr; try { logger.Info("DVDPlayer:cleanup DShow graph"); if (_mediaCtrl != null) { hr = _mediaCtrl.Stop(); _mediaCtrl = null; } _state = PlayState.Stopped; _mediaEvt = null; _visible = false; _videoWin = null; // videoStep = null; _dvdCtrl = null; _dvdInfo = null; _basicVideo = null; _basicAudio = null; _mediaPos = null; _mediaSeek = null; _mediaStep = null; //if (_vmr7 != null) //{ // _vmr7.RemoveVMR7(); //} //_vmr7 = null; if (_vmr9Filter != null) { while ((hr = DirectShowUtil.ReleaseComObject(_vmr9Filter)) > 0) { ; } _vmr9Filter = null; } if (_dvdbasefilter != null) { while ((hr = DirectShowUtil.ReleaseComObject(_dvdbasefilter)) > 0) { ; } _dvdbasefilter = null; } if (_cmdOption != null) { DirectShowUtil.ReleaseComObject(_cmdOption); } _cmdOption = null; _pendingCmd = false; if (_line21Decoder != null) { while ((hr = DirectShowUtil.ReleaseComObject(_line21Decoder)) > 0) { ; } _line21Decoder = null; } if (_graphBuilder != null) { DirectShowUtil.RemoveFilters(_graphBuilder); if (_rotEntry != null) { _rotEntry.Dispose(); _rotEntry = null; } while ((hr = DirectShowUtil.ReleaseComObject(_graphBuilder)) > 0) { ; } _graphBuilder = null; } if (_dvdGraph != null) { while ((hr = DirectShowUtil.ReleaseComObject(_dvdGraph)) > 0) { ; } _dvdGraph = null; } _state = PlayState.Init; } catch (Exception ex) { logger.Error("DVDPlayer:exception while cleanuping DShow graph {0} {1}", ex.Message, ex.StackTrace); } }
private void GetTunerCapabilities() { Log.Log.WriteFile("b2c2: GetTunerCapabilities"); _graphBuilder = (IFilterGraph2) new FilterGraph(); _rotEntry = new DsROTEntry(_graphBuilder); _capBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); _capBuilder.SetFiltergraph(_graphBuilder); //========================================================================================================= // add the B2C2 specific filters //========================================================================================================= Log.Log.WriteFile("b2c2:GetTunerCapabilities() create B2C2 adapter"); _filterB2C2Adapter = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_B2C2Adapter, false)) as IBaseFilter; if (_filterB2C2Adapter == null) { Log.Log.Error("b2c2:GetTunerCapabilities() _filterB2C2Adapter not found"); return; } _interfaceB2C2TunerCtrl = _filterB2C2Adapter as IB2C2MPEG2TunerCtrl4; if (_interfaceB2C2TunerCtrl == null) { Log.Log.Error("b2c2: cannot get IB2C2MPEG2TunerCtrl4"); return; } _interfaceB2C2DataCtrl = _filterB2C2Adapter as IB2C2MPEG2DataCtrl6; if (_interfaceB2C2DataCtrl == null) { Log.Log.Error("b2c2: cannot get IB2C2MPEG2DataCtrl6"); return; } int hr = _interfaceB2C2DataCtrl.SelectDevice(_deviceInfo.DeviceId); if (hr != 0) { Log.Log.Error("b2c2: select device failed: {0:X}", hr); } //========================================================================================================= // Get tuner type (DVBS, DVBC, DVBT, ATSC) //========================================================================================================= int lTunerCapSize = Marshal.SizeOf(typeof(tTunerCapabilities)); IntPtr ptCaps = Marshal.AllocHGlobal(lTunerCapSize); hr = _interfaceB2C2TunerCtrl.GetTunerCapabilities(ptCaps, ref lTunerCapSize); if (hr != 0) { Log.Log.Error("b2c2: Tuner Type failed:0x{0:X}", hr); return; } tTunerCapabilities tc = (tTunerCapabilities)Marshal.PtrToStructure(ptCaps, typeof(tTunerCapabilities)); switch (tc.eModulation) { case TunerType.ttSat: Log.Log.WriteFile("b2c2: Card type = DVBS"); _cardType = CardType.DvbS; break; case TunerType.ttCable: Log.Log.WriteFile("b2c2: Card type = DVBC"); _cardType = CardType.DvbC; break; case TunerType.ttTerrestrial: Log.Log.WriteFile("b2c2: Card type = DVBT"); _cardType = CardType.DvbT; break; case TunerType.ttATSC: Log.Log.WriteFile("b2c2: Card type = ATSC"); _cardType = CardType.Atsc; break; case TunerType.ttUnknown: Log.Log.WriteFile("b2c2: Card type = unknown?"); _cardType = CardType.Unknown; break; } Marshal.FreeHGlobal(ptCaps); // Release all used object if (_filterB2C2Adapter != null) { Release.ComObject("tuner filter", _filterB2C2Adapter); _filterB2C2Adapter = null; } _rotEntry.Dispose(); if (_capBuilder != null) { Release.ComObject("capture builder", _capBuilder); _capBuilder = null; } if (_graphBuilder != null) { Release.ComObject("graph builder", _graphBuilder); _graphBuilder = null; } }
private void BuildGraph() { int hr; try { lblTotalTime.Text = mvs.PlayTime.ToString(); TimeSpan tt = TimeSpan.Parse(mvs.PlayTime); DateTime dt = new DateTime(tt.Ticks); lblTotalTime.Text = String.Format("{0:HH:mm:ss}", dt); if (mvs.LocalMedia[0].IsDVD) { mediaToPlay = mvs.LocalMedia[0]; MediaState mediaState = mediaToPlay.State; if (mediaState == MediaState.NotMounted) { MountResult result = mediaToPlay.Mount(); } string videoPath = mediaToPlay.GetVideoPath(); if (videoPath != null) FirstPlayDvd(videoPath); else FirstPlayDvd(mvs.LocalMedia[0].File.FullName); // Add delegates for Windowless operations AddHandlers(); MainForm_ResizeMove(null, null); } else { _graphBuilder = (IFilterGraph2)new FilterGraph(); _rotEntry = new DsROTEntry((IFilterGraph)_graphBuilder); _mediaCtrl = (IMediaControl)_graphBuilder; _mediaSeek = (IMediaSeeking)_graphBuilder; _mediaPos = (IMediaPosition)_graphBuilder; _mediaStep = (IVideoFrameStep)_graphBuilder; _vmr9Filter = (IBaseFilter)new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); AddHandlers(); MainForm_ResizeMove(null, null); hr = _graphBuilder.AddFilter(_vmr9Filter, "Video Mixing Render 9"); AddPreferedCodecs(_graphBuilder); DsError.ThrowExceptionForHR(hr); hr = _graphBuilder.RenderFile(mvs.LocalMedia[0].File.FullName, null); DsError.ThrowExceptionForHR(hr); } } catch (Exception e) { CloseDVDInterfaces(); logger.ErrorException("An error occured during the graph building : \r\n\r\n",e); } }
/// <summary> /// Builds the graph. /// </summary> public override void BuildGraph() { try { Log.Log.WriteFile("b2c2: build graph"); if (_graphState != GraphState.Idle) { Log.Log.Error("b2c2: Graph already built"); throw new TvException("Graph already built"); } DevicesInUse.Instance.Add(_tunerDevice); _managedThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId; _graphBuilder = (IFilterGraph2) new FilterGraph(); _rotEntry = new DsROTEntry(_graphBuilder); _capBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); _capBuilder.SetFiltergraph(_graphBuilder); //========================================================================================================= // add the B2C2 specific filters //========================================================================================================= Log.Log.WriteFile("b2c2:CreateGraph() create B2C2 adapter"); _filterB2C2Adapter = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_B2C2Adapter, false)) as IBaseFilter; Log.Log.WriteFile("b2c2: Filter instance: " + _filterB2C2Adapter); if (_filterB2C2Adapter == null) { Log.Log.Error("b2c2:creategraph() _filterB2C2Adapter not found"); DevicesInUse.Instance.Remove(_tunerDevice); return; } Log.Log.WriteFile("b2c2:creategraph() add filters to graph"); int hr = _graphBuilder.AddFilter(_filterB2C2Adapter, "B2C2-Source"); if (hr != 0) { Log.Log.Error("b2c2: FAILED to add B2C2-Adapter"); DevicesInUse.Instance.Remove(_tunerDevice); return; } // get interfaces _interfaceB2C2TunerCtrl = _filterB2C2Adapter as IB2C2MPEG2TunerCtrl4; if (_interfaceB2C2TunerCtrl == null) { Log.Log.Error("b2c2: cannot get IB2C2MPEG2TunerCtrl4"); DevicesInUse.Instance.Remove(_tunerDevice); return; } _interfaceB2C2DataCtrl = _filterB2C2Adapter as IB2C2MPEG2DataCtrl6; if (_interfaceB2C2DataCtrl == null) { Log.Log.Error("b2c2: cannot get IB2C2MPEG2DataCtrl6"); DevicesInUse.Instance.Remove(_tunerDevice); return; } hr = _interfaceB2C2DataCtrl.SelectDevice(_deviceInfo.DeviceId); if (hr != 0) { Log.Log.Error("b2c2: select device failed: {0:X}", hr); } //========================================================================================================= // initialize B2C2 tuner //========================================================================================================= Log.Log.WriteFile("b2c2: Initialize Tuner()"); hr = _interfaceB2C2TunerCtrl.Initialize(); if (hr != 0) { //System.Diagnostics.Debugger.Launch(); Log.Log.Error("b2c2: Tuner initialize failed:0x{0:X}", hr); // if the B2C2 card is detected as analogue, it needs a device reset ((IMediaControl)_graphBuilder).Stop(); FreeAllSubChannels(); FilterGraphTools.RemoveAllFilters(_graphBuilder); if (_graphBuilder != null) { Release.ComObject("graph builder", _graphBuilder); _graphBuilder = null; } if (_capBuilder != null) { Release.ComObject("capBuilder", _capBuilder); _capBuilder = null; } DevicesInUse.Instance.Remove(_tunerDevice); CardPresent = false; return; } // call checklock once, the return value dont matter hr = _interfaceB2C2TunerCtrl.CheckLock(); AddTsWriterFilterToGraph(); IBaseFilter lastFilter; ConnectInfTeeToB2C2(out lastFilter); AddMdPlugs(ref lastFilter); if (!ConnectTsWriter(lastFilter)) { throw new TvExceptionGraphBuildingFailed("Graph building failed"); } SendHwPids(new List <ushort>()); _graphState = GraphState.Created; } catch (Exception ex) { Log.Log.Write(ex); Dispose(); _graphState = GraphState.Idle; throw new TvExceptionGraphBuildingFailed("Graph building failed", ex); } }
public override bool Play(string strFile) { updateTimer = DateTime.Now; m_speedRate = 10000; GUIGraphicsContext.IsWindowVisible = false; m_iVolume = 100; _state = PlayState.Init; m_strCurrentFile = strFile; m_bFullScreen = true; m_ar = GUIGraphicsContext.ARType; VideoRendererStatistics.VideoState = VideoRendererStatistics.State.VideoPresent; _updateNeeded = true; Log.Info("RTSPPlayer:play {0}", strFile); //lock ( typeof(VideoPlayerVMR7) ) { CloseInterfaces(); m_bStarted = false; if (!GetInterfaces()) { m_strCurrentFile = ""; CloseInterfaces(); return false; } int hr = mediaEvt.SetNotifyWindow(GUIGraphicsContext.ActiveForm, WM_GRAPHNOTIFY, IntPtr.Zero); if (hr < 0) { Error.SetError("Unable to play movie", "Can not set notifications"); m_strCurrentFile = ""; CloseInterfaces(); return false; } DirectShowUtil.SetARMode(graphBuilder, AspectRatioMode.Stretched); _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder); // DsUtils.DumpFilters(graphBuilder); hr = _mediaCtrl.Run(); if (hr < 0) { Error.SetError("Unable to play movie", "Unable to start movie"); m_strCurrentFile = ""; CloseInterfaces(); return false; } GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_PLAYBACK_STARTED, 0, 0, 0, 0, 0, null); msg.Label = strFile; GUIWindowManager.SendThreadMessage(msg); _state = PlayState.Playing; //Brutus GUIGraphicsContext.IsFullScreenVideo=true; m_iPositionX = GUIGraphicsContext.VideoWindow.X; m_iPositionY = GUIGraphicsContext.VideoWindow.Y; m_iWidth = GUIGraphicsContext.VideoWindow.Width; m_iHeight = GUIGraphicsContext.VideoWindow.Height; m_ar = GUIGraphicsContext.ARType; _updateNeeded = true; SetVideoWindow(); mediaPos.get_Duration(out _duration); Log.Info("RTSPPlayer:Duration:{0}", _duration); if (_mediaType == g_Player.MediaType.TV) { //if (_duration < 1) _duration = 1; //SeekAbsolute(_duration - 1); } else { //SeekAbsolute(0); } OnInitialized(); } // Wait for a while to wait VMR9 to get ready. // Implemented due to problems starting to play before VMR9 was ready resulting in black screen. Thread.Sleep(200); return true; }
public void Open(bool run = true) { DsDevice device = null; foreach (DsDevice d in DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice)) { if (d.Name == this.Name) { device = d; break; } } if (device == null) { throw new NullReferenceException("DsDevice"); } try { // Create this.captureGraphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; if (this.captureGraphBuilder == null) { throw new NullReferenceException("ICaptureGraphBuilder2"); } this.filterGraph = new FilterGraph() as IFilterGraph2; if (this.filterGraph == null) { throw new NullReferenceException("IFilterGraph2"); } this.mediaControl = this.filterGraph as IMediaControl; if (this.mediaControl == null) { throw new NullReferenceException("IMediaControl"); } // Filter Graph (Video Capture -> Sample Grabber -> Null Renderer) int hr = this.captureGraphBuilder.SetFiltergraph(this.filterGraph); if (hr < 0) { throw new COMException("ICaptureGraphBuilder2::SetFiltergraph", hr); } // Video Capture hr = this.filterGraph.AddSourceFilterForMoniker(device.Mon, null, device.Name, out this.videoCapture); if (hr < 0) { throw new COMException("IFilterGraph2::AddSourceFilterForMoniker", hr); } if (run) { hr = this.captureGraphBuilder.FindInterface(PinCategory.Capture, DirectShowLib.MediaType.Video, this.videoCapture, typeof(IAMStreamConfig).GUID, out object intrface); if (hr < 0) { throw new COMException("ICaptureGraphBuilder2::FindInterface::IAMStreamConfig", hr); } IAMStreamConfig streamConfig = intrface as IAMStreamConfig; if (streamConfig == null) { throw new NullReferenceException("IAMStreamConfig"); } hr = streamConfig.GetFormat(out AMMediaType media); if (hr < 0) { throw new COMException("IAMStreamConfig::GetFormat", hr); } if (this.MediaType == null) { this.MediaType = new VideoInfoHeader(); Marshal.PtrToStructure(media.formatPtr, this.MediaType); DsUtils.FreeAMMediaType(media); media = null; } else { Marshal.StructureToPtr(this.MediaType, media.formatPtr, false); hr = streamConfig.SetFormat(media); DsUtils.FreeAMMediaType(media); media = null; if (hr < 0) { throw new COMException("IAMStreamConfig::SetFormat", hr); } } this.Width = this.MediaType.BmiHeader.Width; this.Height = this.MediaType.BmiHeader.Height; this.FrameRate = 10000000.0 / this.MediaType.AvgTimePerFrame; // Sample Grabber ISampleGrabber sampleGrabber = new SampleGrabber() as ISampleGrabber; media = new AMMediaType(); media.majorType = DirectShowLib.MediaType.Video; media.subType = MediaSubType.RGB24; media.formatType = FormatType.VideoInfo; hr = sampleGrabber.SetMediaType(media); DsUtils.FreeAMMediaType(media); media = null; if (hr < 0) { throw new COMException("ISampleGrabber::SetMediaType", hr); } hr = sampleGrabber.SetCallback(this, 1); if (hr < 0) { throw new COMException("ISampleGrabber::SetCallback", hr); } hr = this.filterGraph.AddFilter(sampleGrabber as IBaseFilter, "SampleGrabber"); if (hr < 0) { throw new COMException("IFilterGraph2::AddFilter::SampleGrabber", hr); } // Null Renderer NullRenderer nullRenderer = new NullRenderer(); hr = this.filterGraph.AddFilter(nullRenderer as IBaseFilter, "NullRenderer"); if (hr < 0) { throw new COMException("IFilterGraph2::AddFilter::NullRenderer", hr); } hr = this.captureGraphBuilder.RenderStream(PinCategory.Capture, DirectShowLib.MediaType.Video, this.videoCapture, sampleGrabber as IBaseFilter, nullRenderer as IBaseFilter); if (hr < 0) { throw new COMException("ICaptureGraphBuilder2::RenderStream", hr); } // ROT (Running Object Table) Entry this.rotEntry = new DsROTEntry(this.filterGraph); // Frames this.frames = new LinkedList <Frame>(); for (int b = 0; b < this.Backtrace; b++) { this.frames.AddLast(new Frame()); } // Run Filter Graph hr = this.mediaControl.Run(); if (hr < 0) { throw new COMException("IMediaControl::Run", hr); } } } catch (Exception e) { this.Close(); throw e; } }
private void InitializeGraph(bool isDvd) { int hr = 0; _isDvd = isDvd; m_filterGraph = new FilterGraphNoThread(); m_graph = (m_filterGraph as DirectShowLib.IGraphBuilder); // QueryInterface for DirectShow interfaces _mediaControl = (DirectShowLib.IMediaControl)m_graph; _mediaEventEx = (DirectShowLib.IMediaEventEx)m_graph; _mediaSeeking = (DirectShowLib.IMediaSeeking)m_graph; _mediaPosition = (DirectShowLib.IMediaPosition)m_graph; // Query for video interfaces, which may not be relevant for audio files _videoWindow = m_graph as DirectShowLib.IVideoWindow; _basicVideo = m_graph as DirectShowLib.IBasicVideo; // Query for audio interfaces, which may not be relevant for video-only files _basicAudio = m_graph as DirectShowLib.IBasicAudio; // Set up event notification. if (isDvd) hr = _mediaEventEx.SetNotifyWindow(VideoWindowHandle, DirectShowPlayerConfiguration.WM_DVD_EVENT, IntPtr.Zero); else hr = _mediaEventEx.SetNotifyWindow(VideoWindowHandle, DirectShowPlayerConfiguration.WM_GRAPH_NOTIFY, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); if (_config.PublishGraph) m_dsRot = new DsROTEntry(m_graph as IFilterGraph); }
/// <summary> /// Configures the DirectShow graph to play the selected video capture /// device with the selected parameters /// </summary> private void SetupGraph() { /* Clean up any messes left behind */ FreeResources(); try { logger.Info("Graph Setup"); /* Create a new graph */ m_pBridge = (IGMFBridgeController) new GMFBridgeController(); int hr = m_pBridge.AddStream(true, eFormatType.MuxInputs, true); DsError.ThrowExceptionForHR(hr); m_graph = (IGraphBuilder) new FilterGraphNoThread(); #if DEBUG m_rotEntry = new DsROTEntry(m_graph); #endif /* Create a capture graph builder to help * with rendering a capture graph */ var graphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); hr = m_pBridge.InsertSinkFilter(m_graph, out m_pSourceGraphSinkFilter); DsError.ThrowExceptionForHR(hr); /* Set our filter graph to the capture graph */ logger.Info("VideoCaptureSource:" + VideoCaptureSource); if (VideoCaptureDevice != null) { logger.Info("VideoCaptureDevice.DevicePath:" + VideoCaptureDevice.DevicePath); } /* Add our capture device source to the graph */ if (m_videoCaptureSourceChanged) { m_captureDevice = AddFilterByName(m_graph, FilterCategory.VideoInputDevice, VideoCaptureSource); m_videoCaptureSourceChanged = false; } else if (m_videoCaptureDeviceChanged) { m_captureDevice = AddFilterByDevicePath(m_graph, FilterCategory.VideoInputDevice, VideoCaptureDevice.DevicePath); m_videoCaptureDeviceChanged = false; } /* If we have a null capture device, we have an issue */ if (m_captureDevice == null) { throw new Exception(string.Format("Capture device {0} not found or could not be created", VideoCaptureSource)); } object crossbar; var a = graphBuilder.FindInterface(null, null, m_captureDevice as IBaseFilter, typeof(IAMCrossbar).GUID, out crossbar); if (UseYuv && !EnableSampleGrabbing) { /* Configure the video output pin with our parameters and if it fails * then just use the default media subtype*/ if (!SetVideoCaptureParameters(graphBuilder, m_captureDevice, MediaSubType.YUY2)) { SetVideoCaptureParameters(graphBuilder, m_captureDevice, Guid.Empty); } } else { /* Configure the video output pin with our parameters */ SetVideoCaptureParameters(graphBuilder, m_captureDevice, new Guid("73646976-0000-0010-8000-00AA00389B71")); } var rendererType = VideoRendererType.VideoMixingRenderer9; /* Creates a video renderer and register the allocator with the base class */ m_renderer = CreateVideoRenderer(rendererType, m_graph, 1); if (rendererType == VideoRendererType.VideoMixingRenderer9) { var mixer = m_renderer as IVMRMixerControl9; if (mixer != null && !EnableSampleGrabbing && UseYuv) { VMR9MixerPrefs dwPrefs; mixer.GetMixingPrefs(out dwPrefs); dwPrefs &= ~VMR9MixerPrefs.RenderTargetMask; dwPrefs |= VMR9MixerPrefs.RenderTargetYUV; /* Prefer YUV */ mixer.SetMixingPrefs(dwPrefs); } } if (EnableSampleGrabbing) { m_sampleGrabber = (ISampleGrabber) new SampleGrabber(); SetupSampleGrabber(m_sampleGrabber); hr = m_graph.AddFilter(m_sampleGrabber as IBaseFilter, "SampleGrabber"); DsError.ThrowExceptionForHR(hr); } hr = graphBuilder.SetFiltergraph(m_graph); DsError.ThrowExceptionForHR(hr); IBaseFilter mux = null; IFileSinkFilter sink = null; if (!string.IsNullOrEmpty(this.fileName)) { } hr = graphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, m_captureDevice, null, m_renderer); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, m_captureDevice, null, m_pSourceGraphSinkFilter); DsError.ThrowExceptionForHR(hr); hr = graphBuilder.FindPin(m_captureDevice, PinDirection.Output, PinCategory.Capture, MediaType.Video, false, 0, out m_pCapOutput); if (hr >= 0) { IAMStreamControlBridge pSC = (IAMStreamControlBridge)m_pCapOutput; pSC.StartAt(NEVER, 0); // Ignore any error } /* Register the filter graph * with the base classes */ SetupFilterGraph(m_graph); /* Sets the NaturalVideoWidth/Height */ SetNativePixelSizes(m_renderer); HasVideo = true; /* Make sure we Release() this COM reference */ if (mux != null) { Marshal.ReleaseComObject(mux); } if (sink != null) { Marshal.ReleaseComObject(sink); } Marshal.ReleaseComObject(graphBuilder); } catch (Exception ex) { /* Something got fuct up */ FreeResources(); InvokeMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); } /* Success */ InvokeMediaOpened(); }
protected bool GetInterfaces(string filename, int titleBD) { try { Log.Debug("BDPlayer: GetInterfaces()"); _graphBuilder = (IGraphBuilder)new FilterGraph(); _rotEntry = new DsROTEntry(_graphBuilder as IFilterGraph); filterConfig = GetFilterConfiguration(); if (filterConfig.AudioRenderer.Length > 0) { _audioRendererFilter = DirectShowUtil.AddAudioRendererToGraph(_graphBuilder, filterConfig.AudioRenderer, true); } BDReader reader = new BDReader(); _interfaceBDReader = reader as IBaseFilter; _ireader = reader as IBDReader; if (_interfaceBDReader == null || _ireader == null) { // todo: add exception return false; } // add the BD reader int hr = _graphBuilder.AddFilter(_interfaceBDReader, BD_READER_GRAPH_NAME); DsError.ThrowExceptionForHR(hr); Log.Debug("BDPlayer: Add BDReader to graph"); IFileSourceFilter interfaceFile = (IFileSourceFilter)_interfaceBDReader; LoadSettings(_ireader); _ireader.SetD3DDevice(DirectShowUtil.GetUnmanagedDevice(GUIGraphicsContext.DX9Device)); _ireader.SetBDReaderCallback(this); hr = interfaceFile.Load(filename, null); DsError.ThrowExceptionForHR(hr); Log.Debug("BDPlayer: BDReader loaded: {0}", filename); List<TitleInfo> titles = GetTitleInfoCollection(_ireader); while (true) { if (g_Player.ForcePlay && g_Player.SetResumeBDTitleState < g_Player.BdDefaultTitle) { if (titles.Count == 1) { _titleToPlay = 0; g_Player.SetResumeBDTitleState = g_Player.BdRemuxTitle; } else { _titleToPlay = g_Player.SetResumeBDTitleState; } _forceTitle = true; g_Player.ForcePlay = false; } else { if (titles.Count == 1) { // BD has only one title (remux one) _forceTitle = true; _titleToPlay = 0; g_Player.SetResumeBDTitleState = g_Player.BdRemuxTitle; if (g_Player.SetResumeBDTitleState == -1) { // user cancelled dialog titles.Dispose(); g_Player.Stop(); return false; } } else { _titleToPlay = SelectTitle(titles); g_Player.SetResumeBDTitleState = _titleToPlay; Log.Info("BDPlayer: BDReader _titleToPlay : {0}", _titleToPlay); if (_titleToPlay > -1) { // a specific title was selected _forceTitle = true; if (g_Player.SetResumeBDTitleState == -1) { // user cancelled dialog titles.Dispose(); g_Player.Stop(); return false; } } else { if (_titleToPlay == -1) { // user cancelled dialog g_Player.Stop(); titles.Dispose(); return false; } // user choose to display menu _forceTitle = false; } } } _ireader.ForceTitleBasedPlayback(_forceTitle, (uint)_titleToPlay); Log.Debug("BDPlayer: Starting BDReader"); eventBuffer.Clear(); hr = _ireader.Start(); if (hr != 0) { if (!_forceTitle) { Log.Error("BDPlayer: Failed to start file:{0} :0x{1:x}", filename, hr); continue; } Log.Error("BDPlayer: Failed to start in title based mode file:{0} :0x{1:x}", filename, hr); titles.Dispose(); return false; } else { Log.Info("BDPlayer: BDReader started"); } break; } titles.Dispose(); #region Filters Log.Info("BDPlayer: Adding filters"); _vmr9 = new VMR9Util(); _vmr9.AddVMR9(_graphBuilder); _vmr9.Enable(false); // Set VideoDecoder and VC1Override before adding filter in graph SetVideoDecoder(); SetVC1Override(); // Add preferred video filters UpdateFilters("Video"); // Add preferred audio filters UpdateFilters("Audio"); // Let the subtitle engine handle the proper filters try { SubtitleRenderer.GetInstance().AddSubtitleFilter(_graphBuilder); } catch (Exception e) { Log.Error(e); } #endregion #region PostProcessingEngine Detection IPostProcessingEngine postengine = PostProcessingEngine.GetInstance(true); if (!postengine.LoadPostProcessing(_graphBuilder)) { PostProcessingEngine.engine = new PostProcessingEngine.DummyEngine(); } #endregion #region render BDReader output pins Log.Info("BDPlayer: Render BDReader outputs"); if (_interfaceBDReader != null) { DirectShowUtil.RenderGraphBuilderOutputPins(_graphBuilder, _interfaceBDReader); } //remove InternalScriptRenderer as it takes subtitle pin disableISR(); //disable Closed Captions! disableCC(); //RemoveAudioR(); DirectShowUtil.RemoveUnusedFiltersFromGraph(_graphBuilder); #endregion _mediaCtrl = (IMediaControl)_graphBuilder; _mediaEvt = (IMediaEventEx)_graphBuilder; _mediaSeeking = (IMediaSeeking)_graphBuilder; try { SubtitleRenderer.GetInstance().SetPlayer(this); _dvbSubRenderer = SubtitleRenderer.GetInstance(); } catch (Exception e) { Log.Error(e); } _subtitleStream = (Player.TSReaderPlayer.ISubtitleStream)_interfaceBDReader; if (_subtitleStream == null) { Log.Error("BDPlayer: Unable to get ISubtitleStream interface"); } // if only dvb subs are enabled, pass null for ttxtDecoder _subSelector = new SubtitleSelector(_subtitleStream, _dvbSubRenderer, null); EnableSubtitle = _subtitlesEnabled; //Sync Audio Renderer SyncAudioRenderer(); if (!_vmr9.IsVMR9Connected) { Log.Error("BDPlayer: Failed vmr9 not connected"); return false; } _vmr9.SetDeinterlaceMode(); return true; } catch (Exception ex) { Log.Error("BDPlayer: Exception while creating DShow graph {0}", ex.Message); return false; } }
protected override void FreeResources() { /* We run the StopInternal() to avoid any * Dispatcher VeryifyAccess() issues */ StopInternal(); /* Let's clean up the base * class's stuff first */ base.FreeResources(); #if DEBUG if (m_rotEntry != null) { m_rotEntry.Dispose(); } m_rotEntry = null; #endif if (m_videoFrame != null) { m_videoFrame.Dispose(); m_videoFrame = null; } if (m_renderer != null) { Marshal.FinalReleaseComObject(m_renderer); m_renderer = null; } if (m_captureDevice != null) { Marshal.FinalReleaseComObject(m_captureDevice); m_captureDevice = null; } if (m_sampleGrabber != null) { Marshal.FinalReleaseComObject(m_sampleGrabber); m_sampleGrabber = null; } if (m_graph != null) { Marshal.FinalReleaseComObject(m_graph); m_graph = null; InvokeMediaClosed(new EventArgs()); } if (m_pBridge != null) { Marshal.ReleaseComObject(m_pBridge); m_pBridge = null; } if (m_pSourceGraphSinkFilter != null) { Marshal.ReleaseComObject(m_pSourceGraphSinkFilter); m_pSourceGraphSinkFilter = null; } if (m_pSourceGraphSinkFilter != null) { Marshal.ReleaseComObject(m_pSourceGraphSinkFilter); m_pSourceGraphSinkFilter = null; } }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(string FileName) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter baseGrabFlt = null; IBaseFilter capFilter = null; IBaseFilter nullrenderer = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; m_mediaCtrl = m_FilterGraph as IMediaControl; m_MediaEvent = m_FilterGraph as IMediaEvent; IMediaFilter mediaFilt = m_FilterGraph as IMediaFilter; try { #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif // Add the video source hr = m_FilterGraph.AddSourceFilter(FileName, "Ds.NET FileFilter", out capFilter); DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); // --------------------------------- // Connect the file filter to the sample grabber // Hopefully this will be the video pin, we could check by reading it's mediatype IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); // Get the input pin from the sample grabber IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); hr = m_FilterGraph.Connect(iPinOut, iPinIn); DsError.ThrowExceptionForHR(hr); // Add the null renderer to the graph nullrenderer = new NullRenderer() as IBaseFilter; hr = m_FilterGraph.AddFilter(nullrenderer, "Null renderer"); DsError.ThrowExceptionForHR(hr); // --------------------------------- // Connect the sample grabber to the null renderer iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0); hr = m_FilterGraph.Connect(iPinOut, iPinIn); DsError.ThrowExceptionForHR(hr); // Turn off the clock. This causes the frames to be sent // thru the graph as fast as possible hr = mediaFilt.SetSyncSource(null); DsError.ThrowExceptionForHR(hr); // Read and cache the image sizes SaveSizeInfo(sampGrabber); } finally { if (capFilter != null) { Marshal.ReleaseComObject(capFilter); capFilter = null; } if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (nullrenderer != null) { Marshal.ReleaseComObject(nullrenderer); nullrenderer = null; } } }
private void CloseInterfaces() { ////stop previewing data if ((this.MediaControl != null)) { this.MediaControl.StopWhenReady(); } this.CurrentState = PlayState.Stopped; ////stop recieving events if ((this.MediaEventEx != null)) { this.MediaEventEx.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero); } //// Relinquish ownership (IMPORTANT!) of the video window. //// Failing to call put_Owner can lead to assert failures within //// the video renderer, as it still assumes that it has a valid //// parent window. if ((this.VideoWindow != null)) { this.VideoWindow.put_Visible(OABool.False); this.VideoWindow.put_Owner(IntPtr.Zero); } // // Remove filter graph from the running object table if ((rot != null)) { rot.Dispose(); rot = null; } //// Release DirectShow interfaces if (this.MediaControl != null) { Marshal.ReleaseComObject(this.MediaControl); this.MediaControl = null; } if (this.MediaEventEx != null) { Marshal.ReleaseComObject(this.MediaEventEx); this.MediaEventEx = null; } if (this.VideoWindow != null) { Marshal.ReleaseComObject(this.VideoWindow); this.VideoWindow = null; } if (this.GraphBuilder != null) { Marshal.ReleaseComObject(this.GraphBuilder); this.GraphBuilder = null; } if (this.CaptureGraphBuilder != null) { Marshal.ReleaseComObject(this.CaptureGraphBuilder); this.CaptureGraphBuilder = null; } }
/// <summary> build the capture graph for grabber. </summary> private void SetupGraph(DsDevice dev, int iWidth, int iHeight, short iBPP, Control hControl) { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin pCaptureOut = null; IPin pSampleIn = null; IPin pRenderIn = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; try { #if DEBUG m_rot = new DsROTEntry(m_FilterGraph); #endif // add the video input device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); // Find the still pin m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0); // Didn't find one. Is there a preview pin? if (m_pinStill == null) { m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0); } // Still haven't found one. Need to put a splitter in so we have // one stream to capture the bitmap from, and one to display. Ok, we // don't *have* to do it that way, but we are going to anyway. if (m_pinStill == null) { IPin pRaw = null; IPin pSmart = null; // There is no still pin m_VidControl = null; // Add a splitter IBaseFilter iSmartTee = (IBaseFilter)new SmartTee(); try { hr = m_FilterGraph.AddFilter(iSmartTee, "SmartTee"); DsError.ThrowExceptionForHR(hr); // Find the find the capture pin from the video device and the // input pin for the splitter, and connnect them pRaw = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); pSmart = DsFindPin.ByDirection(iSmartTee, PinDirection.Input, 0); hr = m_FilterGraph.Connect(pRaw, pSmart); DsError.ThrowExceptionForHR(hr); // Now set the capture and still pins (from the splitter) m_pinStill = DsFindPin.ByName(iSmartTee, "Preview"); pCaptureOut = DsFindPin.ByName(iSmartTee, "Capture"); // If any of the default config items are set, perform the config // on the actual video device (rather than the splitter) if (iHeight + iWidth + iBPP > 0) { SetConfigParms(pRaw, iWidth, iHeight, iBPP); } } finally { if (pRaw != null) { Marshal.ReleaseComObject(pRaw); } if (pRaw != pSmart) { Marshal.ReleaseComObject(pSmart); } if (pRaw != iSmartTee) { Marshal.ReleaseComObject(iSmartTee); } } } else { // Get a control pointer (used in Click()) m_VidControl = capFilter as IAMVideoControl; pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); // If any of the default config items are set if (iHeight + iWidth + iBPP > 0) { SetConfigParms(m_pinStill, iWidth, iHeight, iBPP); } } // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; // Configure the sample grabber IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); // Get the default video renderer IBaseFilter pRenderer = new VideoRendererDefault() as IBaseFilter; hr = m_FilterGraph.AddFilter(pRenderer, "Renderer"); DsError.ThrowExceptionForHR(hr); pRenderIn = DsFindPin.ByDirection(pRenderer, PinDirection.Input, 0); // Add the sample grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); if (m_VidControl == null) { // Connect the Still pin to the sample grabber hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); // Connect the capture pin to the renderer hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn); DsError.ThrowExceptionForHR(hr); } else { // Connect the capture pin to the renderer hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn); DsError.ThrowExceptionForHR(hr); // Connect the Still pin to the sample grabber hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); } // Learn the video properties SaveSizeInfo(sampGrabber); ConfigVideoWindow(hControl); // Start the graph IMediaControl mediaCtrl = m_FilterGraph as IMediaControl; hr = mediaCtrl.Run(); DsError.ThrowExceptionForHR(hr); } finally { if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (pCaptureOut != null) { Marshal.ReleaseComObject(pCaptureOut); pCaptureOut = null; } if (pRenderIn != null) { Marshal.ReleaseComObject(pRenderIn); pRenderIn = null; } if (pSampleIn != null) { Marshal.ReleaseComObject(pSampleIn); pSampleIn = null; } } }
private void SetupGraph(DsDevice dev, int iWidth, int iHeight, short iBPP, Control hControl) { int hr; IAMVideoControl m_VidControl = null; IFilterGraph2 m_FilterGraph = null; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin pCaptureOut = null; IPin pSampleIn = null; IPin pRenderIn = null; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; try { #if DEBUG DsROTEntry m_rot = new DsROTEntry(m_FilterGraph); #endif // add the video input device hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); // Find the still pin IPin m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0); // Didn't find one. Is there a preview pin? if (m_pinStill == null) { m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0); } // Still haven't found one. Need to put a splitter in so we have // one stream to capture the bitmap from, and one to display. Ok, we // don't *have* to do it that way, but we are going to anyway. if (m_pinStill == null) { IPin pRaw = null; IPin pSmart = null; // There is no still pin m_VidControl = null; // Add a splitter IBaseFilter iSmartTee = (IBaseFilter) new SmartTee(); try { hr = m_FilterGraph.AddFilter(iSmartTee, "SmartTee"); DsError.ThrowExceptionForHR(hr); // Find the find the capture pin from the video device and the // input pin for the splitter, and connnect them pRaw = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); pSmart = DsFindPin.ByDirection(iSmartTee, PinDirection.Input, 0); hr = m_FilterGraph.Connect(pRaw, pSmart); DsError.ThrowExceptionForHR(hr); // Now set the capture and still pins (from the splitter) m_pinStill = DsFindPin.ByName(iSmartTee, "Preview"); pCaptureOut = DsFindPin.ByName(iSmartTee, "Capture"); // If any of the default config items are set, perform the config // on the actual video device (rather than the splitter) if (iHeight + iWidth + iBPP > 0) { //SetConfigParms(pRaw, iWidth, iHeight, iBPP); } } finally { if (pRaw != null) { Marshal.ReleaseComObject(pRaw); } if (pRaw != pSmart) { Marshal.ReleaseComObject(pSmart); } if (pRaw != iSmartTee) { Marshal.ReleaseComObject(iSmartTee); } } } else { // Get a control pointer (used in Click()) m_VidControl = capFilter as IAMVideoControl; pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); // If any of the default config items are set if (iHeight + iWidth + iBPP > 0) { //SetConfigParms(m_pinStill, iWidth, iHeight, iBPP); } } // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; // Configure the sample grabber IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; //ConfigureSampleGrabber(sampGrabber); pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); // Get the default video renderer IBaseFilter pRenderer = new VideoRendererDefault() as IBaseFilter; hr = m_FilterGraph.AddFilter(pRenderer, "Renderer"); DsError.ThrowExceptionForHR(hr); pRenderIn = DsFindPin.ByDirection(pRenderer, PinDirection.Input, 0); // Add the sample grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); /*if (m_VidControl == null) * { * // Connect the Still pin to the sample grabber * hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); * DsError.ThrowExceptionForHR(hr); * * // Connect the capture pin to the renderer * hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn); * DsError.ThrowExceptionForHR(hr); * } * else * { * // Connect the capture pin to the renderer * hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn); * DsError.ThrowExceptionForHR(hr); * * // Connect the Still pin to the sample grabber * hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); * DsError.ThrowExceptionForHR(hr); * }*/ // Learn the video properties //SaveSizeInfo(sampGrabber); //ConfigVideoWindow(hControl); // Start the graph IMediaControl mediaCtrl = m_FilterGraph as IMediaControl; hr = mediaCtrl.Run(); DsError.ThrowExceptionForHR(hr); } finally { if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (pCaptureOut != null) { Marshal.ReleaseComObject(pCaptureOut); pCaptureOut = null; } if (pRenderIn != null) { Marshal.ReleaseComObject(pRenderIn); pRenderIn = null; } if (pSampleIn != null) { Marshal.ReleaseComObject(pSampleIn); pSampleIn = null; } } }
public bool Transcode(TranscodeInfo info, MediaPortal.Core.Transcoding.VideoFormat format, MediaPortal.Core.Transcoding.Quality quality, Standard standard) { if (!Supports(format)) return false; string ext = System.IO.Path.GetExtension(info.file); if (ext.ToLower() != ".dvr-ms" && ext.ToLower() != ".sbe") return false; //Type comtype = null; //object comobj = null; try { Log.Info("DVR2MPG: create graph"); graphBuilder = (IGraphBuilder)new FilterGraph(); _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder); Log.Info("DVR2MPG: add streambuffersource"); bufferSource = (IStreamBufferSource)new StreamBufferSource(); IBaseFilter filter = (IBaseFilter)bufferSource; graphBuilder.AddFilter(filter, "SBE SOURCE"); Log.Info("DVR2MPG: load file:{0}", info.file); IFileSourceFilter fileSource = (IFileSourceFilter)bufferSource; int hr = fileSource.Load(info.file, null); Log.Info("DVR2MPG: Add Cyberlink MPEG2 multiplexer to graph"); string monikerPowerDvdMuxer = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{7F2BBEAF-E11C-4D39-90E8-938FB5A86045}"; powerDvdMuxer = Marshal.BindToMoniker(monikerPowerDvdMuxer) as IBaseFilter; if (powerDvdMuxer == null) { Log.Warn("DVR2MPG: FAILED:Unable to create Cyberlink MPEG Muxer (PowerDVD)"); Cleanup(); return false; } hr = graphBuilder.AddFilter(powerDvdMuxer, "PDR MPEG Muxer"); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:Add Cyberlink MPEG Muxer to filtergraph :0x{0:X}", hr); Cleanup(); return false; } //add filewriter Log.Info("DVR2MPG: Add FileWriter to graph"); string monikerFileWrite = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{3E8868CB-5FE8-402C-AA90-CB1AC6AE3240}"; IBaseFilter fileWriterbase = Marshal.BindToMoniker(monikerFileWrite) as IBaseFilter; if (fileWriterbase == null) { Log.Warn("DVR2MPG: FAILED:Unable to create FileWriter"); Cleanup(); return false; } fileWriterFilter = fileWriterbase as IFileSinkFilter; if (fileWriterFilter == null) { Log.Warn("DVR2MPG: FAILED:Add unable to get IFileSinkFilter for filewriter"); Cleanup(); return false; } hr = graphBuilder.AddFilter(fileWriterbase, "FileWriter"); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:Add FileWriter to filtergraph :0x{0:X}", hr); Cleanup(); return false; } //connect output #0 of streambuffer source->powerdvd audio in //connect output #1 of streambuffer source->powerdvd video in Log.Info("DVR2MPG: connect streambuffer->multiplexer"); IPin pinOut0, pinOut1; IPin pinIn0, pinIn1; pinOut0 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 0); pinOut1 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 1); pinIn0 = DsFindPin.ByDirection(powerDvdMuxer, PinDirection.Input, 0); pinIn1 = DsFindPin.ByDirection(powerDvdMuxer, PinDirection.Input, 1); if (pinOut0 == null || pinOut1 == null || pinIn0 == null || pinIn1 == null) { Log.Warn("DVR2MPG: FAILED:unable to get pins of muxer&source"); Cleanup(); return false; } bool usingAc3 = false; AMMediaType amAudio = new AMMediaType(); amAudio.majorType = MediaType.Audio; amAudio.subType = MediaSubType.Mpeg2Audio; hr = pinOut0.Connect(pinIn1, amAudio); if (hr != 0) { amAudio.subType = MediaSubType.DolbyAC3; hr = pinOut0.Connect(pinIn1, amAudio); usingAc3 = true; } if (hr != 0) { Log.Warn("DVR2MPG: FAILED: unable to connect audio pins: 0x{0:X}", hr); Cleanup(); return false; } if (usingAc3) Log.Info("DVR2MPG: using AC3 audio"); else Log.Info("DVR2MPG: using MPEG audio"); AMMediaType amVideo = new AMMediaType(); amVideo.majorType = MediaType.Video; amVideo.subType = MediaSubType.Mpeg2Video; hr = pinOut1.Connect(pinIn0, amVideo); if (hr != 0) { Log.Warn("DVR2MPG: FAILED: unable to connect video pins: 0x{0:X}", hr); Cleanup(); return false; } //connect output of powerdvd muxer->input of filewriter Log.Info("DVR2MPG: connect multiplexer->filewriter"); IPin pinOut, pinIn; pinOut = DsFindPin.ByDirection(powerDvdMuxer, PinDirection.Output, 0); if (pinOut == null) { Log.Warn("DVR2MPG: FAILED:cannot get output pin of Cyberlink MPEG muxer :0x{0:X}", hr); Cleanup(); return false; } pinIn = DsFindPin.ByDirection(fileWriterbase, PinDirection.Input, 0); if (pinIn == null) { Log.Warn("DVR2MPG: FAILED:cannot get input pin of Filewriter :0x{0:X}", hr); Cleanup(); return false; } AMMediaType mt = new AMMediaType(); hr = pinOut.Connect(pinIn, mt); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:connect muxer->filewriter :0x{0:X}", hr); Cleanup(); return false; } //set output filename string outputFileName = System.IO.Path.ChangeExtension(info.file, ".mpg"); Log.Info("DVR2MPG: set output file to :{0}", outputFileName); mt.majorType = MediaType.Stream; mt.subType = MediaSubTypeEx.MPEG2; hr = fileWriterFilter.SetFileName(outputFileName, mt); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:unable to set filename for filewriter :0x{0:X}", hr); Cleanup(); return false; } mediaControl = graphBuilder as IMediaControl; mediaSeeking = graphBuilder as IMediaSeeking; mediaEvt = graphBuilder as IMediaEventEx; Log.Info("DVR2MPG: start transcoding"); hr = mediaControl.Run(); if (hr != 0) { Log.Warn("DVR2MPG: FAILED:unable to start graph :0x{0:X}", hr); Cleanup(); return false; } } catch (Exception ex) { Log.Error("DVR2MPG: Unable create graph: {0}", ex.Message); Cleanup(); return false; } return true; }
/// <summary> /// Connects to the property changed events of the camera settings. /// </summary> //private void Initialize() //{ // //Settings.Instance.Camera.OnCameraControlPropertyChanged += OnCameraControlPropertyChanged; // //Settings.Instance.Camera.OnVideoProcAmpPropertyChanged += OnVideoProcAmpPropertyChanged; // //Settings.Instance.Camera.OnVideoControlFlagsChanged += OnVideoControlFlagsChanged; // //stopwatch = new Stopwatch(); //} /// <summary> /// Build the capture graph for grabber. /// </summary> /// <param name="dev">The index of the new capture device.</param> /// <param name="frameRate">The framerate to use.</param> /// <param name="width">The width to use.</param> /// <param name="height">The height to use.</param> /// <returns>True, if succesfull, otherwise false.</returns> private bool SetupGraph(DsDevice dev, int frameRate, int width, int height) { int hr; fps = frameRate; // Not measured, only to expose FPS externally cameraControl = null; capFilter = null; // Get the graphbuilder object graphBuilder = (IFilterGraph2) new FilterGraph(); mediaControl = graphBuilder as IMediaControl; try { // Create the ICaptureGraphBuilder2 capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); // Create the SampleGrabber interface sampGrabber = (ISampleGrabber) new SampleGrabber(); // Start building the graph hr = capGraph.SetFiltergraph(graphBuilder); //if (hr != 0) // ErrorLogger.WriteLine("Error in capGraph.SetFiltergraph. Could not build graph. Message: " + // DsError.GetErrorText(hr)); #if DEBUG this.rotEntry = new DsROTEntry(this.graphBuilder); #endif this.capFilter = CreateFilter( FilterCategory.VideoInputDevice, dev.Name); if (this.capFilter != null) { hr = graphBuilder.AddFilter(this.capFilter, "Video Source"); DsError.ThrowExceptionForHR(hr); } //// Add the video device //hr = graphBuilder.AddSourceFilterForMoniker(dev.Mon, null, "Video input", out capFilter); //if (hr != 0) // ErrorLogger.WriteLine( // "Error in m_graphBuilder.AddSourceFilterForMoniker(). Could not add source filter. Message: " + // DsError.GetErrorText(hr)); var baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber"); //if (hr != 0) // ErrorLogger.WriteLine("Error in m_graphBuilder.AddFilter(). Could not add filter. Message: " + // DsError.GetErrorText(hr)); // turn on the infrared leds ONLY FOR THE GENIUS WEBCAM /* * if (!defaultMode) * { * m_icc = capFilter as IAMCameraControl; * CameraControlFlags CamFlags = new CameraControlFlags(); * int pMin, pMax, pStep, pDefault; * * hr = m_icc.GetRange(CameraControlProperty.Focus, out pMin, out pMax, out pStep, out pDefault, out CamFlags); * m_icc.Set(CameraControlProperty.Focus, pMax, CameraControlFlags.None); * } */ //IBaseFilter smartTee = new SmartTee() as IBaseFilter; //// Add the smart tee filter to the graph //hr = this.graphBuilder.AddFilter(smartTee, "Smart Tee"); //Marshal.ThrowExceptionForHR(hr); // Connect the video source output to the smart tee //hr = capGraph.RenderStream(null, null, capFilter, null, smartTee); hr = capGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, null, baseGrabFlt); var errorText = DsError.GetErrorText(hr); cameraControl = capFilter as IAMCameraControl; // Set videoProcAmp object obj; var iid_IBaseFilter = new Guid("56a86895-0ad4-11ce-b03a-0020af0ba770"); DirectShowDevices.Instance.Cameras[deviceNumber].DirectshowDevice.Mon.BindToObject( null, null, ref iid_IBaseFilter, out obj); videoProcAmp = obj as IAMVideoProcAmp; // If any of the default config items are set if (frameRate + height + width > 0) { SetConfigParms(capGraph, capFilter, frameRate, width, height); } // Check for succesful rendering, if this failed the class cannot be used, so dispose the resources and return false. if (hr < 0) { Cleanup(); return(false); } else { // Otherwise update the SampleGrabber. SaveSizeInfo(sampGrabber); hr = sampGrabber.SetBufferSamples(false); if (hr == 0) { hr = sampGrabber.SetOneShot(false); hr = sampGrabber.SetCallback(this, 1); } //if (hr < 0) // ErrorLogger.WriteLine("Could not set callback function (SetupGraph) in Camera.Capture()"); } } catch (Exception ex) { //ErrorLogger.ProcessException(ex, false); Console.WriteLine("ERROR! " + ex.ToString()); MessageBox.Show(ex.Message); Cleanup(); return(false); } return(true); }
private void CloseInterfaces() { int hr = 0; try { lock(this) { // Relinquish ownership (IMPORTANT!) after hiding video window if (!this.isAudioOnly) { hr = this.videoWindow.put_Visible(OABool.False); DsError.ThrowExceptionForHR(hr); hr = this.videoWindow.put_Owner(IntPtr.Zero); DsError.ThrowExceptionForHR(hr); } if (this.mediaEventEx != null) { hr = this.mediaEventEx.SetNotifyWindow(IntPtr.Zero, 0, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); } #if DEBUG if (rot != null) { rot.Dispose(); rot = null; } #endif // Release and zero DirectShow interfaces if (this.mediaEventEx != null) this.mediaEventEx = null; if (this.mediaSeeking != null) this.mediaSeeking = null; if (this.mediaPosition != null) this.mediaPosition = null; if (this.mediaControl != null) this.mediaControl = null; if (this.basicAudio != null) this.basicAudio = null; if (this.basicVideo != null) this.basicVideo = null; if (this.videoWindow != null) this.videoWindow = null; if (this.frameStep != null) this.frameStep = null; if (this.graphBuilder != null) Marshal.ReleaseComObject(this.graphBuilder); this.graphBuilder = null; GC.Collect(); } } catch { } }
/// <summary> /// Opens the media by initializing the DirectShow graph /// </summary> protected virtual void OpenSource() { /* Make sure we clean up any remaining mess */ FreeResources(); if (m_sourceUri == null) { return; } string fileSource = m_sourceUri.OriginalString; if (string.IsNullOrEmpty(fileSource)) { return; } try { /* Creates the GraphBuilder COM object */ m_graph = new FilterGraphNoThread() as IGraphBuilder; if (m_graph == null) { throw new Exception("Could not create a graph"); } var filterGraph = m_graph as IFilterGraph2; if (filterGraph == null) { throw new Exception("Could not QueryInterface for the IFilterGraph2"); } IBaseFilter sourceFilter; int hr; // Set LAV Splitter /* LAVSplitterSource reader = new LAVSplitterSource(); * sourceFilter = reader as IBaseFilter; * var objectWithSite = reader as IObjectWithSite; * if (objectWithSite != null) * { * objectWithSite.SetSite(this); * } * * * hr = m_graph.AddFilter(sourceFilter, SplitterSource); * DsError.ThrowExceptionForHR(hr);*/ sourceFilter = DirectShowUtil.AddFilterToGraph(m_graph, SplitterSource, Guid.Empty); IFileSourceFilter interfaceFile = (IFileSourceFilter)sourceFilter; hr = interfaceFile.Load(fileSource, null); DsError.ThrowExceptionForHR(hr); // Set Video Codec // Remove Pin var videoPinFrom = DirectShowLib.DsFindPin.ByName(sourceFilter, "Video"); IPin videoPinTo; if (videoPinFrom != null) { hr = videoPinFrom.ConnectedTo(out videoPinTo); if (hr >= 0 && videoPinTo != null) { PinInfo pInfo; videoPinTo.QueryPinInfo(out pInfo); FilterInfo fInfo; pInfo.filter.QueryFilterInfo(out fInfo); DirectShowUtil.DisconnectAllPins(m_graph, pInfo.filter); m_graph.RemoveFilter(pInfo.filter); DsUtils.FreePinInfo(pInfo); Marshal.ReleaseComObject(fInfo.pGraph); Marshal.ReleaseComObject(videoPinTo); videoPinTo = null; } Marshal.ReleaseComObject(videoPinFrom); videoPinFrom = null; } DirectShowUtil.AddFilterToGraph(m_graph, VideoDecoder, Guid.Empty); try { // Set Audio Codec // Remove Pin var audioPinFrom = DirectShowLib.DsFindPin.ByName(sourceFilter, "Audio"); IPin audioPinTo; if (audioPinFrom != null) { hr = audioPinFrom.ConnectedTo(out audioPinTo); if (hr >= 0 && audioPinTo != null) { PinInfo pInfo; audioPinTo.QueryPinInfo(out pInfo); FilterInfo fInfo; pInfo.filter.QueryFilterInfo(out fInfo); DirectShowUtil.DisconnectAllPins(m_graph, pInfo.filter); m_graph.RemoveFilter(pInfo.filter); DsUtils.FreePinInfo(pInfo); Marshal.ReleaseComObject(fInfo.pGraph); Marshal.ReleaseComObject(audioPinTo); audioPinTo = null; } Marshal.ReleaseComObject(audioPinFrom); audioPinFrom = null; } DirectShowUtil.AddFilterToGraph(m_graph, AudioDecoder, Guid.Empty); /* Add our prefered audio renderer */ InsertAudioRenderer(AudioRenderer); } catch { // No Audio available Trace.TraceError("No Audio Device found!"); } IBaseFilter renderer = CreateVideoRenderer(VideoRenderer, m_graph, 2); /* We will want to enum all the pins on the source filter */ IEnumPins pinEnum; hr = sourceFilter.EnumPins(out pinEnum); DsError.ThrowExceptionForHR(hr); IntPtr fetched = IntPtr.Zero; IPin[] pins = { null }; /* Counter for how many pins successfully rendered */ int pinsRendered = 0; if (VideoRenderer == VideoRendererType.VideoMixingRenderer9) { var mixer = renderer as IVMRMixerControl9; if (mixer != null) { VMR9MixerPrefs dwPrefs; mixer.GetMixingPrefs(out dwPrefs); dwPrefs &= ~VMR9MixerPrefs.RenderTargetMask; dwPrefs |= VMR9MixerPrefs.RenderTargetRGB; //mixer.SetMixingPrefs(dwPrefs); } } /* Loop over each pin of the source filter */ while (pinEnum.Next(pins.Length, pins, fetched) == 0) { if (filterGraph.RenderEx(pins[0], AMRenderExFlags.RenderToExistingRenderers, IntPtr.Zero) >= 0) { pinsRendered++; } Marshal.ReleaseComObject(pins[0]); } Marshal.ReleaseComObject(pinEnum); Marshal.ReleaseComObject(sourceFilter); if (pinsRendered == 0) { throw new Exception("Could not render any streams from the source Uri"); } #if DEBUG /* Adds the GB to the ROT so we can view * it in graphedit */ m_dsRotEntry = new DsROTEntry(m_graph); #endif /* Configure the graph in the base class */ SetupFilterGraph(m_graph); HasVideo = true; } catch (Exception ex) { /* This exection will happen usually if the media does * not exist or could not open due to not having the * proper filters installed */ // Fallback try auto graph: var result = oldOpenSource(); if (!result) { FreeResources(); /* Fire our failed event */ InvokeMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); } } InvokeMediaOpened(); }
private void CloseInterfaces() { try { lock (this) { #if DEBUG if (rot != null) { rot.Dispose(); rot = null; } #endif //ChangeState(PlayState.Exiting); //// Release the thread (if the thread was started) //if (this.currentState != PlayState.Exiting) //{ // this.currentState = PlayState.Exiting; // if (this.manualResetEvent != null) // { // manualResetEvent.Set(); // manualResetEvent.SafeWaitHandle.Dispose(); // manualResetEvent.Close(); // manualResetEvent = null; // } //} //if (eventThread != null) //{ // eventThread.Abort(); //} // Release and zero DirectShow interfaces if (this.mediaSeeking != null) this.mediaSeeking = null; //if (this.mediaEvent != null) // this.mediaEvent = null; if (this.mediaControl != null) this.mediaControl = null; if (this.basicAudio != null) this.basicAudio = null; if (this.filterGraph != null) { //// End event thread. //int hr = ((IMediaEventSink)this.filterGraph).Notify(EventCode.UserAbort, IntPtr.Zero, IntPtr.Zero); //DsError.ThrowExceptionForHR(hr); ////EventCode code; ////hr = ((IMediaEvent)this.filterGraph).WaitForCompletion(0, out code); ////DsError.ThrowExceptionForHR(hr); //while (!threadCompleted) //{ // Thread.Sleep(2); //} Marshal.ReleaseComObject(this.filterGraph); } this.filterGraph = null; GC.Collect(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception occured"); } }
public void SetMediaItem(IResourceLocator locator, string mediaItemTitle, MediaItem mediaItem) { _mediaItem = mediaItem; // free previous opened resource FilterGraphTools.TryDispose(ref _resourceAccessor); FilterGraphTools.TryDispose(ref _rot); _state = PlayerState.Active; _isPaused = true; try { _resourceLocator = locator; _mediaItemTitle = mediaItemTitle; CreateResourceAccessor(); // Create a DirectShow FilterGraph CreateGraphBuilder(); // Add it in ROT (Running Object Table) for debug purpose, it allows to view the Graph from outside (i.e. graphedit) _rot = new DsROTEntry(_graphBuilder); // Add a notification handler (see WndProc) _instancePtr = IntPtr.Zero; if (_me != null) { _me.SetNotifyWindow(SkinContext.Form.Handle, WM_GRAPHNOTIFY, _instancePtr); } // Create the Allocator / Presenter object AddPresenter(); ServiceRegistration.Get <ILogger>().Debug("{0}: Adding audio renderer", PlayerTitle); AddAudioRenderer(); ServiceRegistration.Get <ILogger>().Debug("{0}: Adding preferred codecs", PlayerTitle); AddPreferredCodecs(); ServiceRegistration.Get <ILogger>().Debug("{0}: Adding source filter", PlayerTitle); AddSourceFilter(); ServiceRegistration.Get <ILogger>().Debug("{0}: Adding subtitle filter", PlayerTitle); SetSubtitleRenderer(); AddSubtitleFilter(true); ServiceRegistration.Get <ILogger>().Debug("{0}: Run graph", PlayerTitle); //This needs to be done here before we check if the evr pins are connected //since this method gives players the chance to render the last bits of the graph OnBeforeGraphRunning(); // Now run the graph, i.e. the DVD player needs a running graph before getting informations from dvd filter. int hr = _mc.Run(); new HRESULT(hr).Throw(); _initialized = true; OnGraphRunning(); } catch (Exception) { Shutdown(); throw; } }