More improvements to voice support

This commit is contained in:
Bruce Marriner 2016-01-10 18:01:05 -06:00
parent 3357c56626
commit 093d3be770
3 changed files with 22 additions and 22 deletions

View file

@ -74,7 +74,7 @@ type Session struct {
// Everything below here is used for Voice testing. // Everything below here is used for Voice testing.
// This stuff is almost guarenteed to change a lot // This stuff is almost guarenteed to change a lot
// and is even a bit hackish right now. // and is even a bit hackish right now.
Voice *voice // Stores all details related to voice connections Voice *Voice // Stores all details related to voice connections
// Managed state object, updated with events. // Managed state object, updated with events.
State *State State *State

View file

@ -29,7 +29,7 @@ import (
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// A Voice struct holds all data and functions related to Discord Voice support. // A Voice struct holds all data and functions related to Discord Voice support.
type voice struct { type Voice struct {
sync.Mutex sync.Mutex
Ready bool Ready bool
Debug bool Debug bool
@ -75,7 +75,7 @@ type voiceHandshakeOp struct {
// Open opens a voice connection. This should be called // Open opens a voice connection. This should be called
// after VoiceChannelJoin is used and the data VOICE websocket events // after VoiceChannelJoin is used and the data VOICE websocket events
// are captured. // are captured.
func (v *voice) Open() (err error) { func (v *Voice) Open() (err error) {
// TODO: How do we handle changing channels? // TODO: How do we handle changing channels?
// Don't open a websocket if one is already open // Don't open a websocket if one is already open
@ -108,7 +108,7 @@ func (v *voice) Open() (err error) {
} }
// Close closes the voice connection // Close closes the voice connection
func (v *voice) Close() { func (v *Voice) Close() {
if v.UDPConn != nil { if v.UDPConn != nil {
v.UDPConn.Close() v.UDPConn.Close()
@ -122,7 +122,7 @@ func (v *voice) Close() {
// wsListen listens on the voice websocket for messages and passes them // wsListen listens on the voice websocket for messages and passes them
// to the voice event handler. This is automaticly called by the WS.Open // to the voice event handler. This is automaticly called by the WS.Open
// func when needed. // func when needed.
func (v *voice) wsListen() { func (v *Voice) wsListen() {
for { for {
messageType, message, err := v.wsConn.ReadMessage() messageType, message, err := v.wsConn.ReadMessage()
@ -142,7 +142,7 @@ func (v *voice) wsListen() {
// wsEvent handles any voice websocket events. This is only called by the // wsEvent handles any voice websocket events. This is only called by the
// wsListen() function. // wsListen() function.
func (v *voice) wsEvent(messageType int, message []byte) { func (v *Voice) wsEvent(messageType int, message []byte) {
if v.Debug { if v.Debug {
fmt.Println("wsEvent received: ", messageType) fmt.Println("wsEvent received: ", messageType)
@ -195,7 +195,7 @@ type voiceHeartbeatOp struct {
// wsHeartbeat sends regular heartbeats to voice Discord so it knows the client // wsHeartbeat sends regular heartbeats to voice Discord so it knows the client
// is still connected. If you do not send these heartbeats Discord will // is still connected. If you do not send these heartbeats Discord will
// disconnect the websocket connection after a few seconds. // disconnect the websocket connection after a few seconds.
func (v *voice) wsHeartbeat(i time.Duration) { func (v *Voice) wsHeartbeat(i time.Duration) {
ticker := time.NewTicker(i * time.Millisecond) ticker := time.NewTicker(i * time.Millisecond)
for { for {
@ -223,7 +223,7 @@ type voiceSpeakingOp struct {
// This must be sent as true prior to sending audio and should be set to false // This must be sent as true prior to sending audio and should be set to false
// once finished sending audio. // once finished sending audio.
// b : Send true if speaking, false if not. // b : Send true if speaking, false if not.
func (v *voice) Speaking(b bool) (err error) { func (v *Voice) Speaking(b bool) (err error) {
if v.wsConn == nil { if v.wsConn == nil {
return fmt.Errorf("No Voice websocket.") return fmt.Errorf("No Voice websocket.")
@ -263,7 +263,7 @@ type voiceUDPOp struct {
// initial required handshake. This connect is left open in the session // initial required handshake. This connect is left open in the session
// and can be used to send or receive audio. This should only be called // and can be used to send or receive audio. This should only be called
// from voice.wsEvent OP2 // from voice.wsEvent OP2
func (v *voice) udpOpen() (err error) { func (v *Voice) udpOpen() (err error) {
host := fmt.Sprintf("%s:%d", strings.TrimSuffix(v.endpoint, ":80"), v.OP2.Port) host := fmt.Sprintf("%s:%d", strings.TrimSuffix(v.endpoint, ":80"), v.OP2.Port)
addr, err := net.ResolveUDPAddr("udp", host) addr, err := net.ResolveUDPAddr("udp", host)

View file

@ -532,8 +532,9 @@ func (s *Session) Heartbeat(i time.Duration) {
} }
} }
// Everything below is experimental Voice support code // ------------------------------------------------------------------------------------------------
// all of it will get changed and moved around. // Code related to voice connections that initiate over the data websocket
// ------------------------------------------------------------------------------------------------
// A VoiceServerUpdate stores the data received during the Voice Server Update // A VoiceServerUpdate stores the data received during the Voice Server Update
// data websocket event. This data is used during the initial Voice Channel // data websocket event. This data is used during the initial Voice Channel
@ -571,8 +572,10 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (err error)
} }
// Create new voice{} struct if one does not exist. // Create new voice{} struct if one does not exist.
// If you create this prior to calling this func then you can manually
// set some variables if needed, such as to enable debugging.
if s.Voice == nil { if s.Voice == nil {
s.Voice = &voice{} s.Voice = &Voice{}
} }
// TODO : Determine how to properly change channels and change guild // TODO : Determine how to properly change channels and change guild
// and channel when you are already connected to an existing channel. // and channel when you are already connected to an existing channel.
@ -588,10 +591,6 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (err error)
s.Voice.guildID = gID s.Voice.guildID = gID
s.Voice.channelID = cID s.Voice.channelID = cID
// NOTE: This could remain open and monitor for the followup
// websocket events and then the voice ws/udp
// connection then if that fails, return with an error
// but doing so would add a lot of delay to the response..
return return
} }
@ -600,6 +599,12 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (err error)
// for the session user. // for the session user.
func (s *Session) onVoiceStateUpdate(st *VoiceState) { func (s *Session) onVoiceStateUpdate(st *VoiceState) {
// If s.Voice is nil, we must not have even requested to join
// a voice channel yet, so this shouldn't be processed.
if s.Voice == nil {
return
}
// Need to have this happen at login and store it in the Session // Need to have this happen at login and store it in the Session
// TODO : This should be done upon connecting to Discord, or // TODO : This should be done upon connecting to Discord, or
// be moved to a small helper function // be moved to a small helper function
@ -616,11 +621,6 @@ func (s *Session) onVoiceStateUpdate(st *VoiceState) {
return return
} }
// This shouldn't ever be the case, I don't think.
if s.Voice == nil {
s.Voice = &voice{}
}
// Store the SessionID for later use. // Store the SessionID for later use.
s.Voice.userID = self.ID // TODO: Review s.Voice.userID = self.ID // TODO: Review
s.Voice.sessionID = st.SessionID s.Voice.sessionID = st.SessionID
@ -633,7 +633,7 @@ func (s *Session) onVoiceServerUpdate(st *VoiceServerUpdate) {
// This shouldn't ever be the case, I don't think. // This shouldn't ever be the case, I don't think.
if s.Voice == nil { if s.Voice == nil {
s.Voice = &voice{} return
} }
// Store values for later use // Store values for later use