From 7eed50a6ca8d6dffb8d69dc269e68043448c9440 Mon Sep 17 00:00:00 2001 From: Mario Guggenberger Date: Fri, 26 Jan 2024 20:31:13 +0100 Subject: [PATCH] feat: read FFmpeg samples without intermediary buffer Use newer .NET API to read samples from native layer directly into managed memory and avoid the overhead of copying into a transfer buffer. --- src/Aurio.FFmpeg/Aurio.FFmpeg.csproj | 1 + src/Aurio.FFmpeg/FFmpegReader.cs | 19 +++---------------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/src/Aurio.FFmpeg/Aurio.FFmpeg.csproj b/src/Aurio.FFmpeg/Aurio.FFmpeg.csproj index 02da2c55..ebc41c32 100644 --- a/src/Aurio.FFmpeg/Aurio.FFmpeg.csproj +++ b/src/Aurio.FFmpeg/Aurio.FFmpeg.csproj @@ -12,6 +12,7 @@ ..\..\nativesrc\out\build\linux-$(FFmpegProxyBuildConfig)\aurioffmpegproxy true Extension library for Aurio, which provides audio decoding through FFmpeg (see https://ffmpeg.org/). + True diff --git a/src/Aurio.FFmpeg/FFmpegReader.cs b/src/Aurio.FFmpeg/FFmpegReader.cs index 4e81d9ba..1757c9f3 100644 --- a/src/Aurio.FFmpeg/FFmpegReader.cs +++ b/src/Aurio.FFmpeg/FFmpegReader.cs @@ -75,31 +75,18 @@ public FFmpegReader(FileInfo fileInfo, Type mode) /// the stream to decode /// the types of data to read /// optional filename as a hint for FFmpeg to determine the data format - public FFmpegReader(Stream stream, Type mode, string fileName) + public unsafe FFmpegReader(Stream stream, Type mode, string fileName) { ValidateNativeLibraryAvailability(); this.filename = fileName ?? "bufferedIO_stream"; this.mode = mode; - var transferBuffer = new byte[0]; readPacketDelegate = delegate(IntPtr opaque, IntPtr buffer, int bufferSize) { - /* NOTE there's no way to cast the IntPtr to a byte array which is required - * for stream reading, so we need to add an intermediary transfer buffer. - */ - // Increase transfer buffer's size if too small - if (transferBuffer.Length < bufferSize) - { - transferBuffer = new byte[bufferSize]; - } - // Read data into transfer buffer - int bytesRead = stream.Read(transferBuffer, 0, bufferSize); - - // Transfer data to unmanaged memory - Marshal.Copy(transferBuffer, 0, buffer, bytesRead); + var bufferSpan = new Span(buffer.ToPointer(), bufferSize); + int bytesRead = stream.Read(bufferSpan); - // Return number of bytes read return bytesRead; }; seekDelegate = delegate(IntPtr opaque, long offset, int whence)