Don't require unsafe to use voice

This commit is contained in:
Rohan Singh 2017-10-26 09:35:09 -04:00
parent a3a4c87ede
commit 60933de240
3 changed files with 37 additions and 27 deletions

View File

@ -21,17 +21,17 @@ public void GetVoice()
int unCompressed = 0; int unCompressed = 0;
int compressed = 0; int compressed = 0;
client.Voice.OnCompressedData = ( ptr, length ) => client.Voice.OnCompressedData = ( buffer, length ) =>
{ {
compressed += length; compressed += length;
if ( !client.Voice.Decompress( ptr, 0, length, decompressStream ) ) if ( !client.Voice.Decompress( buffer, length, decompressStream ) )
{ {
Assert.Fail( "Decompress returned false" ); Assert.Fail( "Decompress returned false" );
} }
}; };
client.Voice.OnUncompressedData = ( ptr, length ) => client.Voice.OnUncompressedData = ( buffer, length ) =>
{ {
unCompressed += length; unCompressed += length;
}; };
@ -62,7 +62,7 @@ public void CompressedOnly()
{ {
int compressed = 0; int compressed = 0;
client.Voice.OnCompressedData = ( ptr, length ) => client.Voice.OnCompressedData = ( buffer, length ) =>
{ {
compressed += length; compressed += length;
}; };
@ -89,7 +89,7 @@ public void UnCompressedOnly()
{ {
int unCompressed = 0; int unCompressed = 0;
client.Voice.OnUncompressedData = ( ptr, length ) => client.Voice.OnUncompressedData = ( buffer, length ) =>
{ {
unCompressed += length; unCompressed += length;
}; };

View File

@ -158,7 +158,6 @@ public override void Dispose()
{ {
if ( Voice != null ) if ( Voice != null )
{ {
Voice.Dispose();
Voice = null; Voice = null;
} }

View File

@ -7,19 +7,19 @@
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
public class Voice : IDisposable public class Voice
{ {
const int ReadBufferSize = 1024 * 128; const int ReadBufferSize = 1024 * 128;
internal Client client; internal Client client;
internal IntPtr ReadCompressedBuffer; internal byte[] ReadCompressedBuffer = new byte[ReadBufferSize];
internal IntPtr ReadUncompressedBuffer; internal byte[] ReadUncompressedBuffer = new byte[ReadBufferSize];
internal byte[] UncompressBuffer = new byte[1024 * 256]; internal byte[] UncompressBuffer = new byte[1024 * 256];
public Action<IntPtr, int> OnCompressedData; public Action<byte[], int> OnCompressedData;
public Action<IntPtr, int> OnUncompressedData; public Action<byte[], int> OnUncompressedData;
private System.Diagnostics.Stopwatch UpdateTimer = System.Diagnostics.Stopwatch.StartNew(); private System.Diagnostics.Stopwatch UpdateTimer = System.Diagnostics.Stopwatch.StartNew();
@ -73,21 +73,12 @@ public bool WantsRecording
internal Voice( Client client ) internal Voice( Client client )
{ {
this.client = client; this.client = client;
ReadCompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize );
ReadUncompressedBuffer = Marshal.AllocHGlobal( ReadBufferSize );
}
public void Dispose()
{
Marshal.FreeHGlobal( ReadCompressedBuffer );
Marshal.FreeHGlobal( ReadUncompressedBuffer );
} }
/// <summary> /// <summary>
/// This gets called inside Update - so there's no need to call this manually if you're calling update /// This gets called inside Update - so there's no need to call this manually if you're calling update
/// </summary> /// </summary>
public void Update() public unsafe void Update()
{ {
if ( OnCompressedData == null && OnUncompressedData == null ) if ( OnCompressedData == null && OnUncompressedData == null )
return; return;
@ -109,9 +100,13 @@ public void Update()
return; return;
} }
result = client.native.user.GetVoice( OnCompressedData != null, ReadCompressedBuffer, ReadBufferSize, out bufferCompressedLastWrite, fixed (byte* compressedPtr = ReadCompressedBuffer)
OnUncompressedData != null, (IntPtr) ReadUncompressedBuffer, ReadBufferSize, out bufferRegularLastWrite, fixed (byte* uncompressedPtr = ReadUncompressedBuffer)
DesiredSampleRate == 0 ? OptimalSampleRate : DesiredSampleRate ); {
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; 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 ) 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 ) if ( samepleRate == 0 )
samepleRate = OptimalSampleRate; samepleRate = OptimalSampleRate;