Allow the voice thread to take longer sleeps when possible.

This commit is contained in:
RogueException
2015-10-17 03:38:42 -03:00
parent 04021bc2e6
commit aced598aad

View File

@@ -18,7 +18,8 @@ namespace Discord.WebSockets.Voice
{
internal partial class VoiceWebSocket : WebSocket
{
private const int MaxOpusSize = 4000;
private const int MaxOpusSize = 4000; //Max size of a single 20ms Opus frame
private const double SpinLockMilliseconds = 3.0; //If we're going to send audio in the next X milliseconds, dont use Task.Delay or Thread.Sleep
private const string EncryptedMode = "xsalsa20_poly1305";
private const string UnencryptedMode = "plain";
@@ -350,8 +351,8 @@ namespace Discord.WebSockets.Voice
uint timestamp = 0;
double nextTicks = 0.0;
double ticksPerMillisecond = Stopwatch.Frequency / 1000.0;
double spinLockThreshold = SpinLockMilliseconds * ticksPerMillisecond;
double ticksPerFrame = ticksPerMillisecond * _encoder.FrameLength;
double spinLockThreshold = 3 * ticksPerMillisecond;
uint samplesPerFrame = (uint)_encoder.SamplesPerFrame;
Stopwatch sw = Stopwatch.StartNew();
@@ -427,14 +428,27 @@ namespace Discord.WebSockets.Voice
}
}
}
//Dont sleep for 1 millisecond if we need to output audio in the next 1.5
else if (_sendQueue.Count == 0 || ticksToNextFrame >= spinLockThreshold)
//Dont sleep if we need to output audio in the next spinLockThreshold
else if (ticksToNextFrame > spinLockThreshold)
{
int time = (int)Math.Ceiling((ticksToNextFrame - spinLockThreshold) / ticksPerMillisecond);
#if USE_THREAD
Thread.Sleep(1);
Thread.Sleep(time);
#else
await Task.Delay(1).ConfigureAwait(false);
await Task.Delay(time).ConfigureAwait(false);
#endif
}
//Don't spinlock if we're not actually sending audio (or buffer underrunning)
else if (_sendQueue.Count == 0)
{
int time = (int)Math.Ceiling(ticksToNextFrame / ticksPerMillisecond);
#if USE_THREAD
Thread.Sleep(time);
#else
await Task.Delay(time).ConfigureAwait(false);
#endif
}
}
}
catch (OperationCanceledException) { }
catch (InvalidOperationException) { } //Includes ObjectDisposedException