-
Notifications
You must be signed in to change notification settings - Fork 915
/
player.go
171 lines (148 loc) · 3.96 KB
/
player.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package soundboard
import (
"github.com/jonas747/dca"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/common"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"io"
"os"
"sync"
"time"
)
type PlayRequest struct {
ChannelID int64
GuildID int64
CommandRanFrom int64
Sound uint
}
var (
playQueues = make(map[int64][]*PlayRequest)
playQueuesMutex sync.Mutex
Silence = []byte{0xF8, 0xFF, 0xFE}
)
// RequestPlaySound either queues up a sound to be played in an existing player or creates a new one
func RequestPlaySound(guildID int64, channelID, channelRanFrom int64, soundID uint) (queued bool) {
item := &PlayRequest{
ChannelID: channelID,
GuildID: guildID,
Sound: soundID,
CommandRanFrom: channelRanFrom,
}
// If there is a queue setup there is alaso a player running, so just add it to the queue then
playQueuesMutex.Lock()
if queue, ok := playQueues[guildID]; ok {
playQueues[guildID] = append(queue, item)
queued = true
} else {
playQueues[guildID] = []*PlayRequest{item}
go runPlayer(guildID)
}
playQueuesMutex.Unlock()
return
}
func runPlayer(guildID int64) {
var lastChannel int64
var vc *discordgo.VoiceConnection
for {
playQueuesMutex.Lock()
var item *PlayRequest
// Get the next item in the queue or quit life
if queue, ok := playQueues[guildID]; ok && len(queue) > 0 {
item = queue[0]
playQueues[guildID] = queue[1:]
} else {
break
}
playQueuesMutex.Unlock()
// Should probably to changechannel but eh..
if lastChannel != item.ChannelID && vc != nil {
vc.Disconnect()
vc.Close()
vc = nil
}
var err error
vc, err = playSound(vc, bot.ShardManager.SessionForGuild(guildID), item)
if err != nil {
logrus.WithError(err).WithField("guild", guildID).Error("Failed playing sound")
if item.CommandRanFrom != 0 {
common.BotSession.ChannelMessageSend(item.CommandRanFrom, "Failed playing the sound: `"+err.Error()+"` make sure you put a proper audio file, and did not for example link to a youtube video.")
}
}
lastChannel = item.ChannelID
}
if vc != nil {
vc.Disconnect()
vc.Close()
}
logrus.Info("Done playing")
// When we break out, playqueuemutex is locked
delete(playQueues, guildID)
playQueuesMutex.Unlock()
}
func playSound(vc *discordgo.VoiceConnection, session *discordgo.Session, req *PlayRequest) (*discordgo.VoiceConnection, error) {
logrus.Info("Playing sound ", req.Sound)
// Open the sound and create a new decoder
file, err := os.Open(SoundFilePath(req.Sound, TranscodingStatusReady))
if err != nil {
return vc, common.ErrWithCaller(err)
}
defer file.Close()
decoder := dca.NewDecoder(file)
// Either use the passed voice connection, or create a new one
if vc == nil || !vc.Ready {
vc, err = session.GatewayManager.ChannelVoiceJoin(req.GuildID, req.ChannelID, false, true)
if err != nil {
return nil, common.ErrWithCaller(err)
}
<-vc.Connected
vc.Speaking(true)
}
// Start by sending some frames of silence
err = sendSilence(vc, 10)
if err != nil {
return vc, common.ErrWithCaller(err)
}
// Then play the actual sound
for {
frame, err := decoder.OpusFrame()
if err != nil {
if err != io.EOF {
return vc, common.ErrWithCaller(err)
}
break
}
err = sendAudio(vc, frame)
if err != nil {
return vc, common.ErrWithCaller(err)
}
}
// And finally stop with another small number of silcece frame
err = sendSilence(vc, 5)
if err != nil {
return vc, common.ErrWithCaller(err)
}
return vc, nil
}
// Send n silence frames
func sendSilence(vc *discordgo.VoiceConnection, n int) error {
for i := n - 1; i >= 0; i-- {
err := sendAudio(vc, Silence)
if err != nil {
return err
}
}
return nil
}
var (
ErrVoiceSendTimeout = errors.New("Voice send timeout")
)
func sendAudio(vc *discordgo.VoiceConnection, frame []byte) error {
select {
case vc.OpusSend <- frame:
case <-time.After(time.Second):
return ErrVoiceSendTimeout
}
return nil
}