From bdc95473ed0a2bf9afb476e5187393a5a26c7040 Mon Sep 17 00:00:00 2001 From: RogueException Date: Thu, 21 Jul 2016 18:22:13 -0300 Subject: [PATCH] Share a buffer between Sodium and Opus --- src/Discord.Net/Audio/Opus/OpusEncoder.cs | 1 - src/Discord.Net/Audio/Streams/OpusEncodeStream.cs | 6 ++--- src/Discord.Net/Audio/Streams/RTPWriteStream.cs | 33 ++++++++++++----------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/Discord.Net/Audio/Opus/OpusEncoder.cs b/src/Discord.Net/Audio/Opus/OpusEncoder.cs index 447613d1c..c1eb3843d 100644 --- a/src/Discord.Net/Audio/Opus/OpusEncoder.cs +++ b/src/Discord.Net/Audio/Opus/OpusEncoder.cs @@ -28,7 +28,6 @@ namespace Discord.Audio throw new Exception($"Opus Error: {error}"); } - /// Produces Opus encoded audio from PCM samples. /// PCM samples to encode. /// Buffer to store the encoded frame. diff --git a/src/Discord.Net/Audio/Streams/OpusEncodeStream.cs b/src/Discord.Net/Audio/Streams/OpusEncodeStream.cs index a6b3fbd66..ef773ca56 100644 --- a/src/Discord.Net/Audio/Streams/OpusEncodeStream.cs +++ b/src/Discord.Net/Audio/Streams/OpusEncodeStream.cs @@ -4,15 +4,13 @@ { public int SampleRate = 48000; public int Channels = 2; - - private readonly byte[] _buffer; + private readonly OpusEncoder _encoder; internal OpusEncodeStream(AudioClient audioClient, byte[] secretKey, int samplesPerFrame, uint ssrc, int? bitrate = null, OpusApplication application = OpusApplication.MusicOrMixed, int bufferSize = 4000) - : base(audioClient, secretKey, samplesPerFrame, ssrc) + : base(audioClient, secretKey, samplesPerFrame, ssrc, bufferSize) { - _buffer = new byte[bufferSize]; _encoder = new OpusEncoder(SampleRate, Channels); _encoder.SetForwardErrorCorrection(true); diff --git a/src/Discord.Net/Audio/Streams/RTPWriteStream.cs b/src/Discord.Net/Audio/Streams/RTPWriteStream.cs index 8cdd9dc38..d547f021a 100644 --- a/src/Discord.Net/Audio/Streams/RTPWriteStream.cs +++ b/src/Discord.Net/Audio/Streams/RTPWriteStream.cs @@ -6,47 +6,48 @@ namespace Discord.Audio public class RTPWriteStream : Stream { private readonly AudioClient _audioClient; - private readonly byte[] _buffer, _nonce, _secretKey; + private readonly byte[] _nonce, _secretKey; private int _samplesPerFrame; private uint _ssrc, _timestamp = 0; + protected readonly byte[] _buffer; + public override bool CanRead => false; public override bool CanSeek => false; public override bool CanWrite => true; - internal RTPWriteStream(AudioClient audioClient, byte[] secretKey, int samplesPerFrame, uint ssrc, int bufferSize = 4000) { _audioClient = audioClient; _secretKey = secretKey; _samplesPerFrame = samplesPerFrame; _ssrc = ssrc; - _nonce = new byte[24]; _buffer = new byte[bufferSize]; - _buffer[0] = 0x80; - _buffer[1] = 0x78; - _buffer[8] = (byte)(_ssrc >> 24); - _buffer[9] = (byte)(_ssrc >> 16); - _buffer[10] = (byte)(_ssrc >> 8); - _buffer[11] = (byte)(_ssrc >> 0); + _nonce = new byte[24]; + _nonce[0] = 0x80; + _nonce[1] = 0x78; + _nonce[8] = (byte)(_ssrc >> 24); + _nonce[9] = (byte)(_ssrc >> 16); + _nonce[10] = (byte)(_ssrc >> 8); + _nonce[11] = (byte)(_ssrc >> 0); } public override void Write(byte[] buffer, int offset, int count) { unchecked { - if (_buffer[3]++ == byte.MaxValue) - _buffer[2]++; + if (_nonce[3]++ == byte.MaxValue) + _nonce[2]++; _timestamp += (uint)_samplesPerFrame; - _buffer[4] = (byte)(_timestamp >> 24); - _buffer[5] = (byte)(_timestamp >> 16); - _buffer[6] = (byte)(_timestamp >> 8); - _buffer[7] = (byte)(_timestamp >> 0); + _nonce[4] = (byte)(_timestamp >> 24); + _nonce[5] = (byte)(_timestamp >> 16); + _nonce[6] = (byte)(_timestamp >> 8); + _nonce[7] = (byte)(_timestamp >> 0); } - Buffer.BlockCopy(_buffer, 0, _nonce, 0, 12); //Copy the 12-byte header to be used for nonce count = SecretBox.Encrypt(buffer, offset, count, _buffer, 12, _nonce, _secretKey); + Buffer.BlockCopy(_nonce, 0, _buffer, 0, 12); //Copy the RTP header from nonce to buffer _audioClient.Send(_buffer, count + 12); }