Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
markheath committed Jul 31, 2022
2 parents 0cd2aba + 3c3c87c commit 5449b95
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
59 changes: 37 additions & 22 deletions NAudio.Wasapi/MediaFoundationEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ public static MediaType[] GetOutputMediaTypes(Guid audioSubtype)
throw;
}
}
int count;
availableTypes.GetElementCount(out count);
availableTypes.GetElementCount(out int count);
var mediaTypes = new List<MediaType>(count);
for (int n = 0; n < count; n++)
{
object mediaTypeObject;
availableTypes.GetElement(n, out mediaTypeObject);
availableTypes.GetElement(n, out object mediaTypeObject);
var mediaType = (IMFMediaType)mediaTypeObject;
mediaTypes.Add(new MediaType(mediaType));
}
Expand Down Expand Up @@ -222,8 +220,7 @@ public void Encode(string outputFile, IWaveProvider inputProvider)
var writer = CreateSinkWriter(outputFile);
try
{
int streamIndex;
writer.AddStream(outputMediaType.MediaFoundationObject, out streamIndex);
writer.AddStream(outputMediaType.MediaFoundationObject, out int streamIndex);

// n.b. can get 0xC00D36B4 - MF_E_INVALIDMEDIATYPE here
writer.SetInputMediaType(streamIndex, inputMediaType.MediaFoundationObject, null);
Expand All @@ -232,8 +229,14 @@ public void Encode(string outputFile, IWaveProvider inputProvider)
}
finally
{
Marshal.ReleaseComObject(writer);
Marshal.ReleaseComObject(inputMediaType.MediaFoundationObject);
if (writer != null)
{
Marshal.ReleaseComObject(writer);
}
if (inputMediaType.MediaFoundationObject != null)
{
Marshal.ReleaseComObject(inputMediaType.MediaFoundationObject);
}
}
}

Expand All @@ -243,24 +246,35 @@ public void Encode(string outputFile, IWaveProvider inputProvider)
/// <param name="outputStream">Output stream</param>
/// <param name="inputProvider">Input provider (should be PCM, some encoders will also allow IEEE float)</param>
/// <param name="transcodeContainerType">One of <see cref="TranscodeContainerTypes"/></param>
public void Encode(Stream outputStream, IWaveProvider inputProvider, Guid transcodeContainerType) {
if (inputProvider.WaveFormat.Encoding != WaveFormatEncoding.Pcm && inputProvider.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat) {
public void Encode(Stream outputStream, IWaveProvider inputProvider, Guid transcodeContainerType)
{
if (inputProvider.WaveFormat.Encoding != WaveFormatEncoding.Pcm && inputProvider.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat)
{
throw new ArgumentException("Encode input format must be PCM or IEEE float");
}

var inputMediaType = new MediaType(inputProvider.WaveFormat);

var writer = CreateSinkWriter(new ComStream(outputStream), transcodeContainerType);
try {
try
{
writer.AddStream(outputMediaType.MediaFoundationObject, out int streamIndex);

// n.b. can get 0xC00D36B4 - MF_E_INVALIDMEDIATYPE here
writer.SetInputMediaType(streamIndex, inputMediaType.MediaFoundationObject, null);

PerformEncode(writer, streamIndex, inputProvider);
} finally {
Marshal.ReleaseComObject(writer);
Marshal.ReleaseComObject(inputMediaType.MediaFoundationObject);
}
finally
{
if (writer != null)
{
Marshal.ReleaseComObject(writer);
}
if (inputMediaType.MediaFoundationObject != null)
{
Marshal.ReleaseComObject(inputMediaType.MediaFoundationObject);
}
}
}

Expand Down Expand Up @@ -292,18 +306,22 @@ private static IMFSinkWriter CreateSinkWriter(string outputFile)
return writer;
}

private static IMFSinkWriter CreateSinkWriter(IStream outputStream, Guid TranscodeContainerType) {
private static IMFSinkWriter CreateSinkWriter(IStream outputStream, Guid TranscodeContainerType)
{
// n.b. could try specifying the container type using attributes, but I think
// it does a decent job of working it out from the file extension
// n.b. AAC encode on Win 8 can have AAC extension, but use MP4 in win 7
// http://msdn.microsoft.com/en-gb/library/windows/desktop/dd389284%28v=vs.85%29.aspx
IMFSinkWriter writer;
var attributes = MediaFoundationApi.CreateAttributes(1);
attributes.SetGUID(MediaFoundationAttributes.MF_TRANSCODE_CONTAINERTYPE, TranscodeContainerType);
try {
try
{
MediaFoundationInterop.MFCreateMFByteStreamOnStream(outputStream, out var ppByteStream);
MediaFoundationInterop.MFCreateSinkWriterFromURL(null, ppByteStream, attributes, out writer);
} finally {
}
finally
{
Marshal.ReleaseComObject(attributes);
}
return writer;
Expand Down Expand Up @@ -336,16 +354,13 @@ private static long BytesToNsPosition(int bytes, WaveFormat waveFormat)
private long ConvertOneBuffer(IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider, long position, byte[] managedBuffer)
{
long durationConverted = 0;
int maxLength;
IMFMediaBuffer buffer = MediaFoundationApi.CreateMemoryBuffer(managedBuffer.Length);
buffer.GetMaxLength(out maxLength);
buffer.GetMaxLength(out var maxLength);

IMFSample sample = MediaFoundationApi.CreateSample();
sample.AddBuffer(buffer);

IntPtr ptr;
int currentLength;
buffer.Lock(out ptr, out maxLength, out currentLength);
buffer.Lock(out var ptr, out maxLength, out int currentLength);
int read = inputProvider.Read(managedBuffer, 0, maxLength);
if (read > 0)
{
Expand Down
10 changes: 5 additions & 5 deletions NAudio.sln
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30717.126
# Visual Studio Version 17
VisualStudioVersion = 17.2.32602.215
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NAudio", "NAudio\NAudio.csproj", "{DA4F02E3-0B5E-42CD-B8D9-5583FA51D66E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MidiFileConverter", "MidiFileConverter\MidiFileConverter.csproj", "{5B5897BD-A423-4AF8-8A59-1C1372ED77DB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MidiFileConverter", "MidiFileConverter\MidiFileConverter.csproj", "{5B5897BD-A423-4AF8-8A59-1C1372ED77DB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MixDiff", "MixDiff\MixDiff.csproj", "{1293DD10-378A-4370-AEE2-AA1E9E87039B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MixDiff", "MixDiff\MixDiff.csproj", "{1293DD10-378A-4370-AEE2-AA1E9E87039B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NAudioTests", "NAudioTests\NAudioTests.csproj", "{5080281A-F9A1-403F-85C7-0DFF6839B07B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NAudioDemo", "NAudioDemo\NAudioDemo.csproj", "{C37A547B-F31E-45FB-870A-CFA704D06152}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AudioFileInspector", "AudioFileInspector\AudioFileInspector.csproj", "{D29C1659-635C-497B-847E-FE9A5A69ED03}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AudioFileInspector", "AudioFileInspector\AudioFileInspector.csproj", "{D29C1659-635C-497B-847E-FE9A5A69ED03}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NAudioWpfDemo", "NAudioWpfDemo\NAudioWpfDemo.csproj", "{A7B74F85-D353-4ED4-A321-E6E4AD4D7D32}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public MediaTypeViewModel SelectedMediaType

private void Encode()
{
if (string.IsNullOrEmpty(InputFile)||!File.Exists(InputFile))
if (String.IsNullOrEmpty(InputFile) || !File.Exists(InputFile))
{
MessageBox.Show("Please select a valid input file to convert");
return;
Expand All @@ -193,8 +193,16 @@ private void Encode()
if (outputUrl == null) return;
using (var encoder = new MediaFoundationEncoder(SelectedMediaType.MediaType))
{
encoder.Encode(outputUrl, reader);
}
try
{

encoder.Encode(outputUrl, reader);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Failed to encode");
}
}
}
}

Expand Down

0 comments on commit 5449b95

Please sign in to comment.