protected override IMFSourceReader CreateReader(MediaFoundationReader.MediaFoundationReaderSettings settings) { IMFSourceReader expr_15 = MediaFoundationApi.CreateSourceReaderFromByteStream(MediaFoundationApi.CreateByteStream(new ComStream(this.stream))); expr_15.SetStreamSelection(-2, false); expr_15.SetStreamSelection(-3, true); expr_15.SetCurrentMediaType(-3, IntPtr.Zero, new MediaType { MajorType = MediaTypes.MFMediaType_Audio, SubType = settings.RequestFloatOutput ? AudioSubtypes.MFAudioFormat_Float : AudioSubtypes.MFAudioFormat_PCM }.MediaFoundationObject); return(expr_15); }
/// <summary> /// Creates the reader (overridable by ) /// </summary> protected virtual IMFSourceReader CreateReader2(byte[] buffer, MediaFoundationReaderSettings settings) { 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; } } Marshal.ReleaseComObject(currentMediaType.MediaFoundationObject); return(reader); }
/// <summary> /// Selects or deselects one or more streams. /// </summary> /// <param name="sourceReader">A valid IMFSourceReader instance.</param></param></param> /// <param name="streamIndex">The stream to set.</param> /// <param name="selected">Specify True to select streams or False to deselect streams. If a stream is deselected, it will not generate data.</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 SetStreamSelection(this IMFSourceReader sourceReader, SourceReaderStreams streamIndex, bool selected) { if (sourceReader == null) { throw new ArgumentNullException("sourceReader"); } return(sourceReader.SetStreamSelection((int)streamIndex, selected)); }
protected void Init(MediaFoundationReader.MediaFoundationReaderSettings initialSettings) { MediaFoundationApi.Startup(); this.settings = (initialSettings ?? new MediaFoundationReader.MediaFoundationReaderSettings()); IMFSourceReader iMFSourceReader = this.CreateReader(this.settings); this.waveFormat = this.GetCurrentWaveFormat(iMFSourceReader); iMFSourceReader.SetStreamSelection(-3, true); this.length = this.GetLength(iMFSourceReader); if (this.settings.SingleReaderObject) { this.pReader = iMFSourceReader; } }
public MediaFoundationReader(string file, MediaFoundationReader.MediaFoundationReaderSettings settings) { MediaFoundationApi.Startup(); this.settings = settings; this.file = file; IMFSourceReader imfsourceReader = this.CreateReader(settings); this.waveFormat = this.GetCurrentWaveFormat(imfsourceReader); imfsourceReader.SetStreamSelection(-3, true); this.length = this.GetLength(imfsourceReader); if (settings.SingleReaderObject) { this.pReader = imfsourceReader; } }
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)); }