Beispiel #1
0
        private void GetBaseFilter(IGraphBuilder pGraphBuilder)
        {
            IEnumFilters pEnum = null;
            IBaseFilter  pFilter;
            int          cFetched;

            int hr = pGraphBuilder.EnumFilters(out pEnum);

            if (DsHlp.SUCCEEDED(hr))
            {
                while (pEnum.Next(1, out pFilter, out cFetched) == DsHlp.S_OK)
                {
                    Guid clsid;
                    if (pFilter.GetClassID(out clsid) == DsHlp.S_OK && clsid == RendererID)
                    {
                        BaseFilter = pFilter;
                        break;
                    }
                    else
                    {
                        Marshal.ReleaseComObject(pFilter);
                    }
                }

                Marshal.ReleaseComObject(pEnum);
            }
        }
Beispiel #2
0
        public bool DisplayFilterPropPage(IntPtr hParent, string strFilter, bool bDisplay)
        {
            if (_graphBuilder == null)
            {
                return(false);
            }

            IBaseFilter pFilter;

            _graphBuilder.FindFilterByName(strFilter, out pFilter);
            if (pFilter == null)
            {
                return(false);
            }

            var bRet  = false;
            var pProp = pFilter as ISpecifyPropertyPages;

            if (pProp != null)
            {
                CAUUID caGuid;
                var    hr = pProp.GetPages(out caGuid);
                if (DsHlp.SUCCEEDED(hr) && caGuid.cElems > 0)
                {
                    bRet = true;

                    if (bDisplay)
                    {
                        // Show the page
                        object pFilterUnk = pFilter;
                        DsUtils.OleCreatePropertyFrame(
                            hParent,                // Parent window
                            0, 0,                   // Reserved
                            strFilter,              // Caption for the dialog box
                            1,                      // Number of objects (just the filter)
                            ref pFilterUnk,         // Array of object pointers.
                            caGuid.cElems,          // Number of property pages
                            caGuid.pElems,          // Array of property page CLSIDs
                            0,                      // Locale identifier
                            0, IntPtr.Zero          // Reserved
                            );
                    }
                }

                // Clean up
                if (caGuid.pElems != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(caGuid.pElems);
                }
            }

            Marshal.ReleaseComObject(pFilter);
            return(bRet);
        }
Beispiel #3
0
        public static bool SelectStream(this IAMStreamSelect pStreamSelect, int index)
        {
            var result = false;

            int count;
            var hr = pStreamSelect.Count(out count);

            if (DsHlp.SUCCEEDED(hr) && count > index)
            {
                hr     = pStreamSelect.Enable(index, AMStreamSelectEnableFlags.Enable);
                result = DsHlp.SUCCEEDED(hr);
            }

            return(result);
        }
Beispiel #4
0
        public void HandleGraphEvent()
        {
            if (_mediaEventEx == null)
            {
                return;
            }

            int evCode, lParam1, lParam2;

            while (DsHlp.SUCCEEDED(_mediaEventEx.GetEvent(out evCode, out lParam1, out lParam2, 0)))
            {
                HandleGraphEvent(evCode, lParam1, lParam2);

                _mediaEventEx.FreeEventParams(evCode, lParam1, lParam2);
            }
        }
Beispiel #5
0
        public override bool GetCurrentImage(out BITMAPINFOHEADER header, out IntPtr dibFull, out IntPtr dibDataOnly)
        {
            int hr = _pVMRWindowlessControl9.GetCurrentImage(out dibFull);

            if (DsHlp.SUCCEEDED(hr))
            {
                header      = (BITMAPINFOHEADER)Marshal.PtrToStructure(dibFull, typeof(BITMAPINFOHEADER));
                dibDataOnly = new IntPtr(dibFull.ToInt64() + Marshal.SizeOf(typeof(BITMAPINFOHEADER)));
                return(true);
            }
            else
            {
                header      = new BITMAPINFOHEADER();
                dibDataOnly = IntPtr.Zero;
                return(false);
            }
        }
Beispiel #6
0
        public static bool IsStreamSelected(this IAMStreamSelect pStreamSelect, int index)
        {
            var result = false;

            int count;
            var hr = pStreamSelect.Count(out count);

            if (DsHlp.SUCCEEDED(hr) && count > index)
            {
                var stream = pStreamSelect.GetSelectableStream(index);
                if (stream != null)
                {
                    result = stream.Enabled;
                }
            }

            return(result);
        }
        public override bool GetCurrentImage(out BITMAPINFOHEADER header, out IntPtr dibFull, out IntPtr dibDataOnly)
        {
            int  cbDib;
            long timestamp = 0;

            header        = new BITMAPINFOHEADER();
            header.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
            int hr = _pMFVideoDisplayControl.GetCurrentImage(ref header, out dibFull, out cbDib, ref timestamp);

            if (DsHlp.SUCCEEDED(hr))
            {
                dibDataOnly = new IntPtr(dibFull.ToInt64() + Marshal.SizeOf(typeof(BITMAPINFOHEADER)));
                return(true);
            }
            else
            {
                dibDataOnly = IntPtr.Zero;
                return(false);
            }
        }
Beispiel #8
0
        public virtual bool ResumeGraph()
        {
            if (_mediaControl == null)
            {
                return(false);
            }

            if (DsHlp.SUCCEEDED(_mediaControl.Run()))
            {
                GraphState = GraphState.Running;
                return(true); // ok, we're running
            }

            if (UpdateGraphState())
            {
                GraphState = GraphState.Running;
                return(true); // ok, we're running
            }

            return(false);
        }
Beispiel #9
0
        public static IEnumerable <SelectableStream> GetSelectableStreams(this IAMStreamSelect pStreamSelect)
        {
            var result = new List <SelectableStream>();

            int count;
            var hr = pStreamSelect.Count(out count);

            if (DsHlp.SUCCEEDED(hr) && count > 0)
            {
                for (var i = 0; i < count; i++)
                {
                    var stream = pStreamSelect.GetSelectableStream(i);
                    if (stream != null)
                    {
                        result.Add(stream);
                    }
                }
            }

            return(result);
        }
Beispiel #10
0
        public override bool GetCurrentImage(out BITMAPINFOHEADER header, out IntPtr dibFull, out IntPtr dibDataOnly)
        {
            int bufferSize = 0;
            int hr         = BasicVideo2.GetCurrentImage(ref bufferSize, IntPtr.Zero); // get the required buffer size first

            if (DsHlp.SUCCEEDED(hr))
            {
                dibFull = Marshal.AllocCoTaskMem(bufferSize);
                hr      = BasicVideo2.GetCurrentImage(ref bufferSize, dibFull); // actually get the image
                if (DsHlp.SUCCEEDED(hr))
                {
                    header      = (BITMAPINFOHEADER)Marshal.PtrToStructure(dibFull, typeof(BITMAPINFOHEADER));
                    dibDataOnly = new IntPtr(dibFull.ToInt64() + Marshal.SizeOf(typeof(BITMAPINFOHEADER)));
                    return(true);
                }
            }

            header      = new BITMAPINFOHEADER();
            dibDataOnly = IntPtr.Zero;
            dibFull     = IntPtr.Zero;
            return(false);
        }
Beispiel #11
0
        public static void InspectStream(this IAMStreamSelect pStreamSelect, int index, Action <AMMediaType, string, bool> inspect)
        {
            IntPtr ppmt;
            AMStreamSelectInfoFlags pdwFlags;
            int    plcid;
            int    pdwGroup;
            IntPtr ppszName;
            IntPtr ppObject;
            IntPtr ppUnk;

            var hr = pStreamSelect.Info(index, out ppmt, out pdwFlags, out plcid, out pdwGroup, out ppszName, out ppObject, out ppUnk);

            if (DsHlp.SUCCEEDED(hr))
            {
                var mt      = ppmt != IntPtr.Zero ? (AMMediaType)Marshal.PtrToStructure(ppmt, typeof(AMMediaType)) : new AMMediaType();
                var name    = Marshal.PtrToStringAuto(ppszName);
                var enabled = (pdwFlags & AMStreamSelectInfoFlags.Enabled) != AMStreamSelectInfoFlags.Disabled ||
                              (pdwFlags & AMStreamSelectInfoFlags.Exclusive) != AMStreamSelectInfoFlags.Disabled;

                inspect(mt, name, enabled);

                if (ppmt != IntPtr.Zero)
                {
                    DsUtils.FreeFormatBlock(ppmt);
                    Marshal.FreeCoTaskMem(ppmt);
                }

                Marshal.FreeCoTaskMem(ppszName);
                if (ppObject != IntPtr.Zero)
                {
                    Marshal.Release(ppObject);
                }
                if (ppUnk != IntPtr.Zero)
                {
                    Marshal.Release(ppUnk);
                }
            }
        }
        /// <summary>
        /// Get a read-only list of all renderers that are supported on the current system.
        /// </summary>
        public ReadOnlyCollection <Renderer> GetPresentVideoRenderers()
        {
            IList <Renderer> renderers = new List <Renderer>();

            if (_pMapper != null)
            {
                IEnumMoniker pEnum;
                IMoniker[]   pMonikers       = new IMoniker[1];
                Guid         IID_IBaseFilter = typeof(IBaseFilter).GUID;

                Guid[] inTypes = new Guid[] { DirectShow.MediaType.Video, Guid.Empty };
                int    hr      = _pMapper.EnumMatchingFilters(out pEnum,
                                                              0,                  // Reserved
                                                              true,               // Use exact match?
                                                              (int)MERIT.MERIT_DO_NOT_USE,
                                                              true,               // At least one input pin?
                                                              inTypes.Length / 2, // Number of major type/subtype pairs for input
                                                              inTypes,            // Array of major type/subtype pairs for input
                                                              IntPtr.Zero,        // Input medium
                                                              IntPtr.Zero,        // Input pin category
                                                              true,               // Must be a renderer?
                                                              false,              // At least one output pin?
                                                              0,                  // Number of major type/subtype pairs for output
                                                              null,               // Array of major type/subtype pairs for output
                                                              IntPtr.Zero,        // Output medium
                                                              IntPtr.Zero);       // Output pin category
                if (DsHlp.SUCCEEDED(hr))
                {
                    try
                    {
                        object o;
                        IntPtr cFetched = IntPtr.Zero;
                        while (pEnum.Next(1, pMonikers, cFetched) == DsHlp.S_OK)
                        {
                            o = null;
                            Guid clsId = Guid.Empty;

                            try
                            {
                                pMonikers[0].BindToObject(null, null,
                                                          ref IID_IBaseFilter, out o);
                                if (o != null && o is IBaseFilter)
                                {
                                    IBaseFilter pBaseFilter = (IBaseFilter)o;
                                    o = null;

                                    pBaseFilter.GetClassID(out clsId);

                                    Marshal.FinalReleaseComObject(pBaseFilter);
                                }
                            }
                            finally
                            {
                                Marshal.ReleaseComObject(pMonikers[0]);
                            }

                            IRenderer renderer = RendererBase.GetRenderer(clsId);
                            if (renderer != null)
                            {
                                renderers.Add(renderer.Renderer);
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(pEnum);
                    }
                }
            }
            return(new ReadOnlyCollection <Renderer>(renderers));
        }
        /// <summary>
        /// Get a read-only collection of descriptions of the filters that can be used to render specified media types.
        /// </summary>
        /// <param name="mediaTypes">Collection of media types.</param>
        /// <returns></returns>
        public ReadOnlyCollection <FilterDescription> GetMatchingFilters(ICollection <MediaType> mediaTypes)
        {
            var listFilters = new List <FilterDescription>();

            if (mediaTypes == null || !mediaTypes.Any())
            {
                return(new ReadOnlyCollection <FilterDescription>(listFilters));
            }

            var listNames = new HashSet <string>();

            IEnumMoniker pEnum;
            var          pMonikers        = new IMoniker[1];
            Guid         IID_IPropertyBag = typeof(IPropertyBag).GUID;
            Guid         IID_IBaseFilter  = typeof(IBaseFilter).GUID;

            var arrayInTypes = mediaTypes.SelectMany(t => t.ToIdPair()).ToArray();

            var hr = _pMapper.EnumMatchingFilters(out pEnum,
                                                  0,                // Reserved
                                                  true,             // Use exact match?
                                                  (int)MERIT.MERIT_DO_NOT_USE + 1,
                                                  true,             // At least one input pin?
                                                  mediaTypes.Count, // Number of major type/subtype pairs for input
                                                  arrayInTypes,     // Array of major type/subtype pairs for input
                                                  IntPtr.Zero,      // Input medium
                                                  IntPtr.Zero,      // Input pin category
                                                  false,            // Must be a renderer?
                                                  false,            // At least one output pin?
                                                  0,                // Number of major type/subtype pairs for output
                                                  null,             // Array of major type/subtype pairs for output
                                                  IntPtr.Zero,      // Output medium
                                                  IntPtr.Zero);     // Output pin category

            if (DsHlp.SUCCEEDED(hr))
            {
                object o;
                IntPtr cFetched = IntPtr.Zero;
                while (pEnum.Next(1, pMonikers, cFetched) == DsHlp.S_OK)
                {
                    o = null;
                    pMonikers[0].BindToStorage(null, null,
                                               ref IID_IPropertyBag, out o);
                    if (o != null && o is IPropertyBag)
                    {
                        var pPropBag = (IPropertyBag)o;
                        o  = String.Empty;
                        hr = pPropBag.Read("FriendlyName", ref o, null);
                        if (DsHlp.SUCCEEDED(hr) && o is String)
                        {
                            string name = (string)o;
                            if (!listNames.Contains(name))
                            {
                                o = null;
                                pMonikers[0].BindToObject(null, null,
                                                          ref IID_IBaseFilter, out o);
                                if (o != null && o is IBaseFilter)
                                {
                                    var pBaseFilter = (IBaseFilter)o;
                                    o = null;

                                    listNames.Add(name);

                                    Guid clsid;
                                    pBaseFilter.GetClassID(out clsid);

                                    var filterDescription = new FilterDescription(name, clsid);
                                    listFilters.Add(filterDescription);

                                    Marshal.FinalReleaseComObject(pBaseFilter);
                                }
                            }
                        }
                        Marshal.FinalReleaseComObject(pPropBag);
                    }

                    Marshal.ReleaseComObject(pMonikers[0]);
                }

                Marshal.ReleaseComObject(pEnum);
            }

            return(new ReadOnlyCollection <FilterDescription>(listFilters));
        }
Beispiel #14
0
        internal static IRenderer SubstituteRenderer(IGraphBuilder pGraphBuilder, IntPtr hMediaWindow, Renderer desiredRenderer)
        {
            RendererBase existingRenderer = (RendererBase)GetExistingRenderer(pGraphBuilder, hMediaWindow);
            // existingRenderer is either VR or VMR windowed (default mode for VMR); VMR9 and EVR are never default renderers
            // VR and VMRWindowed need delayed initialization or it's ok to reconnect them before we call GetNativeVideoSize

            // if desiredRenderer fails we will return the existing one
            IRenderer renderer = existingRenderer;

            if (existingRenderer.Renderer != desiredRenderer && desiredRenderer != Renderer.VR) // substitution with VR doesn't work well at least on Vista 64bit
            {
                RendererBase newRenderer = null;
                try
                {
                    newRenderer = (RendererBase)AddRenderer(pGraphBuilder,
                                                            desiredRenderer,
                                                            delegate(int hrCode, GraphBuilderError error) { },
                                                            hMediaWindow,
                                                            false);
                    IPin existingRendererInput, decoderOut;
                    if (existingRenderer.GetPins(out existingRendererInput, out decoderOut))
                    {
                        IPin newRendererInput = newRenderer.GetInputPin();
                        if (newRendererInput != null)
                        {
                            int hr = pGraphBuilder.Connect(decoderOut, newRendererInput);
                            if (DsHlp.SUCCEEDED(hr))
                            {
                                renderer    = newRenderer;
                                newRenderer = null; // so that we don't close it (see finally)

                                Marshal.ReleaseComObject(existingRendererInput);
                                existingRendererInput = null;
                                existingRenderer.RemoveFromGraph();
                            }
                            else
                            {
                                hr = pGraphBuilder.Connect(decoderOut, existingRendererInput);
                            }
                            Marshal.ReleaseComObject(newRendererInput);
                        }

                        if (existingRendererInput != null)
                        {
                            Marshal.ReleaseComObject(existingRendererInput);
                        }
                        Marshal.ReleaseComObject(decoderOut);
                    }
                }
                catch // VR throws it if it can't be instantiated; also renderers will throw an expection if they can't be added to the graph
                {     // we just ignore it here and return existingRenderer
                }
                finally
                {
                    if (newRenderer != null)
                    {
                        newRenderer.Close();
                    }
                }
            }
            return(renderer);
        }
Beispiel #15
0
        internal static IRenderer GetExistingRenderer(IGraphBuilder pGraphBuilder, IntPtr hMediaWindow)
        {
            // this method is to be called to create a wrapper for a renderer that is already in the graph (added by intelligent connect)
            // at the momemnt it is assumed that this is a Windowed Renderer (compatability assumption)
            RendererBase renderer = null;

            try
            {
                IEnumFilters pEnum = null;
                IBaseFilter  pFilter;
                int          cFetched;

                int hr = pGraphBuilder.EnumFilters(out pEnum);
                if (DsHlp.SUCCEEDED(hr))
                {
                    while (pEnum.Next(1, out pFilter, out cFetched) == DsHlp.S_OK)
                    {
                        Guid clsid;
                        if (pFilter.GetClassID(out clsid) == DsHlp.S_OK)
                        {
                            renderer = GetRenderer(clsid);
                            if (renderer != null)
                            {
                                renderer.BaseFilter = pFilter;
                                break;
                            }
                            else
                            {
                                Marshal.ReleaseComObject(pFilter);
                            }
                        }
                        else
                        {
                            Marshal.ReleaseComObject(pFilter);
                        }
                    }

                    Marshal.ReleaseComObject(pEnum);
                }

                if (renderer == null)
                {
                    renderer = TryGetUnknownRenderer(pGraphBuilder); // last resort
                }
                if (renderer == null)
                {
                    throw new FilterGraphBuilderException(GraphBuilderError.VideoRenderer); // we've tried hard enough, there is no point to continue
                }
                if (!renderer.IsDelayedInitialize)
                {
                    renderer.Initialize(pGraphBuilder, hMediaWindow);
                }

                renderer.GraphBuilder      = pGraphBuilder;
                renderer.MediaWindowHandle = hMediaWindow;
                renderer._ready            = true;
                return(renderer);
            }
            finally
            {
                if (renderer != null && !renderer._ready)
                {
                    renderer.Close();
                }
            }
        }
Beispiel #16
0
        private static RendererBase TryGetUnknownRenderer(IGraphBuilder pGraphBuilder)
        {
            // this is the last resort
            RendererBase renderer = null;

            IEnumFilters pEnum = null;
            IBaseFilter  pFilter;
            int          cFetched;

            int hr = pGraphBuilder.EnumFilters(out pEnum);

            if (DsHlp.SUCCEEDED(hr))
            {
                bool bFound = false;
                while (!bFound && pEnum.Next(1, out pFilter, out cFetched) == DsHlp.S_OK)
                {
                    IPin pPin = null;
                    // there should be no output pins
                    if ((pPin = DsUtils.GetPin(pFilter, PinDirection.Output, false)) != null)
                    {
                        // there is an unconnected output pin, this is not a renderer
                        Marshal.ReleaseComObject(pPin);
                    }
                    else if ((pPin = DsUtils.GetPin(pFilter, PinDirection.Output, true)) != null)
                    {
                        // there is a connected output pin, this is not a renderer
                        Marshal.ReleaseComObject(pPin);
                    }
                    else
                    {
                        // let's check the input pins: there must be at least one connected of type 'video'
                        int nSkip = 0;
                        while ((pPin = DsUtils.GetPin(pFilter, PinDirection.Input, true, nSkip)) != null)
                        {
                            if (DsUtils.IsMediaTypeSupported(pPin, MediaType.Video) == 0)
                            {
                                // there is connected input pin of type 'video'; this looks like a renderer
                                Marshal.ReleaseComObject(pPin);
                                renderer            = GetRenderer(Renderer.VR); // let's just default it VR
                                renderer.BaseFilter = pFilter;
                                bFound = true;
                                break;
                            }
                            else
                            {
                                nSkip++;
                                Marshal.ReleaseComObject(pPin);
                            }
                        }
                    }

                    if (!bFound)
                    {
                        Marshal.ReleaseComObject(pFilter);
                    }
                }

                Marshal.ReleaseComObject(pEnum);
            }

            return(renderer);
        }