/// <summary> /// Gets the major media type of a IMFMediaType as a text string /// /// Adapted from /// https://msdn.microsoft.com/en-us/library/windows/desktop/ee663602(v=vs.85).aspx /// </summary> /// <returns>S_OK for success, nz for fail</returns> /// <param name="mediaTypeObj">the media type object</param> /// <param name="outSb">The output string</param> public static HResult GetMediaMajorTypeAsText(IMFMediaType mediaTypeObj, out StringBuilder outSb) { // we always return something here outSb = new StringBuilder(); // sanity check if (mediaTypeObj == null) { return(HResult.E_FAIL); } // MF_MT_MAJOR_TYPE // Major type GUID, we return this as human readable text HResult hr = mediaTypeObj.GetMajorType(out Guid majorType); if (hr == HResult.S_OK) { // only report success outSb.Append("MF_MT_MAJOR_TYPE=" + WMFUtils.ConvertGuidToName(majorType)); } return(HResult.S_OK); }
/// <summary> /// Gets the major media type of a IMFMediaType as a text string /// /// Adapted from /// https://msdn.microsoft.com/en-us/library/windows/desktop/ee663602(v=vs.85).aspx /// </summary> /// <returns>S_OK for success, nz for fail</returns> /// <param name="mediaTypeObj">the media type object</param> /// <param name="outSb">The output string</param> public static HResult GetMediaSubTypeAsText(IMFMediaType mediaTypeObj, out StringBuilder outSb) { // we always return something here outSb = new StringBuilder(); // sanity check if (mediaTypeObj == null) { return(HResult.E_FAIL); } // MF_MT_SUBTYPE // Subtype GUID which describes the basic media type, we return this as human readable text HResult hr = mediaTypeObj.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out Guid subType); if (hr == HResult.S_OK) { // only report success outSb.Append("MF_MT_SUBTYPE=" + WMFUtils.ConvertGuidToName(subType)); } return(HResult.S_OK); }
public override void StartStream() { Task.Run(() => { // check our source filename is correct and usable StreamReaderHandler = new StreamHandler(ProcessFrame, MFBPressed); HResult hr; IMFSourceResolver pSourceResolver = null; IMFPresentationDescriptor sourcePresentationDescriptor = null; IMFStreamDescriptor videoStreamDescriptor = null; try { // reset everything CloseAllMediaDevices(); // Create the media session. hr = MFExtern.MFCreateMediaSession(null, out mediaSession); if (hr != HResult.S_OK) { throw new Exception("PrepareSessionAndTopology call to MFExtern.MFCreateMediaSession failed. Err=" + hr.ToString()); } // set up our media session call back handler. mediaSessionAsyncCallbackHandler = new AsyncCallbackHandler(); mediaSessionAsyncCallbackHandler.Initialize(); mediaSessionAsyncCallbackHandler.MediaSession = mediaSession; mediaSessionAsyncCallbackHandler.MediaSessionAsyncCallBackError = HandleMediaSessionAsyncCallBackErrors; mediaSessionAsyncCallbackHandler.MediaSessionAsyncCallBackEvent = HandleMediaSessionAsyncCallBackEvent; // Register the callback handler with the session and tell it that events can // start. This does not actually trigger an event it just lets the media session // know that it can now send them if it wishes to do so. hr = mediaSession.BeginGetEvent(mediaSessionAsyncCallbackHandler, null); if (hr != HResult.S_OK) { throw new Exception("PrepareSessionAndTopology call to mediaSession.BeginGetEvent failed. Err=" + hr.ToString()); } StreamReader = WMFUtils.CreateSourceReaderAsyncFromDevice(UnderlyingDevice, StreamReaderHandler); try { SetCurrentMediaType(StreamReader, IMFStreamIndex, 0); } catch { } try { SetCurrentMediaType(StreamReader, IMFSnapIndex, 1); } catch { } StreamReaderHandler.StreamReader = StreamReader; StreamReader.GetCurrentMediaType(1, out IMFMediaType ppMediaType); StreamReader.GetNativeMediaType(1, 0, out IMFMediaType PPMediaType); StreamReader.GetCurrentMediaType(0, out IMFMediaType ppMediaType1); StreamReader.GetNativeMediaType(0, 0, out IMFMediaType PPMediaType1); ppMediaType.GetMajorType(out Guid ppMediaTypeMGUID); PPMediaType.GetMajorType(out Guid PPMediaTypeMGUID); ppMediaType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out Guid ppMediaTypeSGUID); PPMediaType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out Guid PPMediaTypeSGUID); Console.WriteLine(WMFUtils.ConvertGuidToName(ppMediaTypeMGUID)); Console.WriteLine(WMFUtils.ConvertGuidToName(PPMediaTypeMGUID)); Console.WriteLine(WMFUtils.ConvertGuidToName(ppMediaTypeSGUID)); Console.WriteLine(WMFUtils.ConvertGuidToName(PPMediaTypeSGUID)); ppMediaType1.GetMajorType(out Guid ppMediaTypeMGUID1); PPMediaType1.GetMajorType(out Guid PPMediaTypeMGUID1); ppMediaType1.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out Guid ppMediaTypeSGUID1); PPMediaType1.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out Guid PPMediaTypeSGUID1); Console.WriteLine(WMFUtils.ConvertGuidToName(ppMediaTypeMGUID1)); Console.WriteLine(WMFUtils.ConvertGuidToName(PPMediaTypeMGUID1)); Console.WriteLine(WMFUtils.ConvertGuidToName(ppMediaTypeSGUID1)); Console.WriteLine(WMFUtils.ConvertGuidToName(PPMediaTypeSGUID1)); Paused = false; StreamReader.SetStreamSelection(0, true); StreamReader.SetStreamSelection(1, true); hr = StreamReader.ReadSample( 0, MediaFoundation.ReadWrite.MF_SOURCE_READER_CONTROL_FLAG.None, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ); hr = StreamReader.ReadSample(1, MediaFoundation.ReadWrite.MF_SOURCE_READER_CONTROL_FLAG.None, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ); if (hr != HResult.S_OK) { // we failed throw new Exception("Failed on calling the first ReadSample on the reader, retVal=" + hr.ToString()); } } catch (Exception ex) { Console.WriteLine("PrepareSessionAndTopology Error: " + ex.Message); } finally { // Clean up if (pSourceResolver != null) { Marshal.ReleaseComObject(pSourceResolver); } if (sourcePresentationDescriptor != null) { Marshal.ReleaseComObject(sourcePresentationDescriptor); } if (videoStreamDescriptor != null) { Marshal.ReleaseComObject(videoStreamDescriptor); } } }); }
/// <summary> /// Gets a list of all supported video formats from a media type /// as a nice displayable bit of text. outSb will never be null can be /// empty. /// /// Adapted from /// https://msdn.microsoft.com/en-us/library/windows/desktop/ee663602(v=vs.85).aspx /// </summary> /// <returns>S_OK for success, nz for fail</returns> /// <param name="mediaTypeObj">the media type object</param> /// <param name="outSb">The output string</param> public static HResult GetSupportedFormatsFromMediaTypeAsText(IMFMediaType mediaTypeObj, out StringBuilder outSb) { // we always return something here outSb = new StringBuilder(); // sanity check if (mediaTypeObj == null) { return(HResult.E_FAIL); } // Retrieves the number of attributes that are set on this object. HResult hr = mediaTypeObj.GetCount(out int attributeCount); if (hr != HResult.S_OK) { // if we failed here, bail out outSb.Append("failed getting attributeCount, retVal=" + hr.ToString()); outSb.Append("\r\n"); return(HResult.E_FAIL); } // MF_MT_MAJOR_TYPE // Major type GUID, we return this as human readable text hr = mediaTypeObj.GetMajorType(out Guid majorType); if (hr == HResult.S_OK) { // only report success outSb.Append("MF_MT_MAJOR_TYPE=" + WMFUtils.ConvertGuidToName(majorType) + ", "); } // MF_MT_SUBTYPE // Subtype GUID which describes the basic media type, we return this as human readable text hr = mediaTypeObj.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out Guid subType); if (hr == HResult.S_OK) { // only report success outSb.Append("MF_MT_SUBTYPE=" + WMFUtils.ConvertGuidToName(subType) + ", "); } // MF_MT_FRAME_SIZE // the Width and height of a video frame, in pixels hr = MFExtern.MFGetAttributeSize(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_SIZE, out int frameSizeWidth, out int frameSizeHeight); if (hr == HResult.S_OK) { // only report success outSb.Append("MF_MT_FRAME_SIZE (W,H)=(" + frameSizeWidth.ToString() + "," + frameSizeHeight.ToString() + "), "); } // MF_MT_FRAME_RATE // The frame rate is expressed as a ratio.The upper 32 bits of the attribute value contain the numerator and the lower 32 bits contain the denominator. // For example, if the frame rate is 30 frames per second(fps), the ratio is 30 / 1.If the frame rate is 29.97 fps, the ratio is 30,000 / 1001. // we report this back to the user as a decimal hr = MFExtern.MFGetAttributeRatio(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_RATE, out int frameRate, out int frameRateDenominator); if (hr == HResult.S_OK) { // only report success if (frameRateDenominator < 0) { outSb.Append("MF_MT_FRAME_RATE (frames/s)=(undefined),"); } else { outSb.Append("MF_MT_FRAME_RATE=" + ((decimal)frameRate / (decimal)frameRateDenominator).ToString() + "f/s, "); } } // MF_MT_FRAME_RATE_RANGE_MIN // The frame rate is expressed as a ratio.The upper 32 bits of the attribute value contain the numerator and the lower 32 bits contain the denominator. // For example, if the frame rate is 30 frames per second(fps), the ratio is 30 / 1.If the frame rate is 29.97 fps, the ratio is 30,000 / 1001. // we report this back to the user as a decimal hr = MFExtern.MFGetAttributeRatio(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_RATE_RANGE_MIN, out int frameRateMin, out int frameRateMinDenominator); if (hr == HResult.S_OK) { // only report success if (frameRateMinDenominator < 0) { outSb.Append("MF_MT_FRAME_RATE_RANGE_MIN (frames/s)=(undefined),"); } else { outSb.Append("MF_MT_FRAME_RATE_RANGE_MIN=" + ((decimal)frameRateMin / (decimal)frameRateMinDenominator).ToString() + "f/s, "); } } // MF_MT_FRAME_RATE_RANGE_MAX // The frame rate is expressed as a ratio.The upper 32 bits of the attribute value contain the numerator and the lower 32 bits contain the denominator. // For example, if the frame rate is 30 frames per second(fps), the ratio is 30 / 1.If the frame rate is 29.97 fps, the ratio is 30,000 / 1001. // we report this back to the user as a decimal hr = MFExtern.MFGetAttributeRatio(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_RATE_RANGE_MAX, out int frameRateMax, out int frameRateMaxDenominator); if (hr == HResult.S_OK) { // only report success if (frameRateMaxDenominator < 0) { outSb.Append("MF_MT_FRAME_RATE_RANGE_MAX (frames/s)=(undefined),"); } else { outSb.Append("MF_MT_FRAME_RATE_RANGE_MAX=" + ((decimal)frameRateMax / (decimal)frameRateMaxDenominator).ToString() + "f/s, "); } } // 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) { outSb.Append("\r\n"); outSb.Append(" AllAttrs=" + allAttrs.ToString()); } return(HResult.S_OK); }