private static void LogSourceTypes(SourceReader sourceReader) { int streamIndex = 0; while (true) { bool invalidStreamNumber = false; int _streamIndex = -1; for (int mediaIndex = 0; ; mediaIndex++) { try { var nativeMediaType = sourceReader.GetNativeMediaType(streamIndex, mediaIndex); if (_streamIndex != streamIndex) { _streamIndex = streamIndex; Console.WriteLine("====================== StreamIndex#" + streamIndex + "====================="); } Console.WriteLine(MfTool.LogMediaType(nativeMediaType)); nativeMediaType?.Dispose(); } catch (SharpDX.SharpDXException ex) { if (ex.ResultCode == SharpDX.MediaFoundation.ResultCode.NoMoreTypes) { Console.WriteLine(""); break; } else if (ex.ResultCode == SharpDX.MediaFoundation.ResultCode.InvalidStreamNumber) { invalidStreamNumber = true; break; } else { throw; } } } if (invalidStreamNumber) { break; } streamIndex++; } }
private void Initialize(SourceReader reader) { // Invalidate selection for all streams reader.SetStreamSelection(SourceReaderIndex.AllStreams, false); // Select only audio stream reader.SetStreamSelection(SourceReaderIndex.FirstAudioStream, true); // Get the media type for the current stream. using (var mediaType = reader.GetNativeMediaType(SourceReaderIndex.FirstAudioStream, 0)) { var majorType = mediaType.Get(MediaTypeAttributeKeys.MajorType); if (majorType != MediaTypeGuids.Audio) throw new InvalidOperationException("Input stream doesn't contain an audio stream."); } // Set the type on the source reader to use PCM using (var partialType = new MediaType()) { partialType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Audio); partialType.Set(MediaTypeAttributeKeys.Subtype, AudioFormatGuids.Pcm); reader.SetCurrentMediaType(SourceReaderIndex.FirstAudioStream, partialType); } // Retrieve back the real media type using (var realMediaType = reader.GetCurrentMediaType(SourceReaderIndex.FirstAudioStream)) { int sizeRef; WaveFormat = realMediaType.ExtracttWaveFormat(out sizeRef); } Duration = new TimeSpan(reader.GetPresentationAttribute(SourceReaderIndex.MediaSource, PresentationDescriptionAttributeKeys.Duration)); }