Added support for partial PCM frames

This commit is contained in:
RogueException
2016-12-30 16:02:01 -04:00
parent c38a786039
commit 3b72d48950
5 changed files with 49 additions and 28 deletions

View File

@@ -169,29 +169,29 @@ namespace Discord.Audio
await _disconnectedEvent.InvokeAsync(ex).ConfigureAwait(false);
}
public Stream CreateOpusStream(int samplesPerFrame, int bufferSize = 4000)
public Stream CreateOpusStream(int samplesPerFrame)
{
CheckSamplesPerFrame(samplesPerFrame);
var target = new BufferedAudioTarget(ApiClient, samplesPerFrame, _cancelTokenSource.Token);
return new RTPWriteStream(target, _secretKey, samplesPerFrame, _ssrc, bufferSize = 4000);
return new RTPWriteStream(target, _secretKey, samplesPerFrame, _ssrc);
}
public Stream CreateDirectOpusStream(int samplesPerFrame, int bufferSize = 4000)
public Stream CreateDirectOpusStream(int samplesPerFrame)
{
CheckSamplesPerFrame(samplesPerFrame);
var target = new DirectAudioTarget(ApiClient);
return new RTPWriteStream(target, _secretKey, samplesPerFrame, _ssrc, bufferSize = 4000);
return new RTPWriteStream(target, _secretKey, samplesPerFrame, _ssrc);
}
public Stream CreatePCMStream(int samplesPerFrame, int? bitrate = null, int bufferSize = 4000)
public Stream CreatePCMStream(int samplesPerFrame, int channels = 2, int? bitrate = null)
{
CheckSamplesPerFrame(samplesPerFrame);
var target = new BufferedAudioTarget(ApiClient, samplesPerFrame, _cancelTokenSource.Token);
return new OpusEncodeStream(target, _secretKey, samplesPerFrame, _ssrc, bitrate, bufferSize);
return new OpusEncodeStream(target, _secretKey, channels, samplesPerFrame, _ssrc, bitrate);
}
public Stream CreateDirectPCMStream(int samplesPerFrame, int? bitrate = null, int bufferSize = 4000)
public Stream CreateDirectPCMStream(int samplesPerFrame, int channels = 2, int? bitrate = null)
{
CheckSamplesPerFrame(samplesPerFrame);
var target = new DirectAudioTarget(ApiClient);
return new OpusEncodeStream(target, _secretKey, samplesPerFrame, _ssrc, bitrate, bufferSize);
return new OpusEncodeStream(target, _secretKey, channels, samplesPerFrame, _ssrc, bitrate);
}
private void CheckSamplesPerFrame(int samplesPerFrame)
{

View File

@@ -1,16 +1,22 @@
namespace Discord.Audio
using System;
namespace Discord.Audio
{
internal class OpusEncodeStream : RTPWriteStream
{
public int SampleRate = 48000;
public int Channels = 2;
public const int SampleRate = 48000;
private int _frameSize;
private byte[] _partialFrameBuffer;
private int _partialFramePos;
private readonly OpusEncoder _encoder;
internal OpusEncodeStream(IAudioTarget target, byte[] secretKey, int samplesPerFrame, uint ssrc, int? bitrate = null, int bufferSize = 4000)
: base(target, secretKey, samplesPerFrame, ssrc, bufferSize)
internal OpusEncodeStream(IAudioTarget target, byte[] secretKey, int channels, int samplesPerFrame, uint ssrc, int? bitrate = null)
: base(target, secretKey, samplesPerFrame, ssrc)
{
_encoder = new OpusEncoder(SampleRate, Channels);
_encoder = new OpusEncoder(SampleRate, channels);
_frameSize = samplesPerFrame * channels * 2;
_partialFrameBuffer = new byte[_frameSize];
_encoder.SetForwardErrorCorrection(true);
if (bitrate != null)
@@ -19,8 +25,27 @@
public override void Write(byte[] buffer, int offset, int count)
{
count = _encoder.EncodeFrame(buffer, offset, count, _buffer, 0);
base.Write(_buffer, 0, count);
//Assume threadsafe
while (count > 0)
{
if (_partialFramePos + count >= _frameSize)
{
int partialSize = _frameSize - _partialFramePos;
Buffer.BlockCopy(buffer, offset, _partialFrameBuffer, _partialFramePos, partialSize);
offset += partialSize;
count -= partialSize;
_partialFramePos = 0;
int encFrameSize = _encoder.EncodeFrame(_partialFrameBuffer, 0, _frameSize, _buffer, 0);
base.Write(_buffer, 0, encFrameSize);
}
else
{
Buffer.BlockCopy(buffer, offset, _partialFrameBuffer, _partialFramePos, count);
_partialFramePos += count;
break;
}
}
}
protected override void Dispose(bool disposing)

View File

@@ -16,13 +16,13 @@ namespace Discord.Audio
public override bool CanSeek => false;
public override bool CanWrite => true;
internal RTPWriteStream(IAudioTarget target, byte[] secretKey, int samplesPerFrame, uint ssrc, int bufferSize = 4000)
internal RTPWriteStream(IAudioTarget target, byte[] secretKey, int samplesPerFrame, uint ssrc)
{
_target = target;
_secretKey = secretKey;
_samplesPerFrame = samplesPerFrame;
_ssrc = ssrc;
_buffer = new byte[bufferSize];
_buffer = new byte[4000];
_nonce = new byte[24];
_nonce[0] = 0x80;
_nonce[1] = 0x78;

View File

@@ -75,4 +75,4 @@ namespace Discord.Audio
Dispose(true);
}
}
}
}