/// <summary> /// Gets a MFVideoFormatContainer from an IMFMediaType /// </summary> /// <returns>S_OK for success, nz for fail</returns> /// <param name="mediaTypeObj">the media type object</param> /// <param name="currentDevice">the video device that created the source reader</param> public static MFVideoFormatContainer GetVideoFormatContainerFromMediaTypeObject(IMFMediaType mediaTypeObj, MFDevice currentDevice) { if (mediaTypeObj == null) { // we failed throw new Exception("GetVideoFormatContainerFromMediaTypeObject failedmediaTypeObj == null"); } if (currentDevice == null) { // we failed throw new Exception("GetVideoFormatContainerFromMediaTypeObject currentDevice == null"); } // get the formats for this type HResult hr = GetSupportedFormatsFromMediaType(mediaTypeObj, out Guid majorType, out Guid subType, out int attributeCount, out int frameSizeWidth, out int frameSizeHeight, out int frameRate, out int frameRateDenominator, out int frameRateMin, out int frameRateMinDenominator, out int frameRateMax, out int frameRateMaxDenominator); if (hr != HResult.S_OK) { // we failed throw new Exception("GetSupportedVideoFormatsFromSourceReaderInFormatContainers failed on call to GetSupportedFormatsFromMediaType, retVal=" + hr.ToString()); } // enumerate all of the possible Attributes so we can see which ones are present that we did not report on hr = EnumerateAllAttributeNamesInMediaTypeAsText(mediaTypeObj, attributeCount, out StringBuilder allAttrs); if (hr != HResult.S_OK) { // we failed throw new Exception("GetSupportedVideoFormatsFromSourceReaderInFormatContainers failed on call to EnumerateAllAttributeNamesInMediaTypeAsText, retVal=" + hr.ToString()); } // create the container here MFVideoFormatContainer tmpContainer = new MFVideoFormatContainer { MajorType = majorType, SubType = subType, AttributeCount = attributeCount, FrameSizeWidth = frameSizeWidth, FrameSizeHeight = frameSizeHeight, FrameRate = frameRate, FrameRateDenominator = frameRateDenominator, FrameRateMin = frameRateMin, FrameRateMinDenominator = frameRateMinDenominator, FrameRateMax = frameRateMax, FrameRateMaxDenominator = frameRateMaxDenominator, AllAttributes = allAttrs.ToString(), VideoDevice = currentDevice }; return(tmpContainer); }
/// <summary> /// Gets a list of all supported video formats from a video source device /// as a list of MFVideoFormatContainer's /// /// </summary> /// <returns>S_OK for success, nz for fail</returns> /// <param name="currentDevice">the video device that created the source reader</param> /// <param name="sourceReader">the source reader object</param> /// <param name="maxFormatsToTestFor">the max number of formats we test for</param> /// <param name="formatList">the list of video formats supported by the SourceReader</param> public static HResult GetSupportedVideoFormatsFromSourceReaderInFormatContainers(MFDevice currentDevice, IMFSourceReaderAsync sourceReader, int maxFormatsToTestFor, out List <MFVideoFormatContainer> formatList) { IMFMediaType mediaTypeObj = null; HResult hr; // init this, we never return null here formatList = new List <MFVideoFormatContainer>(); // sanity check if (currentDevice == null) { return(HResult.E_FAIL); } if (sourceReader == null) { return(HResult.E_FAIL); } try { for (int typeIndex = 0; typeIndex < maxFormatsToTestFor; typeIndex++) { // test this hr = sourceReader.GetNativeMediaType(WMFUtils.MF_SOURCE_READER_FIRST_VIDEO_STREAM, typeIndex, out mediaTypeObj); if (hr == HResult.MF_E_NO_MORE_TYPES) { // we are all done. The outSb container has been populated return(HResult.S_OK); } else if (hr != HResult.S_OK) { // we failed throw new Exception("GetSupportedVideoFormatsFromSourceReaderInFormatContainers failed on call to GetNativeMediaType, retVal=" + hr.ToString()); } // get a format container from the media type MFVideoFormatContainer tmpContainer = GetVideoFormatContainerFromMediaTypeObject(mediaTypeObj, currentDevice); if (tmpContainer == null) { // we failed throw new Exception("GetSupportedVideoFormatsFromSourceReaderInFormatContainers failed on call to GetVideoFormatContainerFromMediaTypeObject"); } // now add it formatList.Add(tmpContainer); } } finally { // always release our mediaType if we have one if (mediaTypeObj != null) { Marshal.ReleaseComObject(mediaTypeObj); } } // all done return(HResult.S_OK); }