Aiko-IT-Systems / DisCatSharp

Your library to write discord apps in C# with focus on always providing access to the latest discord features
https://docs.dcs.aitsys.dev
Other
126 stars 40 forks source link

VoiceNext stopped working due to change in protocol #508

Open Licho1 opened 4 months ago

Licho1 commented 4 months ago

Summary

VoiceNext cannot receive messages

Reproduction Steps

[2024-06-10 16:42:45 +02:00] [303 /VoiceReceive] [Error] Exception occurred when decoding incoming audio data
System.ArgumentOutOfRangeException: CurrentSequenceWrapState was out of range
   at DisCatSharp.VoiceNext.Entities.AudioSender.GetTrueSequenceAfterWrapping(UInt16 originalSequence) in C:\work\Vocairos\DisCatSharp\DisCatSharp.VoiceNext\Entities\AudioSender.cs:line 141
   at DisCatSharp.VoiceNext.VoiceNextConnection.ProcessPacket(ReadOnlySpan`1 data, Memory`1& opus, Memory`1& pcm, List`1 pcmPackets, AudioSender& voiceSender, AudioFormat& outputFormat) in C:\work\Vocairos\DisCatSharp\DisCatSharp.VoiceNext\VoiceNextConnection.cs:line 694
   at DisCatSharp.VoiceNext.VoiceNextConnection.ProcessVoicePacket(Byte[] data) in C:\work\Vocairos\DisCatSharp\DisCatSharp.VoiceNext\VoiceNextConnection.cs:line 804

Minimal Reproducible Code

        guild = await disc.GetGuildAsync(serverId);
        var channels = await guild.GetChannelsAsync();
        voiceChannel = channels.First(x => x.Id == voiceChannelId);

        var voiceNext = disc.GetVoiceNext();
        audioConn = await voiceNext.ConnectAsync(voiceChannel);

        audioConn.UserJoined += (conn, args) =>
        {
            Console.WriteLine($"User {args.User.Username} joined");
            return Task.CompletedTask;
        };

        audioConn.UserSpeaking += (conn, args) =>
        {
            Console.WriteLine($"User {args.User.Username} speaking");
            return Task.CompletedTask;
        };

        audioConn.VoiceReceived += (conn, args) =>
        {
        };

Expected Results

In the past identical code worked

Actual Results

Exception occurs

System Information

Not relevant, windows 11

Checklist

Additional Context

No response

Lulalaby commented 3 months ago

Does this still occur? Might be a problem with Discord. I'm not aware of any changes since we're still using the default voice gw version which is so old, it won't really be changed anyways, normally.

Licho1 commented 3 months ago

Yes this problem still occurs. It should be fully replicable (happens 100% of time)

Lulalaby commented 3 months ago

ah damn it @TheXorog do you still run a version with vnext? Or do I have to put nyuw back to life

TheXorog commented 3 months ago

I don't but i could set it up test stuff with Makoto with pretty minimal effort (basically just adding vn and a few test commands)

TheXorog commented 3 months ago

Can confirm this happens with IncomingAudio enabled.

Tested with:

[SlashCommand("test", "Test.")]
public async Task Test(InteractionContext ctx, [Option("test", "test"), MinimumValue(1), MaximumValue(2000)] int number)
{
    try
    {
        var guild = ctx.Guild;
        var channels = await guild.GetChannelsAsync();
        var voiceChannel = channels.First(x => x.Id == 1036569241488474192ul);

        var voiceNext = ctx.Client.GetVoiceNext();
        var audioConn = await voiceNext.ConnectAsync(voiceChannel);

        audioConn.UserJoined += (conn, args) =>
        {
            Log.Debug("User {user} joined", args.User.Username);
            return Task.CompletedTask;
        };

        audioConn.UserSpeaking += (conn, args) =>
        {
            Log.Debug("User {user} speaking", args.User.Username);
            return Task.CompletedTask;
        };

        audioConn.VoiceReceived += (conn, args) =>
        {
            return Task.CompletedTask;
        };
    }
    catch (Exception ex)
    {
        Log.Error(ex, "Error");
    }
}

@Lulalaby I don't think we ever fixed incoming audio, did we?

@Licho1 For the time being, if you do not need incoming voice data you can disable that and no exception will be thrown anymore. If you do, you'll need to wait for us to fix it (or make a PR to fix it, if you'd like). I'll take a look at it today but i can't promise anything, i have basically no idea about VoiceNext.

Licho1 commented 3 months ago

Thanks! I do use incoming audio (for a talking AI cat), and it has worked fine for me in the past (since about this PR https://github.com/Aiko-IT-Systems/DisCatSharp/pull/396 )

So it seems that discord has changed something. I have no idea how this protocol works, is there some documentation for voice protocol? Or is it all reverse engineered?

TheXorog commented 3 months ago

The only thing i can tell from my testing is that the issue lies in AudioSender, some method that looks to be a workaround for out of order packets, i'm not really sure at all. Tried playing around with it earlier but got nowhere really. I'm not really experienced with this kind of stuff unfortunately, so that was to be expected (and neither do i have the motivation to actually try and learn it atm).

As for docs this seems to be the only docs. I'd assume you'd the same for receiving as sending just backwards.

We'll have to wait for Lala to fix it (or for me to sit my ass down and learn lol). We're gonna fix this one way or another, probably

Lulalaby commented 3 months ago

@Licho1 dumb idea, but could you try 10.6.5-nightly-003 in a while (building right now) with the DisCatSharp.VoiceNext.Natives update

Licho1 commented 3 weeks ago

I have forgotton to mention, it didn't fix it. Bug still persist in 10.6.6.-nightly-018