From 60933de24019a0680428b4a66a44241f46011323 Mon Sep 17 00:00:00 2001 From: Rohan Singh Date: Thu, 26 Oct 2017 09:35:09 -0400 Subject: [PATCH] Don't require unsafe to use voice --- Facepunch.Steamworks.Test/Client/Voice.cs | 10 ++--- Facepunch.Steamworks/Client.cs | 1 - Facepunch.Steamworks/Client/Voice.cs | 53 ++++++++++++++--------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Facepunch.Steamworks.Test/Client/Voice.cs b/Facepunch.Steamworks.Test/Client/Voice.cs index 46c4c8a..a894179 100644 --- a/Facepunch.Steamworks.Test/Client/Voice.cs +++ b/Facepunch.Steamworks.Test/Client/Voice.cs @@ -21,17 +21,17 @@ public void GetVoice() int unCompressed = 0; int compressed = 0; - client.Voice.OnCompressedData = ( ptr, length ) => + client.Voice.OnCompressedData = ( buffer, length ) => { compressed += length; - if ( !client.Voice.Decompress( ptr, 0, length, decompressStream ) ) + if ( !client.Voice.Decompress( buffer, length, decompressStream ) ) { Assert.Fail( "Decompress returned false" ); } }; - client.Voice.OnUncompressedData = ( ptr, length ) => + client.Voice.OnUncompressedData = ( buffer, length ) => { unCompressed += length; }; @@ -62,7 +62,7 @@ public void CompressedOnly() { int compressed = 0; - client.Voice.OnCompressedData = ( ptr, length ) => + client.Voice.OnCompressedData = ( buffer, length ) => { compressed += length; }; @@ -89,7 +89,7 @@ public void UnCompressedOnly() { int unCompressed = 0; - client.Voice.OnUncompressedData = ( ptr, length ) => + client.Voice.OnUncompressedData = ( buffer, length ) => { unCompressed += length; }; diff --git a/Facepunch.Steamworks/Client.cs b/Facepunch.Steamworks/Client.cs index d8c5490..0a4bbb6 100644 --- a/Facepunch.Steamworks/Client.cs +++ b/Facepunch.Steamworks/Client.cs @@ -158,7 +158,6 @@ public override void Dispose() { if ( Voice != null ) { - Voice.Dispose(); Voice = null; } diff --git a/Facepunch.Steamworks/Client/Voice.cs b/Facepunch.Steamworks/Client/Voice.cs index c4e5846..fa0cf68 100644 --- a/Facepunch.Steamworks/Client/Voice.cs +++ b/Facepunch.Steamworks/Client/Voice.cs @@ -7,19 +7,19 @@ namespace Facepunch.Steamworks { - public class Voice : IDisposable + public class Voice { const int ReadBufferSize = 1024 * 128; internal Client client; - internal IntPtr ReadCompressedBuffer; - internal IntPtr ReadUncompressedBuffer; + internal byte[] ReadCompressedBuffer = new byte[ReadBufferSize]; + internal byte[] ReadUncompressedBuffer = new byte[ReadBufferSize]; internal byte[] UncompressBuffer = new byte[1024 * 256]; - public Action OnCompressedData; - public Action OnUncompressedData; + public Action OnCompressedData; + public Action OnUncompressedData; private System.Diagnostics.Stopwatch UpdateTimer = System.Diagnostics.Stopwatch.StartNew(); @@ -73,21 +73,12 @@ public bool WantsRecording internal Voice( Client client ) { this.client = client; - - ReadCompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize ); - ReadUncompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize ); - } - - public void Dispose() - { - Marshal.FreeHGlobal( ReadCompressedBuffer ); - Marshal.FreeHGlobal( ReadUncompressedBuffer ); } /// /// This gets called inside Update - so there's no need to call this manually if you're calling update /// - public void Update() + public unsafe void Update() { if ( OnCompressedData == null && OnUncompressedData == null ) return; @@ -109,9 +100,13 @@ public void Update() return; } - result = client.native.user.GetVoice( OnCompressedData != null, ReadCompressedBuffer, ReadBufferSize, out bufferCompressedLastWrite, - OnUncompressedData != null, (IntPtr) ReadUncompressedBuffer, ReadBufferSize, out bufferRegularLastWrite, - DesiredSampleRate == 0 ? OptimalSampleRate : DesiredSampleRate ); + fixed (byte* compressedPtr = ReadCompressedBuffer) + fixed (byte* uncompressedPtr = ReadUncompressedBuffer) + { + result = client.native.user.GetVoice( OnCompressedData != null, (IntPtr) compressedPtr, ReadBufferSize, out bufferCompressedLastWrite, + OnUncompressedData != null, (IntPtr) uncompressedPtr, ReadBufferSize, out bufferRegularLastWrite, + DesiredSampleRate == 0 ? OptimalSampleRate : DesiredSampleRate ); + } IsRecording = true; @@ -136,15 +131,31 @@ public void Update() } - public unsafe bool Decompress( byte[] input, MemoryStream output, uint samepleRate = 0 ) + public bool Decompress( byte[] input, MemoryStream output, uint samepleRate = 0 ) { + return Decompress( input, 0, input.Length, output, samepleRate ); + } + + public bool Decompress( byte[] input, int inputsize, MemoryStream output, uint samepleRate = 0 ) + { + return Decompress( input, 0, inputsize, output, samepleRate ); + } + + public unsafe bool Decompress( byte[] input, int inputoffset, int inputsize, MemoryStream output, uint samepleRate = 0 ) + { + if ( inputoffset < 0 || inputoffset >= input.Length ) + throw new ArgumentOutOfRangeException( "inputoffset" ); + + if ( inputsize <= 0 || inputoffset + inputsize > input.Length ) + throw new ArgumentOutOfRangeException( "inputsize" ); + fixed ( byte* p = input ) { - return Decompress( (IntPtr)p, 0, input.Length, output, samepleRate ); + return Decompress( (IntPtr)p, inputoffset, inputsize, output, samepleRate ); } } - public unsafe bool Decompress( IntPtr input, int inputoffset, int inputsize, MemoryStream output, uint samepleRate = 0 ) + private unsafe bool Decompress( IntPtr input, int inputoffset, int inputsize, MemoryStream output, uint samepleRate = 0 ) { if ( samepleRate == 0 ) samepleRate = OptimalSampleRate;