Example #1
0
        public static List <AMMediaType> GetMediaTypes(IPin pin)
        {
            List <AMMediaType> mediaTypes     = new List <AMMediaType>();
            IEnumMediaTypes    mediaTypesEnum = null;

            AMMediaType[] mta = new AMMediaType[1];

            if (pin.EnumMediaTypes(out mediaTypesEnum) == 0)
            {
                int n;

                try
                {
                    while (mediaTypesEnum.Next(1, mta, out n) == 0)
                    {
                        if (n == 1)
                        {
                            mediaTypes.Add(mta[0]);
                        }
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(mediaTypesEnum);
                }
            }

            return(mediaTypes);
        }
Example #2
0
        /// <summary>
        /// Gets iSC's available _AMMediaTypes, without freeing pbFormat
        /// Caller should call MediaType.Free(_AMMediaType[]) when done
        /// </summary>
        public static _AMMediaType[] GetMediaTypes(IPin pin)
        {
            IEnumMediaTypes iEnum;
            pin.EnumMediaTypes(out iEnum);

            ArrayList alMTs = new ArrayList();

            IntPtr[] ptrs = new IntPtr[1];
            uint fetched;

            iEnum.Next(1, ptrs, out fetched);

            while(fetched == 1)
            {
                _AMMediaType mt = (_AMMediaType)Marshal.PtrToStructure(ptrs[0], typeof(_AMMediaType));
                alMTs.Add(mt);

                Marshal.FreeCoTaskMem(ptrs[0]);
                ptrs[0] = IntPtr.Zero;

                iEnum.Next(1, ptrs, out fetched);
            }

            _AMMediaType[] mts = new _AMMediaType[alMTs.Count];
            alMTs.CopyTo(mts);

            return mts;
        }
        private static IEnumMediaTypes GetEnumMediaTypes()
        {
            IBaseFilter filter;
            int         hr;
            IPin        pRet = null;

            DsDevice [] capDevices;

            // Get the collection of video devices
            capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            if (capDevices.Length == 0)
            {
                throw new Exception("No video capture devices found!");
            }

            DsDevice dev = capDevices[0];
            string   s;

            dev.Mon.GetDisplayName(null, null, out s);
            filter = Marshal.BindToMoniker(s) as IBaseFilter;

            pRet = DsFindPin.ByDirection(filter, PinDirection.Output, 0);

            IEnumMediaTypes enumMediaTypes;

            hr = pRet.EnumMediaTypes(out enumMediaTypes);
            DsError.ThrowExceptionForHR(hr);

            return(enumMediaTypes);
        }
        public static bool IsVideo(IPin pin)
        {
            if (pin == null)
            {
                throw new ArgumentNullException("pin");
            }

            int             hr;
            bool            bRet = false;
            var             pmt  = new AMMediaType[1];
            IEnumMediaTypes ppEnum;
            IntPtr          i = IntPtr.Zero;

            // Walk the MediaTypes for the pin
            hr = pin.EnumMediaTypes(out ppEnum);
            DESError.ThrowExceptionForHR(hr);

            try
            {
                // Just read the first one
                hr = ppEnum.Next(1, pmt, i);
                DESError.ThrowExceptionForHR(hr);

                bRet = pmt[0].majorType == MediaType.Video;
            }
            finally
            {
                Marshal.ReleaseComObject(ppEnum);
            }
            DsUtils.FreeAMMediaType(pmt[0]);

            return(bRet);
        }
Example #5
0
        private static bool checkMediaTypes(IPin pin, Guid mediaType, Guid mediaSubType)
        {
            int reply = 0;

            IEnumMediaTypes enumMediaTypes = null;

            AMMediaType[] mediaTypes = new AMMediaType[1];

            reply = pin.EnumMediaTypes(out enumMediaTypes);
            DsError.ThrowExceptionForHR(reply);

            while (enumMediaTypes.Next(mediaTypes.Length, mediaTypes, IntPtr.Zero) == 0)
            {
                foreach (AMMediaType currentMediaType in mediaTypes)
                {
                    if (currentMediaType != null)
                    {
                        if (currentMediaType.majorType == mediaType &&
                            currentMediaType.subType == mediaSubType)
                        {
                            Marshal.ReleaseComObject(enumMediaTypes);
                            return(true);
                        }
                    }
                }
            }

            Marshal.ReleaseComObject(enumMediaTypes);

            return(false);
        }
Example #6
0
        /// <summary>
        /// Determine whether a specified pin is audio or video
        /// </summary>
        /// <param name="pPin">Pin to check</param>
        /// <returns>True if pin is video</returns>
        private bool IsVideo(IPin pPin)
        {
            int  hr;
            bool bRet = false;

            AMMediaType[]   pmt = new AMMediaType[1];
            IEnumMediaTypes ppEnum;

            // Walk the MediaTypes for the pin
            hr = pPin.EnumMediaTypes(out ppEnum);
            DESError.ThrowExceptionForHR(hr);

            try
            {
                // Just read the first one
                hr = ppEnum.Next(1, pmt, IntPtr.Zero);
                DESError.ThrowExceptionForHR(hr);

                bRet = pmt[0].majorType == MediaType.Video;
            }
            finally
            {
                Marshal.ReleaseComObject(ppEnum);
            }
            DsUtils.FreeAMMediaType(pmt[0]);

            return(bRet);
        }
Example #7
0
        /// <summary>
        /// Gets iSC's available _AMMediaTypes, without freeing pbFormat
        /// Caller should call MediaType.Free(_AMMediaType[]) when done
        /// </summary>
        public static _AMMediaType[] GetMediaTypes(IPin pin)
        {
            IEnumMediaTypes iEnum;

            pin.EnumMediaTypes(out iEnum);

            ArrayList alMTs = new ArrayList();

            IntPtr[] ptrs = new IntPtr[1];
            uint     fetched;

            iEnum.Next(1, ptrs, out fetched);

            while (fetched == 1)
            {
                _AMMediaType mt = (_AMMediaType)Marshal.PtrToStructure(ptrs[0], typeof(_AMMediaType));
                alMTs.Add(mt);

                Marshal.FreeCoTaskMem(ptrs[0]);
                ptrs[0] = IntPtr.Zero;

                iEnum.Next(1, ptrs, out fetched);
            }

            _AMMediaType[] mts = new _AMMediaType[alMTs.Count];
            alMTs.CopyTo(mts);

            return(mts);
        }
Example #8
0
        private int VideoIndex(IPin pPin)
        {
            int hr;
            int bRet = -1;

            AMMediaType[]   pmt = new AMMediaType[1];
            IEnumMediaTypes ppEnum;

            // Walk the MediaTypes for the pin
            hr = pPin.EnumMediaTypes(out ppEnum);
            DsError.ThrowExceptionForHR(hr);

            try {
                do
                {
                    bRet++;
                    // Just read the first one
                    hr = ppEnum.Next(1, pmt, IntPtr.Zero);
                    DsError.ThrowExceptionForHR(hr);
                } while (pmt[0].majorType != MediaType.Video);
            } catch {
                bRet = -1;
            } finally {
                Marshal.ReleaseComObject(ppEnum);
            }
            DsUtils.FreeAMMediaType(pmt[0]);

            return(bRet);
        }
Example #9
0
        // if subtype is not supported by the pin the return value is -1
        // if subtype is supported the return value indicates how many other types
        // were skipped before we got this one.
        public static int IsSubTypeSupported(IPin pPin, Guid subtype)
        {
            IEnumMediaTypes pEnumTypes;
            int             cFetched;
            int             nSkipped = 0;
            bool            bFound   = false;

            int hr = pPin.EnumMediaTypes(out pEnumTypes);

            if (DsHlp.FAILED(hr))
            {
                return(-1);
            }

            IntPtr ptr;

            while (pEnumTypes.Next(1, out ptr, out cFetched) == DsHlp.S_OK)
            {
                AMMediaType mt = (AMMediaType)Marshal.PtrToStructure(ptr, typeof(AMMediaType));
                if (mt.subType == subtype)
                {
                    FreeFormatBlock(ptr);
                    Marshal.FreeCoTaskMem(ptr);
                    bFound = true;
                    break;
                }
                // free the allocated memory
                FreeFormatBlock(ptr);
                Marshal.FreeCoTaskMem(ptr);
                nSkipped++;
            }

            Marshal.ReleaseComObject(pEnumTypes);
            return(bFound ? nSkipped : -1);
        }
Example #10
0
        public static MediaType[] GetMediaTypes(this IPin pPin)
        {
            var mediaTypes = new List <MediaType>();

            pPin.EnumMediaTypes(mt => mediaTypes.Add(new MediaType(mt.majorType, mt.subType)));

            return(mediaTypes.ToArray());
        }
        /// <summary>
        /// Returns a point with the max size of capture device capture window
        /// </summary>
        /// <param name="p_capPin">The pin that we are polling</param>
        /// <returns></returns>
        private int getMedia(IPin p_capPin, out AMMediaType p_refMedia)
        {
            int hr     = 0;
            int curMax = 0;

            p_refMedia = null;

            //Holds the video info
            VideoInfoHeader v = new VideoInfoHeader();

            //Enumeration of media types
            IEnumMediaTypes mediaTypeEnum;

            hr = p_capPin.EnumMediaTypes(out mediaTypeEnum);
            DsError.ThrowExceptionForHR(hr);

            //This array is size one because we will only be loading in one value at a time
            AMMediaType[] mediaTypes = new AMMediaType[1];
            IntPtr        fetched    = IntPtr.Zero;

            hr = mediaTypeEnum.Next(1, mediaTypes, fetched);
            DsError.ThrowExceptionForHR(hr);

            //Iterate through viable mediatypes and find the largest media
            while (fetched != null && mediaTypes[0] != null)
            {
                //Load the format pointer into v
                Marshal.PtrToStructure(mediaTypes[0].formatPtr, v);
                if (v.BmiHeader.Size != 0 && v.BmiHeader.ImageSize != 0)
                {
                    //Find the largest image with format of VideoFormat.  There will be two one with Video
                    // and another with VideoFormat2, SammpleGrabber will ony accept VideoFormat
                    // For simplicity down the road i want an RGB24 format
                    if (v.BmiHeader.ImageSize > curMax && mediaTypes[0].formatType == FormatType.VideoInfo &&
                        (mediaTypes[0].subType == MediaSubType.MJPG))
                    {
                        p_refMedia = mediaTypes[0];
                        curMax     = v.BmiHeader.ImageSize;
                    }
                }

                hr = mediaTypeEnum.Next(1, mediaTypes, fetched);
                DsError.ThrowExceptionForHR(hr);
            }

            hr = 0;

            if (p_refMedia == null)
            {
                hr = -1;
            }

            Marshal.ReleaseComObject(mediaTypeEnum);

            return(hr);
        }
Example #12
0
        private void RenderNetDemux()
        {
            int hr;

            IEnumPins enumPins;

            IPin[] pins = new IPin[1];

            hr = _netDmx.EnumPins(out enumPins);
            DsError.ThrowExceptionForHR(hr);
            try
            {
                while (enumPins.Next(pins.Length, pins, IntPtr.Zero) == 0)
                {
                    try
                    {
                        PinInfo pinInfo;
                        IPin    upstreamPin = pins[0];
                        hr = upstreamPin.QueryPinInfo(out pinInfo);
                        DsError.ThrowExceptionForHR(hr);
                        if (pinInfo.dir == PinDirection.Output)
                        {
                            IEnumMediaTypes enumMediaTypes;
                            hr = upstreamPin.EnumMediaTypes(out enumMediaTypes);
                            DsError.ThrowExceptionForHR(hr);
                            AMMediaType[] mediaTypes = new AMMediaType[1];

                            if (enumMediaTypes.Next(1, mediaTypes, IntPtr.Zero) == 0)
                            {
                                AMMediaType mediaType = mediaTypes[0];
                                if (mediaType.majorType == MediaType.Video)
                                {
                                    hr = _captureGraphBuilder.RenderStream(null, null, upstreamPin, _videoDecoder, _videoRender);
                                }
                                else
                                {
                                    AddAudioVolumeFilter();
                                    AddAudioRenderer();
                                    ConnectToAudio(upstreamPin, true);
                                }
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(pins[0]);
                    }
                }
            }
            finally
            {
                Marshal.ReleaseComObject(enumPins);
            }
        }
Example #13
0
        /// <summary>
        /// Finds a pin that exists in a graph.
        /// </summary>
        /// <param name="majorOrMinorMediaType">The GUID of the major or minor type of the media</param>
        /// <param name="pinDirection">The direction of the pin - in/out</param>
        /// <param name="graph">The graph to search in</param>
        /// <returns>Returns null if the pin was not found, or if a pin is found, returns the first instance of it</returns>
        protected static IPin FindPinInGraphByMediaType(Guid majorOrMinorMediaType, PinDirection pinDirection, IGraphBuilder graph)
        {
            IEnumFilters enumFilters;

            /* Get the filter enum */
            graph.EnumFilters(out enumFilters);

            /* Init our vars */
            var             filters        = new IBaseFilter[1];
            var             fetched        = IntPtr.Zero;
            IPin            pin            = null;
            IEnumMediaTypes mediaTypesEnum = null;

            /* Loop over each filter in the graph */
            while (enumFilters.Next(1, filters, fetched) == 0)
            {
                var filter = filters[0];

                int i = 0;

                /* Loop over each pin in the filter */
                while ((pin = DsFindPin.ByDirection(filter, pinDirection, i)) != null)
                {
                    /* Get the pin enumerator */
                    pin.EnumMediaTypes(out mediaTypesEnum);
                    var mediaTypesFetched = IntPtr.Zero;
                    var mediaTypes        = new AMMediaType[1];

                    /* Enumerate the media types on the pin */
                    while (mediaTypesEnum.Next(1, mediaTypes, mediaTypesFetched) == 0)
                    {
                        /* See if the major or subtype meets our requirements */
                        if (mediaTypes[0].majorType.Equals(majorOrMinorMediaType) || mediaTypes[0].subType.Equals(majorOrMinorMediaType))
                        {
                            /* We found a match */
                            goto done;
                        }
                    }
                    i++;
                }
            }

done:
            if (mediaTypesEnum != null)
            {
                mediaTypesEnum.Reset();
                Marshal.ReleaseComObject(mediaTypesEnum);
            }

            enumFilters.Reset();
            Marshal.ReleaseComObject(enumFilters);

            return(pin);
        }
Example #14
0
        public void TestEnumMediaTypes()
        {
            IPin            testPin = GetSmartTeeInputPin();
            IEnumMediaTypes enumMediaTypes;
            int             hr = testPin.EnumMediaTypes(out enumMediaTypes);

            Marshal.ThrowExceptionForHR(hr);

            Assert.IsNotNull(enumMediaTypes);

            hr = enumMediaTypes.Reset();
            Marshal.ThrowExceptionForHR(hr);
        }
Example #15
0
        /// <summary>
        /// QueryConnect checks if two Pins can be connected.
        /// </summary>
        /// <param name="pin">Pin 1</param>
        /// <param name="other">Pin 2</param>
        /// <returns>True if they accept connection</returns>
        static bool QueryConnect(IPin pin, IPin other)
        {
            IEnumMediaTypes enumTypes;
            int             hr = pin.EnumMediaTypes(out enumTypes);

            if (hr != 0 || enumTypes == null)
            {
                return(false);
            }

            int    count      = 0;
            IntPtr ptrFetched = Marshal.AllocCoTaskMem(4);

            try
            {
                for (; ;)
                {
                    AMMediaType[] types = new AMMediaType[1];
                    hr = enumTypes.Next(1, types, ptrFetched);
                    if (hr != 0 || Marshal.ReadInt32(ptrFetched) == 0)
                    {
                        break;
                    }

                    count++;
                    try
                    {
                        if (other.QueryAccept(types[0]) == 0)
                        {
                            return(true);
                        }
                    }
                    finally
                    {
                        FilterGraphTools.FreeAMMediaType(types[0]);
                    }
                }
                PinInfo info;
                PinInfo infoOther;
                pin.QueryPinInfo(out info);
                other.QueryPinInfo(out infoOther);
                ServiceRegistration.Get <ILogger>().Info("Pins {0} and {1} do not accept each other. Tested {2} media types", info.name, infoOther.name, count);
                FilterGraphTools.FreePinInfo(info);
                FilterGraphTools.FreePinInfo(infoOther);
                return(false);
            }
            finally
            {
                Marshal.FreeCoTaskMem(ptrFetched);
            }
        }
Example #16
0
        private int getPinMediaType(IPin pPin, Guid majorType, Guid minorType, Guid formatType, ref AMMediaType mediaType)
        {
            if (pPin == null)
            {
                throw new Exception("No pin");
            }

            int hr;

            IEnumMediaTypes pEnum = null;

            hr = pPin.EnumMediaTypes(out pEnum);
            DsError.ThrowExceptionForHR(hr);
            bool   found   = false;
            IntPtr fetched = IntPtr.Zero;

            AMMediaType[] pMT = new AMMediaType[1];
            hr = pEnum.Next(1, pMT, fetched);
            while (hr == 0)
            {
                if ((majorType.Equals(Guid.Empty)) || (majorType.Equals(pMT[0].majorType)))
                {
                    if ((minorType.Equals(Guid.Empty)) || (minorType.Equals(pMT[0].subType)))
                    {
                        if ((formatType.Equals(Guid.Empty)) || (formatType.Equals(pMT[0].formatType)))
                        // Match
                        {
                            found     = true;
                            mediaType = pMT[0];
                            break;
                        }
                    }
                }

                DsUtils.FreeAMMediaType(pMT[0]);

                hr = pEnum.Next(1, pMT, fetched);
            }

            // End
            Marshal.ReleaseComObject(pEnum);
            if (found)
            {
                return(0);
            }
            else
            {
                return(-1);
            }
        }
Example #17
0
        public static string DumpPinInfo(IPin pin)
        {
            StringBuilder sb = new StringBuilder();
            int           hr;

            sb.AppendLine(">>>>> Pin Info");

            {
                PinInfo pi;
                hr = pin.QueryPinInfo(out pi);
                if (0 == hr)
                {
                    sb.AppendLine(string.Format("PinInfo  name: {0}  direction: {1}", pi.name, pi.dir.ToString()));
                }
                else
                {
                    sb.AppendLine("PinInfo: " + GetErrorText(hr));
                }
            }

            // Pins info
            {
                IEnumMediaTypes enumMediaTypes = null;
                AMMediaType[]   mediaTypes     = new AMMediaType[1] {
                    null
                };

                try
                {
                    hr = pin.EnumMediaTypes(out enumMediaTypes);
                    DsError.ThrowExceptionForHR(hr);

                    while (enumMediaTypes.Next(1, mediaTypes, IntPtr.Zero) == 0)
                    {
                        sb.AppendLine(DumpAMMediaTypeInfo(mediaTypes[0]));
                        DsUtils.FreeAMMediaType(mediaTypes[0]);
                        mediaTypes[0] = null;
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(enumMediaTypes);
                }
            }

            sb.AppendLine("<<<<< Pin Info");

            return(sb.ToString());
        }
Example #18
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);
        }
Example #19
0
        private void logMediaTypes(IPin pin)
        {
            IEnumMediaTypes mediaTypes = null;

            AMMediaType[] mediaType = new AMMediaType[1];

            AMMediaType connectedMediaType = new AMMediaType();

            reply = pin.ConnectionMediaType(connectedMediaType);

            reply = pin.EnumMediaTypes(out mediaTypes);
            if (reply != 0)
            {
                LogMessage("Media types cannot be determined at this time (not connected yet?)");
                return;
            }

            while (mediaTypes.Next(mediaType.Length, mediaType, IntPtr.Zero) == 0)
            {
                foreach (AMMediaType currentMediaType in mediaType)
                {
                    PinInfo pinInfo;
                    reply = pin.QueryPinInfo(out pinInfo);
                    DsError.ThrowExceptionForHR(reply);

                    string majorType = TranslateMediaMajorType(currentMediaType.majorType);
                    string subType   = TranslateMediaSubType(currentMediaType.subType);

                    string connectedComment;

                    if (currentMediaType.majorType == connectedMediaType.majorType && currentMediaType.subType == connectedMediaType.subType)
                    {
                        connectedComment = "** Connected **";
                    }
                    else
                    {
                        connectedComment = string.Empty;
                    }

                    LogMessage("Media type: " +
                               majorType + " ; " +
                               subType + " " +
                               currentMediaType.fixedSizeSamples + " " +
                               currentMediaType.sampleSize + " " +
                               connectedComment);
                }
            }
        }
Example #20
0
        private static IPin FindPinByMediaType(IBaseFilter filter, PinDirection direction, Guid mType, Guid sType)
        {
            IPin pRet = null;
            IPin tPin = null;
            int  hr;
            int  index = 0;

            tPin = DsFindPin.ByDirection(filter, direction, index);
            while (tPin != null)
            {
                IEnumMediaTypes emtDvr = null;
                AMMediaType[]   amtDvr = new AMMediaType[1];

                try
                {
                    tPin.EnumMediaTypes(out emtDvr);

                    hr = emtDvr.Next(1, amtDvr, IntPtr.Zero);
                    DsError.ThrowExceptionForHR(hr);

                    if (amtDvr[0] != null && amtDvr[0].majorType == mType && (amtDvr[0].subType == sType || sType == MediaSubType.Null))
                    {
                        pRet = tPin;
                        break;
                    }
                }
                finally
                {
                    DsUtils.FreeAMMediaType(amtDvr[0]);
                    if (emtDvr != null)
                    {
                        Marshal.ReleaseComObject(emtDvr);
                    }
                }

                if (tPin != null)
                {
                    Marshal.ReleaseComObject(tPin);
                }
                tPin = null;
                index++;
                tPin = DsFindPin.ByDirection(filter, direction, index);
            }

            return(pRet);
        }
Example #21
0
        /// <summary>
        /// Get the IPinIPType for the pin's prefered or connected media type
        /// </summary>
        /// <param name="pin"></param>
        /// <returns></returns>
        private Type GetPinMajorMediaType(IPin pin)
        {
            Type        t       = null;
            AMMediaType contype = new AMMediaType();
            int         hr      = pin.ConnectionMediaType(contype);

            if (hr == 0)
            {
                t = PinDataTypes.GetMajorPinType(contype.majorType);
                DsUtils.FreeAMMediaType(contype);
                return(t);
            }
            else
            {
                // wasn't connected, enumerate the prefered media types and get the major type of the first one
                IEnumMediaTypes penum = null;
                hr = pin.EnumMediaTypes(out penum);
                if (hr == 0 && penum != null)
                {
                    AMMediaType[] mtypes  = new AMMediaType[1];
                    IntPtr        fetched = Marshal.AllocCoTaskMem(4);
                    try
                    {
                        if (penum.Next(1, mtypes, fetched) == 0)
                        {
                            t = PinDataTypes.GetMajorPinType(mtypes[0].majorType);
                            DsUtils.FreeAMMediaType(mtypes[0]);
                            Marshal.ReleaseComObject(penum);
                            return(t);
                        }
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(fetched);
                    }
                }
            }

            // couldn't get the pin's major media type
            return(typeof(PinDataTypes.Unknown));
        }
Example #22
0
        public static void SplitterForFile(string file)
        {
            int hr;

            IBaseFilter reader = (IBaseFilter) new AsyncReader();

            hr = (reader as IFileSourceFilter).Load(file, null);

            IPin oPin = DsFindPin.ByDirection(reader, PinDirection.Output, 0);

            IEnumMediaTypes eMedia = null;

            AMMediaType[] mediaTypes = new AMMediaType[1];

            hr = oPin.EnumMediaTypes(out eMedia);

            while (eMedia.Next(mediaTypes.Length, mediaTypes, IntPtr.Zero) == 0)
            {
                GetDevicesWithThisInPin(mediaTypes[0].majorType, mediaTypes[0].subType);
            }
        }
Example #23
0
            /// <summary>
            /// 指定したピンが指定したMediaTypeをサポートするか?
            /// </summary>
            /// <param name="pin">ピン</param>
            /// <param name="major">majorType</param>
            /// <param name="sub">subType or 検証不要な場合 Guid.Empty</param>
            /// <returns>サポートしてる場合true</returns>
            public static bool IsPinSupportsMediaType(IPin pin, Guid major, Guid sub)
            {
                bool            found      = false;
                IEnumMediaTypes enumerator = null;

                try
                {
                    pin.EnumMediaTypes(out enumerator);
                    IntPtr      pMedia = IntPtr.Zero;
                    AMMediaType media  = new AMMediaType();

                    found = false;
                    enumerator.Reset();
                    while (!found && enumerator.Next(1, out pMedia, IntPtr.Zero) == S_OK)
                    {
                        if (pMedia == IntPtr.Zero)
                        {
                            continue;
                        }
                        Marshal.PtrToStructure(pMedia, media);

                        if (media.majorType == major && (sub == Guid.Empty || media.subType != sub))
                        {
                            found = true;
                        }

                        Util.FreeMediaType(media);
                        Marshal.FreeCoTaskMem(pMedia);
                    }
                }
                finally
                {
                    if (enumerator != null)
                    {
                        Marshal.ReleaseComObject(enumerator);
                    }
                    enumerator = null;
                }
                return(found);
            }
            int IPin.Connect(IPin pReceivePin, AMMediaType pmt)
            {
                IEnumMediaTypes emt;

                if (connectedPin != null)
                {
                    return(DsResults.E_AlreadyConnected);
                }
                if (pReceivePin == null)
                {
                    return(E_POINTER);
                }
                if (pReceivePin.EnumMediaTypes(out emt) == S_OK)
                {
                    var mediaTypes = new AMMediaType[1];

                    while (emt.Next(1, mediaTypes, IntPtr.Zero) == S_OK)
                    {
                        if (mediaTypes[0].majorType == MediaType.Stream && pReceivePin.ReceiveConnection(this, mediaTypes[0]) == S_OK)
                        {
                            emt          = null;
                            connectedPin = pReceivePin;
                            return(S_OK);
                        }
                    }
                    emt = null;
                }
                if (pmt != null && pmt.majorType != MediaType.Null && pmt.majorType != MediaType.Stream)
                {
                    return(DsResults.E_TypeNotAccepted);
                }
                if (pReceivePin.ReceiveConnection(this, mediaType) == S_OK)
                {
                    connectedPin = pReceivePin;
                    return(S_OK);
                }
                return(DsResults.E_NoAcceptableTypes);
            }
Example #25
0
        public static void EnumMediaTypes(this IPin pPin, Action <AMMediaType> action)
        {
            IEnumMediaTypes pEnumTypes;

            var hr = pPin.EnumMediaTypes(out pEnumTypes);

            if (hr == DsHlp.S_OK)
            {
                IntPtr ptr;
                int    cFetched;

                if (pEnumTypes.Next(1, out ptr, out cFetched) == DsHlp.S_OK)
                {
                    AMMediaType mt = (AMMediaType)Marshal.PtrToStructure(ptr, typeof(AMMediaType));

                    action(mt);

                    DsUtils.FreeFormatBlock(ptr);
                    Marshal.FreeCoTaskMem(ptr);
                }
                Marshal.ReleaseComObject(pEnumTypes);
            }
        }
Example #26
0
        /// <summary>
        /// Constructs a DetailPinInfo for the specified pin
        /// </summary>
        /// <param name="pin">IPin interface to get info about</param>
        public DetailPinInfo(IPin pin)
        {
            PinInfo pinInfo = new PinInfo();
            int     hr      = pin.QueryPinInfo(out pinInfo);

            DsError.ThrowExceptionForHR(hr);

            IEnumMediaTypes enumMediaTypes;

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

            AMMediaType[] curMediaType = new AMMediaType[1];

            IntPtr fetched2 = Marshal.AllocCoTaskMem(4);

            try
            {
                if (enumMediaTypes.Next(1, curMediaType, fetched2) == 0)
                {
                    if (Marshal.ReadInt32(fetched2) != 1)
                    {
                        throw new Exception("Cannot enumerate media types for pin!");
                    }
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(fetched2);
            }

            this.Pin  = pin;
            this.Info = pinInfo;
            this.Type = curMediaType[0];

            BaseDSGraph.Release(enumMediaTypes);
        }
Example #27
0
        public static Guid GetMediaType(IPin pPin)
        {
            Guid            mediaType = Guid.Empty;
            IEnumMediaTypes pEnumTypes;
            int             cFetched;

            int hr = pPin.EnumMediaTypes(out pEnumTypes);

            if (DsHlp.SUCCEEDED(hr))
            {
                IntPtr ptr;
                if (pEnumTypes.Next(1, out ptr, out cFetched) == DsHlp.S_OK)
                {
                    AMMediaType mt = (AMMediaType)Marshal.PtrToStructure(ptr, typeof(AMMediaType));
                    mediaType = mt.majorType;
                    // free the allocated memory
                    FreeFormatBlock(ptr);
                    Marshal.FreeCoTaskMem(ptr);
                }

                Marshal.ReleaseComObject(pEnumTypes);
            }
            return(mediaType);
        }
Example #28
0
    /// <summary>
    /// QueryConnect checks if two Pins can be connected.
    /// </summary>
    /// <param name="pin">Pin 1</param>
    /// <param name="other">Pin 2</param>
    /// <returns>True if they accept connection</returns>
    static bool QueryConnect(IPin pin, IPin other)
    {
      IEnumMediaTypes enumTypes;
      int hr = pin.EnumMediaTypes(out enumTypes);
      if (hr != 0 || enumTypes == null)
        return false;

      int count = 0;
      IntPtr ptrFetched = Marshal.AllocCoTaskMem(4);
      try
      {
        for (; ; )
        {
          AMMediaType[] types = new AMMediaType[1];
          hr = enumTypes.Next(1, types, ptrFetched);
          if (hr != 0 || Marshal.ReadInt32(ptrFetched) == 0)
            break;

          count++;
          try
          {
            if (other.QueryAccept(types[0]) == 0)
              return true;
          }
          finally
          {
            FilterGraphTools.FreeAMMediaType(types[0]);
          }
        }
        PinInfo info;
        PinInfo infoOther;
        pin.QueryPinInfo(out info);
        other.QueryPinInfo(out infoOther);
        ServiceRegistration.Get<ILogger>().Info("Pins {0} and {1} do not accept each other. Tested {2} media types", info.name, infoOther.name, count);
        FilterGraphTools.FreePinInfo(info);
        FilterGraphTools.FreePinInfo(infoOther);
        return false;
      }
      finally
      {
        Marshal.FreeCoTaskMem(ptrFetched);
      }
    }
Example #29
0
    protected void RebuildMediaType(IPin pPin)
    {
      if (pPin != null)
      {
        //Detection if the Video Stream is VC-1 on output pin of the splitter
        IEnumMediaTypes enumMediaTypesAudioVideo;
        int hr = pPin.EnumMediaTypes(out enumMediaTypesAudioVideo);
        AMMediaType[] mediaTypes = new AMMediaType[1];
        int typesFetched;
        PinInfo pinInfo;
        pPin.QueryPinInfo(out pinInfo);

        while (0 == enumMediaTypesAudioVideo.Next(1, mediaTypes, out typesFetched))
        {
          if (typesFetched == 0)
            break;

          if (mediaTypes[0].majorType == MediaType.Video && !pinInfo.name.ToLowerInvariant().Contains("sub"))
          {
            if (mediaTypes[0].subType == MediaSubType.VC1)
            {
              Log.Info("VideoPlayer9: found VC-1 video out pin");
              vc1Codec = true;
            }
            if (mediaTypes[0].subType == MediaSubType.H264 || mediaTypes[0].subType == MediaSubType.AVC1)
            {
              Log.Info("VideoPlayer9: found H264 video out pin");
              h264Codec = true;
            }
            if (mediaTypes[0].subType == MediaSubType.XVID || mediaTypes[0].subType == MediaSubType.xvid ||
                mediaTypes[0].subType == MediaSubType.dx50 || mediaTypes[0].subType == MediaSubType.DX50 ||
                mediaTypes[0].subType == MediaSubType.divx || mediaTypes[0].subType == MediaSubType.DIVX)
            {
              Log.Info("VideoPlayer9: found XVID video out pin");
              xvidCodec = true;
            }
            MediatypeVideo = true;
          }
          else if (mediaTypes[0].majorType == MediaType.Audio)
          {
            //Detection if the Audio Stream is AAC on output pin of the splitter
            if (mediaTypes[0].subType == MediaSubType.LATMAAC || mediaTypes[0].subType == MediaSubType.AAC)
            {
              Log.Info("VideoPlayer9: found AAC Audio out pin");
              aacCodec = true;
            }
            if (mediaTypes[0].subType == MediaSubType.LATMAACLAF)
            {
              Log.Info("VideoPlayer9: found AAC LAVF Audio out pin");
              aacCodecLav = true;
            }
            MediatypeAudio = true;
          }
          else if (mediaTypes[0].majorType == MediaType.Subtitle)
          {
            MediatypeSubtitle = true;
          }
        }
        DirectShowUtil.ReleaseComObject(enumMediaTypesAudioVideo);
        enumMediaTypesAudioVideo = null;
      }
    }
        private int VideoIndex(IPin pPin)
        {
            int hr;
            int bRet = -1;
            AMMediaType[] pmt = new AMMediaType[1];
            IEnumMediaTypes ppEnum;

            // Walk the MediaTypes for the pin
            hr = pPin.EnumMediaTypes(out ppEnum);
            DsError.ThrowExceptionForHR(hr);

            try {
                do {
                    bRet++;
                    // Just read the first one
                    hr = ppEnum.Next(1, pmt, IntPtr.Zero);
                    DsError.ThrowExceptionForHR(hr);

                } while (pmt[0].majorType != MediaType.Video);
            } catch {
                bRet = -1;
            } finally {
                Marshal.ReleaseComObject(ppEnum);
            }
            DsUtils.FreeAMMediaType(pmt[0]);

            return bRet;
        }
 private static bool TestMediaTypes(IPin pin, IPin receiver)
 {
   bool ret = false;
   IEnumMediaTypes types;
   pin.EnumMediaTypes(out types);
   types.Reset();
   while (true)
   {
     AMMediaType[] mediaTypes = new AMMediaType[1];
     int typesFetched;
     int hr = types.Next(1, mediaTypes, out typesFetched);
     if (hr != 0 || typesFetched == 0)
     {
       break;
     }
     //Log.Info("Check output type: {0}, {1}", mediaTypes[0].majorType,
     //  mediaTypes[0].subType);
     if (receiver.QueryAccept(mediaTypes[0]) == 0)
     {
       //Log.Info("Accepted!");
       ret = true;
       break;
     }
   }
   ReleaseComObject(types);
   //Log.Info("-----EndofTypes");
   return ret;
 }
            int IPin.Connect(IPin pReceivePin, AMMediaType pmt)
            {
                IEnumMediaTypes emt;

                if (connectedPin != null)
                    return DsResults.E_AlreadyConnected;
                if (pReceivePin == null)
                    return E_POINTER;
                if (pReceivePin.EnumMediaTypes(out emt) == S_OK)
                {
                    var mediaTypes = new AMMediaType[1];

                    while (emt.Next(1, mediaTypes, IntPtr.Zero) == S_OK)
                        if (mediaTypes[0].majorType == MediaType.Stream && pReceivePin.ReceiveConnection(this, mediaTypes[0]) == S_OK)
                        {
                            emt = null;
                            connectedPin = pReceivePin;
                            return S_OK;
                        }
                    emt = null;
                }
                if (pmt != null && pmt.majorType != MediaType.Null && pmt.majorType != MediaType.Stream)
                    return DsResults.E_TypeNotAccepted;
                if (pReceivePin.ReceiveConnection(this, mediaType) == S_OK)
                {
                    connectedPin = pReceivePin;
                    return S_OK;
                }
                return DsResults.E_NoAcceptableTypes;
            }
Example #33
0
        protected override void RenderSource()
        {
            int     hr;
            Boolean videoComplete = false;

            IEnumPins enumPins;

            IPin[] pins = new IPin[1];

            hr = _netSrc.EnumPins(out enumPins);
            DsError.ThrowExceptionForHR(hr);
            try
            {
                while (enumPins.Next(pins.Length, pins, IntPtr.Zero) == 0)
                {
                    try
                    {
                        PinInfo pinInfo;
                        IPin    upstreamPin = pins[0];
                        hr = upstreamPin.QueryPinInfo(out pinInfo);
                        DsError.ThrowExceptionForHR(hr);
                        if (pinInfo.dir == PinDirection.Output)
                        {
                            IEnumMediaTypes enumMediaTypes;
                            hr = upstreamPin.EnumMediaTypes(out enumMediaTypes);
                            DsError.ThrowExceptionForHR(hr);
                            AMMediaType[] mediaTypes = new AMMediaType[1];
                            if (enumMediaTypes.Next(1, mediaTypes, IntPtr.Zero) == 0)
                            {
                                AMMediaType mediaType = mediaTypes[0];
                                if (mediaType.majorType == MediaType.Video)
                                {
                                    if ((mediaType.subType == new Guid("34363268-0000-0010-8000-00AA00389B71")) ||
                                        (mediaType.subType == new Guid("34363248-0000-0010-8000-00aa00389b71")) ||
                                        (mediaType.subType == new Guid("3436324c-0000-0010-8000-00aa00389b71")) ||
                                        (mediaType.subType == new Guid("31637661-5349-4d4f-4544-494154595045")) ||
                                        (mediaType.subType == new Guid("8d2d71cb-243f-45e3-b2d8-5fd7967ec09b")))
                                    {
                                        _decoderFilter = AddFilterByName(_graphBuilder, FilterCategory.LegacyAmFilterCategory, "Microsoft DTV-DVD Video Decoder");
                                        ConnectFilters(_graphBuilder, _netSrc, pinInfo.name, _infPinTee, "Input", true);
                                        ConnectFilters(_graphBuilder, _infPinTee, "Output1", _decoderFilter, "Video Input", true);
                                        ConnectFilters(_graphBuilder, _infPinTee, "Output2", _bridgeSink, "Input 1", true);
                                        ConnectFilters(_graphBuilder, _decoderFilter, "Video Output 1", _videoRender, "VMR Input0", true);
                                        videoComplete = true;
                                    }
                                    else
                                    {
                                        throw new Exception("Can't Render--Unsupported codec in stream");
                                    }
                                }
                                else if (mediaType.majorType == MediaType.Audio && HasAudio)
                                {
                                    if (mediaType.subType == MediaSubType.PCM)
                                    {
                                        if (mediaType.formatType == FormatType.WaveEx)
                                        {
//                                            _audioEncoderFilter = AddFilterByName(_graphBuilder, FilterCategory.AudioCompressorCategory, "WM Speech Encoder DMO");
//                                            _audioDecoderFilter = AddFilterByName(_graphBuilder, FilterCategory.LegacyAmFilterCategory, "WMSpeech Decoder DMO");
//                                            _audioRenderFilter = AddFilterByName(_graphBuilder, FilterCategory.AudioRendererCategory, "Default DirectSound Device");
//                                            ConnectFilters(_graphBuilder, _netSrc, pinInfo.name, _audioEncoderFilter, "in0", true);
//                                            ConnectFilters(_graphBuilder, _audioEncoderFilter, "out0", _audioDecoderFilter, "in0", true);
//                                            ConnectFilters(_graphBuilder, _audioDecoderFilter, "out0", _audioRenderFilter, "Audio Input pin (rendered)", true);
                                        }
                                    }
                                    else if (mediaType.subType == new Guid("000000FF-0000-0010-8000-00AA00389B71")) // AAC Audio
                                    {
                                        _audioDecoderFilter = AddFilterByName(_graphBuilder, FilterCategory.LegacyAmFilterCategory, "Microsoft DTV-DVD Audio Decoder");
                                        _audioRenderFilter  = AddFilterByName(_graphBuilder, FilterCategory.AudioRendererCategory, "Default DirectSound Device");
                                        ConnectFilters(_graphBuilder, _netSrc, pinInfo.name, _audioDecoderFilter, "XForm In", true);
                                        ConnectFilters(_graphBuilder, _audioDecoderFilter, "XFrom Out", _audioRenderFilter, "Audio Input pin (rendered)", true);
                                    }
                                }
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(pins[0]);
                    }
                }
            }
            finally
            {
                Marshal.ReleaseComObject(enumPins);
            }
            if (videoComplete == false)
            {
                throw new Exception("No video found");
            }
        }
Example #34
0
        private static bool checkMediaTypes(IPin pin, Guid mediaType, Guid mediaSubType)
        {
            int reply = 0;

            IEnumMediaTypes enumMediaTypes = null;
            AMMediaType[] mediaTypes = new AMMediaType[1];

            reply = pin.EnumMediaTypes(out enumMediaTypes);
            DsError.ThrowExceptionForHR(reply);

            while (enumMediaTypes.Next(mediaTypes.Length, mediaTypes, IntPtr.Zero) == 0)
            {
                foreach (AMMediaType currentMediaType in mediaTypes)
                {
                    if (currentMediaType != null)
                    {
                        if (currentMediaType.majorType == mediaType &&
                            currentMediaType.subType == mediaSubType)
                        {
                            Marshal.ReleaseComObject(enumMediaTypes);
                            return (true);
                        }
                    }
                }
            }

            Marshal.ReleaseComObject(enumMediaTypes);

            return (false);
        }
Example #35
0
        private void logMediaTypes(IPin pin)
        {
            IEnumMediaTypes mediaTypes = null;
            AMMediaType[] mediaType = new AMMediaType[1];

            AMMediaType connectedMediaType = new AMMediaType();
            reply = pin.ConnectionMediaType(connectedMediaType);

            reply = pin.EnumMediaTypes(out mediaTypes);
            if (reply != 0)
            {
                LogMessage("Media types cannot be determined at this time (not connected yet?)");
                return;
            }

            while (mediaTypes.Next(mediaType.Length, mediaType, IntPtr.Zero) == 0)
            {
                foreach (AMMediaType currentMediaType in mediaType)
                {
                    PinInfo pinInfo;
                    reply = pin.QueryPinInfo(out pinInfo);
                    DsError.ThrowExceptionForHR(reply);

                    string majorType = TranslateMediaMajorType(currentMediaType.majorType);
                    string subType = TranslateMediaSubType(currentMediaType.subType);

                    string connectedComment;

                    if (currentMediaType.majorType == connectedMediaType.majorType && currentMediaType.subType == connectedMediaType.subType)
                        connectedComment = "** Connected **";
                    else
                        connectedComment = string.Empty;

                    LogMessage("Media type: " +
                        majorType + " ; " +
                        subType + " " +
                        currentMediaType.fixedSizeSamples + " " +
                        currentMediaType.sampleSize + " " +
                        connectedComment);
                }
            }
        }
Example #36
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", "")));
                    }
                }
            }
        }
		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;
		}
 public static bool QueryConnect(IPin pin, IPin other)
 {
   IEnumMediaTypes enumTypes;
   int hr = pin.EnumMediaTypes(out enumTypes);
   if (hr != 0 || enumTypes == null)
   {
     return false;
   }
   int count = 0;
   for (;;)
   {
     AMMediaType[] types = new AMMediaType[1];
     int fetched;
     hr = enumTypes.Next(1, types, out fetched);
     if (hr != 0 || fetched == 0)
     {
       break;
     }
     count++;
     if (other.QueryAccept(types[0]) == 0)
     {
       return true;
     }
   }
   PinInfo info;
   PinInfo infoOther;
   pin.QueryPinInfo(out info);
   DsUtils.FreePinInfo(info);
   other.QueryPinInfo(out infoOther);
   DsUtils.FreePinInfo(infoOther);
   Log.Info("Pins {0} and {1} do not accept each other. Tested {2} media types", info.name, infoOther.name, count);
   return false;
 }
Example #39
0
        private int getPinMediaType(IPin pPin, Guid majorType, Guid minorType, Guid formatType, ref AMMediaType mediaType)
        {
            if (pPin == null) throw new Exception("No pin");

            int hr;

            IEnumMediaTypes pEnum = null;

            hr = pPin.EnumMediaTypes(out pEnum);
            DsError.ThrowExceptionForHR(hr);
            bool found = false;
            IntPtr fetched = IntPtr.Zero;
            AMMediaType[] pMT = new AMMediaType[1];
            hr = pEnum.Next(1, pMT, fetched);
            while (hr == 0)
            {
                if ((majorType.Equals(Guid.Empty)) || (majorType.Equals(pMT[0].majorType)))
                    if ((minorType.Equals(Guid.Empty)) || (minorType.Equals(pMT[0].subType)))
                        if ((formatType.Equals(Guid.Empty)) || (formatType.Equals(pMT[0].formatType)))
                        // Match
                        {
                            found = true;
                            mediaType = pMT[0];
                            break;
                        }

                DsUtils.FreeAMMediaType(pMT[0]);

                hr = pEnum.Next(1, pMT, fetched);
            }

            // End
            Marshal.ReleaseComObject(pEnum);
            if (found)
                return 0;
            else
                return -1;
        }
    public static bool TryConnect(IGraphBuilder graphBuilder, string filtername, IPin outputPin, bool TryNewFilters)
    {
      int hr;
      Log.Info("----------------TryConnect-------------");
      PinInfo outputInfo;
      outputPin.QueryPinInfo(out outputInfo);
      DsUtils.FreePinInfo(outputInfo);
      //ListMediaTypes(outputPin);
      ArrayList currentfilters = GetFilters(graphBuilder);
      foreach (IBaseFilter filter in currentfilters)
      {
        if (TryConnect(graphBuilder, filtername, outputPin, filter))
        {
          ReleaseFilters(currentfilters);
          return true;
        }
      }
      ReleaseFilters(currentfilters);
      //not found, try new filter from registry
      if (TryNewFilters)
      {
        Log.Info("No preloaded filter could be connected. Trying to load new one from registry");
        IEnumMediaTypes enumTypes;
        hr = outputPin.EnumMediaTypes(out enumTypes);
        if (hr != 0)
        {
          Log.Debug("Failed: {0:x}", hr);
          return false;
        }
        Log.Debug("Got enum");
        ArrayList major = new ArrayList();
        ArrayList sub = new ArrayList();

        Log.Debug("Getting corresponding filters");
        for (;;)
        {
          AMMediaType[] mediaTypes = new AMMediaType[1];
          int typesFetched;
          hr = enumTypes.Next(1, mediaTypes, out typesFetched);
          if (hr != 0 || typesFetched == 0)
          {
            break;
          }
          major.Add(mediaTypes[0].majorType);
          sub.Add(mediaTypes[0].subType);
        }
        ReleaseComObject(enumTypes);
        Log.Debug("Found {0} media types", major.Count);
        Guid[] majorTypes = (Guid[])major.ToArray(typeof (Guid));
        Guid[] subTypes = (Guid[])sub.ToArray(typeof (Guid));
        Log.Debug("Loading filters");
        ArrayList filters = FilterHelper.GetFilters(majorTypes, subTypes, (Merit)0x00400000);
        Log.Debug("Loaded {0} filters", filters.Count);
        foreach (string name in filters)
        {
          if (!CheckFilterIsLoaded(graphBuilder, name))
          {
            Log.Debug("Loading filter: {0}", name);
            IBaseFilter f = AddFilterToGraph(graphBuilder, name);
            if (f != null)
            {
              if (TryConnect(graphBuilder, filtername, outputPin, f))
              {
                ReleaseComObject(f);
                return true;
              }
              else
              {
                graphBuilder.RemoveFilter(f);
                ReleaseComObject(f);
              }
            }
          }
          else
          {
            Log.Debug("Ignoring filter {0}. Already in graph.", name);
          }
        }
      }
      Log.Debug("TryConnect failed.");
      return outputInfo.name.StartsWith("~");
    }
        public int Connect(IPin pReceivePin, AMMediaType pmt)
        {
            Monitor.Enter(this);
            int hr = S_OK;
            pin = null;
            pintype = null;
            allocator = null;
            string id = "Unnamed pin";
            pReceivePin.QueryId(out id);
            PinInfo pi = new PinInfo();
            hr = pReceivePin.QueryPinInfo(out pi);
            if (hr == S_OK)
            {
                FilterInfo fi = new FilterInfo();
                hr = pi.filter.QueryFilterInfo(out fi);
                if (hr == S_OK)
                {
                    id += (" (" + fi.achName);
                }
                Guid guid;
                hr = pi.filter.GetClassID(out guid);
                if (hr == S_OK)
                {
                    id += (", " + guid.ToString());
                }
                id += ")";
            }
            try
            {
                AMMediaType amt = null;
                if (pmt != null)
                {
                    amt = pmt;
                }
                else
            #if false
                {
                    IEnumMediaTypes ie;
                    hr = pReceivePin.EnumMediaTypes(out ie);
                    int fetched;
                    int alloc = Marshal.SizeOf(typeof(AMMediaType));
                    IntPtr mtypePtr = Marshal.AllocCoTaskMem(alloc);
                    while (ie.Next(1, mtypePtr, out fetched) == S_OK)
                    {
                        amt = new AMMediaType();
                        Marshal.PtrToStructure(mtypePtr, amt);
                        hr = pReceivePin.QueryAccept(amt);
                        if (hr == S_OK)
                        {
                            break;
                        }
                        DsUtils.FreeAMMediaType(amt);
                        amt = null;
                    }
                    if (fetched == 0)
                    {
                        amt = null;
                    }
                    Marshal.FreeCoTaskMem(mtypePtr);
                }
                if (amt == null)
            #endif
                {
                    amt = mediatype;
                }
                hr = pReceivePin.QueryAccept(amt);
                if (hr == S_FALSE)
                {
                    log.InfoFormat("No media type for pin '{0}'", id);
                    Monitor.Exit(this);
                    return VFW_E_NO_ACCEPTABLE_TYPES;
                }

                hr = pReceivePin.ReceiveConnection(this, amt);
                if (hr == VFW_E_TYPE_NOT_ACCEPTED)
                {
                    log.InfoFormat("No connection to pin '{0}'", id);
                    Monitor.Exit(this);
                    return VFW_E_NO_ACCEPTABLE_TYPES;
                }
                DsError.ThrowExceptionForHR(hr);

                pin = pReceivePin;
                pintype = amt;
            }
            catch (Exception e)
            {
                LogUtil.ExceptionLog.ErrorFormat("Caught exception in connect ({0}): {1}{2}", id, e.Message, e.StackTrace);
                pin = null;
                pintype = null;
                allocator = null;
                Monitor.Exit(this);
                return VFW_E_NO_TRANSPORT;
            }
            Monitor.Exit(this);
            log.InfoFormat("Connected to pin '{0}'", id);
            return S_OK;
        }
Example #42
0
            /// <summary>
            /// 指定したピンが指定したMediaTypeをサポートするか?
            /// </summary>
            /// <param name="pin">ピン</param>
            /// <param name="major">majorType</param>
            /// <param name="sub">subType or 検証不要な場合 Guid.Empty</param>
            /// <returns>サポートしてる場合true</returns>
            public static bool IsPinSupportsMediaType(IPin pin, Guid major, Guid sub)
            {
                bool found = false;
                IEnumMediaTypes enumerator = null;
                try
                {
                    pin.EnumMediaTypes(out enumerator);
                    IntPtr pMedia = IntPtr.Zero;
                    AMMediaType media = new AMMediaType();

                    found = false;
                    enumerator.Reset();
                    while (!found && enumerator.Next(1, out pMedia, IntPtr.Zero) == S_OK)
                    {
                        if (pMedia == IntPtr.Zero) continue;
                        Marshal.PtrToStructure(pMedia, media);

                        if (media.majorType == major && (sub == Guid.Empty || media.subType != sub))
                            found = true;

                        Util.FreeMediaType(media);
                        Marshal.FreeCoTaskMem(pMedia);
                    }
                }
                finally
                {
                    if (enumerator != null)
                        Marshal.ReleaseComObject(enumerator);
                    enumerator = null;
                }
                return found;
            }
Example #43
0
        public static IEnumerator <WavFormatInfo> EnumerateFormatsForDirection(IBaseFilter filter, PinDirection direction)
        {
            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 InvalidOperationException("pinsEnum is null");
                }

                IPin[] pins = new IPin[1];

                while (true)
                {
                    try
                    {
                        int pcFetched;
                        hr = pinsEnum.Next(pins.Length, pins, out pcFetched);
                        DsError.ThrowExceptionForHR(hr);

                        if (pcFetched == 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);

                                AMMediaType[] mediaTypes = new AMMediaType[1];

                                int mtFetched;

                                while (true)
                                {
                                    hr = mediaTypesEnum.Next(1, mediaTypes, out mtFetched);
                                    DsError.ThrowExceptionForHR(hr);
                                    if (mtFetched == 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);
                }
            }
        }
Example #44
0
    /// <summary>
    /// Determine whether a specified pin is audio or video
    /// </summary>
    /// <param name="pPin">Pin to check</param>
    /// <returns>True if pin is video</returns>
    private bool IsVideo(IPin pPin)
    {
      int hr;
      bool bRet = false;
      AMMediaType[] pmt = new AMMediaType[1];
      IEnumMediaTypes ppEnum;

      // Walk the MediaTypes for the pin
      hr = pPin.EnumMediaTypes(out ppEnum);
      DESError.ThrowExceptionForHR(hr);

      try
      {
        // Just read the first one
        hr = ppEnum.Next(1, pmt, IntPtr.Zero);
        DESError.ThrowExceptionForHR(hr);

        bRet = pmt[0].majorType == MediaType.Video;
      }
      finally
      {
        Marshal.ReleaseComObject(ppEnum);
      }
      DsUtils.FreeAMMediaType(pmt[0]);

      return bRet;
    }
Example #45
0
        private void RenderSource()
        {
            int hr;

            IEnumPins enumPins;

            IPin[] pins = new IPin[1];

            _muxer = AddFilterByName(_graphBuilder, FilterCategory.LegacyAmFilterCategory, "LEAD MPEG2 Transport Demultiplexer");
            ConnectFilters(_graphBuilder, _netSrc, "Output", _muxer, "Input 01", true);

            hr = _muxer.EnumPins(out enumPins);
            DsError.ThrowExceptionForHR(hr);
            try
            {
                while (enumPins.Next(pins.Length, pins, IntPtr.Zero) == 0)
                {
                    try
                    {
                        PinInfo pinInfo;
                        IPin    upstreamPin = pins[0];
                        hr = upstreamPin.QueryPinInfo(out pinInfo);
                        DsError.ThrowExceptionForHR(hr);
                        if (pinInfo.dir == PinDirection.Output)
                        {
                            IEnumMediaTypes enumMediaTypes;
                            hr = upstreamPin.EnumMediaTypes(out enumMediaTypes);
                            DsError.ThrowExceptionForHR(hr);
                            AMMediaType[] mediaTypes = new AMMediaType[1];
                            if (enumMediaTypes.Next(1, mediaTypes, IntPtr.Zero) == 0)
                            {
                                AMMediaType mediaType = mediaTypes[0];
                                if (mediaType.majorType == MediaType.Video)
                                {
                                    if ((mediaType.subType == new Guid("34363268-0000-0010-8000-00AA00389B71")) ||
                                        (mediaType.subType == new Guid("34363248-0000-0010-8000-00aa00389b71")) ||
                                        (mediaType.subType == new Guid("3436324c-0000-0010-8000-00aa00389b71")) ||
                                        (mediaType.subType == new Guid("31637661-5349-4d4f-4544-494154595045")))  // H264
                                    {
                                        _decoderFilter = AddFilterByName(_graphBuilder, FilterCategory.LegacyAmFilterCategory, "LEAD H264 Decoder (3.0)");
                                        ConnectFilters(_graphBuilder, _muxer, pinInfo.name, _infPinTee, "Input", true);
                                        ConnectFilters(_graphBuilder, _infPinTee, "Output1", _decoderFilter, "XForm In", true);
                                        ConnectFilters(_graphBuilder, _infPinTee, "Output2", _bridgeSink, "Input 1", true);
                                        ConnectFilters(_graphBuilder, _decoderFilter, "XForm Out", _videoCallbackFilter, "Input", true);
                                        ConnectFilters(_graphBuilder, _videoCallbackFilter, "Output", _videoRender, "VMR Input0", true);
                                    }
                                    else if (mediaType.subType == new Guid("4B324A4C-0000-0010-8000-00AA00389B71"))
                                    {
                                        _decoderFilter = AddFilterByName(_graphBuilder, FilterCategory.LegacyAmFilterCategory, "LEAD MJ2K Decoder (2.0)");
                                        ConnectFilters(_graphBuilder, _muxer, pinInfo.name, _decoderFilter, "Input", true);
                                        ConnectFilters(_graphBuilder, _decoderFilter, "XForm Out", _videoRender, "VMR Input0", true);
                                    }
                                    else if (mediaType.subType == MediaSubType.MJPG)
                                    {
                                        _decoderFilter = AddFilterByName(_graphBuilder, FilterCategory.LegacyAmFilterCategory, "LEAD MCMP/MJPEG Decoder (2.0)");
                                        ConnectFilters(_graphBuilder, _decoderFilter, "XForm Out", _videoRender, "VMR Input0", true);
                                    }
                                    else
                                    {
                                        throw new Exception("Can't Render--Unsupported codec in stream");
                                    }
                                }
                                else
                                {
//                                    RALPH
//                                    throw new Exception("Can't Render--Unsupported type - audio? not supported");
                                }
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(pins[0]);
                    }
                }
            }
            finally
            {
                Marshal.ReleaseComObject(enumPins);
            }
        }
Example #46
0
        public static bool IsVideo(IPin pin)
        {
            if (pin == null) throw new ArgumentNullException("pin");

            int hr;
            bool bRet = false;
            var pmt = new AMMediaType[1];
            IEnumMediaTypes ppEnum;
            IntPtr i = IntPtr.Zero;

            // Walk the MediaTypes for the pin
            hr = pin.EnumMediaTypes(out ppEnum);
            DESError.ThrowExceptionForHR(hr);

            try
            {
                // Just read the first one
                hr = ppEnum.Next(1, pmt, i);
                DESError.ThrowExceptionForHR(hr);

                bRet = pmt[0].majorType == MediaType.Video;
            }
            finally
            {
                Marshal.ReleaseComObject(ppEnum);
            }
            DsUtils.FreeAMMediaType(pmt[0]);

            return bRet;
        }
 private static void ListMediaTypes(IPin pin)
 {
   IEnumMediaTypes types;
   pin.EnumMediaTypes(out types);
   types.Reset();
   while (true)
   {
     AMMediaType[] mediaTypes = new AMMediaType[1];
     int typesFetched;
     int hr = types.Next(1, mediaTypes, out typesFetched);
     if (hr != 0 || typesFetched == 0)
     {
       break;
     }
     Log.Info("Has output type: {0}, {1}", mediaTypes[0].majorType,
              mediaTypes[0].subType);
   }
   ReleaseComObject(types);
   Log.Info("-----EndofTypes");
 }