SHA256
Reduce Memory Copy When Frame Capture Success
This commit is contained in:
1 parent
036216c006
commit
6bd4a8975e
3 files changed
+32
-11
No files matched your search
+1
-11
@@ -463,17 +463,7 @@ namespace ADOFAIRenderer.Core
|
|||||||
}
|
}
|
||||||
else if (frame.NativePointer != IntPtr.Zero && frame.Length > 0)
|
else if (frame.NativePointer != IntPtr.Zero && frame.Length > 0)
|
||||||
{
|
{
|
||||||
var buffer = FFmpegProcess.RentRawBytes(frame.Length);
|
videoFFmpeg.SendDataFromNativeBuffer(frame.NativePointer, frame.Length);
|
||||||
unsafe
|
|
||||||
{
|
|
||||||
fixed (byte* destPtr = buffer)
|
|
||||||
{
|
|
||||||
Buffer.MemoryCopy((void*)frame.NativePointer, destPtr, buffer.Length, frame.Length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
videoFFmpeg.SendDataFromBuffer(buffer, frame.Length);
|
|
||||||
ArrayPool<byte>.Shared.Return(buffer);
|
|
||||||
|
|
||||||
if (frame.ReadbackState != null)
|
if (frame.ReadbackState != null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -44,6 +44,13 @@ namespace ADOFAIRenderer.Util.FFmpeg
|
|||||||
_inputStream.Flush();
|
_inputStream.Flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendDataFromNativeBuffer(IntPtr nativePtr, int length)
|
||||||
|
{
|
||||||
|
if (_inputStream == null) return;
|
||||||
|
StreamUtil.WriteNativePtrToStream(nativePtr, length, _inputStream);
|
||||||
|
_inputStream.Flush();
|
||||||
|
}
|
||||||
|
|
||||||
public void Close()
|
public void Close()
|
||||||
{
|
{
|
||||||
if (_ffmpegProcess == null || _ffmpegProcess.HasExited || _inputStream == null) return;
|
if (_ffmpegProcess == null || _ffmpegProcess.HasExited || _inputStream == null) return;
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace ADOFAIRenderer.Util
|
||||||
|
{
|
||||||
|
public static class StreamUtil
|
||||||
|
{
|
||||||
|
public static void WriteNativePtrToStream(IntPtr nativePtr, int length, Stream destinationStream)
|
||||||
|
{
|
||||||
|
if (nativePtr == IntPtr.Zero)
|
||||||
|
throw new ArgumentNullException(nameof(nativePtr));
|
||||||
|
if (length <= 0) return;
|
||||||
|
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
// Create an unmanaged memory stream that wraps the native pointer
|
||||||
|
// (This does not allocate new memory; it references the existing pointer)
|
||||||
|
using var unmanagedStream = new UnmanagedMemoryStream((byte*)nativePtr, length);
|
||||||
|
// Copy the data directly into your target stream
|
||||||
|
unmanagedStream.CopyTo(destinationStream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user