//カメラのPINにアクセス static IPin GetPin(IBaseFilter filter, string pinname) { IEnumPins epins; int hr = filter.EnumPins(out epins); if (hr < 0) { CapTest.CustomMessage.ShowMessage("Can't enumerate pins"); DsError.ThrowExceptionForHR(hr); } IntPtr fetched = Marshal.AllocCoTaskMem(4); IPin[] pins = new IPin[1]; while (epins.Next(1, pins, fetched) == 0) { PinInfo pinfo; pins[0].QueryPinInfo(out pinfo); bool found = (pinfo.name == pinname); CapTest.CustomMessage.ShowMessage(pinfo.name + " is PIN NAME"); DsUtils.FreePinInfo(pinfo); if (found) { return(pins[0]); } } CapTest.CustomMessage.ShowMessage("Pin not found"); DsError.ThrowExceptionForHR(hr); return(null); }
private IPin findPin(IBaseFilter filter, string pinName) { IEnumPins ePins; int hr = 0; hr = filter.EnumPins(out ePins); checkHR(hr, "Couldn't enumerate pins on filter"); IntPtr fetched = Marshal.AllocCoTaskMem(4); IPin[] pins = new IPin[1]; while (ePins.Next(1, pins, fetched) == 0) { PinInfo pinInfo; pins[0].QueryPinInfo(out pinInfo); bool found = (pinInfo.name == pinName); DsUtils.FreePinInfo(pinInfo); if (found) { return(pins[0]); } } checkHR(-1, "Couldn't find pin `" + pinName + "`"); return(null); }
protected static IPin[] GetPins(IBaseFilter filter, string vPinName) { var result = new List <IPin>(); var ppPins = new IPin[1]; IEnumPins ppEnum; DsError.ThrowExceptionForHR(filter.EnumPins(out ppEnum)); try { while (ppEnum.Next(1, ppPins, IntPtr.Zero) == 0) { PinInfo pInfo; DsError.ThrowExceptionForHR(ppPins[0].QueryPinInfo(out pInfo)); if (pInfo.name == vPinName) { result.Add(ppPins[0]); } else { Marshal.ReleaseComObject(ppPins[0]); } DsUtils.FreePinInfo(pInfo); } } finally { Marshal.ReleaseComObject(ppEnum); } return(result.ToArray()); }
/// <summary> /// Initializes a new instance of the <see cref="Pin"/> class. /// </summary> /// <param name="sourcePin">The source pin.</param> public Pin(IPin sourcePin) { Object = sourcePin; _pinName = new Lazy <string>( () => { var result = "pin"; if (Object.QueryPinInfo(out var pinInfo) == 0) { try { result = pinInfo.name; } finally { DsUtils.FreePinInfo(pinInfo); } } return(result); }); _pinDirection = new Lazy <PinDirection>( () => { var hr = Object.QueryDirection(out var pinDirection); DsError.ThrowExceptionForHR(hr); return(pinDirection); }); }
public static bool DisconnectPin(IGraphBuilder graphBuilder, IPin pin) { IPin other; int hr = pin.ConnectedTo(out other); bool allDisconnected = true; PinInfo info; pin.QueryPinInfo(out info); DsUtils.FreePinInfo(info); if (hr == 0 && other != null) { other.QueryPinInfo(out info); if (!DisconnectAllPins(graphBuilder, info.filter)) { allDisconnected = false; } hr = pin.Disconnect(); if (hr != 0) { allDisconnected = false; } hr = other.Disconnect(); if (hr != 0) { allDisconnected = false; } DsUtils.FreePinInfo(info); Marshal.ReleaseComObject(other); } else { } return(allDisconnected); }
/// <summary> /// Retrieves what pin is connected to. /// </summary> /// <returns>Returns <see cref="BaseFilter"/> instance if pin is connected; elsewhere returns <b>null</b>.</returns> public BaseFilter ConnectedToFilter() { var other = ConnectedTo(); if (other != null) { var info = new PinInfo(); BaseFilter result = null; try { if (other.Object.QueryPinInfo(out info) == 0) { result = new BaseFilter(info.filter); } } finally { DsUtils.FreePinInfo(info); } return(result); } return(null); }
string getCatName(IBaseFilter filter) { string retval = ""; IEnumPins epins; int hr = filter.EnumPins(out epins); if (hr < 0) { CapTest.CustomMessage.ShowMessage("Can't enumerate pins"); DsError.ThrowExceptionForHR(hr); } IntPtr fetched = Marshal.AllocCoTaskMem(4); IPin[] pins = new IPin[1]; while (epins.Next(1, pins, fetched) == 0) { PinInfo pinfo; pins[0].QueryPinInfo(out pinfo); bool found = (pinfo.name == "Capture" || pinfo.name == "キャプチャ"); CapTest.CustomMessage.ShowMessage(pinfo.name + " is pinname on getCatName"); DsUtils.FreePinInfo(pinfo); if (found) { retval = pinfo.name; } } CapTest.CustomMessage.ShowMessage("Pin found " + retval); return(retval); }
/// <summary> /// Add, Remove, and Update Pin property pages in the _properties control /// </summary> internal void SyncPinPropertyPages(PropertyPagePanel properties) { if (properties == null) { properties = _properties; } if (properties != null) { // remove any pin property pages that are no longer valid List <IPin> pins = (Node as DSFilterNode).GetPins(); for (int i = properties.TabControl.Controls.Count - 1; i > -1; i--) { try { if (properties.TabControl.Controls[i].Tag is IPin) { if (!pins.Contains(_properties.TabControl.Controls[i].Tag as IPin)) { properties.TabControl.Controls.RemoveAt(i); } } } catch { // the IPin was removed from the filter before we could sync it // go ahead and remove the property page properties.TabControl.Controls.RemoveAt(i); } } // find or create a new tabpage for each remaining pin foreach (IPin pin in pins) { TabPage tp = GetPinPropertyPage(pin); PinPropertiesTextBox tbox = null; if (tp == null) { // we don't have this one yet so create it PinInfo pi; pin.QueryPinInfo(out pi); tp = new TabPage(pi.name); DsUtils.FreePinInfo(pi); tp.Tag = pin; tbox = new PinPropertiesTextBox(pin); tp.Controls.Add(tbox); properties.TabControl.Controls.Add(tp); } else { // we already have this property page, refresh it's text box tbox = tp.Controls[0] as PinPropertiesTextBox; tbox.RefreshProperties(); } } } }
/// <summary> /// Releases all refernces and memory held by this structure /// </summary> public void Dispose() { BaseDSGraph.Release(this.Pin); DsUtils.FreePinInfo(this.Info); if (this.Type != null) { DsUtils.FreeAMMediaType(this.Type); this.Type = null; } }
/// <summary> /// Frees all memory found in an enumerable collection of PinInfo /// </summary> /// <param name="pinInfo"></param> public static void Release(this IEnumerable <PinInfo> pinInfo) { if (pinInfo != null) { foreach (PinInfo p in pinInfo) { DsUtils.FreePinInfo(p); } } }
public void ListPin(IBaseFilter filter) { _class.Var.PinIn = new List <string>(); _class.Var.PinOut = new List <string>(); FilterInfo pOut; filter.QueryFilterInfo(out pOut); _class.Debug.Log("[3] listing Pins [" + pOut.achName + "]"); try { IEnumPins epins; int hr = filter.EnumPins(out epins); if (hr < 0) { _class.Debug.Log("[0] [NG] Can't find pins"); } else { IntPtr fetched = Marshal.AllocCoTaskMem(4); IPin[] pins = new IPin[1]; while (epins.Next(1, pins, fetched) == 0) { PinInfo pinfo; pins[0].QueryPinInfo(out pinfo); if (pinfo.dir.ToString().ToLower() == "input") { _class.Var.PinIn.Add(pinfo.name); } if (pinfo.dir.ToString().ToLower() == "output") { _class.Var.PinOut.Add(pinfo.name); } DsUtils.FreePinInfo(pinfo); } } for (int intCount = 0; intCount < _class.Var.PinIn.Count; intCount++) { _class.Debug.Log("-" + _class.Var.PinIn[intCount] + " (Input)"); } for (int intCount = 0; intCount < _class.Var.PinOut.Count; intCount++) { _class.Debug.Log("-" + _class.Var.PinOut[intCount] + " (Output)"); } } catch { _class.Debug.Log("[0] [FAIL] Error listing pins"); } }
public DSInputPin(IPin pin) : base() { _pin = pin; // get the name of the IPin PinInfo pi; _pin.QueryPinInfo(out pi); Name = pi.name; DsUtils.FreePinInfo(pi); }
private static IPin t最初の入力ピンを探して返す(IBaseFilter baseFilter) { int hr = 0; IPin firstInputPin = null; IEnumPins ePins; hr = baseFilter.EnumPins(out ePins); DsError.ThrowExceptionForHR(hr); try { var pins = new IPin[1]; while (ePins.Next(1, pins, IntPtr.Zero) == CWin32.S_OK) { PinInfo pinfo = new PinInfo() { filter = null }; try { hr = pins[0].QueryPinInfo(out pinfo); DsError.ThrowExceptionForHR(hr); if (pinfo.dir == PinDirection.Input) { firstInputPin = pins[0]; break; } } finally { if (pinfo.filter != null) { CCommon.tReleaseComObject(ref pinfo.filter); } DsUtils.FreePinInfo(pinfo); if (firstInputPin == null) { CCommon.tReleaseComObject(ref pins[0]); } } } } finally { CCommon.tReleaseComObject(ref ePins); } return(firstInputPin); }
public Pin(PinDirection dir, Filter f, IPin _ipin, int _num) //x,y in pixels inside filter rect { direction = dir; filter = f; num = _num; rect = new Rectangle(0, 0, 10, 10); ipin = _ipin; PinInfo pinfo; ipin.QueryPinInfo(out pinfo); name = pinfo.name; uniqname = f.Name + "." + pinfo.name; DsUtils.FreePinInfo(pinfo); }
public static int GetPin(IBaseFilter filter, PinDirection pinDir, string name, out IPin ppPin) { ppPin = null; IEnumPins enumPins = null; IPin[] pins = new IPin[1] { null }; try { int hr = filter.EnumPins(out enumPins); DsError.ThrowExceptionForHR(hr); while (enumPins.Next(1, pins, IntPtr.Zero) == 0) { PinInfo pi; hr = pins[0].QueryPinInfo(out pi); bool found = false; if (hr == 0 && pi.dir == pinDir && pi.name == name) { found = true; ppPin = pins[0]; DsUtils.FreePinInfo(pi); } if (found) { return(0); } Util.ReleaseComObject(ref pins[0]); } // Did not find a matching pin. } catch (COMException) { } finally { Marshal.ReleaseComObject(enumPins); } return(WinAPI.E_FAIL); }
public DSOutputPin(IPin pin) : base() { // store the IPin _pin = pin; // we're only using the UI to work with a DirectShow graph // so we never pass data, and don't allow pin multi-connect AllowMultiConnect = false; PassByClone = PassPinDataAsClone.Never; // get the name of the IPin PinInfo pi; _pin.QueryPinInfo(out pi); Name = pi.name; DsUtils.FreePinInfo(pi); }
private IPin GetPin(IBaseFilter filter, string pinname) { int hr = filter.EnumPins(out IEnumPins epins); checkHR(hr, "Can't enumerate pins"); IntPtr fetched = Marshal.AllocCoTaskMem(4); IPin[] pins = new IPin[1]; while (epins.Next(1, pins, fetched) == 0) { pins[0].QueryPinInfo(out PinInfo pinfo); bool found = (pinfo.name == pinname); DsUtils.FreePinInfo(pinfo); if (found) { return(pins[0]); } } checkHR(-1, "Pin not found"); return(null); }
private IPin GetPin(IBaseFilter filter, Func <PinInfo, bool> SearchCriteria) { int statusCode = filter.EnumPins(out IEnumPins epins); errorHandler.ShowError(statusCode, "Can't enumerate pins"); var fetched = Marshal.AllocCoTaskMem(4); var pins = new IPin[1]; while (epins.Next(1, pins, fetched) == 0) { pins[0].QueryPinInfo(out PinInfo pinInfo); bool found = SearchCriteria(pinInfo); DsUtils.FreePinInfo(pinInfo); if (found) { return(pins[0]); } } errorHandler.ShowError(-1, "Pin not found"); return(null); }
public static bool DisconnectAllPins(IGraphBuilder graphBuilder, IBaseFilter filter) { IEnumPins pinEnum; int hr = filter.EnumPins(out pinEnum); if (hr != 0 || pinEnum == null) { return(false); } FilterInfo info; filter.QueryFilterInfo(out info); Marshal.ReleaseComObject(info.pGraph); bool allDisconnected = true; for (; ;) { IPin[] pins = new IPin[1]; IntPtr fetched = IntPtr.Zero; hr = pinEnum.Next(1, pins, fetched); if (hr != 0 || fetched == IntPtr.Zero) { break; } PinInfo pinInfo; pins[0].QueryPinInfo(out pinInfo); DsUtils.FreePinInfo(pinInfo); if (pinInfo.dir == PinDirection.Output) { if (!DisconnectPin(graphBuilder, pins[0])) { allDisconnected = false; } } Marshal.ReleaseComObject(pins[0]); } Marshal.ReleaseComObject(pinEnum); return(allDisconnected); }
private IPin FindPin(string pin_name, IBaseFilter filter) { IEnumPins enum_pins; ReturnHR = filter.EnumPins(out enum_pins); IPin[] pin = new IPin[1]; while (enum_pins.Next(1, pin, IntPtr.Zero) == 0) { PinInfo pin_info; pin[0].QueryPinInfo(out pin_info); if (String.Compare(pin_info.name, pin_name) == 0) { DsUtils.FreePinInfo(pin_info); return(pin[0]); } } LogError("Pin not found"); return(null); }
protected virtual void InsertAudioFilter(IBaseFilter sourceFilter, string audioDecoder) { if (string.IsNullOrEmpty(audioDecoder)) { return; } // Set Audio Codec // Remove Pin var audioPinFrom = DirectShowLib.DsFindPin.ByName(sourceFilter, "Audio"); IPin audioPinTo; if (audioPinFrom != null) { int hr = audioPinFrom.ConnectedTo(out audioPinTo); if (hr >= 0 && audioPinTo != null) { PinInfo pInfo; audioPinTo.QueryPinInfo(out pInfo); FilterInfo fInfo; pInfo.filter.QueryFilterInfo(out fInfo); DirectShowUtil.DisconnectAllPins(m_graph, pInfo.filter); m_graph.RemoveFilter(pInfo.filter); DsUtils.FreePinInfo(pInfo); Marshal.ReleaseComObject(fInfo.pGraph); Marshal.ReleaseComObject(audioPinTo); audioPinTo = null; } Marshal.ReleaseComObject(audioPinFrom); audioPinFrom = null; } DirectShowUtil.AddFilterToGraph(m_graph, audioDecoder, Guid.Empty); }
public IPin GetPin(IBaseFilter filter, string pinname) { IEnumPins epins; if (filter != null) { int hr = filter.EnumPins(out epins); IntPtr fetched = Marshal.AllocCoTaskMem(4); IPin[] pins = new IPin[1]; while (epins.Next(1, pins, fetched) == 0) { PinInfo pinfo; pins[0].QueryPinInfo(out pinfo); bool found = (pinfo.name == pinname); DsUtils.FreePinInfo(pinfo); if (found) { return(pins[0]); } } } return(null); }
static IPin GetPin(IBaseFilter pFilter, string pinname) { IEnumPins pEnum; IntPtr pPin = Marshal.AllocCoTaskMem(4); int hr = pFilter.EnumPins(out pEnum); checkHR(hr, "Can't enumerate pins"); IPin[] pins = new IPin[1]; while (pEnum.Next(1, pins, pPin) == 0) { PinInfo pinfo; pins[0].QueryPinInfo(out pinfo); bool found = (pinname == pinfo.name); DsUtils.FreePinInfo(pinfo); if (found) { return(pins[0]); } } checkHR(-1, "Pin not found"); return(null); }
/// <summary> /// Opens the media by initializing the DirectShow graph /// </summary> protected virtual void OpenSource() { /* Make sure we clean up any remaining mess */ FreeResources(); if (m_sourceUri == null) { return; } string fileSource = m_sourceUri.OriginalString; if (string.IsNullOrEmpty(fileSource)) { return; } try { /* Creates the GraphBuilder COM object */ m_graph = new FilterGraphNoThread() as IGraphBuilder; if (m_graph == null) { throw new Exception("Could not create a graph"); } var filterGraph = m_graph as IFilterGraph2; if (filterGraph == null) { throw new Exception("Could not QueryInterface for the IFilterGraph2"); } IBaseFilter sourceFilter; int hr; //var file = System.IO.File.CreateText(@"M:\DirectShowLog.txt"); //filterGraph.SetLogFile((file.BaseStream as System.IO.FileStream).SafeFileHandle.DangerousGetHandle()); // Set LAV Splitter LAVSplitterSource reader = new LAVSplitterSource(); sourceFilter = reader as IBaseFilter; var objectWithSite = reader as IObjectWithSite; if (objectWithSite != null) { objectWithSite.SetSite(this); } hr = m_graph.AddFilter(sourceFilter, SplitterSource); DsError.ThrowExceptionForHR(hr); IFileSourceFilter interfaceFile = (IFileSourceFilter)sourceFilter; hr = interfaceFile.Load(fileSource, null); DsError.ThrowExceptionForHR(hr); // Set Video Codec // Remove Pin var videoPinFrom = DirectShowLib.DsFindPin.ByName(sourceFilter, "Video"); IPin videoPinTo; if (videoPinFrom != null) { hr = videoPinFrom.ConnectedTo(out videoPinTo); if (hr >= 0 && videoPinTo != null) { PinInfo pInfo; videoPinTo.QueryPinInfo(out pInfo); FilterInfo fInfo; pInfo.filter.QueryFilterInfo(out fInfo); DirectShowUtil.DisconnectAllPins(m_graph, pInfo.filter); m_graph.RemoveFilter(pInfo.filter); DsUtils.FreePinInfo(pInfo); Marshal.ReleaseComObject(fInfo.pGraph); Marshal.ReleaseComObject(videoPinTo); videoPinTo = null; } Marshal.ReleaseComObject(videoPinFrom); videoPinFrom = null; } DirectShowUtil.AddFilterToGraph(m_graph, VideoDecoder, Guid.Empty); // Set Audio Codec // Remove Pin var audioPinFrom = DirectShowLib.DsFindPin.ByName(sourceFilter, "Audio"); IPin audioPinTo; if (audioPinFrom != null) { hr = audioPinFrom.ConnectedTo(out audioPinTo); if (hr >= 0 && audioPinTo != null) { PinInfo pInfo; audioPinTo.QueryPinInfo(out pInfo); FilterInfo fInfo; pInfo.filter.QueryFilterInfo(out fInfo); DirectShowUtil.DisconnectAllPins(m_graph, pInfo.filter); m_graph.RemoveFilter(pInfo.filter); DsUtils.FreePinInfo(pInfo); Marshal.ReleaseComObject(fInfo.pGraph); Marshal.ReleaseComObject(audioPinTo); audioPinTo = null; } Marshal.ReleaseComObject(audioPinFrom); audioPinFrom = null; } DirectShowUtil.AddFilterToGraph(m_graph, AudioDecoder, Guid.Empty); /* Add our prefered audio renderer */ InsertAudioRenderer(AudioRenderer); IBaseFilter renderer = CreateVideoRenderer(VideoRenderer, m_graph, 2); /* We will want to enum all the pins on the source filter */ IEnumPins pinEnum; hr = sourceFilter.EnumPins(out pinEnum); DsError.ThrowExceptionForHR(hr); IntPtr fetched = IntPtr.Zero; IPin[] pins = { null }; /* Counter for how many pins successfully rendered */ int pinsRendered = 0; if (VideoRenderer == VideoRendererType.VideoMixingRenderer9) { var mixer = renderer as IVMRMixerControl9; if (mixer != null) { VMR9MixerPrefs dwPrefs; mixer.GetMixingPrefs(out dwPrefs); dwPrefs &= ~VMR9MixerPrefs.RenderTargetMask; dwPrefs |= VMR9MixerPrefs.RenderTargetRGB; //mixer.SetMixingPrefs(dwPrefs); } } /* Test using FFDShow Video Decoder Filter * var ffdshow = new FFDShow() as IBaseFilter; * * if (ffdshow != null) * m_graph.AddFilter(ffdshow, "ffdshow"); */ /* Loop over each pin of the source filter */ while (pinEnum.Next(pins.Length, pins, fetched) == 0) { if (filterGraph.RenderEx(pins[0], AMRenderExFlags.RenderToExistingRenderers, IntPtr.Zero) >= 0) { pinsRendered++; } Marshal.ReleaseComObject(pins[0]); } Marshal.ReleaseComObject(pinEnum); Marshal.ReleaseComObject(sourceFilter); if (pinsRendered == 0) { throw new Exception("Could not render any streams from the source Uri"); } #if DEBUG /* Adds the GB to the ROT so we can view * it in graphedit */ m_dsRotEntry = new DsROTEntry(m_graph); #endif /* Configure the graph in the base class */ SetupFilterGraph(m_graph); HasVideo = true; /* Sets the NaturalVideoWidth/Height */ //SetNativePixelSizes(renderer); } catch (Exception ex) { /* This exection will happen usually if the media does * not exist or could not open due to not having the * proper filters installed */ FreeResources(); /* Fire our failed event */ InvokeMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); } InvokeMediaOpened(); }
/// <summary> /// Retreives detailed information about all pins on a given filter. /// Caller must release all returned data. See IEnumerable<DetailPinInfo>.Release() /// </summary> /// <remarks> /// If an exception occurs, it frees all objects that may already be enumerated /// </remarks> /// <param name="filter">filter to get information about</param> public static List <DetailPinInfo> EnumPinsDetails(this IBaseFilter filter) { List <DetailPinInfo> info = new List <DetailPinInfo>(); int hr; IEnumPins enumPins; IEnumMediaTypes enumMediaTypes = null; IPin[] curPin = new IPin[1]; PinInfo curPinInfo = new PinInfo(); AMMediaType[] curMediaType = new AMMediaType[1]; hr = filter.EnumPins(out enumPins); DsError.ThrowExceptionForHR(hr); try { IntPtr fetched = Marshal.AllocCoTaskMem(4); try { while (enumPins.Next(curPin.Length, curPin, fetched) == 0) { if (Marshal.ReadInt32(fetched) == 1) { info.Add(new DetailPinInfo(curPin[0])); } } } finally { Marshal.FreeCoTaskMem(fetched); } return(info); } catch (Exception ex) { ErrorLogger.DumpToDebug(ex); try { BaseDSGraph.Release(curPin[0]); if (curMediaType[0] != null) { DsUtils.FreeAMMediaType(curMediaType[0]); } DsUtils.FreePinInfo(curPinInfo); info.Release(); } catch (Exception cleanUpEx) { ErrorLogger.DumpToDebug(cleanUpEx); } throw; } finally { BaseDSGraph.Release(enumPins); BaseDSGraph.Release(enumMediaTypes); } }
private bool DoesItHaveVideo(string filename) { if (string.IsNullOrEmpty(filename)) { return(false); } bool result; IGraphBuilder temp_graph = new FilterGraphNoThread() as IGraphBuilder; try { if (temp_graph == null) { throw new WPFMediaKitException("Could not create a graph"); } var filterGraph = temp_graph as IFilterGraph2; if (filterGraph == null) { throw new WPFMediaKitException("Could not QueryInterface for the IFilterGraph2"); } IBaseFilter sourceFilter; int hr; sourceFilter = DirectShowUtil.AddFilterToGraph(temp_graph, SplitterSource, LAVFilterDirectory, Guid.Empty); if (sourceFilter == null) { throw new WPFMediaKitException("Could not add SplitterSource to graph."); } IFileSourceFilter interfaceFile = (IFileSourceFilter)sourceFilter; hr = interfaceFile.Load(filename, null); DsError.ThrowExceptionForHR(hr); // Set Video Codec // Remove Pin var videoPinFrom = DirectShowLib.DsFindPin.ByName(sourceFilter, "Video"); IPin videoPinTo; if (videoPinFrom != null) { hr = videoPinFrom.ConnectedTo(out videoPinTo); if (hr >= 0 && videoPinTo != null) { PinInfo pInfo; videoPinTo.QueryPinInfo(out pInfo); FilterInfo fInfo; pInfo.filter.QueryFilterInfo(out fInfo); DirectShowUtil.DisconnectAllPins(temp_graph, pInfo.filter); temp_graph.RemoveFilter(pInfo.filter); DsUtils.FreePinInfo(pInfo); Marshal.ReleaseComObject(fInfo.pGraph); Marshal.ReleaseComObject(videoPinTo); videoPinTo = null; } Marshal.ReleaseComObject(videoPinFrom); videoPinFrom = null; result = true; } else { result = false; } DirectShowUtil.RemoveFilters(temp_graph, SplitterSource.Name); Marshal.FinalReleaseComObject(sourceFilter); } catch { result = false; } finally { Marshal.ReleaseComObject(temp_graph); } return(result); }
// Tear down everything downstream of a given filter public static int NukeDownstream(IFilterGraph graph, IBaseFilter filter) { if (filter == null) { return(WinAPI.E_FAIL); } IEnumPins enumPins = null; IPin[] pins = new IPin[1] { null }; try { int hr = filter.EnumPins(out enumPins); DsError.ThrowExceptionForHR(hr); enumPins.Reset(); // start at the first pin while (enumPins.Next(1, pins, IntPtr.Zero) == 0) { if (pins[0] != null) { PinDirection pindir; pins[0].QueryDirection(out pindir); if (pindir == PinDirection.Output) { IPin pTo = null; pins[0].ConnectedTo(out pTo); if (pTo != null) { PinInfo pi; hr = pTo.QueryPinInfo(out pi); if (hr == 0) { NukeDownstream(graph, pi.filter); graph.Disconnect(pTo); graph.Disconnect(pins[0]); graph.RemoveFilter(pi.filter); Util.ReleaseComObject(ref pi.filter); DsUtils.FreePinInfo(pi); } Marshal.ReleaseComObject(pTo); } } Marshal.ReleaseComObject(pins[0]); } } return(0); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } finally { Marshal.ReleaseComObject(enumPins); } return(WinAPI.E_FAIL); }
// This function is called recursively, every time a new crossbar is // entered as we search upstream. // // Return values: // // 0 - Returned on final exit after recursive search if at least // one routing is possible // 1 - Normal return indicating we've reached the end of a // recursive search, so save the current path // -1 - Unable to route anything private int BuildRoutingList(IPin startingInputPin, Routing routing, int depth) { if (startingInputPin == null || routing == null) { return(-1); // E_POINTER; } // If the pin isn't connected, then it's a terminal pin IPin startingOutputPin = null; int hr = startingInputPin.ConnectedTo(out startingOutputPin); if (hr != 0) { return((depth == 0) ? -1 : 1); } // It is connected, so now find out if the filter supports IAMCrossbar PinInfo pinInfo; if (startingOutputPin.QueryPinInfo(out pinInfo) == 0) { //ASSERT (pinInfo.dir == PINDIR_OUTPUT); IAMCrossbar crossbar = pinInfo.filter as IAMCrossbar; if (crossbar != null) { int inputs, outputs, inputIndex, outputIndex; int inputIndexRelated, outputIndexRelated; PhysicalConnectorType inputPhysicalType, outputPhysicalType; hr = crossbar.get_PinCounts(out outputs, out inputs); // for all output pins for (outputIndex = 0; outputIndex < outputs; outputIndex++) { hr = crossbar.get_CrossbarPinInfo(false, outputIndex, out outputIndexRelated, out outputPhysicalType); // for all input pins for (inputIndex = 0; inputIndex < inputs; inputIndex++) { hr = crossbar.get_CrossbarPinInfo(true, inputIndex, out inputIndexRelated, out inputPhysicalType); // Can we route it? if (crossbar.CanRoute(outputIndex, inputIndex) == 0) { IPin pPin = null; hr = GetCrossbarPinAtIndex(crossbar, inputIndex, true, out pPin); // We've found a route through this crossbar // so save our state before recusively searching // again. Routing routingNext = new Routing(); // doubly linked list routingNext.rightRouting = routing; routing.leftRouting = routingNext; routing.crossbar = crossbar; routing.inputIndex = inputIndex; routing.outputIndex = outputIndex; routing.inputIndexRelated = inputIndexRelated; routing.outputIndexRelated = outputIndexRelated; routing.inputPhysicalType = inputPhysicalType; routing.outputPhysicalType = outputPhysicalType; routing.depth = depth; routing.inputName = this.pinNameByPhysicalConnectorType[inputPhysicalType] as string; hr = BuildRoutingList(pPin, routingNext, depth + 1); if (hr == 1) { routing.leftRouting = null; SaveRouting(routing, inputPhysicalType >= PhysicalConnectorType.Audio_Tuner); } } // if we can route } // for all input pins } //pXbar.Release(); } else { // The filter doesn't support IAMCrossbar, so this // is a terminal pin DsUtils.FreePinInfo(pinInfo); Marshal.ReleaseComObject(startingOutputPin); return((depth == 0) ? -1 : 1); } DsUtils.FreePinInfo(pinInfo); } Marshal.ReleaseComObject(startingOutputPin); return(0); }
/// <summary> /// Opens the media by initializing the DirectShow graph /// </summary> protected virtual void OpenSource() { /* Make sure we clean up any remaining mess */ FreeResources(); string fileSource = FileSource; if (string.IsNullOrEmpty(fileSource)) { return; } try { /* Creates the GraphBuilder COM object */ m_graph = new FilterGraphNoThread() as IGraphBuilder; if (m_graph == null) { throw new WPFMediaKitException("Could not create a graph"); } var filterGraph = m_graph as IFilterGraph2; if (filterGraph == null) { throw new WPFMediaKitException("Could not QueryInterface for the IFilterGraph2"); } IBaseFilter sourceFilter; int hr; // Set LAV Splitter /* LAVSplitterSource reader = new LAVSplitterSource(); * sourceFilter = reader as IBaseFilter; * var objectWithSite = reader as IObjectWithSite; * if (objectWithSite != null) * { * objectWithSite.SetSite(this); * } * * * hr = m_graph.AddFilter(sourceFilter, SplitterSource); * DsError.ThrowExceptionForHR(hr);*/ sourceFilter = DirectShowUtil.AddFilterToGraph(m_graph, SplitterSource, Guid.Empty); if (sourceFilter == null) { throw new WPFMediaKitException("Could not add SplitterSource to graph."); } IFileSourceFilter interfaceFile = (IFileSourceFilter)sourceFilter; hr = interfaceFile.Load(fileSource, null); DsError.ThrowExceptionForHR(hr); // Set Video Codec // Remove Pin var videoPinFrom = DirectShowLib.DsFindPin.ByName(sourceFilter, "Video"); IPin videoPinTo; if (videoPinFrom != null) { hr = videoPinFrom.ConnectedTo(out videoPinTo); if (hr >= 0 && videoPinTo != null) { PinInfo pInfo; videoPinTo.QueryPinInfo(out pInfo); FilterInfo fInfo; pInfo.filter.QueryFilterInfo(out fInfo); DirectShowUtil.DisconnectAllPins(m_graph, pInfo.filter); m_graph.RemoveFilter(pInfo.filter); DsUtils.FreePinInfo(pInfo); Marshal.ReleaseComObject(fInfo.pGraph); Marshal.ReleaseComObject(videoPinTo); videoPinTo = null; } Marshal.ReleaseComObject(videoPinFrom); videoPinFrom = null; HasVideo = true; } else { HasVideo = false; } DirectShowUtil.AddFilterToGraph(m_graph, VideoDecoder, Guid.Empty); try { // use preffered audio filter InsertAudioFilter(sourceFilter, AudioDecoder); } catch (Exception ex) { // codecs misconfigured log.Error(ex, "Cannot add audio decoder: {0}", AudioDecoder); } // use prefered audio renderer try { InsertAudioRenderer(AudioRenderer); } catch (Exception ex) { log.Error(ex, "Cannot add audio render: {0}", AudioRenderer); } IBaseFilter renderer = CreateVideoRenderer(VideoRenderer, m_graph, 2); /* We will want to enum all the pins on the source filter */ IEnumPins pinEnum; hr = sourceFilter.EnumPins(out pinEnum); DsError.ThrowExceptionForHR(hr); IntPtr fetched = IntPtr.Zero; IPin[] pins = { null }; /* Counter for how many pins successfully rendered */ int pinsRendered = 0; if (VideoRenderer == VideoRendererType.VideoMixingRenderer9) { var mixer = renderer as IVMRMixerControl9; if (mixer != null) { VMR9MixerPrefs dwPrefs; mixer.GetMixingPrefs(out dwPrefs); dwPrefs &= ~VMR9MixerPrefs.RenderTargetMask; dwPrefs |= VMR9MixerPrefs.RenderTargetRGB; //mixer.SetMixingPrefs(dwPrefs); } } /* Loop over each pin of the source filter */ while (pinEnum.Next(pins.Length, pins, fetched) == 0) { if (filterGraph.RenderEx(pins[0], AMRenderExFlags.RenderToExistingRenderers, IntPtr.Zero) >= 0) { pinsRendered++; } Marshal.ReleaseComObject(pins[0]); } Marshal.ReleaseComObject(pinEnum); Marshal.ReleaseComObject(sourceFilter); if (pinsRendered == 0) { throw new WPFMediaKitException("Could not render any streams from the source Uri"); } #if DEBUG /* Adds the GB to the ROT so we can view * it in graphedit */ m_dsRotEntry = new DsROTEntry(m_graph); #endif /* Configure the graph in the base class */ SetupFilterGraph(m_graph); } catch (Exception ex) { /* This exection will happen usually if the media does * not exist or could not open due to not having the * proper filters installed */ // Fallback try auto graph: var result = oldOpenSource(); if (!result) { FreeResources(); HasVideo = false; /* Fire our failed event */ InvokeMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); return; } } InvokeMediaOpened(); }
public void ReloadFilters() { log("reload filters.."); IEnumFilters ef; ClearFiltersSelection(); ClearConnections(); filter_positions.Clear(); foreach (Filter f in filters) { filter_positions.Add(f.Name, f.Coords); } filters.Clear(); log("reload_filters 1"); try { int hr = graphBuilder.EnumFilters(out ef); DsError.ThrowExceptionForHR(hr); IBaseFilter[] fs = new IBaseFilter[1]; IntPtr fetched = Marshal.AllocHGlobal(4); while ((hr = ef.Next(1, fs, fetched)) == 0) { log("reload_filters 2"); FilterInfo fi; fs[0].QueryFilterInfo(out fi); log("reload_filters: " + fi.achName); Filter ff = FindFilterByName(fi.achName); if (ff == null) //not found { ff = new Filter(fs[0]); AddFilterHere(ff, false, null); history.AddFilterIfNew(ff.filterProps, ff.Name, ff.srcFileName, ff.dstFileName, ff); } else { ff.ReloadPins(); } log("reload_filters 3"); foreach (Pin pin in ff.Pins) { log("reload_filters 4: " + pin.Name); IPin ip = pin.IPin, connected_ipin; hr = ip.ConnectedTo(out connected_ipin); if (hr != 0) { continue; } PinInfo cpi; connected_ipin.QueryPinInfo(out cpi); FilterInfo cfi; cpi.filter.QueryFilterInfo(out cfi); Filter connected_filter = FindFilterByName(cfi.achName); if (connected_filter != null) { Pin cp = connected_filter.FindPinByName(connected_filter.Name + "." + cpi.name); if (cp != null) { string con_uniqname = (pin.Direction == PinDirection.Input) ? cp.UniqName + "-" + pin.UniqName : pin.UniqName + "-" + cp.UniqName; PinConnection con = FindConnectionByName(con_uniqname); if (con == null) { if (pin.Direction == PinDirection.Input) { Connect(cp, pin); } else { Connect(pin, cp); } } } } DsUtils.FreePinInfo(cpi); } } Marshal.FreeHGlobal(fetched); if (hr < 0) { DsError.ThrowExceptionForHR(hr); } } catch (COMException e) { ShowCOMException(e, "Error while enumerating filters in the graph"); return; } /*catch (Exception e) * { * MessageBox.Show(e.Message, "Error while enumerating filters in the graph"); * return; * }*/ log("reload_filters: almost done"); history.CommitAdded(); log("reload_filters: done"); }