Browse Source

Exposed VoiceWebSocket.VoiceBuffer

tags/docs-0.9
RogueException 9 years ago
parent
commit
29eae77c19
2 changed files with 18 additions and 12 deletions
  1. +11
    -6
      src/Discord.Net/WebSockets/Voice/VoiceBuffer.cs
  2. +7
    -6
      src/Discord.Net/WebSockets/Voice/VoiceWebSocket.cs

+ 11
- 6
src/Discord.Net/WebSockets/Voice/VoiceBuffer.cs View File

@@ -4,7 +4,12 @@ using System.Threading;
namespace Discord.WebSockets.Voice namespace Discord.WebSockets.Voice
{ {
public class VoiceBuffer public class VoiceBuffer
{
{
public int FrameSize => _frameSize;
public int FrameCount => _frameCount;
public ushort ReadPos => _readCursor;
public ushort WritePos => _readCursor;

private readonly int _frameSize, _frameCount, _bufferSize; private readonly int _frameSize, _frameCount, _bufferSize;
private readonly byte[] _buffer; private readonly byte[] _buffer;
private readonly byte[] _blankFrame; private readonly byte[] _blankFrame;
@@ -12,7 +17,7 @@ namespace Discord.WebSockets.Voice
private ManualResetEventSlim _underflowEvent, _notOverflowEvent; private ManualResetEventSlim _underflowEvent, _notOverflowEvent;
private bool _isClearing; private bool _isClearing;


public VoiceBuffer(int frameCount, int frameSize)
internal VoiceBuffer(int frameCount, int frameSize)
{ {
_frameSize = frameSize; _frameSize = frameSize;
_frameCount = frameCount; _frameCount = frameCount;
@@ -25,7 +30,7 @@ namespace Discord.WebSockets.Voice
_notOverflowEvent = new ManualResetEventSlim(); //Notifies when an overflow is solved _notOverflowEvent = new ManualResetEventSlim(); //Notifies when an overflow is solved
} }


public void Push(byte[] buffer, int bytes, CancellationToken cancelToken)
internal void Push(byte[] buffer, int bytes, CancellationToken cancelToken)
{ {
int wholeFrames = bytes / _frameSize; int wholeFrames = bytes / _frameSize;
int expectedBytes = wholeFrames * _frameSize; int expectedBytes = wholeFrames * _frameSize;
@@ -69,7 +74,7 @@ namespace Discord.WebSockets.Voice
} }
} }


public bool Pop(byte[] buffer)
internal bool Pop(byte[] buffer)
{ {
if (_writeCursor == _readCursor) if (_writeCursor == _readCursor)
{ {
@@ -88,7 +93,7 @@ namespace Discord.WebSockets.Voice
return !isClearing; return !isClearing;
} }


public void Clear(CancellationToken cancelToken)
internal void Clear(CancellationToken cancelToken)
{ {
lock (this) lock (this)
{ {
@@ -102,7 +107,7 @@ namespace Discord.WebSockets.Voice
} }
} }


public void Wait(CancellationToken cancelToken)
internal void Wait(CancellationToken cancelToken)
{ {
_underflowEvent.Wait(cancelToken); _underflowEvent.Wait(cancelToken);
} }


+ 7
- 6
src/Discord.Net/WebSockets/Voice/VoiceWebSocket.cs View File

@@ -29,7 +29,7 @@ namespace Discord.WebSockets.Voice
private uint _ssrc; private uint _ssrc;
private ConcurrentDictionary<uint, string> _ssrcMapping; private ConcurrentDictionary<uint, string> _ssrcMapping;


private VoiceBuffer _sendQueue;
private VoiceBuffer _sendBuffer;
private UdpClient _udp; private UdpClient _udp;
private IPEndPoint _endpoint; private IPEndPoint _endpoint;
private bool _isEncrypted; private bool _isEncrypted;
@@ -43,6 +43,7 @@ namespace Discord.WebSockets.Voice


public string CurrentServerId => _serverId; public string CurrentServerId => _serverId;
public string CurrentChannelId => _channelId; public string CurrentChannelId => _channelId;
public VoiceBuffer OutputBuffer => _sendBuffer;


public VoiceWebSocket(DiscordSimpleClient client) public VoiceWebSocket(DiscordSimpleClient client)
: base(client) : base(client)
@@ -53,7 +54,7 @@ namespace Discord.WebSockets.Voice
_encodingBuffer = new byte[MaxOpusSize]; _encodingBuffer = new byte[MaxOpusSize];
_ssrcMapping = new ConcurrentDictionary<uint, string>(); _ssrcMapping = new ConcurrentDictionary<uint, string>();
_encoder = new OpusEncoder(48000, 1, 20, Opus.Application.Audio); _encoder = new OpusEncoder(48000, 1, 20, Opus.Application.Audio);
_sendQueue = new VoiceBuffer((int)Math.Ceiling(client.Config.VoiceBufferLength / (double)_encoder.FrameLength), _encoder.FrameSize);
_sendBuffer = new VoiceBuffer((int)Math.Ceiling(client.Config.VoiceBufferLength / (double)_encoder.FrameLength), _encoder.FrameSize);
} }


public Task SetChannel(string serverId, string channelId) public Task SetChannel(string serverId, string channelId)
@@ -378,7 +379,7 @@ namespace Discord.WebSockets.Voice
{ {
while (sw.ElapsedTicks > nextTicks) while (sw.ElapsedTicks > nextTicks)
{ {
if (_sendQueue.Pop(frame))
if (_sendBuffer.Pop(frame))
{ {
ushort sequence = unchecked(_sequence++); ushort sequence = unchecked(_sequence++);
udpPacket[2] = (byte)((sequence >> 8) & 0xFF); udpPacket[2] = (byte)((sequence >> 8) & 0xFF);
@@ -518,11 +519,11 @@ namespace Discord.WebSockets.Voice


public void SendPCMFrames(byte[] data, int bytes) public void SendPCMFrames(byte[] data, int bytes)
{ {
_sendQueue.Push(data, bytes, _cancelToken);
_sendBuffer.Push(data, bytes, _cancelToken);
} }
public void ClearPCMFrames() public void ClearPCMFrames()
{ {
_sendQueue.Clear(_cancelToken);
_sendBuffer.Clear(_cancelToken);
} }


private void SendIsTalking(bool value) private void SendIsTalking(bool value)
@@ -540,7 +541,7 @@ namespace Discord.WebSockets.Voice


public void WaitForQueue() public void WaitForQueue()
{ {
_sendQueue.Wait(_cancelToken);
_sendBuffer.Wait(_cancelToken);
} }
public Task WaitForConnection(int timeout) public Task WaitForConnection(int timeout)
{ {


Loading…
Cancel
Save