private static IPin?EnumPins(IBaseFilter filter, Func <PIN_INFO, bool> func) { IEnumPins?pins = null; IPin? ipin = null; try { filter.EnumPins(ref pins); int fetched = 0; while (pins?.Next(1, ref ipin, ref fetched) == 0) { if (fetched == 0) { break; } var info = new PIN_INFO(); try { ipin?.QueryPinInfo(info); var rc = func(info); if (rc) { return(ipin); } } finally { if (info.pFilter != null) { Marshal.ReleaseComObject(info.pFilter); } } } } catch { if (ipin != null) { Marshal.ReleaseComObject(ipin); } throw; } finally { if (pins != null) { Marshal.ReleaseComObject(pins); } } return(null); }
// 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 (COMException) { } finally { Marshal.ReleaseComObject(enumPins); } return(WinAPI.E_FAIL); }
public static void AddPreferredFilters(IGraphBuilder graphBuilder, IBaseFilter sourceFilter) { using (Settings xmlreader = new MPSettings()) { bool autodecodersettings = xmlreader.GetValueAsBool("movieplayer", "autodecodersettings", false); if (!autodecodersettings) // the user has not chosen automatic graph building by merits { // bool vc1ICodec,vc1Codec,xvidCodec = false; - will come later bool aacCodec = false; bool h264Codec = false; // check the output pins of the splitter for known media types IEnumPins pinEnum = null; if (sourceFilter.EnumPins(out pinEnum) == 0) { int fetched = 0; IPin[] pins = new IPin[1]; while (pinEnum.Next(1, pins, out fetched) == 0 && fetched > 0) { IPin pin = pins[0]; PinDirection pinDirection; if (pin.QueryDirection(out pinDirection) == 0 && pinDirection == PinDirection.Output) { IEnumMediaTypes enumMediaTypesVideo = null; if (pin.EnumMediaTypes(out enumMediaTypesVideo) == 0) { AMMediaType[] mediaTypes = new AMMediaType[1]; int typesFetched; while (enumMediaTypesVideo.Next(1, mediaTypes, out typesFetched) == 0 && typesFetched > 0) { if (mediaTypes[0].majorType == MediaType.Video && (mediaTypes[0].subType == MediaSubType.H264 || mediaTypes[0].subType == MEDIASUBTYPE_AVC1)) { Log.Instance.Info("found H264 video on output pin"); h264Codec = true; } else if (mediaTypes[0].majorType == MediaType.Audio && mediaTypes[0].subType == MediaSubType.LATMAAC) { Log.Instance.Info("found AAC audio on output pin"); aacCodec = true; } } DirectShowUtil.ReleaseComObject(enumMediaTypesVideo); } } DirectShowUtil.ReleaseComObject(pin); } DirectShowUtil.ReleaseComObject(pinEnum); } // add filters for found media types to the graph as configured in MP if (h264Codec) { DirectShowUtil.ReleaseComObject( DirectShowUtil.AddFilterToGraph(graphBuilder, xmlreader.GetValueAsString("movieplayer", "h264videocodec", ""))); } else { DirectShowUtil.ReleaseComObject( DirectShowUtil.AddFilterToGraph(graphBuilder, xmlreader.GetValueAsString("movieplayer", "mpeg2videocodec", ""))); } if (aacCodec) { DirectShowUtil.ReleaseComObject( DirectShowUtil.AddFilterToGraph(graphBuilder, xmlreader.GetValueAsString("movieplayer", "aacaudiocodec", ""))); } else { DirectShowUtil.ReleaseComObject( DirectShowUtil.AddFilterToGraph(graphBuilder, xmlreader.GetValueAsString("movieplayer", "mpeg2audiocodec", ""))); } } } }
public static IEnumerator <WavFormatInfo> EnumerateFormatsForDirection(IBaseFilter filter, PinDirection direction) { if (filter == null) { throw new ArgumentNullException("filter"); } IEnumPins pinsEnum = null; try { int hr = filter.EnumPins(out pinsEnum); DsError.ThrowExceptionForHR(hr); if (pinsEnum == null) { throw new InvalidOperationException("pinsEnum is null"); } var pins = new IPin[1]; while (true) { try { int fetched = 0; IntPtr pcFetched = Marshal.AllocCoTaskMem(4); try { hr = pinsEnum.Next(pins.Length, pins, pcFetched); DsError.ThrowExceptionForHR(hr); fetched = Marshal.ReadInt32(pcFetched); } finally { Marshal.FreeCoTaskMem(pcFetched); } if (fetched == 1) { // we have something IPin pin = pins[0]; string queryId; hr = pin.QueryId(out queryId); DsError.ThrowExceptionForHR(hr); PinInfo pinInfo; hr = pin.QueryPinInfo(out pinInfo); DsError.ThrowExceptionForHR(hr); if (pinInfo.dir != direction) { continue; } IEnumMediaTypes mediaTypesEnum = null; try { hr = pin.EnumMediaTypes(out mediaTypesEnum); DsError.ThrowExceptionForHR(hr); var mediaTypes = new AMMediaType[1]; while (true) { IntPtr mtFetched = Marshal.AllocCoTaskMem(4); try { hr = mediaTypesEnum.Next(1, mediaTypes, mtFetched); DsError.ThrowExceptionForHR(hr); fetched = Marshal.ReadInt32(mtFetched); } finally { Marshal.FreeCoTaskMem(mtFetched); } if (fetched == 1) { if (mediaTypes[0].formatType == FormatType.WaveEx) { yield return(new WavFormatInfo(mediaTypes[0])); } } else { break; } } } finally { if (mediaTypesEnum != null) { Marshal.ReleaseComObject(mediaTypesEnum); } } } else { break; } } finally { if (pins[0] != null) { Marshal.ReleaseComObject(pins[0]); } pins[0] = null; } } } finally { if (pinsEnum != null) { Marshal.ReleaseComObject(pinsEnum); } } }
private void Button_Click(object sender, RoutedEventArgs e) { OpenFileDialog lopenFileDialog = new OpenFileDialog(); lopenFileDialog.AddExtension = true; var lresult = lopenFileDialog.ShowDialog(); if (lresult != true) { return; } IBaseFilter lDSoundRender = new DSoundRender() as IBaseFilter; m_pGraph.AddFilter(lDSoundRender, "Audio Renderer"); int k = 0; IPin[] lAudioRendererPins = new IPin[1]; IEnumPins ppEnum; k = lDSoundRender.EnumPins(out ppEnum); k = ppEnum.Next(1, lAudioRendererPins, IntPtr.Zero); var lCaptureManagerEVRMultiSinkFactory = CaptureManagerVideoRendererMultiSinkFactory.getInstance().getICaptureManagerEVRMultiSinkFactory(); uint lMaxVideoRenderStreamCount = lCaptureManagerEVRMultiSinkFactory.getMaxVideoRenderStreamCount(); if (lMaxVideoRenderStreamCount == 0) { return; } List <object> lOutputNodesList = new List <object>(); lCaptureManagerEVRMultiSinkFactory.createOutputNodes( IntPtr.Zero, mEVRDisplay.Surface.texture, 1,// lMaxVideoRenderStreamCount, out lOutputNodesList); if (lOutputNodesList.Count == 0) { return; } IBaseFilter lVideoMixingRenderer9 = (IBaseFilter)lOutputNodesList[0]; var h = m_pGraph.AddFilter(lVideoMixingRenderer9, "lVideoMixingRenderer9"); IPin[] lVideoRendererPin = new IPin[1]; k = lVideoMixingRenderer9.EnumPins(out ppEnum); k = ppEnum.Next(1, lVideoRendererPin, IntPtr.Zero); IBaseFilter m_SourceFilter = null; m_pGraph.AddSourceFilter(lopenFileDialog.FileName, null, out m_SourceFilter); IEnumPins lEnumPins = null; m_SourceFilter.EnumPins(out lEnumPins); IPin[] lPins = new IPin[1]; while (lEnumPins.Next(1, lPins, IntPtr.Zero) == 0) { IEnumMediaTypes lIEnumMediaTypes; lPins[0].EnumMediaTypes(out lIEnumMediaTypes); AMMediaType[] ppMediaTypes = new AMMediaType[1]; while (lIEnumMediaTypes.Next(1, ppMediaTypes, IntPtr.Zero) == 0) { var gh = ppMediaTypes[0].subType; if (ppMediaTypes[0].majorType == DirectShowLib.MediaType.Video) { k = m_pGraph.Connect(lPins[0], lVideoRendererPin[0]); } } foreach (var item in lPins) { k = m_pGraph.Render(item); } } IMediaControl lIMediaControl = m_pGraph as IMediaControl; k = lIMediaControl.Run(); }
static int ConnectSampleGrabber(IGraphBuilder graph, IBaseFilter src, IBaseFilter dest) { if ((graph == null) || (src == null) || (dest == null)) { return(WinAPI.E_FAIL); } IEnumPins enumPins = null; // try to connect to source filter unconnected output pins try { IPin[] pins = new IPin[1] { null }; int hr = src.EnumPins(out enumPins); DsError.ThrowExceptionForHR(hr); while (enumPins.Next(1, pins, IntPtr.Zero) == 0) { try { PinDirection thisPinDir; hr = pins[0].QueryDirection(out thisPinDir); if (hr == 0 && thisPinDir == PinDirection.Output) { IPin tmpPin = null; hr = pins[0].ConnectedTo(out tmpPin); if (tmpPin != null) // Already connected, not the pin we want. { Util.ReleaseComObject(ref tmpPin); } else // Unconnected, this is the pin we want. { hr = Util.ConnectFilters(graph, pins[0], dest); if (0 == hr) { return(0); } } } } finally { Util.ReleaseComObject(ref pins[0]); } } } catch (COMException) { } finally { Marshal.ReleaseComObject(enumPins); } // try to connect to filters connected to the source filter try { IPin[] pins = new IPin[1] { null }; int hr = src.EnumPins(out enumPins); DsError.ThrowExceptionForHR(hr); while (enumPins.Next(1, pins, IntPtr.Zero) == 0) { try { PinDirection thisPinDir; hr = pins[0].QueryDirection(out thisPinDir); if (hr == 0 && thisPinDir == PinDirection.Output) { IPin tmpPin = null; hr = pins[0].ConnectedTo(out tmpPin); if (tmpPin != null) // Already connected, the pin we want. { try { PinInfo pinInfo; hr = tmpPin.QueryPinInfo(out pinInfo); IBaseFilter connectedTo = null; if (hr == 0) { connectedTo = pinInfo.filter; } if (connectedTo != null) { hr = ConnectSampleGrabber(graph, connectedTo, dest); if (0 == hr) { return(hr); } } } finally { Util.ReleaseComObject(ref tmpPin); } } } } finally { Util.ReleaseComObject(ref pins[0]); } } } catch (COMException) { } finally { Marshal.ReleaseComObject(enumPins); } return(WinAPI.E_FAIL); }
public static IPin FindPinForMajorType(IBaseFilter filter, PinDirection direction, Guid majorType) { if (filter == null) { throw new ArgumentNullException("filter"); } int hr = 0; IEnumPins pinsEnum = null; try { hr = filter.EnumPins(out pinsEnum); DsError.ThrowExceptionForHR(hr); var pins = new IPin[1]; int numberFetched = 1; while (numberFetched > 0) { IntPtr pcFetched = Marshal.AllocCoTaskMem(4); try { hr = pinsEnum.Next(1, pins, pcFetched); DsError.ThrowExceptionForHR(hr); numberFetched = Marshal.ReadInt32(pcFetched); } finally { Marshal.FreeCoTaskMem(pcFetched); } if (numberFetched > 0) { PinDirection currentPinDirection; hr = pins[0].QueryDirection(out currentPinDirection); DsError.ThrowExceptionForHR(hr); if (currentPinDirection != direction) { continue; } IEnumMediaTypes mediaTypesEnum = null; try { var mediaTypes = new AMMediaType[1]; pins[0].EnumMediaTypes(out mediaTypesEnum); int numberFetched2 = 1; while (numberFetched2 > 0) { IntPtr fetched2 = IntPtr.Zero; try { hr = mediaTypesEnum.Next(1, mediaTypes, fetched2); DsError.ThrowExceptionForHR(hr); numberFetched2 = Marshal.ReadInt32(fetched2); } finally { Marshal.FreeCoTaskMem(fetched2); } if (numberFetched2 > 0) { if (mediaTypes[0].majorType == majorType) { // success, return the pin return(pins[0]); } } Marshal.ReleaseComObject(pins[0]); } } finally { if (mediaTypesEnum != null) { Marshal.ReleaseComObject(mediaTypesEnum); } } } } } finally { if (pinsEnum != null) { Marshal.ReleaseComObject(pinsEnum); } } return(null); }
public static void SetFilterFormat(AMMediaType streamFormat, IBaseFilter filter) { if (filter == null) { throw new ArgumentNullException("filter"); } int hr; IEnumPins pinsEnum = null; try { hr = filter.EnumPins(out pinsEnum); DsError.ThrowExceptionForHR(hr); if (pinsEnum == null) { throw new SplicerException(Resources.ErrorPinsEnumeratorIsNull); } var pins = new IPin[1]; while (true) { try { int fetched = 0; IntPtr pcFetched = Marshal.AllocCoTaskMem(4); try { hr = pinsEnum.Next(pins.Length, pins, pcFetched); DsError.ThrowExceptionForHR(hr); fetched = Marshal.ReadInt32(pcFetched); } finally { Marshal.FreeCoTaskMem(pcFetched); } if (fetched == 1) { // we have something IPin pin = pins[0]; string queryId; hr = pin.QueryId(out queryId); DsError.ThrowExceptionForHR(hr); PinInfo pinInfo; hr = pin.QueryPinInfo(out pinInfo); DsError.ThrowExceptionForHR(hr); if (pinInfo.dir != PinDirection.Output) { continue; } var streamConfig = (IAMStreamConfig)pin; hr = streamConfig.SetFormat(streamFormat); DsError.ThrowExceptionForHR(hr); } else { break; } } finally { if (pins[0] != null) { Marshal.ReleaseComObject(pins[0]); } pins[0] = null; } } } finally { if (pinsEnum != null) { Marshal.ReleaseComObject(pinsEnum); } } }
public static IList <PinQueryInfo> EnumeratePins(IBaseFilter filter) { if (filter == null) { throw new ArgumentNullException("filter"); } int hr = 0; var pinsInfo = new List <PinQueryInfo>(); IEnumPins pinsEnum = null; try { hr = filter.EnumPins(out pinsEnum); DsError.ThrowExceptionForHR(hr); if (pinsEnum == null) { throw new InvalidOperationException("pinsEnum is null"); } var pins = new IPin[1]; while (true) { try { int fetched = 0; IntPtr pcFetched = Marshal.AllocCoTaskMem(4); try { hr = pinsEnum.Next(pins.Length, pins, pcFetched); DsError.ThrowExceptionForHR(hr); fetched = Marshal.ReadInt32(pcFetched); } finally { Marshal.FreeCoTaskMem(pcFetched); } if (fetched == 1) { // we have something IPin pin = pins[0]; string queryId; hr = pin.QueryId(out queryId); DsError.ThrowExceptionForHR(hr); PinInfo pinInfo; hr = pin.QueryPinInfo(out pinInfo); DsError.ThrowExceptionForHR(hr); pinsInfo.Add(new PinQueryInfo(pinInfo.dir, pinInfo.name, queryId)); } else { break; } } finally { if (pins[0] != null) { Marshal.ReleaseComObject(pins[0]); } pins[0] = null; } } } finally { if (pinsEnum != null) { Marshal.ReleaseComObject(pinsEnum); } } return(pinsInfo); }
/// <summary> /// ピンの検索 /// </summary> /// <param name="filter">フィルタ</param> /// <param name="index">番号 [0~]</param> /// <param name="direction">方向 (Unknown を指定した場合、方向に関係なく列挙されます)</param> /// <returns> /// 見つかった場合は、ピンの IPin インターフェースを返します。 /// 見つからなかった場合は、 null を返します。 /// </returns> public static IPin FindPin(IBaseFilter filter, int index, PIN_DIRECTION direction) { IEnumPins enumpins = null; IPin pin = null; try { filter.EnumPins(ref enumpins); int fetched = 0; while (enumpins.Next(1, ref pin, ref fetched) == 0) { if (fetched == 0) { break; } var info = new PIN_INFO(); try { pin.QueryPinInfo(info); if (info.dir == direction) { if (index <= 0) { return(pin); } index--; } if (pin != null) { Marshal.ReleaseComObject(pin); } pin = null; } finally { if (info.pFilter != null) { Marshal.ReleaseComObject(info.pFilter); } } } } catch { if (pin != null) { Marshal.ReleaseComObject(pin); } throw; } finally { if (enumpins != null) { Marshal.ReleaseComObject(enumpins); } } return(null); }