Voice buffer fix (netcore)

This commit is contained in:
Garry Newman 2017-04-05 16:55:40 +01:00
parent fd82d26367
commit a956a1005c

View File

@ -10,14 +10,13 @@ namespace Facepunch.Steamworks
public class Voice : IDisposable public class Voice : IDisposable
{ {
const int ReadBufferSize = 1024 * 128; const int ReadBufferSize = 1024 * 128;
const int UncompressBufferSize = 1024 * 256;
internal Client client; internal Client client;
internal IntPtr ReadCompressedBuffer; internal IntPtr ReadCompressedBuffer;
internal IntPtr ReadUncompressedBuffer; internal IntPtr ReadUncompressedBuffer;
internal IntPtr UncompressBuffer; internal byte[] UncompressBuffer = new byte[1024 * 256];
public Action<IntPtr, int> OnCompressedData; public Action<IntPtr, int> OnCompressedData;
public Action<IntPtr, int> OnUncompressedData; public Action<IntPtr, int> OnUncompressedData;
@ -77,15 +76,12 @@ namespace Facepunch.Steamworks
ReadCompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize ); ReadCompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize );
ReadUncompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize ); ReadUncompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize );
UncompressBuffer = Marshal.AllocHGlobal( UncompressBufferSize );
} }
public void Dispose() public void Dispose()
{ {
Marshal.FreeHGlobal( ReadCompressedBuffer ); Marshal.FreeHGlobal( ReadCompressedBuffer );
Marshal.FreeHGlobal( ReadUncompressedBuffer ); Marshal.FreeHGlobal( ReadUncompressedBuffer );
Marshal.FreeHGlobal( UncompressBuffer );
} }
/// <summary> /// <summary>
@ -154,24 +150,22 @@ namespace Facepunch.Steamworks
samepleRate = OptimalSampleRate; samepleRate = OptimalSampleRate;
uint bytesOut = 0; uint bytesOut = 0;
var result = client.native.user.DecompressVoice( (IntPtr)( ((byte*)input) + inputoffset ), (uint) inputsize, UncompressBuffer, UncompressBufferSize, out bytesOut, samepleRate );
if ( bytesOut > 0 ) SteamNative.VoiceResult result = SteamNative.VoiceResult.NoData;
output.SetLength( bytesOut );
if ( result == SteamNative.VoiceResult.OK ) fixed ( byte* outBuf = UncompressBuffer )
{ {
if ( output.Capacity < bytesOut ) result = client.native.user.DecompressVoice( (IntPtr)(((byte*)input) + inputoffset), (uint)inputsize, (IntPtr)outBuf, (uint)UncompressBuffer.Length, out bytesOut, samepleRate );
output.Capacity = (int) bytesOut; }
if ( result == SteamNative.VoiceResult.OK )
{
output.Write( (byte[])UncompressBuffer, 0, (int)bytesOut );
output.SetLength( bytesOut );
Marshal.Copy( UncompressBuffer, output.GetBuffer(), 0, (int) bytesOut );
return true; return true;
} }
return false; return false;
} }
} }
} }