Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: read FFmpeg samples without intermediary buffer #23

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Aurio.FFmpeg/Aurio.FFmpeg.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<FFmpegProxyLinuxPath>..\..\nativesrc\out\build\linux-$(FFmpegProxyBuildConfig)\aurioffmpegproxy</FFmpegProxyLinuxPath>
<IsPackable>true</IsPackable>
<Description>Extension library for Aurio, which provides audio decoding through FFmpeg (see https://ffmpeg.org/).</Description>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<None Include="..\Aurio.licenseheader" Link="Aurio.licenseheader" />
Expand Down
19 changes: 3 additions & 16 deletions src/Aurio.FFmpeg/FFmpegReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,18 @@ public FFmpegReader(FileInfo fileInfo, Type mode)
/// <param name="stream">the stream to decode</param>
/// <param name="mode">the types of data to read</param>
/// <param name="fileName">optional filename as a hint for FFmpeg to determine the data format</param>
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<byte>(buffer.ToPointer(), bufferSize);
int bytesRead = stream.Read(bufferSpan);

// Return number of bytes read
return bytesRead;
};
seekDelegate = delegate(IntPtr opaque, long offset, int whence)
Expand Down
Loading