Skip to content

[Bug]: User speaking as they join a call makes the bot unable to hear them #3208

@Alexejhero

Description

@Alexejhero
  • I double checked the docs and couldn't find any useful information.
  • I verified the issue was caused by Discord.Net.
  • I double checked that I have the required intents.

Description

If a person using voice activity is actively speaking as they join the call, the bot becomes permanently unable to hear them until either the bot reconnects or the user reconnects.

Version

built from dev branch

Logs

Initial error: (happens when the bug is triggered)

System.IO.InvalidDataException: Opus Error: InvalidPacket
  at Discord.Audio.OpusConverter.CheckError (System.Int32 result) [0x00019] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at Discord.Audio.OpusDecoder.DecodeFrame (System.Byte[] input, System.Int32 inputOffset, System.Int32 inputCount, System.Byte[] output, System.Int32 outputOffset, System.Boolean decodeFEC) [0x0004e] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at Discord.Audio.Streams.OpusDecodeStream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancelToken) [0x0003e] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at Discord.Audio.Streams.RTPReadStream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancelToken) [0x0004c] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at Discord.Audio.Streams.SodiumDecryptStream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancelToken) [0x00092] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at System.IO.Stream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x00009] in <1071a2cb0cb3433aae80a793c277a048>:0 
  at (wrapper remoting-invoke-with-check) System.IO.Stream.WriteAsync(byte[],int,int)
  at Discord.Audio.AudioClient.ProcessPacketAsync (System.Byte[] packet) [0x005e8] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
System.InvalidOperationException: Header received with no payload
  at Discord.Audio.Streams.InputStream.WriteHeader (System.UInt16 seq, System.UInt32 timestamp, System.Boolean missed) [0x00008] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at Discord.Audio.Streams.OpusDecodeStream.WriteHeader (System.UInt16 seq, System.UInt32 timestamp, System.Boolean missed) [0x00021] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at Discord.Audio.Streams.RTPReadStream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancelToken) [0x0003e] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at Discord.Audio.Streams.SodiumDecryptStream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancelToken) [0x00092] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at System.IO.Stream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x00009] in <1071a2cb0cb3433aae80a793c277a048>:0 
  at (wrapper remoting-invoke-with-check) System.IO.Stream.WriteAsync(byte[],int,int)
  at Discord.Audio.AudioClient.ProcessPacketAsync (System.Byte[] packet) [0x005e8] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 

Recurrent error: (happens every packet afterwards)

System.InvalidOperationException: Header received with no payload.
  at Discord.Audio.Streams.OpusDecodeStream.WriteHeader (System.UInt16 seq, System.UInt32 timestamp, System.Boolean missed) [0x00008] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at Discord.Audio.Streams.RTPReadStream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancelToken) [0x0003e] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at Discord.Audio.Streams.SodiumDecryptStream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancelToken) [0x00092] in <73ff753f9fcd4b1989bcbdf045d7f870>:0 
  at System.IO.Stream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x00009] in <1071a2cb0cb3433aae80a793c277a048>:0 
  at (wrapper remoting-invoke-with-check) System.IO.Stream.WriteAsync(byte[],int,int)
  at Discord.Audio.AudioClient.ProcessPacketAsync (System.Byte[] packet) [0x005e8] in <73ff753f9fcd4b1989bcbdf045d7f870>:0

Those errors make no sense because the packet size is pretty large:

Received 690 bytes from user 183249892712513536
Received 578 bytes from user 183249892712513536
Received 562 bytes from user 183249892712513536

I do get Malformed frame constantly spammed in my logs while this bug happens, this issue might be related to #3188

Sample

// Creating the client
_client = new DiscordSocketClient(new DiscordSocketConfig
{
    LogLevel = LogSeverity.Debug
});
// Method to join a voice channel
public async Task<bool> JoinVc(string id, string outputAudioDevice = null, string inputAudioDevice = null)
{
    // ...
    // SocketVoiceChannel voiceChannel = ...

    _busyConnecting = true;

    IAudioClient connection = await voiceChannel.ConnectAsync(selfMute: string.IsNullOrEmpty(inputAudioDevice), selfDeaf: string.IsNullOrEmpty(outputAudioDevice));

    _currentVoiceConnection = connection;

    _busyConnecting = false;
    return true;
}
// VC connection handler
discordBot.Client.UserVoiceStateUpdated += (user, oldState, newState) =>
{
    if (user.Id == discordBot.Client.CurrentUser.Id)
    {
        if (oldState.VoiceChannel != newState.VoiceChannel)
        {
            if (oldState.VoiceChannel == null) _ = HandleVcConnect(newState.VoiceChannel);
            // ...
        }
    }
}

private async Task HandleVcConnect(IVoiceChannel voiceChannel)
{
    await Task.WaitUntil(() => !_busyConnecting);

    _currentVoiceChannel = voiceChannel;

    await Task.WhenAll(_currentVoiceConnection.GetStreams().Select(SetupIncomingAudioStream));
    _connection.StreamCreated += SetupIncomingAudioStream;
    _connection.StreamDestroyed += DestroyIncomingAudioStream;

    // SetupIncomingAudioStream just forwards the AudioInStream through to NAudio, I don't think those snippets are relevant because the exception happens in Discord.NET before it reaches my code
}

Packages

  • Discord.Net.Core latest dev commit
  • Discord.Net.Rest latest dev commit
  • Discord.Net.WebSocket latest dev commit
  • NAudio 2.0.1

Environment

  • Windows 10 22H2, x64
  • Using .NET Standard 2.1 (I'm aware support has been dropped, I re-added in my fork, no other changes were made)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions