예제 #1
0
파일: GraphBase.cs 프로젝트: ttsuki/ttsuki
            /// <summary>
            /// 指定したピンはInputピンか?
            /// </summary>
            /// <param name="pin">ピン</param>
            /// <returns>Inputピンである場合true</returns>
            public static bool IsInputPin(IPin pin)
            {
                PinDirection dir;

                pin.QueryDirection(out dir);
                return(dir == PinDirection.Input);
            }
예제 #2
0
        protected virtual void SetupAudio()
        {
            int hr;

            IEnumFilters enumFilters;

            hr = _graph.EnumFilters(out enumFilters);
            DsError.ThrowExceptionForHR(hr);

            IBaseFilter[] filters = new IBaseFilter[1];
            IntPtr        fetched = new IntPtr();

            while (enumFilters.Next(1, filters, fetched) == 0)
            {
                IBaseFilter filter         = filters[0] as IBaseFilter;
                IPin        unconnectedPin = DsFindPin.ByConnectionStatus((IBaseFilter)filter, PinConnectedStatus.Unconnected, 0);
                if (unconnectedPin != null)
                {
                    PinDirection direction;
                    hr = unconnectedPin.QueryDirection(out direction);
                    DsError.ThrowExceptionForHR(hr);

                    if (direction == PinDirection.Output)
                    {
                        hr = _graph.Render(unconnectedPin);
                        DsError.ThrowExceptionForHR(hr);

                        SetupSampleGrabber();
                    }
                }
            }
        }
예제 #3
0
        public void TestQueryDirection()
        {
            IPin         testPin = GetSmartTeeInputPin();
            PinDirection pDir;
            int          hr = testPin.QueryDirection(out pDir);

            Marshal.ThrowExceptionForHR(hr);

            Assert.IsTrue(pDir == PinDirection.Input || pDir == PinDirection.Output);
        }
        static IPin FindPinByDirection(IBaseFilter filter, PinDirection direction)
        {
            IPin      destPin = null;
            IEnumPins iEnum;
            int       hr;

            IPin[] pins = new IPin[1];

            hr = filter.EnumPins(out iEnum);
            DsError.ThrowExceptionForHR(hr);

            if (iEnum == null)
            {
                throw new InvalidOperationException("iEnum is null");
            }

            IntPtr fetched = Marshal.AllocCoTaskMem(4);

            hr = iEnum.Next(1, pins, fetched);
            DsError.ThrowExceptionForHR(hr);

            while (Marshal.ReadInt32(fetched) == 1)
            {
                PinDirection pinDir;
                IPin         pPin;
                destPin = pins[0];

                hr = destPin.QueryDirection(out pinDir);
                DsError.ThrowExceptionForHR(hr);

                destPin.ConnectedTo(out pPin);

                if (pPin == null && pinDir == direction)
                {
                    break;
                }

                if (pPin != null)
                {
                    Marshal.ReleaseComObject(pPin);
                }

                hr = iEnum.Next(1, pins, fetched);
                DsError.ThrowExceptionForHR(hr);
            }

            Marshal.FreeCoTaskMem(fetched);

            return(destPin);
        }
예제 #5
0
        private bool IsMatchingPin(IPin pin, PinDirection direction, Guid mediaType)
        {
            PinDirection pinDirection;
            int          hr = pin.QueryDirection(out pinDirection);

            DsError.ThrowExceptionForHR(hr);

            if (pinDirection != direction)
            {
                // The pin lacks direction
                return(false);
            }

            IPin connectedPin;

            hr = pin.ConnectedTo(out connectedPin);
            if ((uint)hr != 0x80040209 /* Pin is not connected */)
            {
                DsError.ThrowExceptionForHR(hr);
            }

            if (connectedPin != null)
            {
                // The pin is already connected
                Marshal.ReleaseComObject(connectedPin);
                return(false);
            }

            IEnumMediaTypes mediaTypesEnum;

            hr = pin.EnumMediaTypes(out mediaTypesEnum);
            DsError.ThrowExceptionForHR(hr);

            AMMediaType[] mediaTypes = new AMMediaType[1];

            while (mediaTypesEnum.Next(1, mediaTypes, IntPtr.Zero) == 0)
            {
                Guid majorType = mediaTypes[0].majorType;
                DsUtils.FreeAMMediaType(mediaTypes[0]);

                if (majorType == mediaType)
                {
                    // We have found the pin we were looking for
                    return(true);
                }
            }

            return(false);
        }
예제 #6
0
파일: Utility.cs 프로젝트: mrLunatic/WebCam
        /// <summary>
        /// Query whether a pin has a specified direction (input/output).
        /// </summary>
        /// <param name="pPin">Specifies the pin to check.</param>
        /// <param name="dir">Specifies the direction to match against the actual direction.</param>
        /// <param name="bResult">Specifies the direction matching status.</param>
        /// <returns>An error value < 0 is returned or 0 for success.</returns>
        public static int IsPinDirection(IPin pPin, PinDirection dir, out bool bResult)
        {
            PinDirection pinDir;

            bResult = false;

            int hr = pPin.QueryDirection(out pinDir);

            if (hr == 0)
            {
                bResult = (pinDir == dir) ? true : false;
            }

            return(hr);
        }
예제 #7
0
        public static void LogOutputPinsConnectionRecursive(IBaseFilter filter, string previous = "")
        {
            bool      log     = true;
            IEnumPins pinEnum = null;

            if (filter.EnumPins(out pinEnum) == 0)
            {
                FilterInfo sourceFilterInfo;
                filter.QueryFilterInfo(out sourceFilterInfo);
                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)
                    {
                        log = false;
                        IPin connectedPin;
                        if (pin.ConnectedTo(out connectedPin) == 0 && connectedPin != null)
                        {
                            PinInfo connectedPinInfo;
                            connectedPin.QueryPinInfo(out connectedPinInfo);
                            FilterInfo connectedFilterInfo;
                            connectedPinInfo.filter.QueryFilterInfo(out connectedFilterInfo);
                            if (previous == "")
                            {
                                previous = sourceFilterInfo.achName;
                            }
                            DirectShowUtil.ReleaseComObject(connectedPin, 2000);
                            IBaseFilter connectedFilter;
                            if (connectedFilterInfo.pGraph.FindFilterByName(connectedFilterInfo.achName, out connectedFilter) == 0 && connectedFilter != null)
                            {
                                LogOutputPinsConnectionRecursive(connectedFilter, previous + string.Format(" --> {0}", connectedFilterInfo.achName));
                                DirectShowUtil.ReleaseComObject(connectedFilter);
                            }
                        }
                        DirectShowUtil.ReleaseComObject(pin, 2000);
                    }
                }
            }
            DirectShowUtil.ReleaseComObject(pinEnum, 2000);

            if (log)
            {
                Log.Instance.Debug(previous);
            }
        }
예제 #8
0
        // from 'DShowUtil.cpp'
        public static int GetPin(IBaseFilter filter, PinDirection dirrequired, out IPin ppPin, int which)
        {
            ppPin = null;
            int hr;

            IEnumPins pEnum;
            uint      cFetched;

            hr = filter.EnumPins(out pEnum);
            if ((hr < 0) || (pEnum == null))
            {
                return(hr);
            }

            try
            {
                PinDirection dir;
                do
                {
                    hr  = pEnum.Next(1, out ppPin, out cFetched);
                    dir = (PinDirection)3;
                    hr  = ppPin.QueryDirection(out dir);
                    if ((hr == 0) && (dir == dirrequired))
                    {
                        if (which == 0)
                        {
                            return(0);
                        }
                        else
                        {
                            ppPin = null;
                            which--;
                        }
                    }
                    else
                    {
                        Marshal.ReleaseComObject(ppPin);
                    }
                }while(hr == 0);
            }
            catch (Exception)
            {
                return(hr);
            }

            return(hr);
        }
예제 #9
0
        /// <summary>
        /// Gets the first pin of the specified direction and connection status. You also specify
        /// how many matching pins should be skipped before a desired pin is returned.
        ///
        /// If this function finds a matching pin, it returns an IPin interface pointer
        /// with an outstanding reference count. The caller is responsible for releasing the interface.
        ///
        /// Leaving the last two parameters at their default
        /// values is like saying: "Gimme the first unconnected pin".
        /// </summary>
        /// <param name="pFilter">Filter</param>
        /// <param name="pinDir">Pin direction</param>
        /// <param name="bConnected">Whether the returned Pin should be connected to some other pin</param>
        /// <param name="nPinsToSkip">How many Pins that match the conditions should be skipped</param>
        /// <returns></returns>
        public static IPin GetPin(IBaseFilter pFilter, PinDirection pinDir, bool bConnected, int nPinsToSkip)
        {
            bool      bFound = false;
            IEnumPins pEnum;
            IPin      pPin = null;
            int       cFetched;
            int       nSkipped      = 0;
            IPin      pConnectedPin = null;

            int hr = pFilter.EnumPins(out pEnum);

            if (DsHlp.FAILED(hr) || pEnum == null)
            {
                return(null);
            }

            while ((pEnum.Next(1, out pPin, out cFetched) == DsHlp.S_OK) && pPin != null)
            {
                PinDirection PinDirThis;
                pPin.QueryDirection(out PinDirThis);
                if (pinDir == PinDirThis)
                {
                    hr = pPin.ConnectedTo(out pConnectedPin);
                    if (pConnectedPin != null)
                    {
                        Marshal.ReleaseComObject(pConnectedPin);
                        pConnectedPin = null;
                    }
                    if ((hr == DsHlp.S_OK && bConnected) || ((uint)hr == DsHlp.VFW_E_NOT_CONNECTED && !bConnected))
                    {
                        if (nSkipped == nPinsToSkip)
                        {
                            bFound = true;
                            break;
                        }
                        else
                        {
                            nSkipped++;
                        }
                    }
                }
                Marshal.ReleaseComObject(pPin);
                pPin = null;
            }
            Marshal.ReleaseComObject(pEnum);
            return(bFound ? pPin : null);
        }
예제 #10
0
파일: DsHelper.cs 프로젝트: hpavlov/occurec
        private static bool IsMatchingPin(IPin pin, PinDirection direction, Guid mediaType)
        {
            PinDirection pinDirection;
            int hr = pin.QueryDirection(out pinDirection);
            DsError.ThrowExceptionForHR(hr);

            if (pinDirection != direction)
                // The pin lacks direction
                return false;

            IPin connectedPin;
            hr = pin.ConnectedTo(out connectedPin);
            if ((uint)hr != 0x80040209 /* Pin is not connected */)
                DsError.ThrowExceptionForHR(hr);

            if (connectedPin != null)
            {
                // The pin is already connected
                Marshal.ReleaseComObject(connectedPin);
                return false;
            }

            IEnumMediaTypes mediaTypesEnum;
            hr = pin.EnumMediaTypes(out mediaTypesEnum);
            DsError.ThrowExceptionForHR(hr);

            AMMediaType[] mediaTypes = new AMMediaType[1];

            while (mediaTypesEnum.Next(1, mediaTypes, IntPtr.Zero) == 0)
            {
                Guid majorType = mediaTypes[0].majorType;
                DsUtils.FreeAMMediaType(mediaTypes[0]);

                if (majorType == mediaType)
                {
                    // We have found the pin we were looking for
                    return true;
                }
            }

            return false;
        }
예제 #11
0
        protected IPin GetUnconnectedPin(IBaseFilter filter, PinDirection direction)
        {
            IPin      destPin = null;
            IEnumPins iEnum;
            var       pins = new IPin[1];

            var hr = filter.EnumPins(out iEnum);

            Marshal.ThrowExceptionForHR(hr);

            var fetched = Marshal.AllocCoTaskMem(4);

            hr = iEnum.Next(1, pins, fetched);
            Marshal.ThrowExceptionForHR(hr);

            while (Marshal.ReadInt32(fetched) == 1)
            {
                PinDirection pinDir;
                IPin         pPin;
                destPin = pins[0];

                hr = destPin.QueryDirection(out pinDir);
                Marshal.ThrowExceptionForHR(hr);

                PinInfo info;
                destPin.QueryPinInfo(out info);
                //var name = info.name;
                destPin.ConnectedTo(out pPin);

                if (pPin == null && pinDir == direction)
                {
                    break;
                }

                hr = iEnum.Next(1, pins, fetched);
                Marshal.ThrowExceptionForHR(hr);
            }
            Marshal.FreeCoTaskMem(fetched);
            return(destPin);
        }
예제 #12
0
        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))
                                        {
                                            Logger.Instance.Info("found H264 video on output pin");
                                            h264Codec = true;
                                        }
                                        else if (mediaTypes[0].majorType == MediaType.Audio && mediaTypes[0].subType == MediaSubType.LATMAAC)
                                        {
                                            Logger.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", "")));
                    }
                }
            }
        }
예제 #13
0
파일: GraphBase.cs 프로젝트: ttsuki/ttsuki
 /// <summary>
 /// 指定したピンはOutputピンか?
 /// </summary>
 /// <param name="pin">ピン</param>
 /// <returns>Outputピンである場合true</returns>
 public static bool IsOutputPin(IPin pin)
 {
     PinDirection dir;
     pin.QueryDirection(out dir);
     return dir == PinDirection.Output;
 }
예제 #14
0
        public int Connect(IPin pReceivePin, AMMediaType pmt)
        {
            unchecked
            {
                if (_ConnectedPin != null)
                {
                    return((int)HRESULT.VFW_E_ALREADY_CONNECTED);
                }

                PinDirection pd;
                pReceivePin.QueryDirection(out pd);
                if (pd == _Direction)
                {
                    return((int)HRESULT.VFW_E_INVALID_DIRECTION);
                }

                PinInfo pi;
                pReceivePin.QueryPinInfo(out pi);
                FilterInfo fi;
                pi.filter.QueryFilterInfo(out fi);
                Debug.WriteLine(fi.achName + "." + pi.name, _Name + " connect attempt from");

                if (pi.name == "Subpicture Input")
                {
                }

                HRESULT hr = HRESULT.VFW_E_NO_ACCEPTABLE_TYPES;



                //
                // Try match our media types with the specific one requested
                // if any...
                //
                if (null != (pmt))
                {
                    for (int i = 0; i < MediaTypes.Count; i++)
                    {
                        if (MatchesPartial(MediaTypes[i], pmt))
                        {
                            hr = OnReceiveConnection(pReceivePin, MediaTypes[i]);
                            if (hr != HRESULT.S_OK)
                            {
                                // pReceivePin.Disconnect();
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

                //
                // Otherwise try force-connect all of the ones we like.
                //
                if (hr != HRESULT.S_OK)
                {
                    for (int i = 0; i < MediaTypes.Count; i++)
                    {
                        AMMediaType newMT = new AMMediaType();
                        Copy(MediaTypes[i], newMT);

                        hr = (HRESULT)pReceivePin.QueryAccept(newMT);
                        if (hr == HRESULT.S_OK)
                        {
                            hr = OnReceiveConnection(pReceivePin, newMT);
                            if (hr != HRESULT.S_OK)
                            {
                                //pReceivePin.Disconnect();
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

                //
                // Failing that, try connect with any of their numerous
                // supported types..
                //

                if (hr != HRESULT.S_OK)
                {
                    IEnumMediaTypes en;
                    pReceivePin.EnumMediaTypes(out en);
                    en.Reset();

                    /*
                     * Unsafe pointer edition
                     * ======================
                     * I was experminting AMMediaType as a value struct type
                     * for better marshalling.
                     *
                     * AMMediaType* types = null;
                     * int fetched = 0;
                     * do
                     * {
                     *      HRESULT r = en.Next(1, (AMMediaType**)&types, out fetched);
                     *      if (r != HRESULT.S_OK
                     || fetched == 0)
                     ||             break;
                     ||     AMMediaType tmp = *types;
                     ||     for (int x = 0; x < MediaTypes.Count; x++)
                     ||     {
                     ||             if ((MediaTypes[x].majorType == tmp.majorType
                     ||                        && MediaTypes[x].subType == tmp.subType)
                     || AcceptsAnyMedia)
                     ||             {
                     ||                     hr = OnReceiveConnection(pReceivePin, tmp); //MediaTypes[x]
                     ||
                     ||                     if (hr != HRESULT.S_OK)
                     ||                     {
                     ||                             pReceivePin.Disconnect();
                     ||                     }
                     ||                     else
                     ||                             break;
                     ||             }
                     ||     }
                     ||     AMMediaType.Free(tmp);
                     ||} while (fetched > 0);*/

                    AMMediaType[] types   = new AMMediaType[100];
                    IntPtr        ptr     = ToPtr(types);
                    IntPtr        fetched = IntPtr.Zero;
                    en.Next(10, types, fetched);
                    types = FromIntPtr(ptr, fetched.ToInt32());

                    if (fetched.ToInt32() > 0 &&
                        types != null)
                    {
                        for (int x = 0; x < MediaTypes.Count; x++)
                        {
                            for (int i = 0; i < fetched.ToInt32(); i++)
                            {
                                if ((MediaTypes[x].majorType == types[i].majorType &&
                                     MediaTypes[x].subType == types[i].subType) ||
                                    AcceptsAnyMedia)
                                {
                                    hr = OnReceiveConnection(pReceivePin, types[i]);
                                    if (hr != HRESULT.S_OK)
                                    {
                                        //pReceivePin.Disconnect();
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }

                            if (hr == HRESULT.S_OK)
                            {
                                break;
                            }
                        }
                    }

                    for (int i = 0; i < fetched.ToInt32(); i++)
                    {
                        Free(types[i]);
                    }


                    if (Marshal.IsComObject(en))
                    {
                        Marshal.ReleaseComObject(en);
                    }
                }


                if (hr != HRESULT.S_OK)
                {
                    // Final clean up
                    this.Disconnect();

                    if (Marshal.IsComObject(pReceivePin))
                    {
                        Marshal.ReleaseComObject(pReceivePin);
                    }

                    // Connect failed, but may have returned S_FALSE
                    // so this ensures a better return code.
                    hr = HRESULT.VFW_E_NO_ACCEPTABLE_TYPES;
                }

                if (hr == HRESULT.S_OK)
                {
                    ResolveConnectedFilter();
                }

                return((int)hr);
            }
        }