Skip to content

Commit

Permalink
fix: infinite read loop at end of FFmpeg stream (#24)
Browse files Browse the repository at this point in the history
When FFmpeg doesn't know from file metadata where a stream ends (e.g., MPEG transport streams), it tries to read until it reaches the end. To detect the end, it looks for a specific `EOF` code. Update the `read` function to return the `EOF` code instead of `0` (bytes read) and prevent FFmpeg from endlessly retrying to read.
  • Loading branch information
protyposis committed Jan 26, 2024
1 parent 316cf4d commit 6a99a85
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion nativesrc/aurioffmpegproxy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ int file_close(FILE* f) {
}

int file_read_packet(void* f, uint8_t* buf, int buf_size) {
return (int)fread(buf, 1, buf_size, f);
int bytesRead = (int)fread(buf, 1, buf_size, f);
return bytesRead > 0 ? bytesRead : AVERROR_EOF;
}

int64_t file_seek(void* f, int64_t offset, int whence) {
Expand Down
6 changes: 6 additions & 0 deletions src/Aurio.FFmpeg/FFmpegReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Aurio.FFmpeg
{
public class FFmpegReader : IDisposable
{
private const int AVERROR_EOF = -('E' | ('O' << 8) | ('F' << 16) | (' ' << 24));
private string filename; // store source filename for debugging
private bool disposed = false;
private Type mode;
Expand Down Expand Up @@ -87,6 +88,11 @@ public unsafe FFmpegReader(Stream stream, Type mode, string fileName)
var bufferSpan = new Span<byte>(buffer.ToPointer(), bufferSize);
int bytesRead = stream.Read(bufferSpan);

if (bytesRead <= 0)
{
return AVERROR_EOF;
}

return bytesRead;
};
seekDelegate = delegate(IntPtr opaque, long offset, int whence)
Expand Down

0 comments on commit 6a99a85

Please sign in to comment.