private bool AddCodecs(IGraphBuilder graphBuilder, TranscodeInfo info) { //TODO: Add de-interlacing probably by filter int hr; Log.Info("TSReader2MP4: add h264 video encoder to graph"); //Lead H264 Encoder (4.0) string monikerH264 = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{E2B7DF52-38C5-11D5-91F6-00104BDB8FF9}"; h264Encoder = Marshal.BindToMoniker(monikerH264) as IBaseFilter; if (h264Encoder == null) { Log.Error("TSReader2MP4: FAILED: Unable to create h264 video encoder"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(h264Encoder, "h264 video encoder"); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: h264 video encoder to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } Log.Info("TSReader2MP4: add aac audio encoder to graph"); //Monograph AAC Encoder //string monikerAAC = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{88F36DB6-D898-40B5-B409-466A0EECC26A}"; //Lead AAC Encoder string monikerAAC = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{E2B7DD70-38C5-11D5-91F6-00104BDB8FF9}"; aacEncoder = Marshal.BindToMoniker(monikerAAC) as IBaseFilter; if (aacEncoder == null) { Log.Error("TSReader2MP4: FAILED: Unable to create aac audio encoder"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(aacEncoder, "aac audio encoder"); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: Add aac audio encoder to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } // dump filter ???? //add filewriter Log.Info("TSReader2MP4: add FileWriter to graph"); string monikerFileWrite = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{8596E5F0-0DA5-11D0-BD21-00A0C911CE86}"; IBaseFilter fileWriterbase = Marshal.BindToMoniker(monikerFileWrite) as IBaseFilter; if (fileWriterbase == null) { Log.Error("TSReader2MP4: FAILED: Unable to create FileWriter"); Cleanup(); return(false); } fileWriterFilter = fileWriterbase as IFileSinkFilter2; if (fileWriterFilter == null) { Log.Error("TSReader2MP4: FAILED: Add unable to get IFileSinkFilter for filewriter"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(fileWriterbase, "FileWriter"); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: Add FileWriter to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } //set output filename string outputFileName = System.IO.Path.ChangeExtension(info.file, ".mp4"); Log.Info("TSReader2MP4: set output file to :{0}", outputFileName); hr = fileWriterFilter.SetFileName(outputFileName, null); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: unable to set filename for filewriter :0x{0:X}", hr); Cleanup(); return(false); } // add mp4 muxer Log.Info("TSReader2MP4: add MP4 Muxer to graph"); //Lead ISO Multiplexer string monikermp4Muxer = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{990D1978-E48D-43AF-B12D-24A7456EC89F}"; mp4Muxer = Marshal.BindToMoniker(monikermp4Muxer) as IBaseFilter; if (mp4Muxer == null) { Log.Error("TSReader2MP4: FAILED: Unable to create MP4Mux"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(mp4Muxer, "MP4Mux"); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: Add MP4Mux to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } //connect output of audio codec to aac encoder IPin pinOut, pinIn; Log.Info("TSReader2MP4: connect audio codec->aac encoder"); pinIn = DsFindPin.ByDirection(aacEncoder, PinDirection.Input, 0); if (pinIn == null) { Log.Error("TSReader2MP4: FAILED: cannot get input pin of aac encoder:0x{0:X}", hr); Cleanup(); return(false); } pinOut = DsFindPin.ByDirection(AudioCodec, PinDirection.Output, 0); if (pinOut == null) { Log.Error("TSReader2MP4: FAILED: cannot get output pin of audio codec :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: unable to connect audio codec->aac encoder: 0x{0:X}", hr); Cleanup(); return(false); } //connect output of video codec to h264 encoder Log.Info("TSReader2MP4: connect video codec->h264 encoder"); pinIn = DsFindPin.ByDirection(h264Encoder, PinDirection.Input, 0); if (pinIn == null) { Log.Error("TSReader2MP4: FAILED: cannot get input pin of h264 encoder:0x{0:X}", hr); Cleanup(); return(false); } pinOut = DsFindPin.ByDirection(VideoCodec, PinDirection.Output, 0); if (pinOut == null) { Log.Error("TSReader2MP4: FAILED: cannot get output pin of video codec :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: unable to connect video codec->h264 encoder :0x{0:X}", hr); Cleanup(); return(false); } //connect output of aac encoder to pin#0 of mp4mux Log.Info("TSReader2MP4: connect aac encoder->mp4mux"); pinOut = DsFindPin.ByDirection(aacEncoder, PinDirection.Output, 0); if (pinOut == null) { Log.Error("TSReader2MP4: FAILED: cannot get input pin of aac encoder:0x{0:X}", hr); Cleanup(); return(false); } pinIn = DsFindPin.ByDirection(mp4Muxer, PinDirection.Input, 0); if (pinIn == null) { Log.Error("TSReader2MP4: FAILED: cannot get input pin#1 of mp4 muxer :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: unable to connect aac encoder->mp4mux: 0x{0:X}", hr); Cleanup(); return(false); } //connect output of h264 encoder to pin#1 of mp4mux Log.Info("TSReader2MP4: connect h264 encoder->mp4mux"); pinOut = DsFindPin.ByDirection(h264Encoder, PinDirection.Output, 0); if (pinOut == null) { Log.Error("TSReader2MP4: FAILED: cannot get input pin of h264 encoder :0x{0:X}", hr); Cleanup(); return(false); } pinIn = DsFindPin.ByDirection(mp4Muxer, PinDirection.Input, 1); if (pinIn == null) { Log.Error("TSReader2MP4: FAILED: cannot get input#0 pin of mp4mux :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: unable to connect h264 encoder->mp4mux :0x{0:X}", hr); Cleanup(); return(false); } // dump filter?? //connect mp4 muxer out->filewriter Log.Info("TSReader2MP4: connect mp4mux->filewriter"); pinOut = DsFindPin.ByDirection(mp4Muxer, PinDirection.Output, 0); if (pinOut == null) { Log.Error("TSReader2MP4: FAILED: cannot get output pin of avimux:0x{0:X}", hr); Cleanup(); return(false); } pinIn = DsFindPin.ByDirection(fileWriterbase, PinDirection.Input, 0); if (pinIn == null) { Log.Error("TSReader2MP4: FAILED: cannot get input pin of filewriter :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: connect mp4 muxer->filewriter :0x{0:X}", hr); Cleanup(); return(false); } return(true); }
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); } } } } }
private void StartCapture() { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; if (System.IO.File.Exists(txtAviFileName.Text)) { // Get the graphbuilder object m_FilterGraph = (IFilterGraph2) new FilterGraph(); m_mediaCtrl = m_FilterGraph as IMediaControl; // 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 source hr = m_FilterGraph.AddSourceFilter(txtAviFileName.Text, "File Source (Async.)", out capFilter); DsError.ThrowExceptionForHR(hr); //add AVI Decompressor IBaseFilter pAVIDecompressor = (IBaseFilter) new AVIDec(); hr = m_FilterGraph.AddFilter(pAVIDecompressor, "AVI Decompressor"); DsError.ThrowExceptionForHR(hr); IBaseFilter ffdshow; try { // Create Decoder filter COM object (ffdshow video decoder) Type comtype = Type.GetTypeFromCLSID(new Guid("{04FE9017-F873-410E-871E-AB91661A4EF7}")); if (comtype == null) { throw new NotSupportedException("Creating ffdshow video decoder COM object fails."); } object comobj = Activator.CreateInstance(comtype); ffdshow = (IBaseFilter)comobj; // error ocurrs! raised exception comobj = null; } catch { CustomMessageBox.Show("Please install/reinstall ffdshow"); return; } hr = m_FilterGraph.AddFilter(ffdshow, "ffdshow"); DsError.ThrowExceptionForHR(hr); // IBaseFilter baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); IBaseFilter vidrender = (IBaseFilter) new VideoRenderer(); hr = m_FilterGraph.AddFilter(vidrender, "Render"); DsError.ThrowExceptionForHR(hr); IPin captpin = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); IPin ffdpinin = DsFindPin.ByName(ffdshow, "In"); IPin ffdpinout = DsFindPin.ByName(ffdshow, "Out"); IPin samppin = DsFindPin.ByName(baseGrabFlt, "Input"); hr = m_FilterGraph.Connect(captpin, ffdpinin); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.Connect(ffdpinout, samppin); DsError.ThrowExceptionForHR(hr); FileWriter filewritter = new FileWriter(); IFileSinkFilter filemux = (IFileSinkFilter)filewritter; //filemux.SetFileName("test.avi",); //hr = capGraph.RenderStream(null, MediaType.Video, capFilter, null, vidrender); // DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); // setup buffer if (m_handle == IntPtr.Zero) { m_handle = Marshal.AllocCoTaskMem(m_stride * m_videoHeight); } // tell the callback to ignore new images m_PictureReady = new ManualResetEvent(false); m_bGotOne = false; m_bRunning = false; timer1 = new Thread(timer); timer1.IsBackground = true; timer1.Start(); m_mediaextseek = m_FilterGraph as IAMExtendedSeeking; m_mediapos = m_FilterGraph as IMediaPosition; m_mediaseek = m_FilterGraph as IMediaSeeking; double length = 0; m_mediapos.get_Duration(out length); trackBar_mediapos.Minimum = 0; trackBar_mediapos.Maximum = (int)length; Start(); } else { MessageBox.Show("File does not exist"); } }
/// <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 { /* Create a new graph */ 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 captureGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); /* Set our filter graph to the capture graph */ int hr = captureGraph.SetFiltergraph(m_graph); DsError.ThrowExceptionForHR(hr); /* 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)); } if (UseYuv && !EnableSampleGrabbing) { /* Configure the video output pin with our parameters and if it fails * then just use the default media subtype*/ if (!SetVideoCaptureParameters(captureGraph, m_captureDevice, MediaSubType.YUY2)) { SetVideoCaptureParameters(captureGraph, m_captureDevice, Guid.Empty); } } else { /* Configure the video output pin with our parameters */ SetVideoCaptureParameters(captureGraph, m_captureDevice, Guid.Empty); } 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); } var videoOutPin = DsFindPin.ByDirection(m_captureDevice, PinDirection.Output, 0); if (videoOutPin == null) { throw new Exception("Could not query the video output pin on source filter"); } /* Intelligently connect the pins in the graph to the renderer */ hr = m_graph.Render(videoOutPin); Marshal.ReleaseComObject(videoOutPin); //hr = captureGraph.RenderStream(PinCategory.Capture, // MediaType.Video, // m_captureDevice, // null, // m_renderer); DsError.ThrowExceptionForHR(hr); /* 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 */ Marshal.ReleaseComObject(captureGraph); } catch (Exception ex) { /* Something got fuct up */ FreeResources(); InvokeMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); } /* Success */ InvokeMediaOpened(); }
private void StartCapture() { int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; ICaptureGraphBuilder2 capGraph = null; if (System.IO.File.Exists(txtAviFileName.Text)) { // Get the graphbuilder object m_FilterGraph = (IFilterGraph2) new FilterGraph(); m_mediaCtrl = m_FilterGraph as IMediaControl; // 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 source hr = m_FilterGraph.AddSourceFilter(txtAviFileName.Text, "File Source (Async.)", out capFilter); DsError.ThrowExceptionForHR(hr); //add AVI Decompressor IBaseFilter pAVIDecompressor = (IBaseFilter) new AVIDec(); hr = m_FilterGraph.AddFilter(pAVIDecompressor, "AVI Decompressor"); DsError.ThrowExceptionForHR(hr); // IBaseFilter baseGrabFlt = (IBaseFilter)sampGrabber; ConfigureSampleGrabber(sampGrabber); // Add the frame grabber to the graph hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber"); DsError.ThrowExceptionForHR(hr); IBaseFilter vidrender = (IBaseFilter) new VideoRenderer(); hr = m_FilterGraph.AddFilter(vidrender, "Render"); DsError.ThrowExceptionForHR(hr); IPin captpin = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0); IPin samppin = DsFindPin.ByName(baseGrabFlt, "Input"); hr = m_FilterGraph.Connect(captpin, samppin); DsError.ThrowExceptionForHR(hr); FileWriter filewritter = new FileWriter(); IFileSinkFilter filemux = (IFileSinkFilter)filewritter; //filemux.SetFileName("test.avi",); //hr = capGraph.RenderStream(null, MediaType.Video, capFilter, null, vidrender); // DsError.ThrowExceptionForHR(hr); SaveSizeInfo(sampGrabber); // setup buffer if (m_handle == IntPtr.Zero) { m_handle = Marshal.AllocCoTaskMem(m_stride * m_videoHeight); } // tell the callback to ignore new images m_PictureReady = new ManualResetEvent(false); m_bGotOne = false; m_bRunning = false; timer1 = new Thread(timer); timer1.IsBackground = true; timer1.Start(); m_mediaextseek = m_FilterGraph as IAMExtendedSeeking; m_mediapos = m_FilterGraph as IMediaPosition; m_mediaseek = m_FilterGraph as IMediaSeeking; double length = 0; m_mediapos.get_Duration(out length); trackBar_mediapos.Minimum = 0; trackBar_mediapos.Maximum = (int)length; Start(); } else { MessageBox.Show("File does not exist"); } }
/// <summary> create the used COM components and get the interfaces. </summary> protected bool GetInterfaces() { Vmr9 = null; if (IsRadio == false) { Vmr9 = new VMR9Util(); // switch back to directx fullscreen mode Log.Info("RTSPPlayer: Enabling DX9 exclusive mode"); GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_SWITCH_FULL_WINDOWED, 0, 0, 0, 1, 0, null); GUIWindowManager.SendMessage(msg); } //Type comtype = null; //object comobj = null; DsRect rect = new DsRect(); rect.top = 0; rect.bottom = GUIGraphicsContext.form.Height; rect.left = 0; rect.right = GUIGraphicsContext.form.Width; try { graphBuilder = (IGraphBuilder) new FilterGraph(); Log.Info("RTSPPlayer: add source filter"); if (IsRadio == false) { Vmr9.AddVMR9(graphBuilder); Vmr9.Enable(false); } _mpegDemux = (IBaseFilter) new MPEG2Demultiplexer(); graphBuilder.AddFilter(_mpegDemux, "MPEG-2 Demultiplexer"); _rtspSource = (IBaseFilter) new RtpSourceFilter(); int hr = graphBuilder.AddFilter((IBaseFilter)_rtspSource, "RTSP Source Filter"); if (hr != 0) { Log.Error("RTSPPlayer:unable to add RTSP source filter:{0:X}", hr); return(false); } // add preferred video & audio codecs Log.Info("RTSPPlayer: add video/audio codecs"); string strVideoCodec = ""; string strAudioCodec = ""; string strAudiorenderer = ""; int intFilters = 0; // FlipGer: count custom filters string strFilters = ""; // FlipGer: collect custom filters string postProcessingFilterSection = "mytv"; using (Settings xmlreader = new MPSettings()) { if (_mediaType == g_Player.MediaType.Video) { strVideoCodec = xmlreader.GetValueAsString("movieplayer", "mpeg2videocodec", ""); strAudioCodec = xmlreader.GetValueAsString("movieplayer", "mpeg2audiocodec", ""); strAudiorenderer = xmlreader.GetValueAsString("movieplayer", "audiorenderer", "Default DirectSound Device"); postProcessingFilterSection = "movieplayer"; } else { strVideoCodec = xmlreader.GetValueAsString("mytv", "videocodec", ""); strAudioCodec = xmlreader.GetValueAsString("mytv", "audiocodec", ""); strAudiorenderer = xmlreader.GetValueAsString("mytv", "audiorenderer", "Default DirectSound Device"); postProcessingFilterSection = "mytv"; } enableDvbSubtitles = xmlreader.GetValueAsBool("tvservice", "dvbsubtitles", false); // FlipGer: load infos for custom filters int intCount = 0; while (xmlreader.GetValueAsString(postProcessingFilterSection, "filter" + intCount.ToString(), "undefined") != "undefined") { if (xmlreader.GetValueAsBool(postProcessingFilterSection, "usefilter" + intCount.ToString(), false)) { strFilters += xmlreader.GetValueAsString(postProcessingFilterSection, "filter" + intCount.ToString(), "undefined") + ";"; intFilters++; } intCount++; } } string extension = Path.GetExtension(m_strCurrentFile).ToLower(); if (IsRadio == false) { if (strVideoCodec.Length > 0) { DirectShowUtil.AddFilterToGraph(graphBuilder, strVideoCodec); } } if (strAudioCodec.Length > 0) { DirectShowUtil.AddFilterToGraph(graphBuilder, strAudioCodec); } if (enableDvbSubtitles == true) { try { _subtitleFilter = SubtitleRenderer.GetInstance().AddSubtitleFilter(graphBuilder); SubtitleRenderer.GetInstance().SetPlayer(this); dvbSubRenderer = SubtitleRenderer.GetInstance(); } catch (Exception e) { Log.Error(e); } } Log.Debug("Is subtitle fitler null? {0}", (_subtitleFilter == null)); // FlipGer: add custom filters to graph string[] arrFilters = strFilters.Split(';'); for (int i = 0; i < intFilters; i++) { DirectShowUtil.AddFilterToGraph(graphBuilder, arrFilters[i]); } if (strAudiorenderer.Length > 0) { audioRendererFilter = DirectShowUtil.AddAudioRendererToGraph(graphBuilder, strAudiorenderer, false); } Log.Info("RTSPPlayer: load:{0}", m_strCurrentFile); IFileSourceFilter interfaceFile = (IFileSourceFilter)_rtspSource; if (interfaceFile == null) { Log.Error("RTSPPlayer:Failed to get IFileSourceFilter"); return(false); } //Log.Info("RTSPPlayer: open file:{0}",filename); hr = interfaceFile.Load(m_strCurrentFile, null); if (hr != 0) { Log.Error("RTSPPlayer:Failed to open file:{0} :0x{1:x}", m_strCurrentFile, hr); return(false); } #region connect rtspsource->demux Log.Info("RTSPPlayer:connect rtspsource->mpeg2 demux"); IPin pinTsOut = DsFindPin.ByDirection((IBaseFilter)_rtspSource, PinDirection.Output, 0); if (pinTsOut == null) { Log.Info("RTSPPlayer:failed to find output pin of tsfilesource"); return(false); } IPin pinDemuxIn = DsFindPin.ByDirection(_mpegDemux, PinDirection.Input, 0); if (pinDemuxIn == null) { Log.Info("RTSPPlayer:failed to find output pin of tsfilesource"); return(false); } hr = graphBuilder.Connect(pinTsOut, pinDemuxIn); if (hr != 0) { Log.Info("RTSPPlayer:failed to connect rtspsource->mpeg2 demux:{0:X}", hr); return(false); } DirectShowUtil.ReleaseComObject(pinTsOut); DirectShowUtil.ReleaseComObject(pinDemuxIn); #endregion #region render demux output pins if (IsRadio) { Log.Info("RTSPPlayer:render audio demux outputs"); IEnumPins enumPins; _mpegDemux.EnumPins(out enumPins); IPin[] pins = new IPin[2]; int fetched = 0; while (enumPins.Next(1, pins, out fetched) == 0) { if (fetched != 1) { break; } PinDirection direction; pins[0].QueryDirection(out direction); if (direction == PinDirection.Input) { continue; } IEnumMediaTypes enumMediaTypes; pins[0].EnumMediaTypes(out enumMediaTypes); AMMediaType[] mediaTypes = new AMMediaType[20]; int fetchedTypes; enumMediaTypes.Next(20, mediaTypes, out fetchedTypes); for (int i = 0; i < fetchedTypes; ++i) { if (mediaTypes[i].majorType == MediaType.Audio) { graphBuilder.Render(pins[0]); break; } } } } else { Log.Info("RTSPPlayer:render audio/video demux outputs"); IEnumPins enumPins; _mpegDemux.EnumPins(out enumPins); IPin[] pins = new IPin[2]; int fetched = 0; while (enumPins.Next(1, pins, out fetched) == 0) { if (fetched != 1) { break; } PinDirection direction; pins[0].QueryDirection(out direction); if (direction == PinDirection.Input) { continue; } graphBuilder.Render(pins[0]); } } #endregion // Connect DVB subtitle filter pins in the graph if (_mpegDemux != null && enableDvbSubtitles == true) { IMpeg2Demultiplexer demuxer = _mpegDemux as IMpeg2Demultiplexer; hr = demuxer.CreateOutputPin(GetTSMedia(), "Pcr", out _pinPcr); if (hr == 0) { Log.Info("RTSPPlayer:_pinPcr OK"); IPin pDemuxerPcr = DsFindPin.ByName(_mpegDemux, "Pcr"); IPin pSubtitlePcr = DsFindPin.ByName(_subtitleFilter, "Pcr"); hr = graphBuilder.Connect(pDemuxerPcr, pSubtitlePcr); } else { Log.Info("RTSPPlayer:Failed to create _pinPcr in demuxer:{0:X}", hr); } hr = demuxer.CreateOutputPin(GetTSMedia(), "Subtitle", out _pinSubtitle); if (hr == 0) { Log.Info("RTSPPlayer:_pinSubtitle OK"); IPin pDemuxerSubtitle = DsFindPin.ByName(_mpegDemux, "Subtitle"); IPin pSubtitle = DsFindPin.ByName(_subtitleFilter, "In"); hr = graphBuilder.Connect(pDemuxerSubtitle, pSubtitle); } else { Log.Info("RTSPPlayer:Failed to create _pinSubtitle in demuxer:{0:X}", hr); } hr = demuxer.CreateOutputPin(GetTSMedia(), "PMT", out _pinPMT); if (hr == 0) { Log.Info("RTSPPlayer:_pinPMT OK"); IPin pDemuxerSubtitle = DsFindPin.ByName(_mpegDemux, "PMT"); IPin pSubtitle = DsFindPin.ByName(_subtitleFilter, "PMT"); hr = graphBuilder.Connect(pDemuxerSubtitle, pSubtitle); } else { Log.Info("RTSPPlayer:Failed to create _pinPMT in demuxer:{0:X}", hr); } } if (IsRadio == false) { if (!Vmr9.IsVMR9Connected) { //VMR9 is not supported, switch to overlay Log.Info("RTSPPlayer: vmr9 not connected"); _mediaCtrl = null; Cleanup(); return(false); } Vmr9.SetDeinterlaceMode(); } _mediaCtrl = (IMediaControl)graphBuilder; mediaEvt = (IMediaEventEx)graphBuilder; _mediaSeeking = (IMediaSeeking)graphBuilder; mediaPos = (IMediaPosition)graphBuilder; basicAudio = graphBuilder as IBasicAudio; //DirectShowUtil.SetARMode(graphBuilder,AspectRatioMode.Stretched); DirectShowUtil.EnableDeInterlace(graphBuilder); if (Vmr9 != null) { m_iVideoWidth = Vmr9.VideoWidth; m_iVideoHeight = Vmr9.VideoHeight; } if (audioRendererFilter != null) { Log.Info("RTSPPlayer9:set reference clock"); IMediaFilter mp = graphBuilder as IMediaFilter; IReferenceClock clock = audioRendererFilter as IReferenceClock; hr = mp.SetSyncSource(null); hr = mp.SetSyncSource(clock); Log.Info("RTSPPlayer9:set reference clock:{0:X}", hr); } Log.Info("RTSPPlayer: graph build successfull"); return(true); } catch (Exception ex) { Error.SetError("Unable to play movie", "Unable build graph for VMR9"); Log.Error("RTSPPlayer:exception while creating DShow graph {0} {1}", ex.Message, ex.StackTrace); return(false); } }
private void SetupSampleGrabber() { if (_graph == null) { return; } int hr; //Get directsound filter IBaseFilter directSoundFilter; hr = _graph.FindFilterByName(DEFAULT_AUDIO_RENDERER_NAME, out directSoundFilter); DsError.ThrowExceptionForHR(hr); IPin rendererPinIn = DsFindPin.ByConnectionStatus(directSoundFilter, PinConnectedStatus.Connected, 0); if (rendererPinIn != null) { IPin audioPinOut; hr = rendererPinIn.ConnectedTo(out audioPinOut); DsError.ThrowExceptionForHR(hr); if (audioPinOut != null) { //Disconect audio decoder to directsound renderer hr = audioPinOut.Disconnect(); DsError.ThrowExceptionForHR(hr); hr = _graph.RemoveFilter(directSoundFilter); DsError.ThrowExceptionForHR(hr); //Add Sample Grabber ISampleGrabber sampleGrabber = new SampleGrabber() as ISampleGrabber; hr = sampleGrabber.SetCallback(this, 1); DsError.ThrowExceptionForHR(hr); AMMediaType media; media = new AMMediaType(); media.majorType = MediaType.Audio; media.subType = MediaSubType.PCM; media.formatType = FormatType.WaveEx; hr = sampleGrabber.SetMediaType(media); DsError.ThrowExceptionForHR(hr); IPin sampleGrabberPinIn = DsFindPin.ByDirection((IBaseFilter)sampleGrabber, PinDirection.Input, 0); IPin sampleGrabberPinOut = DsFindPin.ByDirection((IBaseFilter)sampleGrabber, PinDirection.Output, 0); hr = _graph.AddFilter((IBaseFilter)sampleGrabber, "SampleGrabber"); DsError.ThrowExceptionForHR(hr); PinInfo pinInfo; hr = audioPinOut.QueryPinInfo(out pinInfo); DsError.ThrowExceptionForHR(hr); FilterInfo filterInfo; hr = pinInfo.filter.QueryFilterInfo(out filterInfo); DsError.ThrowExceptionForHR(hr); hr = _graph.Connect(audioPinOut, sampleGrabberPinIn); DsError.ThrowExceptionForHR(hr); //Add null renderer NullRenderer nullRenderer = new NullRenderer(); hr = _graph.AddFilter((IBaseFilter)nullRenderer, "NullRenderer"); DsError.ThrowExceptionForHR(hr); IPin nullRendererPinIn = DsFindPin.ByDirection((IBaseFilter)nullRenderer, PinDirection.Input, 0); hr = _graph.Connect(sampleGrabberPinOut, nullRendererPinIn); DsError.ThrowExceptionForHR(hr); _audioEngine.Setup(this.GetSampleGrabberFormat(sampleGrabber)); } } }
public static IBaseFilter AddToGraph(IGraphBuilder graphBuilder) { IBaseFilter vob = null; DirectShowUtil.FindFilterByClassID(graphBuilder, ClassId.DirectVobSubAutoload, out vob); if (vob == null) { //Try the "normal" filter then. DirectShowUtil.FindFilterByClassID(graphBuilder, ClassId.DirectVobSubNormal, out vob); } //if the directvobsub filter has not been added to the graph. (i.e. with evr) //we add a bit more intelligence to determine if subtitles are enabled. //and if subtitles are present for the video / movie then we add it if necessary to the graph. if (vob == null) { Log.Info("VideoPlayerVMR9: No VobSub filter in the current graph"); //the filter has not been added lets check if it should be added or not. //add the filter to the graph vob = DirectShowUtil.AddFilterToGraph(graphBuilder, "DirectVobSub"); if (vob == null) { Log.Warn("VideoPlayerVMR9: DirectVobSub filter not found! You need to install VSFilter"); return(null); } else { Log.Debug("VideoPlayerVMR9: VobSub filter added to graph"); } } else // VobSub already loaded { return(vob); } // Check if Haali Media Splitter is in the graph. IBaseFilter hms = null; DirectShowUtil.FindFilterByClassID(graphBuilder, ClassId.HaaliGuid, out hms); if (hms == null) { DirectShowUtil.FindFilterByClassID(graphBuilder, ClassId.MPCMatroska, out hms); } if (hms == null) { DirectShowUtil.FindFilterByClassID(graphBuilder, ClassId.MPCMatroskaSource, out hms); } if (hms == null) { DirectShowUtil.FindFilterByClassID(graphBuilder, ClassId.LAVFilter, out hms); } if (hms == null) { DirectShowUtil.FindFilterByClassID(graphBuilder, ClassId.LAVFilterSource, out hms); } if (hms != null) { IPin pinSubTo = null; // It is. Connect it' subtitle output pin (if any) to Vobsub's subtitle input. pinSubTo = DirectShowUtil.FindPin(hms, PinDirection.Output, "Subtitle"); if (pinSubTo == null) { while (true) { IPin freeSubtitle = DirectShowUtil.FindFirstFreePinSub(hms, PinDirection.Output, ""); IPin freeVobSub = DirectShowUtil.FindFirstFreePin(vob, PinDirection.Input, "Input"); if (freeSubtitle != null && freeVobSub != null) { Log.Debug("VideoPlayerVMR9: Connecting Matroska's subtitle output to VobSub's input."); graphBuilder.Connect(freeSubtitle, freeVobSub); DirectShowUtil.ReleaseComObject(freeSubtitle); freeSubtitle = null; DirectShowUtil.ReleaseComObject(freeVobSub); freeVobSub = null; } else { break; } } } DirectShowUtil.ReleaseComObject(hms); hms = null; if (pinSubTo != null) { Log.Debug("VideoPlayerVMR9: Connecting Haali's subtitle output to VobSub's input."); // Try to render pins IPin pinVobSubSub = DirectShowUtil.FindPin(vob, PinDirection.Input, "Input"); // If pinSubTo is already connected (disconnect it) graphBuilder.Disconnect(pinSubTo); graphBuilder.Connect(pinSubTo, pinVobSubSub); DirectShowUtil.ReleaseComObject(pinSubTo); pinSubTo = null; DirectShowUtil.ReleaseComObject(pinVobSubSub); pinVobSubSub = null; } } // Now check if vobsub's video input is not connected. // Check only if vmr9 is connected (render was successful). VMR9Util Vmr9 = VMR9Util.g_vmr9; if (Vmr9.IsVMR9Connected) { Log.Debug("VideoPlayerVMR9: Connect VobSub's video pins"); IPin pinVobSubVideoIn = DsFindPin.ByDirection(vob, PinDirection.Input, 0); IPin pinVobSubVideoOut = DsFindPin.ByDirection(vob, PinDirection.Output, 0); // This is the pin that we will connect to vobsub's input. IPin pinVideoTo = Vmr9.PinConnectedTo; IPin pinVideoFrom = null; pinVideoTo.ConnectedTo(out pinVideoFrom); pinVideoTo.Disconnect(); pinVideoFrom.Disconnect(); //Now make connection to VobSub int hr = graphBuilder.Connect(pinVideoTo, pinVobSubVideoIn); //hr = graphBuilder.Render(pinVideoFrom); if (hr != 0) { Log.Error("VideoPlayerVMR9: could not connect Vobsub's input video pin"); return(null); } hr = graphBuilder.Connect(pinVobSubVideoOut, pinVideoFrom); //hr = graphBuilder.Render(pinVobSubVideoOut); if (hr != 0) { Log.Error("VideoPlayerVMR9: could not connect Vobsub's output video pin"); return(null); } Log.Debug("VideoPlayerVMR9: Vobsub's video pins connected"); DirectShowUtil.ReleaseComObject(pinVideoTo); pinVideoTo = null; DirectShowUtil.ReleaseComObject(pinVobSubVideoIn); pinVobSubVideoIn = null; DirectShowUtil.ReleaseComObject(pinVobSubVideoOut); pinVobSubVideoOut = null; DirectShowUtil.ReleaseComObject(pinVideoFrom); pinVideoFrom = null; } Vmr9 = null; return(vob); }
public static void RemoveFromGraph(IGraphBuilder graphBuilder) { IBaseFilter vob = null; DirectShowUtil.FindFilterByClassID(graphBuilder, ClassId.DirectVobSubAutoload, out vob); if (vob == null) { //Try the "normal" filter then. DirectShowUtil.FindFilterByClassID(graphBuilder, ClassId.DirectVobSubNormal, out vob); if (vob == null) { return; } } Log.Info("VideoPlayerVMR9: DirectVobSub in graph, removing..."); // Check where video inputs are connected IPin pinVideoIn = DsFindPin.ByDirection(vob, PinDirection.Input, 0); IPin pinVideoOut = DsFindPin.ByDirection(vob, PinDirection.Output, 0); //find directvobsub's video output pin source input pin IPin pinVideoTo = null; pinVideoOut.ConnectedTo(out pinVideoTo); //find directvobsub's video input pin source output pin IPin pinVideoFrom = null; pinVideoIn.ConnectedTo(out pinVideoFrom); int hr; if (pinVideoFrom != null) { hr = pinVideoFrom.Disconnect(); if (hr != 0) { Log.Error("VideoPlayerVMR9: DirectVobSub failed disconnecting source pin"); } } if (pinVideoTo != null) { hr = pinVideoTo.Disconnect(); if (hr != 0) { Log.Error("VideoPlayerVMR9: DirectVobSub failed disconnecting destination pin"); } } //remove the DirectVobSub filter from the graph graphBuilder.RemoveFilter(vob); DirectShowUtil.ReleaseComObject(vob); vob = null; //reconnect the source output pin to the vmr9/evr filter hr = graphBuilder.Connect(pinVideoFrom, pinVideoTo); //hr = graphBuilder.Render(pinVideoFrom); DirectShowUtil.ReleaseComObject(pinVideoFrom); pinVideoFrom = null; DirectShowUtil.ReleaseComObject(pinVideoTo); pinVideoTo = null; DirectShowUtil.ReleaseComObject(pinVideoOut); pinVideoOut = null; DirectShowUtil.ReleaseComObject(pinVideoIn); pinVideoIn = null; if (hr != 0) { Log.Error("VideoPlayerVMR9: Could not connect video out to video renderer: {0}", hr); } else { Log.Debug("VideoPlayerVMR9: DirectVobSub graph rebuild finished"); } }
/// <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 Exception("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 Exception("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 Exception("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 Exception("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(); }
public static VideoInfoHeader2 GetSBEFrameSize(string pathToFile) { int hr = 0; IGraphBuilder graph = null; IBaseFilter capFilter = null; IBaseFilter nRender = null; try { graph = (IGraphBuilder) new FilterGraph(); hr = graph.AddSourceFilter(pathToFile, "Source", out capFilter); DsError.ThrowExceptionForHR(hr); #if DEBUG using (DsROTEntry rot = new DsROTEntry(graph)) { #endif IPin vPin = null; IBaseFilter dec = null; IPin sgIn = null; IBaseFilter mpegDec = null; try { dec = (IBaseFilter) new DTFilter(); hr = graph.AddFilter(dec, "Decrypt"); DsError.ThrowExceptionForHR(hr); nRender = (IBaseFilter) new NullRenderer(); hr = graph.AddFilter((IBaseFilter)nRender, "Video Null Renderer"); DsError.ThrowExceptionForHR(hr); IBaseFilter dec1 = FilterDefinition.AddToFilterGraph(FatAttitude.WTVTranscoder.FilterDefinitions.Decrypt.DTFilterPBDA, ref graph, "Decrypt1"); if (dec1 != null) { Marshal.ReleaseComObject(dec1); } dec1 = null; mpegDec = FilterDefinition.AddToFilterGraph(FatAttitude.WTVTranscoder.FilterDefinitions.Video.VideoDecoderMpeg, ref graph, "MS MPEG Decoder"); sgIn = DsFindPin.ByDirection(mpegDec, PinDirection.Input, 0); IEnumPins ppEnum; IPin[] pPins = new IPin[1]; hr = capFilter.EnumPins(out ppEnum); DsError.ThrowExceptionForHR(hr); try { while (ppEnum.Next(1, pPins, IntPtr.Zero) == 0) { IEnumMediaTypes emtDvr = null; AMMediaType[] amtDvr = new AMMediaType[1]; try { pPins[0].EnumMediaTypes(out emtDvr); hr = emtDvr.Next(1, amtDvr, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); if (amtDvr[0].majorType == MediaType.Video) { if (graph.Connect(pPins[0], sgIn) >= 0) { vPin = pPins[0]; break; } } if (pPins[0] != null) { Marshal.ReleaseComObject(pPins[0]); } } finally { if (emtDvr != null) { Marshal.ReleaseComObject(emtDvr); } DsUtils.FreeAMMediaType(amtDvr[0]); } } } finally { if (ppEnum != null) { Marshal.ReleaseComObject(ppEnum); } } FilterGraphTools.RenderPin(graph, mpegDec, "Video Output 1"); } finally { if (vPin != null) { Marshal.ReleaseComObject(vPin); } if (dec != null) { Marshal.ReleaseComObject(dec); } if (sgIn != null) { Marshal.ReleaseComObject(sgIn); } if (mpegDec != null) { Marshal.ReleaseComObject(mpegDec); } } EventCode ec; IMediaControl mControl = graph as IMediaControl; IMediaEvent mEvent = graph as IMediaEvent; hr = mControl.Pause(); DsError.ThrowExceptionForHR(hr); hr = mControl.Run(); DsError.ThrowExceptionForHR(hr); hr = mEvent.WaitForCompletion(1000, out ec); //DsError.ThrowExceptionForHR(hr); hr = mControl.Pause(); DsError.ThrowExceptionForHR(hr); hr = mControl.Stop(); DsError.ThrowExceptionForHR(hr); IPin mpgOut = null; sgIn = null; AMMediaType mt = new AMMediaType(); try { sgIn = DsFindPin.ByDirection(nRender, PinDirection.Input, 0); if (sgIn != null) { hr = sgIn.ConnectedTo(out mpgOut); DsError.ThrowExceptionForHR(hr); hr = graph.RemoveFilter(nRender); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(nRender); nRender = null; nRender = (IBaseFilter) new NullRenderer(); hr = graph.AddFilter((IBaseFilter)nRender, "Video Null Renderer"); DsError.ThrowExceptionForHR(hr); hr = graph.Render(mpgOut); DsError.ThrowExceptionForHR(hr); hr = mpgOut.ConnectionMediaType(mt); DsError.ThrowExceptionForHR(hr); if (mt.formatType == FormatType.VideoInfo2) { VideoInfoHeader2 vih = (VideoInfoHeader2)Marshal.PtrToStructure(mt.formatPtr, typeof(VideoInfoHeader2)); return(vih); } } } finally { DsUtils.FreeAMMediaType(mt); if (mpgOut != null) { Marshal.ReleaseComObject(mpgOut); } if (sgIn != null) { Marshal.ReleaseComObject(sgIn); } } #if DEBUG } #endif } finally { if (nRender != null) { Marshal.ReleaseComObject(nRender); } if (capFilter != null) { Marshal.ReleaseComObject(capFilter); } if (graph != null) { while (Marshal.ReleaseComObject(graph) > 0) { ; } } } return(null); }
public void SetUpForTs(ISampleGrabberCB grabber, int methodToCall) { FilterGraphTools.DisconnectPins(mpeg2Demux); //FilterGraphTools.DisconnectPins(demodulator); FilterGraphTools.DisconnectPins(audioRenderer); FilterGraphTools.DisconnectPins(videoRenderer); //graphBuilder.RemoveFilter(audioRenderer); //graphBuilder.RemoveFilter(videoRenderer); sampleGrabber = (ISampleGrabber) new SampleGrabber(); AMMediaType media = new AMMediaType(); media.majorType = MediaType.Stream; media.subType = MediaSubType.Mpeg2Transport; media.formatType = FormatType.MpegStreams; sampleGrabber.SetOneShot(false); sampleGrabber.SetBufferSamples(true); int hr = sampleGrabber.SetMediaType(media); DsError.ThrowExceptionForHR(hr); graphBuilder.AddFilter((IBaseFilter)sampleGrabber, "Sample Grabber"); nullRenderer = (IBaseFilter) new NullRenderer(); graphBuilder.AddFilter(nullRenderer, "NULL Renderer"); IPin pinIn = DsFindPin.ByName((IBaseFilter)sampleGrabber, "Input"); IPin pinOut = DsFindPin.ByDirection(capture, PinDirection.Output, 0); IEnumMediaTypes eMedia; pinOut.EnumMediaTypes(out eMedia); AMMediaType[] mediaTypes = new AMMediaType[1]; eMedia.Next(mediaTypes.Length, mediaTypes, IntPtr.Zero); hr = sampleGrabber.SetMediaType(mediaTypes[0]); DsError.ThrowExceptionForHR(hr); pinOut.Disconnect(); PinInfo info; pinOut.QueryPinInfo(out info); hr = graphBuilder.ConnectDirect(pinOut, pinIn, mediaTypes[0]); //hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = DsFindPin.ByName(nullRenderer, "In"); pinOut = DsFindPin.ByName((IBaseFilter)sampleGrabber, "Output"); hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); sampleGrabber.SetCallback(grabber, methodToCall); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = null; }
// This method changed to work with Windows 7 // Under this OS, the MPEG-2 Demux now have dozens of outputs pins. // Rendering all of them is not a good solution. // The rendering process must be more smart... private void ConnectFilters() { int hr = 0; int pinNumber = 0; IPin pinOut, pinIn; // After the rendering process, our 4 downstream filters must be rendered bool bdaTIFRendered = false; bool bdaSecTabRendered = false; bool audioRendered = false; bool videoRendered = false; // for each output pins... while (true) { pinOut = DsFindPin.ByDirection(mpeg2Demux, PinDirection.Output, pinNumber); // Is the last pin reached ? if (pinOut == null) { break; } IEnumMediaTypes enumMediaTypes = null; AMMediaType[] mediaTypes = new AMMediaType[1]; try { // Get Pin's MediaType enumerator hr = pinOut.EnumMediaTypes(out enumMediaTypes); DsError.ThrowExceptionForHR(hr); // for each media types... while (enumMediaTypes.Next(mediaTypes.Length, mediaTypes, IntPtr.Zero) == 0) { // Store the majortype and the subtype and free the structure Guid majorType = mediaTypes[0].majorType; Guid subType = mediaTypes[0].subType; DsUtils.FreeAMMediaType(mediaTypes[0]); if (majorType == MediaType.Audio) { // Is the Audio already rendered ? if (!audioRendered) { // Get the first input pin pinIn = DsFindPin.ByDirection(audioRenderer, PinDirection.Input, 0); // Render it with IntelliConnect (a decoder should be added between the two filters. hr = graphBuilder.Connect(pinOut, pinIn); DsError.ThrowExceptionForHR(hr); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = null; // Notify that the audio renderer is connected audioRendered = true; } } else if (majorType == MediaType.Video) { // Is the Video already rendered ? if (!videoRendered) { // Get the first input pin pinIn = DsFindPin.ByDirection(videoRenderer, PinDirection.Input, 0); // Render it with IntelliConnect (a decoder should be added between the two filters. //hr = graphBuilder.Connect(pinOut, pinIn); //DsError.ThrowExceptionForHR(hr); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = null; // Notify that the video renderer is connected videoRendered = true; } } else if (majorType == MediaType.Mpeg2Sections) { if (subType == MediaSubType.Mpeg2Data) { // Is the MPEG-2 Sections and Tables Filter already rendered ? if (!bdaSecTabRendered) { // Get the first input pin pinIn = DsFindPin.ByDirection(bdaSecTab, PinDirection.Input, 0); // A direct connection is enough hr = graphBuilder.ConnectDirect(pinOut, pinIn, null); DsError.ThrowExceptionForHR(hr); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = null; // Notify that the MPEG-2 Sections and Tables Filter is connected bdaSecTabRendered = true; } } // This sample only support DVB-T or DVB-S so only supporting this subtype is enough. // If you want to support ATSC or ISDB, don't forget to handle these network types. else if (subType == MediaSubType.DvbSI) { // Is the BDA MPEG-2 Transport Information Filter already rendered ? if (!bdaTIFRendered) { // Get the first input pin pinIn = DsFindPin.ByDirection(bdaTIF, PinDirection.Input, 0); // A direct connection is enough hr = graphBuilder.ConnectDirect(pinOut, pinIn, null); DsError.ThrowExceptionForHR(hr); // Release the Pin Marshal.ReleaseComObject(pinIn); pinIn = null; // Notify that the BDA MPEG-2 Transport Information Filter is connected bdaTIFRendered = true; } } } } } finally { // Free COM objects Marshal.ReleaseComObject(enumMediaTypes); enumMediaTypes = null; Marshal.ReleaseComObject(pinOut); pinOut = null; } // Next pin, please ! pinNumber++; } }
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.ToLowerInvariant() != ".ts" && ext.ToLowerInvariant() != ".mpg") { Log.Info("TSReader2MP4: wrong file format"); return(false); } try { graphBuilder = (IGraphBuilder) new FilterGraph(); _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder); Log.Info("TSReader2MP4: add filesource"); TsReader reader = new TsReader(); tsreaderSource = (IBaseFilter)reader; IBaseFilter filter = (IBaseFilter)tsreaderSource; graphBuilder.AddFilter(filter, "TSReader Source"); IFileSourceFilter fileSource = (IFileSourceFilter)tsreaderSource; Log.Info("TSReader2MP4: 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("TSReader2MP4: 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("TSReader2MP4: 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("TSReader2MP4: 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("TSReader2MP4: 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("TSReader2MP4: add audio/video decoders to graph"); if (usingH264 == false) { Log.Info("TSReader2MP4: add mpeg2 video decoder:{0}", strVideoCodec); VideoCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strVideoCodec); if (VideoCodec == null) { Log.Error("TSReader2MP4: unable to add mpeg2 video decoder"); Cleanup(); return(false); } } else { Log.Info("TSReader2MP4: add h264 video codec:{0}", strH264VideoCodec); VideoCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strH264VideoCodec); if (VideoCodec == null) { Log.Error("TSReader2MP4: FAILED:unable to add h264 video codec"); Cleanup(); return(false); } } if (usingAAC == false) { Log.Info("TSReader2MP4: add mpeg2 audio codec:{0}", strAudioCodec); AudioCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strAudioCodec); if (AudioCodec == null) { Log.Error("TSReader2MP4: FAILED:unable to add mpeg2 audio codec"); Cleanup(); return(false); } } else { Log.Info("TSReader2MP4: add aac audio codec:{0}", strAACAudioCodec); AudioCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strAACAudioCodec); if (AudioCodec == null) { Log.Error("TSReader2MP4: FAILED:unable to add aac audio codec"); Cleanup(); return(false); } } Log.Info("TSReader2MP4: 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("TSReader2MP4: FAILED: unable to get pins of video/audio codecs"); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut0, pinIn0); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: unable to connect audio pins :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut1, pinIn1); if (hr != 0) { Log.Error("TSReader2MP4: FAILED: unable to connect video pins :0x{0:X}", hr); Cleanup(); return(false); } //add encoders, muxer & filewriter if (!AddCodecs(graphBuilder, info)) { return(false); } //setup graph controls mediaControl = graphBuilder as IMediaControl; mediaSeeking = tsreaderSource as IMediaSeeking; mediaEvt = graphBuilder as IMediaEventEx; mediaPos = graphBuilder as IMediaPosition; //get file duration Log.Info("TSReader2MP4: Get duration of recording"); 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("TSReader2MP4: recording duration: {0}", MediaPortal.Util.Utils.SecondsToHMSString((int)duration)); //run the graph to initialize the filters to be sure hr = mediaControl.Run(); if (hr != 0) { Log.Error("TSReader2MP4: 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; } } mediaControl.Stop(); FilterState state; mediaControl.GetState(500, out state); GC.Collect(); GC.Collect(); GC.Collect(); GC.WaitForPendingFinalizers(); graphBuilder.RemoveFilter(mp4Muxer); graphBuilder.RemoveFilter(h264Encoder); graphBuilder.RemoveFilter(aacEncoder); graphBuilder.RemoveFilter((IBaseFilter)fileWriterFilter); if (!AddCodecs(graphBuilder, info)) { return(false); } //Set Encoder quality & Muxer settings if (!EncoderSet(graphBuilder, info)) { return(false); } //start transcoding - run the graph Log.Info("TSReader2MP4: start transcoding"); //setup flow control //need to leverage CBAsePin, CPullPin & IAsyncReader methods. IAsyncReader synchVideo = null; mediaSample = VideoCodec as IMediaSample; hr = synchVideo.SyncReadAligned(mediaSample); //So we only parse decoder output whent the encoders are ready. hr = mediaControl.Run(); if (hr != 0) { Log.Error("TSReader2MP4: FAILED:unable to start graph :0x{0:X}", hr); Cleanup(); return(false); } } catch (Exception ex) { Log.Error("TSReader2MP4: Unable create graph: {0}", ex.Message); Cleanup(); return(false); } return(true); }
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.ToLowerInvariant() != ".ts" && ext.ToLowerInvariant() != ".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); }
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); } } IBDA_Topology topo = (IBDA_Topology)bdaTuner; int[] nodeTypes = new int[10]; int nodeTypesCount; // Get all nodes in the BDA Tuner hr = topo.GetNodeTypes(out nodeTypesCount, nodeTypes.Length, nodeTypes); DsError.ThrowExceptionForHR(hr); // For each nodes types for (int i = 0; i < nodeTypesCount; i++) { Guid[] nodeGuid = new Guid[10]; int nodeGuidCount; // Get its exposed interfaces hr = topo.GetNodeInterfaces(nodeTypes[i], out nodeGuidCount, nodeGuid.Length, nodeGuid); DsError.ThrowExceptionForHR(hr); // For each exposed interfaces for (int j = 0; j < nodeGuidCount; j++) { // Is IBDA_SignalStatistics supported by this node ? if (nodeGuid[j] == typeof(IBDA_SignalStatistics).GUID) { // Yes, retrieve this node object ctrlNode; hr = topo.GetControlNode(0, 1, nodeTypes[i], out ctrlNode); DsError.ThrowExceptionForHR(hr); // Do the cast (it should not fail) signalStats = ctrlNode as IBDA_SignalStatistics; // Exit the for j loop if found a SignalStatistics object if (signalStats != null) { break; } } } // If already found a SignalStatistics object, exit the for i loop if (signalStats != null) { break; } } } } }
private bool AddWmAsfWriter(string fileName, Quality quality, Standard standard) { //add asf file writer IPin pinOut0, pinOut1; IPin pinIn0, pinIn1; Log.Info("TSReader2WMV: add WM ASF Writer to graph"); string monikerAsfWriter = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{7C23220E-55BB-11D3-8B16-00C04FB6BD3D}"; fileWriterbase = Marshal.BindToMoniker(monikerAsfWriter) as IBaseFilter; if (fileWriterbase == null) { Log.Error("TSReader2WMV:FAILED:Unable to create ASF WM Writer"); Cleanup(); return(false); } int hr = graphBuilder.AddFilter(fileWriterbase, "WM ASF Writer"); if (hr != 0) { Log.Error("TSReader2WMV:FAILED:Add ASF WM Writer to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } IFileSinkFilter2 fileWriterFilter = fileWriterbase as IFileSinkFilter2; if (fileWriterFilter == null) { Log.Error("DVR2XVID:FAILED:Add unable to get IFileSinkFilter for filewriter"); Cleanup(); return(false); } hr = fileWriterFilter.SetFileName(fileName, null); hr = fileWriterFilter.SetMode(AMFileSinkFlags.OverWrite); Log.Info("TSReader2WMV: connect audio/video codecs outputs -> ASF WM Writer"); //connect output #0 of videocodec->asf writer pin 1 //connect output #0 of audiocodec->asf writer pin 0 pinOut0 = DsFindPin.ByDirection((IBaseFilter)AudioCodec, PinDirection.Output, 0); pinOut1 = DsFindPin.ByDirection((IBaseFilter)VideoCodec, PinDirection.Output, 0); if (pinOut0 == null || pinOut1 == null) { Log.Error("TSReader2WMV:FAILED:unable to get outpins of video codec"); Cleanup(); return(false); } pinIn0 = DsFindPin.ByDirection(fileWriterbase, PinDirection.Input, 0); if (pinIn0 == null) { Log.Error("TSReader2WMV:FAILED:unable to get pins of asf wm writer"); 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); } pinIn1 = DsFindPin.ByDirection(fileWriterbase, PinDirection.Input, 1); if (pinIn1 == null) { Log.Error("TSReader2WMV:FAILED:unable to get pins of asf wm writer"); 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); } IConfigAsfWriter config = fileWriterbase as IConfigAsfWriter; IWMProfileManager profileManager = null; IWMProfileManager2 profileManager2 = null; IWMProfile profile = null; hr = WMLib.WMCreateProfileManager(out profileManager); string strprofileType = ""; switch (quality) { case Quality.HiDef: //hr = WMLib.WMCreateProfileManager(out profileManager); if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHiDef-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHiDef-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHiDef-PAL.prx"); } Log.Info("TSReader2WMV: set WMV HiDef quality profile {0}", strprofileType); break; case Quality.VeryHigh: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPVeryHigh-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPVeryHigh-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPVeryHigh-PAL.prx"); } Log.Info("TSReader2WMV: set WMV Very High quality profile {0}", strprofileType); break; case Quality.High: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHigh-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHigh-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHigh-PAL.prx"); } Log.Info("TSReader2WMV: set WMV High quality profile {0}", strprofileType); break; case Quality.Medium: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPMedium-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPMedium-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPMedium-PAL.prx"); } Log.Info("TSReader2WMV: set WMV Medium quality profile {0}", strprofileType); break; case Quality.Low: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPLow-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPLow-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPLow-PAL.prx"); } Log.Info("TSReader2WMV: set WMV Low quality profile {0}", strprofileType); break; case Quality.Portable: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPPortable-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPPortable-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPPortable-PAL.prx"); } Log.Info("TSReader2WMV: set WMV Portable quality profile {0}", strprofileType); break; case Quality.Custom: //load custom profile string customBitrate = ""; //Adjust the parameters to suit the custom settings the user has selected. switch (bitrate) { case 0: customBitrate = "100Kbs"; break; case 1: customBitrate = "256Kbs"; break; case 2: customBitrate = "384Kbs"; break; case 3: customBitrate = "768Kbs"; break; case 4: customBitrate = "1536Kbs"; break; case 5: customBitrate = "3072Kbs"; break; case 6: customBitrate = "5376Kbs"; break; } Log.Info("TSReader2WMV: custom bitrate = {0}", customBitrate); //TODO: get fps values & frame size //TODO: adjust settings required //Call the SetCutomPorfile method to load the custom profile, adjust it's params from user settings & then save it. //SetCutomProfile(videoBitrate, audioBitrate, videoHeight, videoWidth, videoFps); //based on user inputs //We then reload it after as per other quality settings / profiles. strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPCustom.prx"); Log.Info("TSReader2WMV: set WMV Custom quality profile {0}", strprofileType); break; } //Loads profile from the above quality selection. using (StreamReader prx = new StreamReader(strprofileType)) { String profileContents = prx.ReadToEnd(); profileManager2 = profileManager as IWMProfileManager2; hr = profileManager2.LoadProfileByData(profileContents, out profile); } if (hr != 0) { Log.Info("TSReader2WMV: get WMV profile - FAILED! {0}", hr); Cleanup(); return(false); } Log.Info("TSReader2WMV: load profile - SUCCESS!"); //configures the WM ASF Writer to the chosen profile hr = config.ConfigureFilterUsingProfile(profile); if (hr != 0) { Log.Info("TSReader2WMV: configure profile - FAILED! {0}", hr); Cleanup(); return(false); } Log.Info("TSReader2WMV: configure profile - SUCCESS!"); //TODO: Add DB recording information into WMV. //Release resorces if (profile != null) { Marshal.ReleaseComObject(profile); profile = null; } if (profileManager != null) { Marshal.ReleaseComObject(profileManager); profileManager = null; } return(true); }
private IBaseFilter BuildFileCaptureGraph(DsDevice dev, DsDevice compressor, VideoFormatHelper.SupportedVideoFormat selectedFormat, string fileName, ref float iFrameRate, ref int iWidth, ref int iHeight) { // Capture Source (Capture/Video) --> (Input) Sample Grabber (Output) --> (Input) Video Compressor (Output) --> (Input 01/Video/) AVI Mux (Output) --> (In) FileSink IBaseFilter muxFilter = null; IFileSinkFilter fileWriterFilter = null; IBaseFilter compressorFilter = null; try { IBaseFilter capFilter; // Add the video device int hr = filterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); if (capFilter != null) { SetConfigParms(capBuilder, capFilter, selectedFormat, ref iFrameRate, ref iWidth, ref iHeight); } IBaseFilter baseGrabFlt = (IBaseFilter)samplGrabber; ConfigureSampleGrabber(samplGrabber); hr = filterGraph.AddFilter(baseGrabFlt, "ASCOM Video Grabber"); DsError.ThrowExceptionForHR(hr); // Connect the video device output to the sample grabber IPin videoCaptureOutputPin = FindPin(capFilter, PinDirection.Output, MediaType.Video, Guid.Empty, "Capture"); IPin smartTeeInputPin = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0); hr = filterGraph.Connect(videoCaptureOutputPin, smartTeeInputPin); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(videoCaptureOutputPin); Marshal.ReleaseComObject(smartTeeInputPin); // Create the file writer and AVI Mux (already connected to each other) hr = capBuilder.SetOutputFileName(MediaSubType.Avi, fileName, out muxFilter, out fileWriterFilter); DsError.ThrowExceptionForHR(hr); if (compressor != null) { // Create the compressor compressorFilter = CreateFilter(FilterCategory.VideoCompressorCategory, compressor.Name); } if (compressorFilter != null) { hr = filterGraph.AddFilter(compressorFilter, "ASCOM Video Compressor"); DsError.ThrowExceptionForHR(hr); // Connect the sample grabber Output pin to the compressor IPin grabberOutputPin = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); IPin compressorInputPin = DsFindPin.ByDirection(compressorFilter, PinDirection.Input, 0); hr = filterGraph.Connect(grabberOutputPin, compressorInputPin); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(grabberOutputPin); Marshal.ReleaseComObject(compressorInputPin); // Connect the compressor output to the AVI Mux IPin compressorOutputPin = DsFindPin.ByDirection(compressorFilter, PinDirection.Output, 0); IPin aviMuxVideoInputPin = DsFindPin.ByDirection(muxFilter, PinDirection.Input, 0); hr = filterGraph.Connect(compressorOutputPin, aviMuxVideoInputPin); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(compressorOutputPin); Marshal.ReleaseComObject(aviMuxVideoInputPin); } else { // Connect the sample grabber Output pin to the AVI Mux IPin grabberOutputPin = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0); IPin aviMuxVideoInputPin = DsFindPin.ByDirection(muxFilter, PinDirection.Input, 0); hr = filterGraph.Connect(grabberOutputPin, aviMuxVideoInputPin); DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(grabberOutputPin); Marshal.ReleaseComObject(aviMuxVideoInputPin); } return(capFilter); } finally { if (fileWriterFilter != null) { Marshal.ReleaseComObject(fileWriterFilter); } if (muxFilter != null) { Marshal.ReleaseComObject(muxFilter); } if (compressorFilter != null) { Marshal.ReleaseComObject(compressorFilter); } } }
/// <summary> /// Build the filter graph /// </summary> private void SetupGraph(string fileName, CaptureOptions.VideoCompression quality) { int hr; // Get the graphbuilder object m_FilterGraph = (IFilterGraph2) new FilterGraph(); // Get a ICaptureGraphBuilder2 to help build the graph ICaptureGraphBuilder2 icgb2 = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); 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 // Our data source IBaseFilter ipsb = (IBaseFilter) new GenericSampleSourceFilter(); try { // Get the pin from the filter so we can configure it IPin ipin = DsFindPin.ByDirection(ipsb, PinDirection.Output, 0); try { // Configure the pin using the provided BitmapInfo ConfigurePusher((IGenericSampleConfig)ipin); } finally { Marshal.ReleaseComObject(ipin); } // Add the filter to the graph hr = m_FilterGraph.AddFilter(ipsb, "GenericSampleSourceFilter"); Marshal.ThrowExceptionForHR(hr); // Create a filter for the output avi file var asfWriter = ConfigAsf(icgb2, fileName, quality); // Connect everything together hr = icgb2.RenderStream(null, null, ipsb, null, asfWriter); DsError.ThrowExceptionForHR(hr); } finally { Marshal.ReleaseComObject(ipsb); } // Grab some other interfaces m_mediaCtrl = m_FilterGraph as IMediaControl; } finally { Marshal.ReleaseComObject(icgb2); } }
/// <summary> /// Creates the filter based on the configuration file /// </summary> /// <param name="tvAudio">The tvaudio component</param> /// <param name="crossbar">The crossbar componen</param> /// <param name="tuner">The tuner component</param> /// <param name="graph">The stored graph</param> /// <param name="graphBuilder">The graphBuilder</param> /// <param name="capBuilder">The Capture graph builder</param> /// <returns>true, if the graph building was successful</returns> private bool CreateConfigurationBasedFilterInstance(Graph graph, ICaptureGraphBuilder2 capBuilder, IFilterGraph2 graphBuilder, Tuner tuner, Crossbar crossbar, TvAudio tvAudio) { string audioDeviceName = graph.Capture.AudioCaptureName; string videoDeviceName = graph.Capture.Name; DsDevice[] devices; bool videoConnected = false; bool audioConnected = false; //get a list of all video capture devices try { if (tuner.TunerName == "Adaptec USB TvTuner") { Log.Log.WriteFile("analog: Adaptec USB device detected!"); devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); } else { devices = DsDevice.GetDevicesOfCat(FilterCategory.AMKSCapture); //shouldn't be VideoInputDevice devices = DeviceSorter.Sort(devices, tuner.TunerName, tvAudio.TvAudioName, crossbar.CrossBarName); } } catch (Exception) { Log.Log.WriteFile("analog: AddTvCaptureFiler error in allocating devices collection"); return(false); } if (devices.Length == 0) { Log.Log.WriteFile("analog: AddTvCaptureFilter no tvcapture devices found"); return(false); } //try each video capture filter for (int i = 0; i < devices.Length; i++) { bool filterUsed = false; IBaseFilter tmp; if (_badCaptureDevices.Contains(devices[i].Name)) { Log.Log.WriteFile("analog: AddTvCaptureFilter bypassing: {0}", devices[i].Name); continue; } Log.Log.WriteFile("analog: AddTvCaptureFilter try:{0} {1}", devices[i].Name, i); // if video capture filter is in use, then we can skip it if (DevicesInUse.Instance.IsUsed(devices[i])) { Log.Log.WriteFile("analog: Device: {0} in use?", devices[i].Name); continue; } if (!videoDeviceName.Equals(devices[i].Name) && (audioDeviceName == null || !audioDeviceName.Equals(devices[i].Name))) { continue; } int hr; try { // add video capture filter to graph hr = graphBuilder.AddSourceFilterForMoniker(devices[i].Mon, null, devices[i].Name, out tmp); } catch (Exception) { Log.Log.WriteFile("analog: cannot add filter to graph"); continue; } if (hr != 0) { //cannot add video capture filter to graph, try next one if (tmp != null) { Log.Log.WriteFile("analog: cannot add filter: {0} to graph", devices[i].Name); graphBuilder.RemoveFilter(tmp); Release.ComObject("TvCaptureFilter", tmp); } continue; } // connect crossbar->video capture filter if (videoDeviceName.Equals(devices[i].Name) && FilterGraphTools.ConnectPin(graphBuilder, crossbar.VideoOut, tmp, graph.Capture.VideoIn)) { _filterVideoCapture = tmp; _videoCaptureDevice = devices[i]; if (_audioCaptureDevice != _videoCaptureDevice) { DevicesInUse.Instance.Add(_videoCaptureDevice); } Log.Log.WriteFile("analog: AddTvCaptureFilter connected video to crossbar successfully"); videoConnected = true; filterUsed = true; } // crossbar->audio capture filter // Many video capture are also the audio capture filter, so we can always try it again if (audioDeviceName.Equals(devices[i].Name) && FilterGraphTools.ConnectPin(graphBuilder, crossbar.AudioOut, tmp, graph.Capture.AudioIn)) { _filterAudioCapture = tmp; _audioCaptureDevice = devices[i]; if (_audioCaptureDevice != _videoCaptureDevice) { DevicesInUse.Instance.Add(_audioCaptureDevice); } Log.Log.WriteFile("analog: AddTvCaptureFilter connected audio to crossbar successfully"); audioConnected = true; filterUsed = true; } // _audioCaptureDevice should never be null - avoids null exception crashes with Encoder.cs else { _audioCaptureDevice = devices[i]; } if (!filterUsed) { // cannot connect crossbar->video capture filter, remove filter from graph // cand continue with the next vieo capture filter Log.Log.WriteFile("analog: AddTvCaptureFilter failed to connect to crossbar"); graphBuilder.RemoveFilter(tmp); Release.ComObject("capture filter", tmp); } else { i = -1; // Go through the devices again from the start... } if (videoConnected && audioConnected) { break; } } if (_filterVideoCapture != null) { if (graph.Capture.TeletextPin != -1) { _pinVBI = DsFindPin.ByDirection(_filterVideoCapture, PinDirection.Output, graph.Capture.TeletextPin); } _videoProcAmp = _filterVideoCapture as IAMVideoProcAmp; _analogVideoDecoder = _filterVideoCapture as IAMAnalogVideoDecoder; _streamConfig = _filterVideoCapture as IAMStreamConfig; _videoFormats = graph.Capture.AvailableVideoStandard; _defaultVideoProcAmpValues = graph.Capture.VideoProcAmpValues; _frameRate = graph.Capture.FrameRate; _imageWidth = graph.Capture.ImageWidth; _imageHeight = graph.Capture.ImageHeight; CheckCapabilitiesStreamConfig(graph, capBuilder); SetCaptureConfiguration(graph); } return(_filterVideoCapture != null); }
public void PrepareCapture(int i_width, int i_height, float i_frame_rate) { const int BBP = 32; //既にキャプチャ中なら諦める。 if (this.m_graphi_active) { throw new Exception(); } //現在確保中のグラフインスタンスを全て削除 CleanupGraphiObjects(); int hr; ISampleGrabber sampGrabber = null; IBaseFilter capFilter = null; IPin pSampleIn = null; //グラフビルダを作る。 this.m_FilterGraph = new FilterGraph() as IFilterGraph2; try { //フィルタグラフにキャプチャを追加して、capFilterにピンを受け取る。 hr = m_FilterGraph.AddSourceFilterForMoniker(this.m_dev.Mon, null, this.m_dev.Name, out capFilter); DsError.ThrowExceptionForHR(hr); //stillピンを探す m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0); //stillピンが無ければPreviewを探す。 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) { // There is no still pin m_VidControl = null; m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); } else { // Get a control pointer (used in Click()) m_VidControl = capFilter as IAMVideoControl; m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0); } if (i_height + i_width + BBP > 0) { SetConfigParms(m_pinStill, i_width, i_height, i_frame_rate, BBP); } // Get the SampleGrabber interface sampGrabber = new SampleGrabber() as ISampleGrabber; //sampGrabberの設定 IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter; ConfigureSampleGrabber(sampGrabber); pSampleIn = DsFindPin.ByDirection(baseGrabFlt, 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); } else { // Connect the Still pin to the sample grabber hr = m_FilterGraph.Connect(m_pinStill, pSampleIn); DsError.ThrowExceptionForHR(hr); } hr = sampGrabber.GetConnectedMediaType(this._capture_mediatype); DsError.ThrowExceptionForHR(hr); //ビデオフォーマット等の更新 upateVideoInfo(sampGrabber); } finally { if (sampGrabber != null) { Marshal.ReleaseComObject(sampGrabber); sampGrabber = null; } if (pSampleIn != null) { Marshal.ReleaseComObject(pSampleIn); pSampleIn = null; } } }
private bool AddCodecs(IGraphBuilder graphBuilder, TranscodeInfo info) { int hr; Log.Info("DVRMS2DIVX: add ffdshow (Divx) codec to graph"); string monikerXVID = @"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\ffdshow video encoder"; divxCodec = Marshal.BindToMoniker(monikerXVID) as IBaseFilter; if (divxCodec == null) { Log.Error("DVRMS2DIVX:FAILED:Unable to create Divx MPEG-4 Codec"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(divxCodec, "ffdshow video encoder"); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:Add DivX MPEG-4 Codec to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } Log.Info("DVRMS2DIVX: add MPEG3 codec to graph"); string monikerMPEG3 = @"@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\85MPEG Layer-3"; mp3Codec = Marshal.BindToMoniker(monikerMPEG3) as IBaseFilter; if (mp3Codec == null) { Log.Error("DVRMS2DIVX:FAILED:Unable to create MPEG Layer-3 Codec"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(mp3Codec, "MPEG Layer-3"); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:Add MPEG Layer-3 to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } //add filewriter Log.Info("DVRMS2DIVX: add FileWriter to graph"); string monikerFileWrite = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{8596E5F0-0DA5-11D0-BD21-00A0C911CE86}"; IBaseFilter fileWriterbase = Marshal.BindToMoniker(monikerFileWrite) as IBaseFilter; if (fileWriterbase == null) { Log.Error("DVRMS2DIVX:FAILED:Unable to create FileWriter"); Cleanup(); return(false); } fileWriterFilter = fileWriterbase as IFileSinkFilter2; if (fileWriterFilter == null) { Log.Error("DVRMS2DIVX:FAILED:Add unable to get IFileSinkFilter for filewriter"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(fileWriterbase, "FileWriter"); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:Add FileWriter to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } //set output filename //AMMediaType mt = new AMMediaType(); string outputFileName = System.IO.Path.ChangeExtension(info.file, ".avi"); Log.Info("DVRMS2DIVX: set output file to :{0}", outputFileName); hr = fileWriterFilter.SetFileName(outputFileName, null); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:unable to set filename for filewriter :0x{0:X}", hr); Cleanup(); return(false); } // add avi muxer Log.Info("DVRMS2DIVX: add AVI Muxer to graph"); string monikerAviMuxer = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{E2510970-F137-11CE-8B67-00AA00A3F1A6}"; aviMuxer = Marshal.BindToMoniker(monikerAviMuxer) as IBaseFilter; if (aviMuxer == null) { Log.Error("DVRMS2DIVX:FAILED:Unable to create AviMux"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(aviMuxer, "AviMux"); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:Add AviMux to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } //connect output of mpeg2 codec to xvid codec Log.Info("DVRMS2DIVX: connect mpeg2 video codec->divx codec"); IPin pinOut, pinIn; pinIn = DsFindPin.ByDirection(divxCodec, PinDirection.Input, 0); if (pinIn == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get input pin of divx codec:0x{0:X}", hr); Cleanup(); return(false); } pinOut = DsFindPin.ByDirection(Mpeg2VideoCodec, PinDirection.Output, 0); if (pinOut == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get output pin of mpeg2 video codec :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:unable to connect mpeg2 video codec->divx:0x{0:X}", hr); Cleanup(); return(false); } //connect output of mpeg2 audio codec to mpeg3 codec Log.Info("DVRMS2DIVX: connect mpeg2 audio codec->mp3 codec"); pinIn = DsFindPin.ByDirection(mp3Codec, PinDirection.Input, 0); if (pinIn == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get input pin of mp3 codec:0x{0:X}", hr); Cleanup(); return(false); } pinOut = DsFindPin.ByDirection(Mpeg2AudioCodec, PinDirection.Output, 0); if (pinOut == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get output pin of mpeg2 audio codec :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:unable to connect mpeg2 audio codec->mpeg3:0x{0:X}", hr); Cleanup(); return(false); } //connect output of mpeg3 codec to pin#0 of avimux Log.Info("DVRMS2DIVX: connect mp3 codec->avimux"); pinOut = DsFindPin.ByDirection(mp3Codec, PinDirection.Output, 0); if (pinOut == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get input pin of mp3 codec:0x{0:X}", hr); Cleanup(); return(false); } pinIn = DsFindPin.ByDirection(aviMuxer, PinDirection.Input, 0); if (pinIn == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get output pin of mpeg2 audio codec :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:unable to connect mpeg3 codec->avimux:0x{0:X}", hr); Cleanup(); return(false); } //connect output of xvid codec to pin#1 of avimux Log.Info("DVRMS2DIVX: connect divx codec->avimux"); pinOut = DsFindPin.ByDirection(divxCodec, PinDirection.Output, 0); if (pinOut == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get input pin of mp3 codec:0x{0:X}", hr); Cleanup(); return(false); } pinIn = DsFindPin.ByDirection(aviMuxer, PinDirection.Input, 1); if (pinIn == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get output#1 pin of avimux :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:unable to connect divx codec->avimux:0x{0:X}", hr); Cleanup(); return(false); } //connect avi mux out->filewriter in Log.Info("DVRMS2DIVX: connect avimux->filewriter"); pinOut = DsFindPin.ByDirection(aviMuxer, PinDirection.Output, 0); if (pinOut == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get output pin of avimux:0x{0:X}", hr); Cleanup(); return(false); } pinIn = DsFindPin.ByDirection(fileWriterbase, PinDirection.Input, 0); if (pinIn == null) { Log.Error("DVRMS2DIVX:FAILED:cannot get input pin of Filewriter :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut, pinIn); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:connect muxer->filewriter :0x{0:X}", hr); Cleanup(); return(false); } return(true); }
/// <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; } } }
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.ToLowerInvariant() != ".dvr-ms" && ext.ToLowerInvariant() != ".sbe") { Log.Info("DVRMS2DIVX: wrong file format"); return(false); } //disable xvid status window while encoding /* try * { * using (RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"Software\GNU\XviD", true)) * { * if (subkey != null) * { * Int32 uivalue = 0; * subkey.SetValue("display_status", (Int32)uivalue); * subkey.SetValue("debug", (Int32)uivalue); * subkey.SetValue("bitrate", (Int32)bitrate); * * uivalue = 1; * subkey.SetValue("interlacing", (Int32)uivalue); * } * } * } * catch (Exception) * { * }*/ //Type comtype = null; //object comobj = null; try { graphBuilder = (IGraphBuilder) new FilterGraph(); _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder); Log.Info("DVRMS2DIVX: add filesource"); bufferSource = (IStreamBufferSource) new StreamBufferSource(); IBaseFilter filter = (IBaseFilter)bufferSource; graphBuilder.AddFilter(filter, "SBE SOURCE"); IFileSourceFilter fileSource = (IFileSourceFilter)bufferSource; Log.Info("DVRMS2DIVX: load file:{0}", info.file); int hr = fileSource.Load(info.file, null); /*string strDemuxerMoniker = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{AFB6C280-2C41-11D3-8A60-0000F81E0E4A}"; * * mpegDemuxer = Marshal.BindToMoniker(strDemuxerMoniker) as IBaseFilter; * if (mpegDemuxer == null) * { * Log.Error("DVRMS2DIVX:FAILED:unable to add mpeg2 demuxer"); * Cleanup(); * return false; * } * hr = graphBuilder.AddFilter(mpegDemuxer, "MPEG-2 Demultiplexer"); * if (hr != 0) * { * Log.Error("DVRMS2DIVX:FAILED:Add mpeg2 demuxer to filtergraph :0x{0:X}", hr); * Cleanup(); * return false; * }*/ //add mpeg2 audio/video codecs string strVideoCodecMoniker = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{39F498AF-1A09-4275-B193-673B0BA3D478}"; string strAudioCodec = "MPC - MPA Decoder Filter"; Log.Info("DVRMS2DIVX: add MPV mpeg2 video decoder"); Mpeg2VideoCodec = Marshal.BindToMoniker(strVideoCodecMoniker) as IBaseFilter; if (Mpeg2VideoCodec == null) { Log.Error("DVRMS2DIVX:FAILED:unable to add MPV mpeg2 video decoder"); Cleanup(); return(false); } hr = graphBuilder.AddFilter(Mpeg2VideoCodec, "MPC - MPEG-2 Video Decoder (Gabest)"); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:Add MPV mpeg2 video to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } Log.Info("DVRMS2DIVX: add MPA mpeg2 audio codec:{0}", strAudioCodec); Mpeg2AudioCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strAudioCodec); if (Mpeg2AudioCodec == null) { Log.Error("DVRMS2DIVX:FAILED:unable to add MPV mpeg2 audio codec"); Cleanup(); return(false); } //connect output #0 of streambuffer source->mpeg2 audio codec pin 1 //connect output #1 of streambuffer source->mpeg2 video codec pin 1 Log.Info("DVRMS2DIVX: connect streambufer source->mpeg audio/video decoders"); IPin pinOut0, pinOut1; IPin pinIn0, pinIn1; pinOut0 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 0); //audio pinOut1 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 1); //video if (pinOut0 == null || pinOut1 == null) { Log.Error("DVRMS2DIVX:FAILED:unable to get pins of source"); Cleanup(); return(false); } pinIn0 = DsFindPin.ByDirection(Mpeg2VideoCodec, PinDirection.Input, 0); //video pinIn1 = DsFindPin.ByDirection(Mpeg2AudioCodec, PinDirection.Input, 0); //audio if (pinIn0 == null || pinIn1 == null) { Log.Error("DVRMS2DIVX:FAILED:unable to get pins of mpeg2 video/audio codec"); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut0, pinIn1); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:unable to connect audio pins :0x{0:X}", hr); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut1, pinIn0); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:unable to connect video pins :0x{0:X}", hr); Cleanup(); return(false); } if (!AddCodecs(graphBuilder, info)) { return(false); } // hr=(graphBuilder as IMediaFilter).SetSyncSource(null); // if (hr!=0) // Log.Error("DVRMS2DIVX:FAILED:to SetSyncSource :0x{0:X}",hr); mediaControl = graphBuilder as IMediaControl; mediaSeeking = bufferSource as IStreamBufferMediaSeeking; mediaEvt = graphBuilder as IMediaEventEx; mediaPos = graphBuilder as IMediaPosition; //get file duration Log.Info("DVRMS2DIVX: Get duration of movie"); 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("DVRMS2DIVX: movie duration:{0}", MediaPortal.Util.Utils.SecondsToHMSString((int)duration)); // hr=(graphBuilder as IMediaFilter).SetSyncSource(null); // if (hr!=0) // Log.Error("DVRMS2DIVX:FAILED:to SetSyncSource :0x{0:X}",hr); hr = mediaControl.Run(); if (hr != 0) { Log.Error("DVRMS2DIVX: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; } } mediaControl.Stop(); FilterState state; mediaControl.GetState(500, out state); GC.Collect(); GC.Collect(); GC.Collect(); GC.WaitForPendingFinalizers(); graphBuilder.RemoveFilter(aviMuxer); graphBuilder.RemoveFilter(divxCodec); graphBuilder.RemoveFilter(mp3Codec); graphBuilder.RemoveFilter((IBaseFilter)fileWriterFilter); if (!AddCodecs(graphBuilder, info)) { return(false); } // hr=(graphBuilder as IMediaFilter).SetSyncSource(null); // if (hr!=0) // Log.Error("DVRMS2DIVX:FAILED:to SetSyncSource :0x{0:X}",hr); Log.Info("DVRMS2DIVX: start transcoding"); hr = mediaControl.Run(); if (hr != 0) { Log.Error("DVRMS2DIVX:FAILED:unable to start graph :0x{0:X}", hr); Cleanup(); return(false); } } catch (Exception ex) { Log.Error("DVRMS2DIVX:Unable create graph: {0}", ex.Message); Cleanup(); return(false); } return(true); }
/// <summary> /// Build the filter graph /// </summary> /// <param name="hWin">Window to draw into</param> private void SetupGraph(string sFileName) { int hr; // Get the graphbuilder object m_FilterGraph = new FilterGraph() as IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph ICaptureGraphBuilder2 icgb2 = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); 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 // Our graph source filter IBaseFilter ipsb = (IBaseFilter) new GenericSamplePullFilter(); try { // Get the output pin from the filter so we can configure it IPin gssfOut = DsFindPin.ByDirection(ipsb, PinDirection.Output, 0); // Configure the pin's media type and callback string sExt = Path.GetExtension(sFileName); ConfigurePuller((IGenericPullConfig)gssfOut, sExt); // Configure the file name ConfigureRdr((ISetFileName)gssfOut, sFileName); // Free the pin Marshal.ReleaseComObject(gssfOut); // Add the filter to the graph hr = m_FilterGraph.AddFilter(ipsb, "GenericSamplePullFilter"); Marshal.ThrowExceptionForHR(hr); // Build the rest of the graph, outputting to the default renderer hr = icgb2.RenderStream(null, null, ipsb, null, null); Marshal.ThrowExceptionForHR(hr); // Connect any audio pin hr = icgb2.RenderStream(null, MediaType.Audio, ipsb, null, null); //Marshal.ThrowExceptionForHR( hr ); // Blindly assume any errors are due to no audio pin } finally { Marshal.ReleaseComObject(ipsb); } // Grab some other interfaces m_mediaCtrl = m_FilterGraph as IMediaControl; } finally { Marshal.ReleaseComObject(icgb2); } }
/// <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; // 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); Rectangle rc = hWin.ClientRectangle; hr = videoWindow.SetWindowPosition(0, 0, rc.Right, rc.Bottom); DsError.ThrowExceptionForHR(hr); } 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; } } }
/// <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; } } }
/// <summary> /// Setup the directshow graph. /// </summary> private void SetupGraph() { Ensure.IsNotNull(Log, this.sampleHandler, "sampleHandler is null"); var captureBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); try { // Link capture builder to the filter graph instance. var hr = captureBuilder.SetFiltergraph(Graph); DsError.ThrowExceptionForHR(hr); // Create GSSF var gssf = (IBaseFilter) new TStreamSourceGSSF(); IPin gssfPin = null; try { // Configure output pin of the GSSF gssfPin = DsFindPin.ByDirection(gssf, PinDirection.Output, 0); var config = (ITStreamSourceFilterConfig)gssfPin; sampleHandler.SetMediaType(config); hr = config.SetSampleCB(sampleHandler); DsError.ThrowExceptionForHR(hr); // Add the GSSF filter to the graph. hr = Graph.AddFilter(gssf, "TStreamSource GSSF"); DsError.ThrowExceptionForHR(hr); IBaseFilter pMpeg2DemuxFilter = null; IPin demuxInput = null; AMMediaType videoType = null; AMMediaType audioType = null; IPin videoOutPin = null; IPin audioOutPin = null; try { #region ADD DEMUX pMpeg2DemuxFilter = DShowUtils.AddFilterById(Graph, typeof(MPEG2Demultiplexer).GUID, "MPEG2 Demultiplexer"); Ensure.IsNotNull(Log, pMpeg2DemuxFilter, "Cannot add MPEG2 Demultiplexer to the graph."); demuxInput = DsFindPin.ByDirection(pMpeg2DemuxFilter, PinDirection.Input, 0); hr = Graph.Connect(gssfPin, demuxInput); DsError.ThrowExceptionForHR(hr); #endregion #region CREATE PINS videoType = GetVideoType(); audioType = GetAudioType(); var tmpDemux = pMpeg2DemuxFilter as IMpeg2Demultiplexer; Ensure.IsNotNull(Log, tmpDemux, "demuxer is invalid"); if (tmpDemux == null) { return; } hr = tmpDemux.CreateOutputPin(videoType, "Video", out videoOutPin); DsError.ThrowExceptionForHR(hr); Ensure.IsNotNull(Log, videoOutPin, "Video output pin is not created."); hr = tmpDemux.CreateOutputPin(audioType, "Audio", out audioOutPin); DsError.ThrowExceptionForHR(hr); Ensure.IsNotNull(Log, audioOutPin, "Audio output pin is not created."); #endregion #region MAP PINS var mapVideo = videoOutPin as IMPEG2PIDMap; Ensure.IsNotNull(Log, mapVideo, "Video mapping interface is invalid."); if (mapVideo == null) { return; } var mapAudio = audioOutPin as IMPEG2PIDMap; Ensure.IsNotNull(Log, mapAudio, "Audio mapping interface is invalid."); if (mapAudio == null) { return; } var pids = new int[1]; pids[0] = StreamInfo.GetTopVideoInfo().First; hr = mapVideo.MapPID(1, pids, MediaSampleContent.ElementaryStream); DsError.ThrowExceptionForHR(hr); pids[0] = StreamInfo.GetTopAudioInfo().First; hr = mapAudio.MapPID(1, pids, MediaSampleContent.ElementaryStream); DsError.ThrowExceptionForHR(hr); #endregion #region RENDER PINS hr = captureBuilder.RenderStream(null, MediaType.Video, pMpeg2DemuxFilter, VideoGrabber, videoRender); DsError.ThrowExceptionForHR(hr); hr = captureBuilder.RenderStream(null, MediaType.Audio, pMpeg2DemuxFilter, AudioGrabber, audioRender); DsError.ThrowExceptionForHR(hr); #endregion //var filter = Graph as IMediaFilter; //var refClock = audioRender as IReferenceClock; //filter.SetSyncSource(refClock); var filter = Graph as IMediaFilter; Ensure.IsNotNull(Log, filter, "Cannot retrieve IMediaFilter interface."); var refClock = gssf as IReferenceClock; Ensure.IsNotNull(Log, refClock, "Cannot retrieve IReferenceClock interface."); if (filter != null) { filter.SetSyncSource(refClock); } } finally { #region CLEANUP if (videoType != null) { DsUtils.FreeAMMediaType(videoType); } if (audioType != null) { DsUtils.FreeAMMediaType(audioType); } if (videoOutPin != null) { Marshal.ReleaseComObject(videoOutPin); } if (audioOutPin != null) { Marshal.ReleaseComObject(audioOutPin); } if (demuxInput != null) { Marshal.ReleaseComObject(demuxInput); } if (pMpeg2DemuxFilter != null) { Marshal.ReleaseComObject(pMpeg2DemuxFilter); } #endregion } } finally { if (gssfPin != null) { Marshal.ReleaseComObject(gssfPin); } Marshal.ReleaseComObject(gssf); } } finally { Marshal.ReleaseComObject(captureBuilder); } }
public bool Play(string fileName, Form form) { fileName += ".tsbuffer"; Log.WriteFile("play:{0}", fileName); _graphBuilder = (IFilterGraph2) new FilterGraph(); _rotEntry = new DsROTEntry(_graphBuilder); Log.WriteFile("add tsfilesource"); _tsFileSource = new TsFileSource(); _graphBuilder.AddFilter((IBaseFilter)_tsFileSource, "TsFileSource"); #region add mpeg-2 demux filter Log.WriteFile("add mpeg-2 demux"); MPEG2Demultiplexer demux = new MPEG2Demultiplexer(); _mpegDemux = (IBaseFilter)demux; int hr = _graphBuilder.AddFilter(_mpegDemux, "MPEG-2 Demultiplexer"); #endregion #region create mpeg2 demux pins Log.WriteFile("create mpeg-2 demux pins"); //create mpeg-2 demux output pins IMpeg2Demultiplexer demuxer = _mpegDemux as IMpeg2Demultiplexer; if (demuxer != null) { hr = demuxer.CreateOutputPin(GetAudioMpg2Media(), "Audio", out _pinAudio); } if (hr != 0) { Log.WriteFile("unable to create audio pin"); return(false); } if (demuxer != null) { hr = demuxer.CreateOutputPin(GetVideoMpg2Media(), "Video", out _pinVideo); } if (hr != 0) { Log.WriteFile("unable to create video pin"); return(false); } #endregion #region load file in tsfilesource Log.WriteFile("load file in tsfilesource"); IFileSourceFilter interfaceFile = (IFileSourceFilter)_tsFileSource; if (interfaceFile == null) { Log.WriteFile("TSStreamBufferPlayer9:Failed to get IFileSourceFilter"); return(false); } AMMediaType mpeg2ProgramStream = new AMMediaType(); mpeg2ProgramStream.majorType = MediaType.Stream; mpeg2ProgramStream.subType = MediaSubType.Mpeg2Program; mpeg2ProgramStream.unkPtr = IntPtr.Zero; mpeg2ProgramStream.sampleSize = 0; mpeg2ProgramStream.temporalCompression = false; mpeg2ProgramStream.fixedSizeSamples = true; mpeg2ProgramStream.formatType = FormatType.None; mpeg2ProgramStream.formatSize = 0; mpeg2ProgramStream.formatPtr = IntPtr.Zero; hr = interfaceFile.Load(fileName, mpeg2ProgramStream); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9:Failed to load file"); return(false); } #region connect tsfilesource->demux Log.WriteFile("connect tsfilesource->demux"); Log.WriteFile("TSStreamBufferPlayer9:connect tsfilesource->mpeg2 demux"); IPin pinTsOut = DsFindPin.ByDirection((IBaseFilter)_tsFileSource, PinDirection.Output, 0); if (pinTsOut == null) { Log.WriteFile("TSStreamBufferPlayer9:failed to find output pin of tsfilesource"); return(false); } IPin pinDemuxIn = DsFindPin.ByDirection(_mpegDemux, PinDirection.Input, 0); if (pinDemuxIn == null) { Log.WriteFile("TSStreamBufferPlayer9:failed to find output pin of tsfilesource"); return(false); } hr = _graphBuilder.Connect(pinTsOut, pinDemuxIn); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9:failed to connect tsfilesource->mpeg2 demux:{0:X}", hr); return(false); } Release.ComObject(pinTsOut); Release.ComObject(pinDemuxIn); #endregion #region map demux pids Log.WriteFile("map mpeg2 pids"); IMPEG2StreamIdMap pStreamId = (IMPEG2StreamIdMap)_pinVideo; hr = pStreamId.MapStreamId(0xe0, MPEG2Program.ElementaryStream, 0, 0); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9: failed to map pid 0xe0->video pin"); return(false); } pStreamId = (IMPEG2StreamIdMap)_pinAudio; hr = pStreamId.MapStreamId(0xc0, MPEG2Program.ElementaryStream, 0, 0); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9: failed to map pid 0xc0->audio pin"); return(false); } #endregion #region render demux audio/video pins Log.WriteFile("render pins"); hr = _graphBuilder.Render(_pinAudio); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9:failed to render video output pin:{0:X}", hr); } hr = _graphBuilder.Render(_pinVideo); if (hr != 0) { Log.WriteFile("TSStreamBufferPlayer9:failed to render audio output pin:{0:X}", hr); } #endregion #endregion _videoWin = _graphBuilder as IVideoWindow; if (_videoWin != null) { _videoWin.put_Visible(OABool.True); _videoWin.put_Owner(form.Handle); _videoWin.put_WindowStyle( (WindowStyle)((int)WindowStyle.Child + (int)WindowStyle.ClipSiblings + (int)WindowStyle.ClipChildren)); _videoWin.put_MessageDrain(form.Handle); _videoWin.SetWindowPosition(190, 250, 150, 150); } Log.WriteFile("run graph"); _mediaCtrl = (IMediaControl)_graphBuilder; hr = _mediaCtrl.Run(); Log.WriteFile("TSStreamBufferPlayer9:running:{0:X}", hr); return(true); }
/// <summary> /// Displays a property page for a filter /// </summary> /// <param name="dev">The filter for which to display a property page</param> private void DisplayPropertyPage(IBaseFilter dev) { //Get the ISpecifyPropertyPages for the filter ISpecifyPropertyPages pProp = dev as ISpecifyPropertyPages; int hr = 0; if (pProp == null) { //If the filter doesn't implement ISpecifyPropertyPages, try displaying IAMVfwCompressDialogs instead! IAMVfwCompressDialogs compressDialog = dev as IAMVfwCompressDialogs; if (compressDialog != null) { hr = compressDialog.ShowDialog(VfwCompressDialogs.Config, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); } return; } //Get the name of the filter from the FilterInfo struct FilterInfo filterInfo; hr = dev.QueryFilterInfo(out filterInfo); DsError.ThrowExceptionForHR(hr); // Get the propertypages from the property bag DsCAUUID caGUID; hr = pProp.GetPages(out caGUID); DsError.ThrowExceptionForHR(hr); // Check for property pages on the output pin IPin pPin = DsFindPin.ByDirection(dev, PinDirection.Output, 0); ISpecifyPropertyPages pProp2 = pPin as ISpecifyPropertyPages; if (pProp2 != null) { DsCAUUID caGUID2; hr = pProp2.GetPages(out caGUID2); DsError.ThrowExceptionForHR(hr); if (caGUID2.cElems > 0) { int soGuid = Marshal.SizeOf(typeof(Guid)); // Create a new buffer to hold all the GUIDs IntPtr p1 = Marshal.AllocCoTaskMem((caGUID.cElems + caGUID2.cElems) * soGuid); // Copy over the pages from the Filter for (int x = 0; x < caGUID.cElems * soGuid; x++) { Marshal.WriteByte(p1, x, Marshal.ReadByte(caGUID.pElems, x)); } // Add the pages from the pin for (int x = 0; x < caGUID2.cElems * soGuid; x++) { Marshal.WriteByte(p1, x + (caGUID.cElems * soGuid), Marshal.ReadByte(caGUID2.pElems, x)); } // Release the old memory Marshal.FreeCoTaskMem(caGUID.pElems); Marshal.FreeCoTaskMem(caGUID2.pElems); // Reset caGUID to include both caGUID.pElems = p1; caGUID.cElems += caGUID2.cElems; } } // Create and display the OlePropertyFrame object oDevice = (object)dev; hr = OleCreatePropertyFrame(this.Handle, 0, 0, filterInfo.achName, 1, ref oDevice, caGUID.cElems, caGUID.pElems, 0, 0, IntPtr.Zero); DsError.ThrowExceptionForHR(hr); // Release COM objects Marshal.FreeCoTaskMem(caGUID.pElems); Marshal.ReleaseComObject(pProp); if (filterInfo.pGraph != null) { Marshal.ReleaseComObject(filterInfo.pGraph); } }