diff --git a/src/Discord.Net.WebSocket/Audio/AudioClient.cs b/src/Discord.Net.WebSocket/Audio/AudioClient.cs index 7645604df..e2586d0f3 100644 --- a/src/Discord.Net.WebSocket/Audio/AudioClient.cs +++ b/src/Discord.Net.WebSocket/Audio/AudioClient.cs @@ -253,7 +253,7 @@ namespace Discord.Audio private async Task RunHeartbeatAsync(int intervalMillis, CancellationToken cancelToken) { - //Clean this up when Discord's session patch is live + //TODO: Clean this up when Discord's session patch is live try { await _audioLogger.DebugAsync("Heartbeat Started").ConfigureAwait(false); @@ -261,17 +261,15 @@ namespace Discord.Audio { var now = Environment.TickCount; - //Did server respond to our last heartbeat, or are we still receiving messages (long load?) + //Did server respond to our last heartbeat? if (_heartbeatTimes.Count != 0 && (now - _lastMessageTime) > intervalMillis && ConnectionState == ConnectionState.Connected) { _connection.Error(new Exception("Server missed last heartbeat")); return; } - _heartbeatTimes.Enqueue(now); - - await Task.Delay(intervalMillis, cancelToken).ConfigureAwait(false); + _heartbeatTimes.Enqueue(now); try { await ApiClient.SendHeartbeatAsync().ConfigureAwait(false); diff --git a/src/Discord.Net.WebSocket/Audio/Opus/OpusEncoder.cs b/src/Discord.Net.WebSocket/Audio/Opus/OpusEncoder.cs index ca87c5fc6..1f0b35d77 100644 --- a/src/Discord.Net.WebSocket/Audio/Opus/OpusEncoder.cs +++ b/src/Discord.Net.WebSocket/Audio/Opus/OpusEncoder.cs @@ -68,9 +68,12 @@ namespace Discord.Audio if (result < 0) throw new Exception($"Opus Error: {(OpusError)result}"); - /*result = EncoderCtl(_ptr, OpusCtl.SetBandwidth, 1105); - if (result < 0) - throw new Exception($"Opus Error: {(OpusError)result}");*/ + /*if (application == AudioApplication.Music) + { + result = EncoderCtl(_ptr, OpusCtl.SetBandwidth, 1105); + if (result < 0) + throw new Exception($"Opus Error: {(OpusError)result}"); + }*/ } /// Produces Opus encoded audio from PCM samples. diff --git a/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs b/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs index 0eccd1a4c..dcd053cc1 100644 --- a/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs +++ b/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs @@ -38,7 +38,7 @@ namespace Discord.Audio.Streams : this(next, samplesPerFrame, bufferMillis, cancelToken, null, maxFrameSize) { } internal BufferedWriteStream(AudioOutStream next, int samplesPerFrame, int bufferMillis, CancellationToken cancelToken, Logger logger, int maxFrameSize = 1500) { - //maxFrameSize = 1275 was too limiting at 128*1024 + //maxFrameSize = 1275 was too limiting at 128kbps,2ch,60ms _next = next; _ticksPerFrame = samplesPerFrame / 48; _logger = logger; @@ -57,11 +57,11 @@ namespace Discord.Audio.Streams private Task Run() { -#if DEBUG - uint num = 0; -#endif return Task.Run(async () => { +#if DEBUG + uint num = 0; +#endif try { while (!_isPreloaded && !_cancelToken.IsCancellationRequested) @@ -70,10 +70,9 @@ namespace Discord.Audio.Streams long nextTick = Environment.TickCount; while (!_cancelToken.IsCancellationRequested) { - const int limit = 1; long tick = Environment.TickCount; long dist = nextTick - tick; - if (dist <= limit) + if (dist <= 0) { Frame frame; if (_queuedFrames.TryDequeue(out frame)) @@ -86,17 +85,20 @@ namespace Discord.Audio.Streams var _ = _logger.DebugAsync($"{num++}: Sent {frame.Bytes} bytes ({_queuedFrames.Count} frames buffered)"); #endif } - else if (dist == 0) + else { - await _next.WriteAsync(_silenceFrame, 0, _silenceFrame.Length).ConfigureAwait(false); - nextTick += _ticksPerFrame; + while ((nextTick - tick) <= 0) + { + await _next.WriteAsync(_silenceFrame, 0, _silenceFrame.Length).ConfigureAwait(false); + nextTick += _ticksPerFrame; + } #if DEBUG var _ = _logger.DebugAsync($"{num++}: Buffer underrun"); #endif } } else - await Task.Delay((int)(dist - (limit - 1))/*, _cancelToken*/).ConfigureAwait(false); + await Task.Delay((int)(dist)/*, _cancelToken*/).ConfigureAwait(false); } } catch (OperationCanceledException) { } diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 52d79a04b..66c25e5f6 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1556,8 +1556,8 @@ namespace Discord.WebSocket return; } } - _heartbeatTimes.Enqueue(now); + _heartbeatTimes.Enqueue(now); try { await ApiClient.SendHeartbeatAsync(_lastSeq).ConfigureAwait(false);