Skip to content

Commit

Permalink
Another fix on filoe#114.
Browse files Browse the repository at this point in the history
  • Loading branch information
filoe committed Jul 9, 2016
1 parent be67770 commit 0bf1d9e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
17 changes: 12 additions & 5 deletions CSCore/Codecs/MP3/DmoMP3Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,32 @@ private void ParseForMp3Frames(Stream stream, bool enableSeeking)
long offsetOfFirstFrame = 0;
stream = new BufferedStream(stream);

while (ID3v2.SkipTag(stream))
if (enableSeeking)
{
/* skip all id3 tags (see https://github.com/filoe/cscore/issues/63)
while (ID3v2.SkipTag(stream))
{
/* skip all id3 tags (see https://github.com/filoe/cscore/issues/63)
* there are some files with multiple id3v2 tags
* not sure whether this is according to the id3 specification but we have to handle it anyway
* as long as the SkipTag method returns true, another id3 tag has been found
*/
}
}

while (frame == null && !stream.IsEndOfStream())
while (frame == null)
{
offsetOfFirstFrame = stream.Position;
if (enableSeeking && stream.IsEndOfStream())
break;

if(enableSeeking)
offsetOfFirstFrame = stream.Position;
frame = Mp3Frame.FromStream(stream);
}

if (frame == null)
throw new Exception("Could not find any MP3-Frames in the stream.");

if (stream.CanSeek)
if (enableSeeking)
{
XingHeader xingHeader = XingHeader.FromFrame(frame);
if (xingHeader != null)
Expand Down
22 changes: 15 additions & 7 deletions CSCore/Codecs/MP3/Mp3Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class Mp3Frame
public static Mp3Frame FromStream(Stream stream)
{
Mp3Frame frame = new Mp3Frame(stream);
return frame.FindFrame(stream, true) ? frame : null;
return frame.FindFrame(stream, stream.CanSeek) ? frame : null;
}

/// <summary>
Expand Down Expand Up @@ -129,7 +129,8 @@ private bool FindFrame(Stream stream, bool seek)

//int totalRead = 0;

_streamPosition = stream.Position;
if(stream.CanSeek)
_streamPosition = stream.Position;

while (!ParseFrame(buffer) || MPEGLayer != MpegLayer.Layer3)
{
Expand All @@ -149,11 +150,13 @@ private bool FindFrame(Stream stream, bool seek)
return false;
}*/

_streamPosition = stream.Position;
if(stream.CanSeek)
_streamPosition = stream.Position;
}

_headerBuffer = buffer;
_dataPosition = stream.Position;
if(stream.CanSeek)
_dataPosition = stream.Position;

if (_stream.CanSeek && seek)
_stream.Position += FrameLength - 4;
Expand All @@ -172,13 +175,18 @@ private bool FindFrame(Stream stream, bool seek)
/// <returns>The number of read bytes.</returns>
public int ReadData(ref byte[] buffer, int offset)
{
long currentPosition = _stream.Position;
long currentPosition = 0;
if (_stream.CanSeek)
{
currentPosition = _stream.Position;
_stream.Position = _streamPosition;
}

buffer = buffer.CheckBuffer(FrameLength + offset);
_stream.Position = _streamPosition;
int read = _stream.Read(buffer, offset, FrameLength);

_stream.Position = currentPosition;
if(_stream.CanSeek)
_stream.Position = currentPosition;

return read;
}
Expand Down
5 changes: 3 additions & 2 deletions CSCore/Codecs/MP3/Mp3WebStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ private void CreateStream(bool async)
_bufferThread.Start(resetEvent);
success = resetEvent.WaitOne();
if(success)
_decoder = new DmoMp3Decoder(new ReadBlockStream(_buffer.ToStream()), false);
}
EventHandler<ConnectionEstablishedEventArgs> handler = this.ConnectionEstablished;
if (handler != null && async)
Expand Down Expand Up @@ -271,13 +273,12 @@ private void BufferProc(object o)
frame.BitRate);
} while (_buffer.Buffered < _buffer.Length / 10 || !format.Equals(prevFormat));

_decoder = new DmoMp3Decoder(new ReadBlockStream(_buffer.ToStream()), false);

if (resetEvent != null)
resetEvent.Set();

do
{
Debug.WriteLine("Buffering");
if (_buffer.Buffered >= _buffer.Length * 0.85 && !_disposing)
Thread.Sleep(250);
else
Expand Down

0 comments on commit 0bf1d9e

Please sign in to comment.