From a956a1005c411b9909e2e57295f39fe3c1f59a9e Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Wed, 5 Apr 2017 16:55:40 +0100 Subject: [PATCH] Voice buffer fix (netcore) --- Facepunch.Steamworks/Client/Voice.cs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/Facepunch.Steamworks/Client/Voice.cs b/Facepunch.Steamworks/Client/Voice.cs index c0ea64d..c4e5846 100644 --- a/Facepunch.Steamworks/Client/Voice.cs +++ b/Facepunch.Steamworks/Client/Voice.cs @@ -10,14 +10,13 @@ namespace Facepunch.Steamworks public class Voice : IDisposable { const int ReadBufferSize = 1024 * 128; - const int UncompressBufferSize = 1024 * 256; internal Client client; internal IntPtr ReadCompressedBuffer; internal IntPtr ReadUncompressedBuffer; - internal IntPtr UncompressBuffer; + internal byte[] UncompressBuffer = new byte[1024 * 256]; public Action OnCompressedData; public Action OnUncompressedData; @@ -77,15 +76,12 @@ namespace Facepunch.Steamworks ReadCompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize ); ReadUncompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize ); - UncompressBuffer = Marshal.AllocHGlobal( UncompressBufferSize ); } public void Dispose() { Marshal.FreeHGlobal( ReadCompressedBuffer ); Marshal.FreeHGlobal( ReadUncompressedBuffer ); - - Marshal.FreeHGlobal( UncompressBuffer ); } /// @@ -154,24 +150,22 @@ namespace Facepunch.Steamworks samepleRate = OptimalSampleRate; uint bytesOut = 0; - var result = client.native.user.DecompressVoice( (IntPtr)( ((byte*)input) + inputoffset ), (uint) inputsize, UncompressBuffer, UncompressBufferSize, out bytesOut, samepleRate ); - if ( bytesOut > 0 ) - output.SetLength( bytesOut ); + SteamNative.VoiceResult result = SteamNative.VoiceResult.NoData; - if ( result == SteamNative.VoiceResult.OK ) + fixed ( byte* outBuf = UncompressBuffer ) { - if ( output.Capacity < bytesOut ) - output.Capacity = (int) bytesOut; + result = client.native.user.DecompressVoice( (IntPtr)(((byte*)input) + inputoffset), (uint)inputsize, (IntPtr)outBuf, (uint)UncompressBuffer.Length, out bytesOut, samepleRate ); + } + + 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 false; - } } }