private long GetLength(IMFSourceReader reader) { var variantPtr = Marshal.AllocHGlobal(MarshalHelpers.SizeOf <PropVariant>()); try { // http://msdn.microsoft.com/en-gb/library/windows/desktop/dd389281%28v=vs.85%29.aspx#getting_file_duration int hResult = reader.GetPresentationAttribute(MediaFoundationInterop.MF_SOURCE_READER_MEDIASOURCE, MediaFoundationAttributes.MF_PD_DURATION, variantPtr); if (hResult == MediaFoundationErrors.MF_E_ATTRIBUTENOTFOUND) { // this doesn't support telling us its duration (might be streaming) return(0); } if (hResult != 0) { Marshal.ThrowExceptionForHR(hResult); } var variant = MarshalHelpers.PtrToStructure <PropVariant>(variantPtr); var lengthInBytes = (((long)variant.Value) * waveFormat.AverageBytesPerSecond) / 10000000L; return(lengthInBytes); } finally { PropVariant.Clear(variantPtr); Marshal.FreeHGlobal(variantPtr); } }
private static long GetDuration(IMFSourceReader reader) { var variantPtr = Marshal.AllocHGlobal(MarshalHelpers.SizeOf <PropVariant>()); try { int hResult = reader.GetPresentationAttribute(MediaFoundationInterop.MF_SOURCE_READER_MEDIASOURCE, MediaFoundationAttributes.MF_PD_DURATION, variantPtr); if (hResult == MediaFoundationErrors.MF_E_ATTRIBUTENOTFOUND) { return(0); } if (hResult != 0) { Marshal.ThrowExceptionForHR(hResult); } var variant = MarshalHelpers.PtrToStructure <PropVariant> (variantPtr); return((long)variant.Value); } finally { PropVariant.Clear(variantPtr); Marshal.FreeHGlobal(variantPtr); } }
private long GetLength(IMFSourceReader reader) { IntPtr intPtr = Marshal.AllocHGlobal(MarshalHelpers.SizeOf <PropVariant>()); long result; try { int presentationAttribute = reader.GetPresentationAttribute(-1, MediaFoundationAttributes.MF_PD_DURATION, intPtr); if (presentationAttribute == -1072875802) { result = 0L; } else { if (presentationAttribute != 0) { Marshal.ThrowExceptionForHR(presentationAttribute); } result = (long)MarshalHelpers.PtrToStructure <PropVariant>(intPtr).Value *(long)this.waveFormat.AverageBytesPerSecond / 10000000L; } } finally { PropVariant.Clear(intPtr); Marshal.FreeHGlobal(intPtr); } return(result); }
/// <summary> /// Gets an attribute from the underlying media source. /// </summary> /// <param name="sourceReader">A valid IMFSourceReader instance.</param></param> /// <param name="streamIndex">The streamIndex to query.</param> /// <param name="mediaTypeIndex">The zero-based index of the media type to retrieve.</param> /// <param name="mediaType">Receives an instance of the IMFMediaType interface.</param> /// <returns>If this function succeeds, it returns the S_OK member. Otherwise, it returns another HResult's member that describe the error.</returns> public static HResult GetPresentationAttribute(this IMFSourceReader sourceReader, SourceReaderFirstStreamOrMediaSource streamIndex, Guid guidAttribute, PropVariant attribute) { if (sourceReader == null) { throw new ArgumentNullException("sourceReader"); } return(sourceReader.GetPresentationAttribute((int)streamIndex, guidAttribute, attribute)); }
private long GetLength(IMFSourceReader reader) { PropVariant propVariant; int presentationAttribute = reader.GetPresentationAttribute(-1, MediaFoundationAttributes.MF_PD_DURATION, out propVariant); if (presentationAttribute == -1072875802) { return(0L); } if (presentationAttribute != 0) { Marshal.ThrowExceptionForHR(presentationAttribute); } long result = (long)propVariant.Value * (long)this.waveFormat.AverageBytesPerSecond / 10000000L; propVariant.Clear(); return(result); }
private long GetLength(IMFSourceReader reader) { PropVariant variant; // http://msdn.microsoft.com/en-gb/library/windows/desktop/dd389281%28v=vs.85%29.aspx#getting_file_duration int hResult = reader.GetPresentationAttribute(MediaFoundationInterop.MF_SOURCE_READER_MEDIASOURCE, MediaFoundationAttributes.MF_PD_DURATION, out variant); if (hResult == MediaFoundationErrors.MF_E_ATTRIBUTENOTFOUND) { // this doesn't support telling us its duration (might be streaming) return 0; } if (hResult != 0) { Marshal.ThrowExceptionForHR(hResult); } var lengthInBytes = (((long)variant.Value) * waveFormat.AverageBytesPerSecond) / 10000000L; variant.Clear(); return lengthInBytes; }