From e49122ea7e7371f22799714e1c69f4e3f4e86d78 Mon Sep 17 00:00:00 2001 From: RogueException Date: Mon, 3 Apr 2017 19:59:03 -0300 Subject: [PATCH] Automatically toggle speaking boolean --- src/Discord.Net.WebSocket/Audio/AudioClient.cs | 2 +- .../Audio/Streams/BufferedWriteStream.cs | 3 +++ .../Audio/Streams/OutputStream.cs | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.WebSocket/Audio/AudioClient.cs b/src/Discord.Net.WebSocket/Audio/AudioClient.cs index d5e9895a0..899951f60 100644 --- a/src/Discord.Net.WebSocket/Audio/AudioClient.cs +++ b/src/Discord.Net.WebSocket/Audio/AudioClient.cs @@ -239,7 +239,7 @@ namespace Discord.Audio throw new InvalidOperationException($"Discord selected an unexpected mode: {data.Mode}"); _secretKey = data.SecretKey; - await ApiClient.SendSetSpeaking(true).ConfigureAwait(false); + //await ApiClient.SendSetSpeaking(true).ConfigureAwait(false); var _ = _connection.CompleteAsync(); } diff --git a/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs b/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs index 5c402785e..4fc50b593 100644 --- a/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs +++ b/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs @@ -78,6 +78,7 @@ namespace Discord.Audio.Streams Frame frame; if (_queuedFrames.TryDequeue(out frame)) { + await _next.SetSpeakingAsync(true).ConfigureAwait(false); await _next.WriteAsync(frame.Buffer, 0, frame.Bytes).ConfigureAwait(false); _bufferPool.Enqueue(frame.Buffer); _queueLock.Release(); @@ -93,6 +94,8 @@ namespace Discord.Audio.Streams { if (_silenceFrames++ < MaxSilenceFrames) await _next.WriteAsync(_silenceFrame, 0, _silenceFrame.Length).ConfigureAwait(false); + else + await _next.SetSpeakingAsync(false).ConfigureAwait(false); nextTick += _ticksPerFrame; } #if DEBUG diff --git a/src/Discord.Net.WebSocket/Audio/Streams/OutputStream.cs b/src/Discord.Net.WebSocket/Audio/Streams/OutputStream.cs index 6238e93b4..14072317a 100644 --- a/src/Discord.Net.WebSocket/Audio/Streams/OutputStream.cs +++ b/src/Discord.Net.WebSocket/Audio/Streams/OutputStream.cs @@ -6,6 +6,8 @@ namespace Discord.Audio.Streams /// Wraps an IAudioClient, sending voice data on write. public class OutputStream : AudioOutStream { + private bool _isSpeaking; + private readonly DiscordVoiceAPIClient _client; public OutputStream(IAudioClient client) : this((client as AudioClient).ApiClient) { } @@ -14,6 +16,14 @@ namespace Discord.Audio.Streams _client = client; } + public async Task SetSpeakingAsync(bool isSpeaking) + { + if (_isSpeaking != isSpeaking) + { + await _client.SendSetSpeaking(isSpeaking).ConfigureAwait(false); + _isSpeaking = isSpeaking; + } + } public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancelToken) { cancelToken.ThrowIfCancellationRequested();