private WaveFormat GetCurrentWaveFormat(IMFSourceReader reader) { IMFMediaType uncompressedMediaType; reader.GetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, out uncompressedMediaType); // Two ways to query it, first is to ask for properties (second is to convert into WaveFormatEx using MFCreateWaveFormatExFromMFMediaType) var outputMediaType = new MediaType(uncompressedMediaType); Guid actualMajorType = outputMediaType.MajorType; Debug.Assert(actualMajorType == MediaTypes.MFMediaType_Audio); Guid audioSubType = outputMediaType.SubType; int channels = outputMediaType.ChannelCount; int bits = outputMediaType.BitsPerSample; int sampleRate = outputMediaType.SampleRate; if (audioSubType == AudioSubtypes.MFAudioFormat_PCM) { return(new WaveFormat(sampleRate, bits, channels)); } if (audioSubType == AudioSubtypes.MFAudioFormat_Float) { return(WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels)); } var subTypeDescription = FieldDescriptionHelper.Describe(typeof(AudioSubtypes), audioSubType); throw new InvalidDataException(String.Format("Unsupported audio sub Type {0}", subTypeDescription)); }
private void AddTransforms(IEnumerable <Activate> effects, string type) { foreach (var mft in effects) { int attributeCount = mft.Count; var sb = new StringBuilder(); sb.AppendFormat(type); sb.AppendLine(); for (int n = 0; n < attributeCount; n++) { Guid key; var value = mft.GetByIndex(n, out key); string propertyName = FieldDescriptionHelper.Describe(typeof(MediaFoundationAttributes), key); if (key == TransformAttributeKeys.MftInputTypesAttributes.Guid || key == TransformAttributeKeys.MftOutputTypesAttributes.Guid) { byte[] blob = (byte[])value; var count = blob.Length / 32; var types = new TRegisterTypeInformation[count]; for (int j = 0; j < count; j++) { types[j].GuidMajorType = new Guid(blob.Skip(j * 32).Take(16).ToArray()); types[j].GuidSubtype = new Guid(blob.Skip(j * 32 + 16).Take(16).ToArray()); } sb.AppendFormat("{0}: {1} items:", propertyName, types.Length); sb.AppendLine(); foreach (var t in types) { sb.AppendFormat(" {0}-{1}", FieldDescriptionHelper.Describe(typeof(MediaTypes), t.GuidMajorType), FieldDescriptionHelper.Describe(typeof(AudioSubtypes), t.GuidSubtype)); sb.AppendLine(); } } else if (key == TransformAttributeKeys.TransformCategoryAttribute.Guid) { sb.AppendFormat("{0}: {1}", propertyName, FieldDescriptionHelper.Describe(typeof(MediaFoundationTransformCategories), (Guid)value)); sb.AppendLine(); } else if (value is byte[]) { var b = (byte[])value; sb.AppendFormat("{0}: Blob of {1} bytes", propertyName, b.Length); sb.AppendLine(); } else { sb.AppendFormat("{0}: {1}", propertyName, value); sb.AppendLine(); } } Transforms.Add(sb.ToString()); } }
private void AddTransforms(IEnumerable <IMFActivate> effects, string type) { foreach (var mft in effects) { int attributeCount; mft.GetCount(out attributeCount); var sb = new StringBuilder(); sb.AppendFormat(type); sb.AppendLine(); for (int n = 0; n < attributeCount; n++) { Guid key; var value = new PropVariant(); mft.GetItemByIndex(n, out key, ref value); string propertyName = FieldDescriptionHelper.Describe(typeof(MediaFoundationAttributes), key); if (key == MediaFoundationAttributes.MFT_INPUT_TYPES_Attributes || key == MediaFoundationAttributes.MFT_OUTPUT_TYPES_Attributes) { var types = value.GetBlobAsArrayOf <MFT_REGISTER_TYPE_INFO>(); sb.AppendFormat("{0}: {1} items:", propertyName, types.Length); sb.AppendLine(); foreach (var t in types) { sb.AppendFormat(" {0}-{1}", FieldDescriptionHelper.Describe(typeof(MediaTypes), t.guidMajorType), FieldDescriptionHelper.Describe(typeof(AudioSubtypes), t.guidSubtype)); sb.AppendLine(); } } else if (key == MediaFoundationAttributes.MF_TRANSFORM_CATEGORY_Attribute) { sb.AppendFormat("{0}: {1}", propertyName, FieldDescriptionHelper.Describe(typeof(MediaFoundationTransformCategories), (Guid)value.Value)); sb.AppendLine(); } else if (value.DataType == (VarEnum.VT_VECTOR | VarEnum.VT_UI1)) { var b = (byte[])value.Value; sb.AppendFormat("{0}: Blob of {1} bytes", propertyName, b.Length); sb.AppendLine(); } else { sb.AppendFormat("{0}: {1}", propertyName, value.Value); sb.AppendLine(); } value.Clear(); } Transforms.Add(sb.ToString()); } }
private string DescribeMediaType(MediaType mediaType) { int attributeCount = mediaType.Count; var sb = new StringBuilder(); for (int n = 0; n < attributeCount; n++) { Guid key; var val = MediaType.GetByIndex(n, out key); string propertyName = FieldDescriptionHelper.Describe(typeof(NAudio.MediaFoundation.MediaFoundationAttributes), key); sb.AppendFormat("{0}={1}\r\n", propertyName, val); //val.Clear(); } return(sb.ToString()); }
private static void AddAttribute(IMFActivate mft, int index, StringBuilder sb) { var variantPtr = Marshal.AllocHGlobal(MarshalHelpers.SizeOf <PropVariant>()); try { Guid key; mft.GetItemByIndex(index, out key, variantPtr); var value = MarshalHelpers.PtrToStructure <PropVariant>(variantPtr); string propertyName = FieldDescriptionHelper.Describe(typeof(MediaFoundationAttributes), key); if (key == MediaFoundationAttributes.MFT_INPUT_TYPES_Attributes || key == MediaFoundationAttributes.MFT_OUTPUT_TYPES_Attributes) { var types = value.GetBlobAsArrayOf <MFT_REGISTER_TYPE_INFO>(); sb.AppendFormat("{0}: {1} items:", propertyName, types.Length); sb.AppendLine(); foreach (var t in types) { sb.AppendFormat(" {0}-{1}", FieldDescriptionHelper.Describe(typeof(MediaTypes), t.guidMajorType), FieldDescriptionHelper.Describe(typeof(AudioSubtypes), t.guidSubtype)); sb.AppendLine(); } } else if (key == MediaFoundationAttributes.MF_TRANSFORM_CATEGORY_Attribute) { sb.AppendFormat("{0}: {1}", propertyName, FieldDescriptionHelper.Describe(typeof(MediaFoundationTransformCategories), (Guid)value.Value)); sb.AppendLine(); } else if (value.DataType == (VarEnum.VT_VECTOR | VarEnum.VT_UI1)) { var b = (byte[])value.Value; sb.AppendFormat("{0}: Blob of {1} bytes", propertyName, b.Length); sb.AppendLine(); } else { sb.AppendFormat("{0}: {1}", propertyName, value.Value); sb.AppendLine(); } } finally { PropVariant.Clear(variantPtr); Marshal.FreeHGlobal(variantPtr); } }
private static void DescribeAttribute(IMFMediaType mediaType, int n, StringBuilder sb) { var variantPtr = Marshal.AllocHGlobal(MarshalHelpers.SizeOf <PropVariant>()); try { Guid key; mediaType.GetItemByIndex(n, out key, variantPtr); var val = MarshalHelpers.PtrToStructure <PropVariant>(variantPtr); string propertyName = FieldDescriptionHelper.Describe(typeof(MediaFoundationAttributes), key); sb.AppendFormat("{0}={1}\r\n", propertyName, val.Value); } finally { PropVariant.Clear(variantPtr); Marshal.FreeHGlobal(variantPtr); } }
private string DescribeMediaType(IMFMediaType mediaType) { int attributeCount; mediaType.GetCount(out attributeCount); var sb = new StringBuilder(); for (int n = 0; n < attributeCount; n++) { Guid key; var val = new PropVariant(); mediaType.GetItemByIndex(n, out key, ref val); string propertyName = FieldDescriptionHelper.Describe(typeof(MediaFoundationAttributes), key); sb.AppendFormat("{0}={1}\r\n", propertyName, val.Value); val.Clear(); } return(sb.ToString()); }
private WaveFormat GetCurrentWaveFormat(IMFSourceReader reader) { IMFMediaType mediaType; reader.GetCurrentMediaType(-3, out mediaType); MediaType expr_10 = new MediaType(mediaType); Guid arg_16_0 = expr_10.MajorType; Guid subType = expr_10.SubType; int channelCount = expr_10.ChannelCount; int bitsPerSample = expr_10.BitsPerSample; int sampleRate = expr_10.SampleRate; if (subType == AudioSubtypes.MFAudioFormat_PCM) { return(new WaveFormat(sampleRate, bitsPerSample, channelCount)); } if (subType == AudioSubtypes.MFAudioFormat_Float) { return(WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channelCount)); } string arg = FieldDescriptionHelper.Describe(typeof(AudioSubtypes), subType); throw new InvalidDataException(string.Format("Unsupported audio sub Type {0}", arg)); }
public static WaveFormat GetCurrentWaveFormat(byte[] buffer) { MediaFoundationApi.Startup(); var settings = new MediaFoundationReaderSettings(); IMFSourceReader reader = null; //MediaFoundationInterop.MFCreateSourceReaderFromURL(file, null, out reader); reader = MediaFoundationApi.CreateSourceReaderFromByteStream(MediaFoundationApi.CreateByteStream(new ComStream(new MemoryStream(buffer)))); reader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_ALL_STREAMS, false); reader.SetStreamSelection(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true); // Create a partial media type indicating that we want uncompressed PCM audio var partialMediaType = new MediaType(); partialMediaType.MajorType = MediaTypes.MFMediaType_Audio; partialMediaType.SubType = settings.RequestFloatOutput ? AudioSubtypes.MFAudioFormat_Float : AudioSubtypes.MFAudioFormat_PCM; var currentMediaType = GetCurrentMediaType(reader); // mono, low sample rate files can go wrong on Windows 10 unless we specify here partialMediaType.ChannelCount = currentMediaType.ChannelCount; partialMediaType.SampleRate = currentMediaType.SampleRate; try { // set the media type // can return MF_E_INVALIDMEDIATYPE if not supported reader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType.MediaFoundationObject); } catch (COMException ex) when(ex.GetHResult() == MediaFoundationErrors.MF_E_INVALIDMEDIATYPE) { // HE-AAC (and v2) seems to halve the samplerate if (currentMediaType.SubType == AudioSubtypes.MFAudioFormat_AAC && currentMediaType.ChannelCount == 1) { partialMediaType.SampleRate = currentMediaType.SampleRate *= 2; partialMediaType.ChannelCount = currentMediaType.ChannelCount *= 2; reader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType.MediaFoundationObject); } else { throw; } } IMFMediaType uncompressedMediaType; reader.GetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, out uncompressedMediaType); // Two ways to query it, first is to ask for properties (second is to convert into WaveFormatEx using MFCreateWaveFormatExFromMFMediaType) var outputMediaType = new MediaType(uncompressedMediaType); Guid actualMajorType = outputMediaType.MajorType; Debug.Assert(actualMajorType == MediaTypes.MFMediaType_Audio); Guid audioSubType = outputMediaType.SubType; int channels = outputMediaType.ChannelCount; int bits = outputMediaType.BitsPerSample; int sampleRate = outputMediaType.SampleRate; Marshal.ReleaseComObject(currentMediaType.MediaFoundationObject); //Marshal.ReleaseComObject(reader); if (audioSubType == AudioSubtypes.MFAudioFormat_PCM) { return(new WaveFormat(sampleRate, bits, channels)); } if (audioSubType == AudioSubtypes.MFAudioFormat_Float) { return(WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels)); } var subTypeDescription = FieldDescriptionHelper.Describe(typeof(AudioSubtypes), audioSubType); throw new InvalidDataException(String.Format("Unsupported audio sub Type {0}", subTypeDescription)); }