1

Closed

System.IO.EndOfStreamException

description

Hello,
I get an EndOfStreamException, when reading data from start to middle, and then jumping back to front (because I want to seek). My code:
namespace AudioCuesheetEditor.AudioBackend.SFML
{
    /// <summary>
    /// Class for mp3 decoded audio files to use in SFML as Soundstream, since SFML doesn't support mp3 decoding (for legal reasons).
    /// </summary>
    public class Mp3StreamSFML : SoundStream
    {
        private static readonly Logfile log = Logfile.getLogfile(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private MpegFile mp3file;
        private Mutex mutex;

        /// <summary>
        /// Initializes a new instance of the <see cref="AudioCuesheetEditor.AudioBackend.SFML.Mp3StreamSFML"/> class.
        /// </summary>
        /// <param name="_filename">Full path to the file</param>
        public Mp3StreamSFML(String _filename)
        {
            log.debug("Constructor called with " + _filename);
            this.mp3file = new MpegFile(_filename);
            this.Initialize((uint)this.mp3file.Channels, (uint)this.mp3file.SampleRate);
            this.mutex = new Mutex();
        }

        public TimeSpan Duration
        {
            get
            {
                log.debug("Duration = " + this.mp3file.Duration);
                return this.mp3file.Duration;
            }
        }

        #region implemented abstract members of SoundStream

        protected override bool OnGetData(out short[] samples)
        {
            log.debug("OnGetData called");
            this.mutex.WaitOne();
            //Buffer data for about 1 second
            float[] normalizedaudiodata = new float[48000];
            int readSamples = this.mp3file.ReadSamples(normalizedaudiodata, 0, normalizedaudiodata.Length);
            short[] pcmaudiodata;
            if (readSamples > 0)
            {
                pcmaudiodata = new short[readSamples]; // converted data
                for (int i = 0; i < readSamples; i++)
                {
                    // clip the data
                    if (normalizedaudiodata[i] > 1.0f)
                    {
                        normalizedaudiodata[i] = 1.0f;
                    }
                    else
                    {
                        if (normalizedaudiodata[i] < -1.0f)
                        {
                            normalizedaudiodata[i] = -1.0f;
                        }
                    }
                    // convert to pcm data
                    pcmaudiodata[i] = (short)(normalizedaudiodata[i] * short.MaxValue);
                }
                samples = pcmaudiodata;
                this.mutex.ReleaseMutex();
                return true;
            }
            else
            {
                samples = null;
                this.mutex.ReleaseMutex();
                return false;
            }
        }

        protected override void OnSeek(TimeSpan timeOffset)
        {
            log.debug("OnSeek called with " + timeOffset);
            this.mutex.WaitOne();
            if ((timeOffset <= this.mp3file.Duration) && (timeOffset >= TimeSpan.Zero))
            {
                while (this.mp3file.CanSeek == false)
                {
                    Thread.Sleep(100);
                }
                this.mp3file.Time = timeOffset;
            }
            this.mutex.ReleaseMutex();
        }

        #endregion
    }
}

Exception:
16.07.2014 15:47:37 AudioCuesheetEditor.AudioBackend.SFML.Mp3StreamSFML: ERROR | System.IO.EndOfStreamException: Es wurde versucht, über das Ende des Datenstroms hinaus zu lesen.
bei NLayer.Decoder.MpegFrame.ReadBits(Int32 bitCount) in d:\tmp\AudioCuesheetEditor\src\NLayer\Decoder\MpegFrame.cs:Zeile 528.
bei NLayer.Decoder.LayerIIIDecoder.ReadSideInfo(IMpegFrame frame) in d:\tmp\AudioCuesheetEditor\src\NLayer\Decoder\LayerIIIDecoder.cs:Zeile 687.
bei NLayer.Decoder.LayerIIIDecoder.DecodeFrame(IMpegFrame frame, Single[] ch0, Single[] ch1) in d:\tmp\AudioCuesheetEditor\src\NLayer\Decoder\LayerIIIDecoder.cs:Zeile 502.
bei NLayer.MpegFrameDecoder.DecodeFrameImpl(IMpegFrame frame, Array dest, Int32 destOffset) in d:\tmp\AudioCuesheetEditor\src\NLayer\MpegFrameDecoder.cs:Zeile 110.
bei NLayer.MpegFrameDecoder.DecodeFrame(IMpegFrame frame, Single[] dest, Int32 destOffset) in d:\tmp\AudioCuesheetEditor\src\NLayer\MpegFrameDecoder.cs:Zeile 74.
bei NLayer.MpegFile.set_Position(Int64 value) in d:\tmp\AudioCuesheetEditor\src\NLayer\MpegFile.cs:Zeile 92.
bei NLayer.MpegFile.set_Time(TimeSpan value) in d:\tmp\AudioCuesheetEditor\src\NLayer\MpegFile.cs:Zeile 107.
bei AudioCuesheetEditor.AudioBackend.SFML.Mp3StreamSFML.OnSeek(TimeSpan timeOffset) in d:\tmp\AudioCuesheetEditor\src\AudioCuesheetEditor\AudioBackend\SFML\Mp3StreamSFML.cs:Zeile 92.
Closed Sep 15, 2014 at 12:10 PM by ioctlLR
User reports fixed

comments

sbaus wrote Sep 11, 2014 at 5:27 PM

Hopefully anybody is reading this:

What works:

The Method "protected override void OnSeek(TimeSpan timeOffset)" is called from SFML everytime a seek is questioned. If I reset the reference to "this.mp3file = new MpegFile(_filename);" everytime before setting the position, no EndOfStreamException is thrown:
protected override void OnSeek(TimeSpan timeOffset)
        {
            log.debug("OnSeek called with " + timeOffset);
            this.mutex.WaitOne();
            this.mp3file = new MpegFile(this.filePath);
            if ((timeOffset <= this.mp3file.Duration) && (timeOffset >= TimeSpan.Zero))
            {
                this.mp3file.Time = timeOffset;
            }
            this.mutex.ReleaseMutex();
        }

sbaus wrote Sep 11, 2014 at 5:28 PM

Resetting the reference everytime is not good, so anybody an idea how to handle the problem?

ioctlLR wrote Sep 12, 2014 at 12:12 PM

The code in your first post looks good to me... The problem is either how SFML is calling your code (unlikely) or how NLayer is handling seeks... I'll take a look.

ioctlLR wrote Sep 12, 2014 at 3:17 PM

OK, I believe I found the issue... NLayer's seek support was completely broken!

Your original code above should now work with the latest source.

sbaus wrote Sep 14, 2014 at 5:04 PM

Thanks, now it seems to do the job. Really thank you for your fast help ;).

wrote Sep 15, 2014 at 12:10 PM