/// <summary> create the used COM components and get the interfaces. </summary> protected virtual bool GetInterfaces(string filename) { Log.Info("StreambufferPlayer:GetInterfaces()"); //Type comtype = null; object comobj = null; try { _graphBuilder = (IGraphBuilder)new FilterGraph(); _bufferSource = (IStreamBufferSource)new StreamBufferSource(); int hr; m_StreamBufferConfig = new StreamBufferConfig(); streamConfig2 = m_StreamBufferConfig as IStreamBufferConfigure2; if (streamConfig2 != null) { // setting the StreamBufferEngine registry key IntPtr HKEY = (IntPtr)unchecked((int)0x80000002L); IStreamBufferInitialize pTemp = (IStreamBufferInitialize)streamConfig2; IntPtr subKey = IntPtr.Zero; RegOpenKeyEx(HKEY, "SOFTWARE\\MediaPortal", 0, 0x3f, out subKey); hr = pTemp.SetHKEY(subKey); hr = streamConfig2.SetFFTransitionRates(8, 32); //Log.Info("set FFTransitionRates:{0:X}",hr); int max, maxnon; hr = streamConfig2.GetFFTransitionRates(out max, out maxnon); //Log.Info("get FFTransitionRates:{0} {1} {2:X}",max,maxnon,hr); streamConfig2.GetBackingFileCount(out _minBackingFiles, out _maxBackingFiles); streamConfig2.GetBackingFileDuration(out _backingFileDuration); } IBaseFilter filter = (IBaseFilter)_bufferSource; _graphBuilder.AddFilter(filter, "SBE SOURCE"); IFileSourceFilter fileSource = (IFileSourceFilter)_bufferSource; hr = fileSource.Load(filename, null); // add preferred video & audio codecs string strVideoCodec = ""; string strAudioCodec = ""; string strAudiorenderer = ""; int intFilters = 0; // FlipGer: count custom filters string strFilters = ""; // FlipGer: collect custom filters using (Settings xmlreader = new MPSettings()) { _CodecSupportsFastSeeking = xmlreader.GetValueAsBool("debug", "CodecSupportsFastSeeking", false); Log.Debug("BaseStreamBufferPlayer: Codec supports fast seeking = {0}", _CodecSupportsFastSeeking); // FlipGer: load infos for custom filters int intCount = 0; while (xmlreader.GetValueAsString("mytv", "filter" + intCount.ToString(), "undefined") != "undefined") { if (xmlreader.GetValueAsBool("mytv", "usefilter" + intCount.ToString(), false)) { strFilters += xmlreader.GetValueAsString("mytv", "filter" + intCount.ToString(), "undefined") + ";"; intFilters++; } intCount++; } strVideoCodec = xmlreader.GetValueAsString("mytv", "videocodec", ""); strAudioCodec = xmlreader.GetValueAsString("mytv", "audiocodec", ""); strAudiorenderer = xmlreader.GetValueAsString("mytv", "audiorenderer", "Default DirectSound Device"); string strValue = xmlreader.GetValueAsString("mytv", "defaultar", "Normal"); GUIGraphicsContext.ARType = Util.Utils.GetAspectRatio(strValue); } if (strVideoCodec.Length > 0) { _videoCodecFilter = DirectShowUtil.AddFilterToGraph(_graphBuilder, strVideoCodec); } if (strAudioCodec.Length > 0) { _audioCodecFilter = DirectShowUtil.AddFilterToGraph(_graphBuilder, strAudioCodec); } if (strAudiorenderer.Length > 0) { _audioRendererFilter = DirectShowUtil.AddAudioRendererToGraph(_graphBuilder, strAudiorenderer, true); } // FlipGer: add custom filters to graph customFilters = new IBaseFilter[intFilters]; string[] arrFilters = strFilters.Split(';'); for (int i = 0; i < intFilters; i++) { customFilters[i] = DirectShowUtil.AddFilterToGraph(_graphBuilder, arrFilters[i]); } //render outputpins of SBE DirectShowUtil.RenderOutputPins(_graphBuilder, (IBaseFilter)fileSource); _mediaCtrl = (IMediaControl)_graphBuilder; _videoWin = _graphBuilder as IVideoWindow; _mediaEvt = (IMediaEventEx)_graphBuilder; _mediaSeeking = _bufferSource as IStreamBufferMediaSeeking; _mediaSeeking2 = _bufferSource as IStreamBufferMediaSeeking2; if (_mediaSeeking == null) { Log.Error("Unable to get IMediaSeeking interface#1"); } if (_mediaSeeking2 == null) { Log.Error("Unable to get IMediaSeeking interface#2"); } if (_audioRendererFilter != null) { IMediaFilter mp = _graphBuilder as IMediaFilter; IReferenceClock clock = _audioRendererFilter as IReferenceClock; hr = mp.SetSyncSource(clock); } _basicVideo = _graphBuilder as IBasicVideo2; _basicAudio = _graphBuilder as IBasicAudio; //Log.Info("StreamBufferPlayer:SetARMode"); DirectShowUtil.SetARMode(_graphBuilder, AspectRatioMode.Stretched); _graphBuilder.SetDefaultSyncSource(); //Log.Info("StreamBufferPlayer: set Deinterlace"); //Log.Info("StreamBufferPlayer: done"); return true; } catch (Exception ex) { Log.Error("StreamBufferPlayer:exception while creating DShow graph {0} {1}", ex.Message, ex.StackTrace); return false; } finally { if (comobj != null) { DirectShowUtil.ReleaseComObject(comobj); } comobj = null; } }
void ITimeShifting.SetRate(double rate) { if (rate >= 0 && rate < 0.1) { rate = 0.1; } else if (rate < 0 && rate > -0.1) { rate = -0.1; } int hr = 0; IStreamBufferMediaSeeking2 mediaSeeking2 = this.streamBufferSource as IStreamBufferMediaSeeking2; if (mediaSeeking2 != null) { //mediaSeeking2.SetRateEx(rate, 25); hr = mediaSeeking2.SetRateEx(rate, 25); if (hr == 0) { return; } } IStreamBufferMediaSeeking mediaSeeking1 = this.streamBufferSource as IStreamBufferMediaSeeking; if (mediaSeeking1 != null) { hr = mediaSeeking1.SetRate(rate); if (hr == 0) { return; } } IMediaSeeking mediaSeeking0 = this.graphBuilder2 as IMediaSeeking; if (mediaSeeking0 != null) { hr = mediaSeeking0.SetRate(rate); if (hr == 0) { return; } } }
/// <summary> do cleanup and release DirectShow. </summary> protected virtual void CloseInterfaces() { int hr; if (_graphBuilder == null) { Log.Info("StreamBufferPlayer:grapbuilder=null"); return; } Log.Info("StreamBufferPlayer:cleanup DShow graph"); try { if (_mediaCtrl != null) { hr = _mediaCtrl.Stop(); _mediaCtrl = null; } _state = PlayState.Init; _mediaEvt = null; _isWindowVisible = false; _isVisible = false; _videoWin = null; _mediaSeeking = null; _mediaSeeking2 = null; _basicAudio = null; _basicVideo = null; _bufferSource = null; if (_videoCodecFilter != null) { while ((hr = DirectShowUtil.ReleaseComObject(_videoCodecFilter)) > 0) { ; } _videoCodecFilter = null; } if (_audioCodecFilter != null) { while ((hr = DirectShowUtil.ReleaseComObject(_audioCodecFilter)) > 0) { ; } _audioCodecFilter = null; } if (_audioRendererFilter != null) { while ((hr = DirectShowUtil.ReleaseComObject(_audioRendererFilter)) > 0) { ; } _audioRendererFilter = null; } // FlipGer: release custom filters for (int i = 0; i < customFilters.Length; i++) { if (customFilters[i] != null) { while ((hr = DirectShowUtil.ReleaseComObject(customFilters[i])) > 0) { ; } } customFilters[i] = null; } if (streamConfig2 != null) { while ((hr = DirectShowUtil.ReleaseComObject(streamConfig2)) > 0) { ; } streamConfig2 = null; } m_StreamBufferConfig = null; if (_graphBuilder != null) { DirectShowUtil.RemoveFilters(_graphBuilder); if (_rotEntry != null) { _rotEntry.SafeDispose(); _rotEntry = null; } while ((hr = DirectShowUtil.ReleaseComObject(_graphBuilder)) > 0) { ; } _graphBuilder = null; } _state = PlayState.Init; GUIGraphicsContext.form.Invalidate(true); } catch (Exception ex) { Log.Error("StreamBufferPlayer:exception while cleanuping DShow graph {0} {1}", ex.Message, ex.StackTrace); } //Log.Info("StreamBufferPlayer:cleanup done"); }
private void Configure() { // In order to lock a profile, you have to have at least one stream // connected to the sink. I connect a video thru the DVVideoEnc into // the StreamBufferSink. int hr; IBaseFilter pFilter; ICaptureGraphBuilder2 icgb = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); StreamBufferSink sbk = new StreamBufferSink(); m_FilterGraph = (IFilterGraph2) new FilterGraph(); DsROTEntry ds = new DsROTEntry(m_FilterGraph); hr = icgb.SetFiltergraph(m_FilterGraph); DsError.ThrowExceptionForHR(hr); hr = m_FilterGraph.AddFilter((IBaseFilter)sbk, "StreamBufferSink"); DsError.ThrowExceptionForHR(hr); DsDevice [] devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); hr = m_FilterGraph.AddSourceFilterForMoniker(devs[0].Mon, null, devs[0].Name, out pFilter); DsError.ThrowExceptionForHR(hr); DVVideoEnc dve = new DVVideoEnc(); IBaseFilter ibfVideoEnc = (IBaseFilter)dve; hr = m_FilterGraph.AddFilter(ibfVideoEnc, "dvenc"); DsError.ThrowExceptionForHR(hr); hr = icgb.RenderStream(null, null, pFilter, ibfVideoEnc, (IBaseFilter)sbk); DsError.ThrowExceptionForHR(hr); IStreamBufferSink isbc = (IStreamBufferSink)sbk; hr = isbc.LockProfile(null); DsError.ThrowExceptionForHR(hr); IPin i = DsFindPin.ByDirection((IBaseFilter)sbk, PinDirection.Input, 0); ((IMediaControl)m_FilterGraph).Run(); // -------------------------- IBaseFilter streamBuffer = null; m_FilterGraph2 = (IFilterGraph2) new FilterGraph(); DsROTEntry ds2 = new DsROTEntry(m_FilterGraph2); streamBuffer = (IBaseFilter) new StreamBufferSource(); hr = m_FilterGraph2.AddFilter(streamBuffer, "Stream buffer sink"); DsError.ThrowExceptionForHR(hr); IStreamBufferSource sbsrc = (IStreamBufferSource)streamBuffer; hr = sbsrc.SetStreamSink(isbc); DsError.ThrowExceptionForHR(hr); IPin i2 = DsFindPin.ByDirection((IBaseFilter)streamBuffer, PinDirection.Output, 0); ((IMediaControl)m_FilterGraph2).Run(); m_sbms = (IStreamBufferMediaSeeking2)sbsrc; // -------------------------- Marshal.ReleaseComObject(pFilter); Marshal.ReleaseComObject(icgb); }