Browse Source

Unified Opus and Sodium errors

tags/1.0-rc
RogueException 9 years ago
parent
commit
b2f39f888e
3 changed files with 22 additions and 12 deletions
  1. +2
    -2
      src/Discord.Net/Audio/Opus/OpusDecoder.cs
  2. +4
    -5
      src/Discord.Net/Audio/Opus/OpusEncoder.cs
  3. +16
    -5
      src/Discord.Net/Audio/Sodium/SecretBox.cs

+ 2
- 2
src/Discord.Net/Audio/Opus/OpusDecoder.cs View File

@@ -18,7 +18,7 @@ namespace Discord.Audio
OpusError error;
_ptr = CreateDecoder(samplingRate, channels, out error);
if (error != OpusError.OK)
throw new InvalidOperationException($"Error occured while creating decoder: {error}");
throw new Exception($"Opus Error: {error}");
}
/// <summary> Produces PCM samples from Opus-encoded audio. </summary>
@@ -33,7 +33,7 @@ namespace Discord.Audio
result = Decode(_ptr, inPtr + inputOffset, inputCount, outPtr + outputOffset, (output.Length - outputOffset) / SampleSize / MaxChannels, 0);

if (result < 0)
throw new Exception(((OpusError)result).ToString());
throw new Exception($"Opus Error: {(OpusError)result}");
return result;
}



+ 4
- 5
src/Discord.Net/Audio/Opus/OpusEncoder.cs View File

@@ -25,13 +25,12 @@ namespace Discord.Audio
OpusError error;
_ptr = CreateEncoder(samplingRate, channels, (int)application, out error);
if (error != OpusError.OK)
throw new InvalidOperationException($"Error occured while creating encoder: {error}");
throw new Exception($"Opus Error: {error}");
}


/// <summary> Produces Opus encoded audio from PCM samples. </summary>
/// <param name="input">PCM samples to encode.</param>
/// <param name="inputOffset">Offset of the frame in pcmSamples.</param>
/// <param name="output">Buffer to store the encoded frame.</param>
/// <returns>Length of the frame contained in outputBuffer.</returns>
public unsafe int EncodeFrame(byte[] input, int inputOffset, int inputCount, byte[] output, int outputOffset)
@@ -42,7 +41,7 @@ namespace Discord.Audio
result = Encode(_ptr, inPtr + inputOffset, inputCount / SampleSize, outPtr + outputOffset, output.Length - outputOffset);

if (result < 0)
throw new Exception(((OpusError)result).ToString());
throw new Exception($"Opus Error: {(OpusError)result}");
return result;
}

@@ -51,7 +50,7 @@ namespace Discord.Audio
{
var result = EncoderCtl(_ptr, OpusCtl.SetInbandFECRequest, value ? 1 : 0);
if (result < 0)
throw new Exception(((OpusError)result).ToString());
throw new Exception($"Opus Error: {(OpusError)result}");
}

/// <summary> Gets or sets whether Forward Error Correction is enabled. </summary>
@@ -62,7 +61,7 @@ namespace Discord.Audio

var result = EncoderCtl(_ptr, OpusCtl.SetBitrateRequest, value * 1000);
if (result < 0)
throw new Exception(((OpusError)result).ToString());
throw new Exception($"Opus Error: {(OpusError)result}");
}

protected override void Dispose(bool disposing)


+ 16
- 5
src/Discord.Net/Audio/Sodium/SecretBox.cs View File

@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System;
using System.Runtime.InteropServices;

namespace Discord.Audio
{
@@ -9,17 +10,27 @@ namespace Discord.Audio
[DllImport("libsodium", EntryPoint = "crypto_secretbox_open_easy", CallingConvention = CallingConvention.Cdecl)]
private static extern int SecretBoxOpenEasy(byte* output, byte* input, long inputLength, byte[] nonce, byte[] secret);

public static int Encrypt(byte[] input, int inputOffset, long inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret)
public static int Encrypt(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret)
{
fixed (byte* inPtr = input)
fixed (byte* outPtr = output)
return SecretBoxEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret);
{
int error = SecretBoxEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret);
if (error != 0)
throw new Exception($"Sodium Error: {error}");
return inputLength + 16;
}
}
public static int Decrypt(byte[] input, int inputOffset, long inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret)
public static int Decrypt(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] nonce, byte[] secret)
{
fixed (byte* inPtr = input)
fixed (byte* outPtr = output)
return SecretBoxOpenEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret);
{
int error = SecretBoxOpenEasy(outPtr + outputOffset, inPtr + inputOffset, inputLength, nonce, secret);
if (error != 0)
throw new Exception($"Sodium Error: {error}");
return inputLength - 16;
}
}
}
}

Loading…
Cancel
Save