2020-04-25 02:36:33 +00:00
|
|
|
package voice
|
|
|
|
|
|
|
|
import (
|
2020-07-11 19:50:32 +00:00
|
|
|
"context"
|
2020-04-25 02:36:33 +00:00
|
|
|
"sync"
|
2020-07-11 19:50:32 +00:00
|
|
|
"time"
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2020-07-29 20:10:48 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
"github.com/diamondburned/arikawa/discord"
|
|
|
|
"github.com/diamondburned/arikawa/gateway"
|
2020-07-29 20:10:48 +00:00
|
|
|
"github.com/diamondburned/arikawa/internal/moreatomic"
|
2020-04-25 02:36:33 +00:00
|
|
|
"github.com/diamondburned/arikawa/session"
|
|
|
|
"github.com/diamondburned/arikawa/utils/wsutil"
|
|
|
|
"github.com/diamondburned/arikawa/voice/udp"
|
|
|
|
"github.com/diamondburned/arikawa/voice/voicegateway"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Protocol = "xsalsa20_poly1305"
|
|
|
|
|
|
|
|
var OpusSilence = [...]byte{0xF8, 0xFF, 0xFE}
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// WSTimeout is the duration to wait for a gateway operation including Session
|
|
|
|
// to complete before erroring out. This only applies to functions that don't
|
|
|
|
// take in a context already.
|
|
|
|
var WSTimeout = 10 * time.Second
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
type Session struct {
|
|
|
|
session *session.Session
|
|
|
|
state voicegateway.State
|
|
|
|
|
|
|
|
ErrorLog func(err error)
|
|
|
|
|
|
|
|
// Filled by events.
|
|
|
|
// sessionID string
|
|
|
|
// token string
|
|
|
|
// endpoint string
|
|
|
|
|
|
|
|
// joining determines the behavior of incoming event callbacks (Update).
|
|
|
|
// If this is true, incoming events will just send into Updated channels. If
|
|
|
|
// false, events will trigger a reconnection.
|
|
|
|
joining moreatomic.Bool
|
|
|
|
incoming chan struct{} // used only when joining == true
|
|
|
|
|
|
|
|
mut sync.RWMutex
|
|
|
|
|
|
|
|
// TODO: expose getters mutex-guarded.
|
|
|
|
gateway *voicegateway.Gateway
|
|
|
|
voiceUDP *udp.Connection
|
|
|
|
|
|
|
|
muted bool
|
|
|
|
deafened bool
|
|
|
|
speaking bool
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func NewSession(ses *session.Session, userID discord.UserID) *Session {
|
2020-04-25 02:36:33 +00:00
|
|
|
return &Session{
|
|
|
|
session: ses,
|
|
|
|
state: voicegateway.State{
|
|
|
|
UserID: userID,
|
|
|
|
},
|
|
|
|
ErrorLog: func(err error) {},
|
2020-07-11 19:50:32 +00:00
|
|
|
incoming: make(chan struct{}, 2),
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) UpdateServer(ev *gateway.VoiceServerUpdateEvent) {
|
2020-07-11 19:50:32 +00:00
|
|
|
if s.state.GuildID != ev.GuildID {
|
|
|
|
// Not our state.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// If this is true, then mutex is acquired already.
|
|
|
|
if s.joining.Get() {
|
|
|
|
s.state.Endpoint = ev.Endpoint
|
|
|
|
s.state.Token = ev.Token
|
|
|
|
|
|
|
|
s.incoming <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconnect.
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
|
|
|
s.state.Endpoint = ev.Endpoint
|
|
|
|
s.state.Token = ev.Token
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), WSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := s.reconnectCtx(ctx); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
s.ErrorLog(errors.Wrap(err, "failed to reconnect after voice server update"))
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) UpdateState(ev *gateway.VoiceStateUpdateEvent) {
|
|
|
|
if s.state.UserID != ev.UserID {
|
|
|
|
// Not our state.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is true, then mutex is acquired already.
|
|
|
|
if s.joining.Get() {
|
|
|
|
s.state.SessionID = ev.SessionID
|
|
|
|
s.state.ChannelID = ev.ChannelID
|
|
|
|
|
|
|
|
s.incoming <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *Session) JoinChannel(gID discord.GuildID, cID discord.ChannelID, muted, deafened bool) error {
|
2020-07-11 19:50:32 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), WSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return s.JoinChannelCtx(ctx, gID, cID, muted, deafened)
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:27:59 +00:00
|
|
|
func (s *Session) JoinChannelCtx(ctx context.Context, gID discord.GuildID, cID discord.ChannelID, muted, deafened bool) error {
|
2020-04-25 02:36:33 +00:00
|
|
|
// Acquire the mutex during join, locking during IO as well.
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
|
|
|
// Set that we're joining.
|
|
|
|
s.joining.Set(true)
|
|
|
|
defer s.joining.Set(false) // reset when done
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// Ensure gateway and voiceUDP are already closed.
|
2020-04-25 02:36:33 +00:00
|
|
|
s.ensureClosed()
|
|
|
|
|
|
|
|
// Set the state.
|
|
|
|
s.state.ChannelID = cID
|
|
|
|
s.state.GuildID = gID
|
|
|
|
|
|
|
|
s.muted = muted
|
|
|
|
s.deafened = deafened
|
|
|
|
s.speaking = false
|
|
|
|
|
|
|
|
// Ensure that if `cID` is zero that it passes null to the update event.
|
2020-07-29 20:58:39 +00:00
|
|
|
channelID := discord.NullChannelID
|
2020-07-29 20:10:17 +00:00
|
|
|
if cID.IsValid() {
|
2020-05-08 21:25:07 +00:00
|
|
|
channelID = cID
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://discordapp.com/developers/docs/topics/voice-connections#retrieving-voice-server-information
|
|
|
|
// Send a Voice State Update event to the gateway.
|
2020-07-11 19:50:32 +00:00
|
|
|
err := s.session.Gateway.UpdateVoiceStateCtx(ctx, gateway.UpdateVoiceStateData{
|
2020-04-25 02:36:33 +00:00
|
|
|
GuildID: gID,
|
|
|
|
ChannelID: channelID,
|
|
|
|
SelfMute: muted,
|
|
|
|
SelfDeaf: deafened,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to send Voice State Update event")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// Wait for 2 replies. The above command should reply with these 2 events.
|
|
|
|
if err := s.waitForIncoming(ctx, 2); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to wait for needed gateway events")
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
// These 2 methods should've updated s.state before sending into these
|
|
|
|
// channels. Since s.state is already filled, we can go ahead and connect.
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
return s.reconnectCtx(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) waitForIncoming(ctx context.Context, n int) error {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
select {
|
|
|
|
case <-s.incoming:
|
|
|
|
continue
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reconnect uses the current state to reconnect to a new gateway and UDP
|
|
|
|
// connection.
|
2020-07-11 19:50:32 +00:00
|
|
|
func (s *Session) reconnectCtx(ctx context.Context) (err error) {
|
2020-04-25 02:36:33 +00:00
|
|
|
s.gateway = voicegateway.New(s.state)
|
|
|
|
|
|
|
|
// Open the voice gateway. The function will block until Ready is received.
|
2020-07-11 19:50:32 +00:00
|
|
|
if err := s.gateway.OpenCtx(ctx); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to open voice gateway")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the Ready event.
|
|
|
|
voiceReady := s.gateway.Ready()
|
|
|
|
|
|
|
|
// Prepare the UDP voice connection.
|
2020-07-11 19:50:32 +00:00
|
|
|
s.voiceUDP, err = udp.DialConnectionCtx(ctx, voiceReady.Addr(), voiceReady.SSRC)
|
2020-04-25 02:36:33 +00:00
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to open voice UDP connection")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the session description from the voice gateway.
|
2020-07-11 19:50:32 +00:00
|
|
|
d, err := s.gateway.SessionDescriptionCtx(ctx, voicegateway.SelectProtocol{
|
2020-04-25 02:36:33 +00:00
|
|
|
Protocol: "udp",
|
|
|
|
Data: voicegateway.SelectProtocolData{
|
|
|
|
Address: s.voiceUDP.GatewayIP,
|
|
|
|
Port: s.voiceUDP.GatewayPort,
|
|
|
|
Mode: Protocol,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to select protocol")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the UDP loop.
|
|
|
|
go s.voiceUDP.Start(&d.SecretKey)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Speaking tells Discord we're speaking. This calls
|
|
|
|
// (*voicegateway.Gateway).Speaking().
|
|
|
|
func (s *Session) Speaking(flag voicegateway.SpeakingFlag) error {
|
|
|
|
// TODO: maybe we don't need to mutex protect IO.
|
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
|
|
|
return s.gateway.Speaking(flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) StopSpeaking() error {
|
|
|
|
// Send 5 frames of silence.
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
if _, err := s.Write(OpusSilence[:]); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrapf(err, "failed to send frame %d", i)
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// Write writes into the UDP voice connection WITHOUT a timeout.
|
2020-04-25 02:36:33 +00:00
|
|
|
func (s *Session) Write(b []byte) (int, error) {
|
2020-07-11 19:50:32 +00:00
|
|
|
return s.WriteCtx(context.Background(), b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteCtx writes into the UDP voice connection with a context for timeout.
|
|
|
|
func (s *Session) WriteCtx(ctx context.Context, b []byte) (int, error) {
|
2020-04-25 02:36:33 +00:00
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
|
|
|
if s.voiceUDP == nil {
|
|
|
|
return 0, ErrCannotSend
|
|
|
|
}
|
2020-07-11 19:50:32 +00:00
|
|
|
|
|
|
|
return s.voiceUDP.WriteCtx(ctx, b)
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) Disconnect() error {
|
2020-07-11 19:50:32 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), WSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return s.DisconnectCtx(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) DisconnectCtx(ctx context.Context) error {
|
2020-04-25 02:36:33 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
|
|
|
// If we're already closed.
|
|
|
|
if s.gateway == nil && s.voiceUDP == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify Discord that we're leaving. This will send a
|
|
|
|
// VoiceStateUpdateEvent, in which our handler will promptly remove the
|
|
|
|
// session from the map.
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
err := s.session.Gateway.UpdateVoiceStateCtx(ctx, gateway.UpdateVoiceStateData{
|
2020-04-25 02:36:33 +00:00
|
|
|
GuildID: s.state.GuildID,
|
2020-07-21 20:27:59 +00:00
|
|
|
ChannelID: discord.ChannelID(discord.NullSnowflake),
|
2020-04-25 02:36:33 +00:00
|
|
|
SelfMute: true,
|
|
|
|
SelfDeaf: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
s.ensureClosed()
|
|
|
|
// wrap returns nil if err is nil
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to update voice state")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close ensures everything is closed. It does not acquire the mutex.
|
|
|
|
func (s *Session) ensureClosed() {
|
|
|
|
// If we're already closed.
|
|
|
|
if s.gateway == nil && s.voiceUDP == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect the UDP connection.
|
|
|
|
if s.voiceUDP != nil {
|
|
|
|
s.voiceUDP.Close()
|
|
|
|
s.voiceUDP = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect the voice gateway, ignoring the error.
|
|
|
|
if s.gateway != nil {
|
|
|
|
if err := s.gateway.Close(); err != nil {
|
|
|
|
wsutil.WSDebug("Uncaught voice gateway close error:", err)
|
|
|
|
}
|
|
|
|
s.gateway = nil
|
|
|
|
}
|
|
|
|
}
|