private bool ProcessPacket(ReadOnlySpan <byte> data, ref Memory <byte> opus, ref Memory <byte> pcm, IList <ReadOnlyMemory <byte> > pcmPackets, out AudioSender voiceSender, out AudioFormat outputFormat) { voiceSender = null; outputFormat = default; if (!this.Rtp.IsRtpHeader(data)) { return(false); } this.Rtp.DecodeHeader(data, out var sequence, out var timestamp, out var ssrc, out var hasExtension); if (!this.TransmittingSSRCs.TryGetValue(ssrc, out var vtx)) { var decoder = Opus.CreateDecoder(); vtx = new AudioSender(ssrc, decoder) { // user isn't present as we haven't received a speaking event yet. User = null }; } voiceSender = vtx; if (sequence <= vtx.LastSequence) // out-of-order packet; discard { return(false); } var gap = vtx.LastSequence != 0 ? sequence - 1 - vtx.LastSequence : 0; if (gap >= 5) { this.Discord.Logger.LogWarning(VoiceNextEvents.VoiceReceiveFailure, "5 or more voice packets were dropped when receiving"); } Span <byte> nonce = stackalloc byte[Sodium.NonceSize]; this.Sodium.GetNonce(data, nonce, this.SelectedEncryptionMode); this.Rtp.GetDataFromPacket(data, out var encryptedOpus, this.SelectedEncryptionMode); var opusSize = Sodium.CalculateSourceSize(encryptedOpus); opus = opus.Slice(0, opusSize); var opusSpan = opus.Span; try { this.Sodium.Decrypt(encryptedOpus, opusSpan, nonce); // Strip extensions, if any if (hasExtension) { // RFC 5285, 4.2 One-Byte header // http://www.rfcreader.com/#rfc5285_line186 if (opusSpan[0] == 0xBE && opusSpan[1] == 0xDE) { var headerLen = opusSpan[2] << 8 | opusSpan[3]; var i = 4; for (; i < headerLen + 4; i++) { var @byte = opusSpan[i]; // ID is currently unused since we skip it anyway //var id = (byte)(@byte >> 4); var length = (byte)(@byte & 0x0F) + 1; i += length; } // Strip extension padding too while (opusSpan[i] == 0) { i++; } opusSpan = opusSpan.Slice(i); } // TODO: consider implementing RFC 5285, 4.3. Two-Byte Header } if (opusSpan[0] == 0x90) { // I'm not 100% sure what this header is/does, however removing the data causes no // real issues, and has the added benefit of removing a lot of noise. opusSpan = opusSpan.Slice(2); } if (gap == 1) { var lastSampleCount = this.Opus.GetLastPacketSampleCount(vtx.Decoder); var fecpcm = new byte[this.AudioFormat.SampleCountToSampleSize(lastSampleCount)]; var fecpcmMem = fecpcm.AsSpan(); this.Opus.Decode(vtx.Decoder, opusSpan, ref fecpcmMem, true, out _); pcmPackets.Add(fecpcm.AsMemory(0, fecpcmMem.Length)); } else if (gap > 1) { var lastSampleCount = this.Opus.GetLastPacketSampleCount(vtx.Decoder); for (var i = 0; i < gap; i++) { var fecpcm = new byte[this.AudioFormat.SampleCountToSampleSize(lastSampleCount)]; var fecpcmMem = fecpcm.AsSpan(); this.Opus.ProcessPacketLoss(vtx.Decoder, lastSampleCount, ref fecpcmMem); pcmPackets.Add(fecpcm.AsMemory(0, fecpcmMem.Length)); } } var pcmSpan = pcm.Span; this.Opus.Decode(vtx.Decoder, opusSpan, ref pcmSpan, false, out outputFormat); pcm = pcm.Slice(0, pcmSpan.Length); } finally { vtx.LastSequence = sequence; } return(true); }
private async Task HandleDispatch(JObject jo) { var opc = (int)jo["op"]; var opp = jo["d"] as JObject; switch (opc) { case 2: // READY Discord.DebugLogger.LogMessage(LogLevel.Debug, "VoiceNext", "OP2 received", DateTime.Now); var vrp = opp.ToObject <VoiceReadyPayload>(); SSRC = vrp.SSRC; ConnectionEndpoint = new ConnectionEndpoint(ConnectionEndpoint.Hostname, vrp.Port); // this is not the valid interval // oh, discord //this.HeartbeatInterval = vrp.HeartbeatInterval; HeartbeatTask = Task.Run(HeartbeatAsync); await Stage1(vrp).ConfigureAwait(false); break; case 4: // SESSION_DESCRIPTION Discord.DebugLogger.LogMessage(LogLevel.Debug, "VoiceNext", "OP4 received", DateTime.Now); var vsd = opp.ToObject <VoiceSessionDescriptionPayload>(); Key = vsd.SecretKey; Sodium = new DSharpPlus.VoiceNext.Codec.Sodium(Key.AsMemory()); await Stage2(vsd).ConfigureAwait(false); break; case 5: // SPEAKING // Don't spam OP5 //this.Discord.DebugLogger.LogMessage(LogLevel.Debug, "VoiceNext", "OP5 received", DateTime.Now); var spd = opp.ToObject <VoiceSpeakingPayload>(); var spk = new UserSpeakingEventArgs(Discord) { Speaking = spd.Speaking, SSRC = spd.SSRC.Value, }; if (spk.UserId != 0 && TransmittingSSRCs.TryGetValue(spk.SSRC, out var txssrc5) && txssrc5.UserId == 0) { txssrc5.UserId = spk.UserId; } else { var opus = Opus.CreateDecoder(); var vtx = new AudioSender(spk.SSRC, opus) { UserId = spd.UserId.Value }; if (!TransmittingSSRCs.TryAdd(spk.SSRC, vtx)) { Opus.DestroyDecoder(opus); } } await _userSpeaking.InvokeAsync(spk).ConfigureAwait(false); break; case 6: // HEARTBEAT ACK var dt = DateTime.Now; var ping = (int)(dt - LastHeartbeat).TotalMilliseconds; Volatile.Write(ref _wsPing, ping); Discord.DebugLogger.LogMessage(LogLevel.Debug, "VoiceNext", $"Received voice heartbeat ACK, ping {ping.ToString("#,##0", CultureInfo.InvariantCulture)}ms", dt); LastHeartbeat = dt; break; case 8: // HELLO // this sends a heartbeat interval that we need to use for heartbeating HeartbeatInterval = opp["heartbeat_interval"].ToObject <int>(); break; case 9: // RESUMED Discord.DebugLogger.LogMessage(LogLevel.Debug, "VoiceNext", "OP9 received", DateTime.Now); HeartbeatTask = Task.Run(HeartbeatAsync); break; case 12: // CLIENT_CONNECTED var ujpd = opp.ToObject <VoiceUserJoinPayload>(); { var opus = Opus.CreateDecoder(); var vtx = new AudioSender(ujpd.SSRC, opus) { UserId = ujpd.UserId }; if (!TransmittingSSRCs.TryAdd(vtx.SSRC, vtx)) { Opus.DestroyDecoder(opus); } } await _userJoined.InvokeAsync(new VoiceUserJoinEventArgs(Discord) { UserId = ujpd.UserId, SSRC = ujpd.SSRC }).ConfigureAwait(false); break; case 13: // CLIENT_DISCONNECTED var ulpd = opp.ToObject <VoiceUserLeavePayload>(); var txssrc = TransmittingSSRCs.FirstOrDefault(x => x.Value.Id == ulpd.UserId); if (TransmittingSSRCs.ContainsKey(txssrc.Key)) { TransmittingSSRCs.TryRemove(txssrc.Key, out var txssrc13); Opus.DestroyDecoder(txssrc13.Decoder); } await _userLeft.InvokeAsync(new VoiceUserLeaveEventArgs(Discord) { UserId = ulpd.UserId, SSRC = txssrc.Key }).ConfigureAwait(false); break; default: Discord.DebugLogger.LogMessage(LogLevel.Warning, "VoiceNext", $"Unknown opcode received: {opc.ToString(CultureInfo.InvariantCulture)}", DateTime.Now); break; } }