Quantcast
Channel: naudio Work Item Rss Feed
Viewing all articles
Browse latest Browse all 738

Commented Unassigned: NAudio.Wave.MediaFoundationEncoder.PerformEncode throws MF_E_SINK_NO_SAMPLES_PROCESSED (0xC00D4A44) [16439]

$
0
0
OS: Windows Server 2012R2
NAudio Version: 1.7.1.17

----------------------------------------- Exception -------------------------------
Exception: "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in NAudio.dll
Additional information: Exception from HRESULT: 0xC00D4A44'

writer.DoFinalize() - line 232, MediaFoundationEncoder.cs

I was attempting to convert several wav files to mp3 files when I came across this error. Sample code that should recreate the error:

----------------------------------------- My Source -------------------------------
```
NAudio.MediaFoundation.MediaFoundationApi.Startup();
using(NAudio.Wave.WaveFileReader readerStream = new NAudio.Wave.WaveFileReader(file))
{
using(NAudio.Wave.WaveStream waveStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(readerStream))
{
NAudio.Wave.MediaFoundationEncoder.EncodeToMp3(waveStream, file + ".mp3");
}
}
NAudio.MediaFoundation.MediaFoundationApi.Shutdown();
```

----------------------------------------- Cause -------------------------------
The writer is performing writes on a sample basis asynchronously to the thread making the writer.WriteSample() method. Therefore, sometimes the queued bytes to write out via the writer have not been written when writer.DoFinalize() is called inside PerformEncode() causing a COMException to be thrown since apparently DoFinalize() does not wait for the writer to become flush before disposal.

----------------------------------------- Solution -------------------------------
```
private void PerformEncode(IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider)
{
int maxLength = inputProvider.WaveFormat.AverageBytesPerSecond * 4;
var managedBuffer = new byte[maxLength];

writer.BeginWriting();

long position = 0;
long duration = 0;
do
{
duration = ConvertOneBuffer(writer, streamIndex, inputProvider, position, managedBuffer);
position += duration;
} while (duration > 0);

//We are going to check the stats of the writer and ensure no more bytes are queued before we call DoFinalize()
MF_SINK_WRITER_STATISTICS stats = new MF_SINK_WRITER_STATISTICS();
stats.cb = Marshal.SizeOf(stats);
bool finished = false;

while (!finished)
{
writer.GetStatistics(streamIndex, stats);

if (stats.dwByteCountQueued == 0)
finished = true;
else
System.Threading.Thread.Sleep(100); //sleeping could be optional, but don't want to hog the CPU
}
//End workaround

writer.DoFinalize();
}
```
Comments: thanks, I'll try to get this into a future NAudio

Viewing all articles
Browse latest Browse all 738

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>