-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.go
127 lines (113 loc) · 2.88 KB
/
voice.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
package main
import (
"fmt"
"log"
"net/url"
"os"
"os/signal"
"path"
"syscall"
"time"
dgo "github.com/bwmarrin/discordgo"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3/pkg/media"
)
var (
// Semaphore signalling when we are listening
listening = make(chan bool, 1) // TODO: Might want to make this per-guild
voiceDisconnect = make(chan bool)
)
type listenHandler func(packets <-chan *dgo.Packet, spkUpdate <-chan *speakerId, done chan<- bool)
type speaker struct {
ssrc uint32
file media.Writer
audioId int64
}
type speakerId struct {
uid string
ssrc uint32
consent bool
}
func joinVoiceFromMessage(s *dgo.Session, m *dgo.MessageCreate) (vc *dgo.VoiceConnection) {
vs, err := s.State.VoiceState(m.GuildID, m.Author.ID)
if err != nil {
log.Panicln("error getting voice state:", err)
}
vc, err = s.ChannelVoiceJoin(m.GuildID, vs.ChannelID, false, false)
defer func() {
if msg := recover(); msg != nil {
if err := vc.Disconnect(); err != nil {
log.Println("failed disconnecting from voice:", err)
}
panic(msg)
}
}()
if err != nil {
log.Panicln("error joining voice channel:", err)
}
return
}
func listen(s *dgo.Session, vc *dgo.VoiceConnection, duration time.Duration, handler listenHandler) {
var (
timeout = time.NewTimer(duration)
quit = make(chan os.Signal, 1)
voiceDone = make(chan bool)
speakerIds = make(chan *speakerId)
)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
stopHandling := s.AddHandler(func(s *dgo.Session, m *dgo.VoiceStateUpdate) {
if m.UserID == vc.UserID {
if m.ChannelID == "" {
log.Printf("forcibly disconnected from '%s'\n",
m.BeforeUpdate.ChannelID)
voiceDisconnect <- true
} else {
// TODO What should happen when we are moved to another channel?
log.Printf("moved to %s", m.ChannelID)
}
}
})
vc.AddHandler(func(vc *dgo.VoiceConnection, vsu *dgo.VoiceSpeakingUpdate) {
log.Printf("speaking update: %+v\n", *vsu)
// Experimentally, vsu.Speaking is never false. Why?
if !vsu.Speaking {
return
}
speakerIds <- &speakerId{vsu.UserID, uint32(vsu.SSRC), dbIsConsenting(vsu.UserID)}
})
defer func() {
stopHandling()
if err := vc.Disconnect(); err != nil {
log.Println(err)
}
signal.Stop(quit)
timeout.Stop()
close(vc.OpusRecv)
close(speakerIds)
<-voiceDone // Wait for handler to clean up
<-listening
}()
go handler(vc.OpusRecv, speakerIds, voiceDone)
select {
case <-quit:
case <-timeout.C:
case <-voiceDisconnect:
}
}
func createRTPPacket(p *dgo.Packet) *rtp.Packet {
return &rtp.Packet{
Header: rtp.Header{
Version: 2,
PayloadType: 0x78,
SequenceNumber: p.Sequence,
Timestamp: p.Timestamp,
SSRC: p.SSRC,
},
Payload: p.Opus,
}
}
func constructUri(convId int64, ssrc uint32) *url.URL {
url := *mediaRoot
url.Path = path.Join(url.Path, fmt.Sprintf("%v_%v.ogg", convId, ssrc))
return &url
}