Skip to content

Commit

Permalink
Merge pull request bwmarrin#1 from otothea/feature/configurable-voice…
Browse files Browse the repository at this point in the history
…-rates

add support for configurable opus rate/size
  • Loading branch information
otothea authored Aug 17, 2019
2 parents 8d8906c + e3db515 commit d3d8646
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
22 changes: 19 additions & 3 deletions voice.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type VoiceConnection struct {
speaking bool
reconnecting bool // If true, voice connection is trying to reconnect

opusRate int // Sample rate for opus, default is 48000
opusSize int // Sample size for opus, default is 960
OpusSend chan []byte // Chan for sending opus audio
OpusRecv chan *Packet // Chan for receiving opus audio

Expand Down Expand Up @@ -404,11 +406,18 @@ func (v *VoiceConnection) onEvent(message []byte) {
}

// Start the opusSender.
// TODO: Should we allow 48000/960 values to be user defined?
if v.OpusSend == nil {
v.OpusSend = make(chan []byte, 2)
}
go v.opusSender(v.udpConn, v.close, v.OpusSend, 48000, 960)
rate := v.opusRate
if rate == 0 {
rate = 48000
}
size := v.opusSize
if size == 0 {
size = 960
}
go v.opusSender(v.udpConn, v.close, v.OpusSend, rate, size)

// Start the opusReceiver
if !v.deaf {
Expand Down Expand Up @@ -863,7 +872,14 @@ func (v *VoiceConnection) reconnect() {

v.log(LogInformational, "trying to reconnect to channel %s", v.ChannelID)

_, err := v.session.ChannelVoiceJoin(v.GuildID, v.ChannelID, v.mute, v.deaf)
_, err := v.session.ChannelVoiceJoinV2(&ChannelVoiceJoinOptions{
GuildID: v.GuildID,
ChannelID: v.ChannelID,
Mute: v.mute,
Deaf: v.deaf,
OpusRate: v.opusRate,
OpusSize: v.opusSize,
})
if err == nil {
v.log(LogInformational, "successfully reconnected to channel %s", v.ChannelID)
return
Expand Down
48 changes: 40 additions & 8 deletions wsapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,37 +586,59 @@ type voiceChannelJoinOp struct {
Data voiceChannelJoinData `json:"d"`
}

// ChannelVoiceJoinOptions options for voice channel connection
type ChannelVoiceJoinOptions struct {
GuildID string // Guild ID of the channel to join.
ChannelID string // Channel ID of the channel to join.
Mute bool // If true, you will be set to muted upon joining.
Deaf bool // If true, you will be set to deafened upon joining.
OpusRate int // Opus sample rate (default 48000)
OpusSize int // Opus sample size (default 960)
}

// ChannelVoiceJoin joins the session user to a voice channel.
// This method is Deprecated, use ChannelVoiceJoinV2(options) instead
//
// gID : Guild ID of the channel to join.
// cID : Channel ID of the channel to join.
// mute : If true, you will be set to muted upon joining.
// deaf : If true, you will be set to deafened upon joining.
func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (voice *VoiceConnection, err error) {
return s.ChannelVoiceJoinV2(&ChannelVoiceJoinOptions{
GuildID: gID,
ChannelID: cID,
Mute: mute,
Deaf: deaf,
})
}

// ChannelVoiceJoinV2 joins the session user to a voice channel.
func (s *Session) ChannelVoiceJoinV2(opts *ChannelVoiceJoinOptions) (voice *VoiceConnection, err error) {
s.log(LogInformational, "called")

s.RLock()
voice, _ = s.VoiceConnections[gID]
voice, _ = s.VoiceConnections[opts.GuildID]
s.RUnlock()

if voice == nil {
voice = &VoiceConnection{}
s.Lock()
s.VoiceConnections[gID] = voice
s.VoiceConnections[opts.GuildID] = voice
s.Unlock()
}

voice.Lock()
voice.GuildID = gID
voice.ChannelID = cID
voice.deaf = deaf
voice.mute = mute
voice.GuildID = opts.GuildID
voice.ChannelID = opts.ChannelID
voice.deaf = opts.Deaf
voice.mute = opts.Mute
voice.opusRate = opts.OpusRate
voice.opusSize = opts.OpusSize
voice.session = s
voice.Unlock()

// Send the request to Discord that we want to join the voice channel
data := voiceChannelJoinOp{4, voiceChannelJoinData{&gID, &cID, mute, deaf}}
data := voiceChannelJoinOp{4, voiceChannelJoinData{&voice.GuildID, &voice.ChannelID, voice.mute, voice.deaf}}
s.wsMutex.Lock()
err = s.wsConn.WriteJSON(data)
s.wsMutex.Unlock()
Expand All @@ -636,6 +658,7 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (voice *Voi
}

// ChannelVoiceJoinManual initiates a voice session to a voice channel, but does not complete it.
// This method is Deprecated, use ChannelVoiceJoinManualV2(opts) instead
//
// This should only be used when the VoiceServerUpdate will be intercepted and used elsewhere.
//
Expand All @@ -644,11 +667,20 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (voice *Voi
// mute : If true, you will be set to muted upon joining.
// deaf : If true, you will be set to deafened upon joining.
func (s *Session) ChannelVoiceJoinManual(gID, cID string, mute, deaf bool) (err error) {
return s.ChannelVoiceJoinManualV2(&ChannelVoiceJoinOptions{
GuildID: gID,
ChannelID: cID,
Mute: mute,
Deaf: deaf,
})
}

// ChannelVoiceJoinManualV2 initiates a voice session to a voice channel, but does not complete it.
func (s *Session) ChannelVoiceJoinManualV2(opts *ChannelVoiceJoinOptions) (err error) {
s.log(LogInformational, "called")

// Send the request to Discord that we want to join the voice channel
data := voiceChannelJoinOp{4, voiceChannelJoinData{&gID, &cID, mute, deaf}}
data := voiceChannelJoinOp{4, voiceChannelJoinData{&opts.GuildID, &opts.ChannelID, opts.Mute, opts.Deaf}}
s.wsMutex.Lock()
err = s.wsConn.WriteJSON(data)
s.wsMutex.Unlock()
Expand Down

0 comments on commit d3d8646

Please sign in to comment.