voice.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.
  6. // This file contains code related to Discord voice suppport
  7. package discordgo
  8. import (
  9. "encoding/binary"
  10. "encoding/json"
  11. "fmt"
  12. "net"
  13. "runtime"
  14. "strings"
  15. "sync"
  16. "time"
  17. "github.com/gorilla/websocket"
  18. )
  19. // ------------------------------------------------------------------------------------------------
  20. // Code related to both Voice Websocket and UDP connections.
  21. // ------------------------------------------------------------------------------------------------
  22. // A Voice struct holds all data and functions related to Discord Voice support.
  23. type Voice struct {
  24. sync.Mutex // future use
  25. Ready bool // If true, voice is ready to send/receive audio
  26. Debug bool // If true, print extra logging
  27. OP2 *voiceOP2 // exported for dgvoice, may change.
  28. OpusSend chan []byte // Chan for sending opus audio
  29. OpusRecv chan *Packet // Chan for receiving opus audio
  30. // FrameRate int // This can be used to set the FrameRate of Opus data
  31. // FrameSize int // This can be used to set the FrameSize of Opus data
  32. wsConn *websocket.Conn
  33. UDPConn *net.UDPConn // this will become unexported soon.
  34. sessionID string
  35. token string
  36. endpoint string
  37. guildID string
  38. channelID string
  39. userID string
  40. // Used to send a close signal to goroutines
  41. close chan struct{}
  42. }
  43. // ------------------------------------------------------------------------------------------------
  44. // Code related to the Voice websocket connection
  45. // ------------------------------------------------------------------------------------------------
  46. // A voiceOP2 stores the data for the voice operation 2 websocket event
  47. // which is sort of like the voice READY packet
  48. type voiceOP2 struct {
  49. SSRC uint32 `json:"ssrc"`
  50. Port int `json:"port"`
  51. Modes []string `json:"modes"`
  52. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  53. }
  54. type voiceHandshakeData struct {
  55. ServerID string `json:"server_id"`
  56. UserID string `json:"user_id"`
  57. SessionID string `json:"session_id"`
  58. Token string `json:"token"`
  59. }
  60. type voiceHandshakeOp struct {
  61. Op int `json:"op"` // Always 0
  62. Data voiceHandshakeData `json:"d"`
  63. }
  64. // Open opens a voice connection. This should be called
  65. // after VoiceChannelJoin is used and the data VOICE websocket events
  66. // are captured.
  67. func (v *Voice) Open() (err error) {
  68. v.Lock()
  69. defer v.Unlock()
  70. // Don't open a websocket if one is already open
  71. if v.wsConn != nil {
  72. return
  73. }
  74. // Connect to Voice Websocket
  75. vg := fmt.Sprintf("wss://%s", strings.TrimSuffix(v.endpoint, ":80"))
  76. v.wsConn, _, err = websocket.DefaultDialer.Dial(vg, nil)
  77. if err != nil {
  78. fmt.Println("VOICE error opening websocket:", err)
  79. return
  80. }
  81. data := voiceHandshakeOp{0, voiceHandshakeData{v.guildID, v.userID, v.sessionID, v.token}}
  82. err = v.wsConn.WriteJSON(data)
  83. if err != nil {
  84. fmt.Println("VOICE error sending init packet:", err)
  85. return
  86. }
  87. // Start a listening for voice websocket events
  88. // TODO add a check here to make sure Listen worked by monitoring
  89. // a chan or bool?
  90. v.close = make(chan struct{})
  91. go v.wsListen(v.wsConn, v.close)
  92. return
  93. }
  94. // wsListen listens on the voice websocket for messages and passes them
  95. // to the voice event handler. This is automatically called by the Open func
  96. func (v *Voice) wsListen(wsConn *websocket.Conn, close <-chan struct{}) {
  97. for {
  98. messageType, message, err := v.wsConn.ReadMessage()
  99. if err != nil {
  100. // TODO: add reconnect, matching wsapi.go:listen()
  101. // TODO: Handle this problem better.
  102. // TODO: needs proper logging
  103. fmt.Println("Voice Listen Error:", err)
  104. return
  105. }
  106. // Pass received message to voice event handler
  107. select {
  108. case <-close:
  109. return
  110. default:
  111. go v.wsEvent(messageType, message)
  112. }
  113. }
  114. return
  115. }
  116. // wsEvent handles any voice websocket events. This is only called by the
  117. // wsListen() function.
  118. func (v *Voice) wsEvent(messageType int, message []byte) {
  119. if v.Debug {
  120. fmt.Println("wsEvent received: ", messageType)
  121. printJSON(message)
  122. }
  123. var e Event
  124. if err := json.Unmarshal(message, &e); err != nil {
  125. fmt.Println("wsEvent Unmarshall error: ", err)
  126. return
  127. }
  128. switch e.Operation {
  129. case 2: // READY
  130. v.OP2 = &voiceOP2{}
  131. if err := json.Unmarshal(e.RawData, v.OP2); err != nil {
  132. fmt.Println("voiceWS.onEvent OP2 Unmarshall error: ", err)
  133. printJSON(e.RawData) // TODO: Better error logging
  134. return
  135. }
  136. // Start the voice websocket heartbeat to keep the connection alive
  137. go v.wsHeartbeat(v.wsConn, v.close, v.OP2.HeartbeatInterval)
  138. // TODO monitor a chan/bool to verify this was successful
  139. // Start the UDP connection
  140. err := v.udpOpen()
  141. if err != nil {
  142. fmt.Println("Error opening udp connection: ", err)
  143. return
  144. }
  145. // Start the opusSender.
  146. // TODO: Should we allow 48000/960 values to be user defined?
  147. if v.OpusSend == nil {
  148. v.OpusSend = make(chan []byte, 2)
  149. }
  150. go v.opusSender(v.UDPConn, v.close, v.OpusSend, 48000, 960)
  151. // Start the opusReceiver
  152. if v.OpusRecv == nil {
  153. v.OpusRecv = make(chan *Packet, 2)
  154. }
  155. go v.opusReceiver(v.UDPConn, v.close, v.OpusRecv)
  156. return
  157. case 3: // HEARTBEAT response
  158. // add code to use this to track latency?
  159. return
  160. case 4:
  161. // TODO
  162. case 5:
  163. // SPEAKING TRUE/FALSE NOTIFICATION
  164. /*
  165. {
  166. "user_id": "1238921738912",
  167. "ssrc": 2,
  168. "speaking": false
  169. }
  170. */
  171. default:
  172. fmt.Println("UNKNOWN VOICE OP: ", e.Operation)
  173. printJSON(e.RawData)
  174. }
  175. return
  176. }
  177. type voiceHeartbeatOp struct {
  178. Op int `json:"op"` // Always 3
  179. Data int `json:"d"`
  180. }
  181. // NOTE :: When a guild voice server changes how do we shut this down
  182. // properly, so a new connection can be setup without fuss?
  183. //
  184. // wsHeartbeat sends regular heartbeats to voice Discord so it knows the client
  185. // is still connected. If you do not send these heartbeats Discord will
  186. // disconnect the websocket connection after a few seconds.
  187. func (v *Voice) wsHeartbeat(wsConn *websocket.Conn, close <-chan struct{}, i time.Duration) {
  188. if close == nil || wsConn == nil {
  189. return
  190. }
  191. var err error
  192. ticker := time.NewTicker(i * time.Millisecond)
  193. for {
  194. err = wsConn.WriteJSON(voiceHeartbeatOp{3, int(time.Now().Unix())})
  195. if err != nil {
  196. fmt.Println("wsHeartbeat send error: ", err)
  197. return
  198. }
  199. select {
  200. case <-ticker.C:
  201. // continue loop and send heartbeat
  202. case <-close:
  203. return
  204. }
  205. }
  206. }
  207. type voiceSpeakingData struct {
  208. Speaking bool `json:"speaking"`
  209. Delay int `json:"delay"`
  210. }
  211. type voiceSpeakingOp struct {
  212. Op int `json:"op"` // Always 5
  213. Data voiceSpeakingData `json:"d"`
  214. }
  215. // Speaking sends a speaking notification to Discord over the voice websocket.
  216. // This must be sent as true prior to sending audio and should be set to false
  217. // once finished sending audio.
  218. // b : Send true if speaking, false if not.
  219. func (v *Voice) Speaking(b bool) (err error) {
  220. if v.wsConn == nil {
  221. return fmt.Errorf("No Voice websocket.")
  222. }
  223. data := voiceSpeakingOp{5, voiceSpeakingData{b, 0}}
  224. err = v.wsConn.WriteJSON(data)
  225. if err != nil {
  226. fmt.Println("Speaking() write json error:", err)
  227. return
  228. }
  229. return
  230. }
  231. // ------------------------------------------------------------------------------------------------
  232. // Code related to the Voice UDP connection
  233. // ------------------------------------------------------------------------------------------------
  234. type voiceUDPData struct {
  235. Address string `json:"address"` // Public IP of machine running this code
  236. Port uint16 `json:"port"` // UDP Port of machine running this code
  237. Mode string `json:"mode"` // plain or ? (plain or encrypted)
  238. }
  239. type voiceUDPD struct {
  240. Protocol string `json:"protocol"` // Always "udp" ?
  241. Data voiceUDPData `json:"data"`
  242. }
  243. type voiceUDPOp struct {
  244. Op int `json:"op"` // Always 1
  245. Data voiceUDPD `json:"d"`
  246. }
  247. // udpOpen opens a UDP connection to the voice server and completes the
  248. // initial required handshake. This connection is left open in the session
  249. // and can be used to send or receive audio. This should only be called
  250. // from voice.wsEvent OP2
  251. func (v *Voice) udpOpen() (err error) {
  252. v.Lock()
  253. defer v.Unlock()
  254. if v.wsConn == nil {
  255. return fmt.Errorf("nil voice websocket")
  256. }
  257. if v.close == nil {
  258. return fmt.Errorf("nil close channel")
  259. }
  260. if v.endpoint == "" {
  261. return fmt.Errorf("empty endpoint")
  262. }
  263. host := fmt.Sprintf("%s:%d", strings.TrimSuffix(v.endpoint, ":80"), v.OP2.Port)
  264. addr, err := net.ResolveUDPAddr("udp", host)
  265. if err != nil {
  266. fmt.Println("udpOpen resolve addr error: ", err)
  267. // TODO better logging
  268. return
  269. }
  270. v.UDPConn, err = net.DialUDP("udp", nil, addr)
  271. if err != nil {
  272. fmt.Println("udpOpen dial udp error: ", err)
  273. // TODO better logging
  274. return
  275. }
  276. // Create a 70 byte array and put the SSRC code from the Op 2 Voice event
  277. // into it. Then send that over the UDP connection to Discord
  278. sb := make([]byte, 70)
  279. binary.BigEndian.PutUint32(sb, v.OP2.SSRC)
  280. _, err = v.UDPConn.Write(sb)
  281. if err != nil {
  282. fmt.Println("udpOpen udp write error : ", err)
  283. // TODO better logging
  284. return
  285. }
  286. // Create a 70 byte array and listen for the initial handshake response
  287. // from Discord. Once we get it parse the IP and PORT information out
  288. // of the response. This should be our public IP and PORT as Discord
  289. // saw us.
  290. rb := make([]byte, 70)
  291. rlen, _, err := v.UDPConn.ReadFromUDP(rb)
  292. if err != nil {
  293. fmt.Println("udpOpen udp read error : ", err)
  294. // TODO better logging
  295. return
  296. }
  297. if rlen < 70 {
  298. fmt.Println("Voice RLEN should be 70 but isn't")
  299. }
  300. // Loop over position 4 though 20 to grab the IP address
  301. // Should never be beyond position 20.
  302. var ip string
  303. for i := 4; i < 20; i++ {
  304. if rb[i] == 0 {
  305. break
  306. }
  307. ip += string(rb[i])
  308. }
  309. // Grab port from position 68 and 69
  310. port := binary.LittleEndian.Uint16(rb[68:70])
  311. // Take the data from above and send it back to Discord to finalize
  312. // the UDP connection handshake.
  313. data := voiceUDPOp{1, voiceUDPD{"udp", voiceUDPData{ip, port, "plain"}}}
  314. err = v.wsConn.WriteJSON(data)
  315. if err != nil {
  316. fmt.Println("udpOpen write json error:", err)
  317. return
  318. }
  319. // start udpKeepAlive
  320. go v.udpKeepAlive(v.UDPConn, v.close, 5*time.Second)
  321. // TODO: find a way to check that it fired off okay
  322. return
  323. }
  324. // udpKeepAlive sends a udp packet to keep the udp connection open
  325. // This is still a bit of a "proof of concept"
  326. func (v *Voice) udpKeepAlive(UDPConn *net.UDPConn, close <-chan struct{}, i time.Duration) {
  327. if UDPConn == nil || close == nil {
  328. return
  329. }
  330. var err error
  331. var sequence uint64
  332. packet := make([]byte, 8)
  333. ticker := time.NewTicker(i)
  334. for {
  335. // TODO: Add a way to break from loop
  336. binary.LittleEndian.PutUint64(packet, sequence)
  337. sequence++
  338. _, err = UDPConn.Write(packet)
  339. if err != nil {
  340. fmt.Println("udpKeepAlive udp write error : ", err)
  341. return
  342. }
  343. select {
  344. case <-ticker.C:
  345. // continue loop and send keepalive
  346. case <-close:
  347. return
  348. }
  349. }
  350. }
  351. // opusSender will listen on the given channel and send any
  352. // pre-encoded opus audio to Discord. Supposedly.
  353. func (v *Voice) opusSender(UDPConn *net.UDPConn, close <-chan struct{}, opus <-chan []byte, rate, size int) {
  354. if UDPConn == nil || close == nil {
  355. return
  356. }
  357. runtime.LockOSThread()
  358. // Voice is now ready to receive audio packets
  359. // TODO: this needs reviewed as I think there must be a better way.
  360. v.Ready = true
  361. defer func() {
  362. v.Ready = false
  363. }()
  364. var sequence uint16
  365. var timestamp uint32
  366. udpHeader := make([]byte, 12)
  367. // build the parts that don't change in the udpHeader
  368. udpHeader[0] = 0x80
  369. udpHeader[1] = 0x78
  370. binary.BigEndian.PutUint32(udpHeader[8:], v.OP2.SSRC)
  371. // start a send loop that loops until buf chan is closed
  372. ticker := time.NewTicker(time.Millisecond * time.Duration(size/(rate/1000)))
  373. for {
  374. // Get data from chan. If chan is closed, return.
  375. recvbuf, ok := <-opus
  376. if !ok {
  377. return
  378. }
  379. // Add sequence and timestamp to udpPacket
  380. binary.BigEndian.PutUint16(udpHeader[2:], sequence)
  381. binary.BigEndian.PutUint32(udpHeader[4:], timestamp)
  382. // Combine the UDP Header and the opus data
  383. sendbuf := append(udpHeader, recvbuf...)
  384. // block here until we're exactly at the right time :)
  385. // Then send rtp audio packet to Discord over UDP
  386. select {
  387. case <-ticker.C:
  388. // continue
  389. case <-close:
  390. return
  391. }
  392. _, err := UDPConn.Write(sendbuf)
  393. if err != nil {
  394. fmt.Println("error writing to udp connection: ", err)
  395. return
  396. }
  397. if (sequence) == 0xFFFF {
  398. sequence = 0
  399. } else {
  400. sequence++
  401. }
  402. if (timestamp + uint32(size)) >= 0xFFFFFFFF {
  403. timestamp = 0
  404. } else {
  405. timestamp += uint32(size)
  406. }
  407. }
  408. }
  409. // A Packet contains the headers and content of a received voice packet.
  410. type Packet struct {
  411. SSRC uint32
  412. Sequence uint16
  413. Timestamp uint32
  414. Type []byte
  415. Opus []byte
  416. PCM []int16
  417. }
  418. // opusReceiver listens on the UDP socket for incoming packets
  419. // and sends them across the given channel
  420. // NOTE :: This function may change names later.
  421. func (v *Voice) opusReceiver(UDPConn *net.UDPConn, close <-chan struct{}, c chan *Packet) {
  422. if UDPConn == nil || close == nil {
  423. return
  424. }
  425. p := Packet{}
  426. recvbuf := make([]byte, 1024)
  427. for {
  428. rlen, err := UDPConn.Read(recvbuf)
  429. if err != nil {
  430. fmt.Println("opusReceiver UDP Read error:", err)
  431. return
  432. }
  433. select {
  434. case <-close:
  435. return
  436. default:
  437. // continue loop
  438. }
  439. // For now, skip anything except audio.
  440. if rlen < 12 || recvbuf[0] != 0x80 {
  441. continue
  442. }
  443. p.Type = recvbuf[0:2]
  444. p.Sequence = binary.BigEndian.Uint16(recvbuf[2:4])
  445. p.Timestamp = binary.BigEndian.Uint32(recvbuf[4:8])
  446. p.SSRC = binary.BigEndian.Uint32(recvbuf[8:12])
  447. p.Opus = recvbuf[12:rlen]
  448. if c != nil {
  449. c <- &p
  450. }
  451. }
  452. }
  453. // Close closes the voice ws and udp connections
  454. func (v *Voice) Close() {
  455. v.Lock()
  456. defer v.Unlock()
  457. v.Ready = false
  458. if v.close != nil {
  459. close(v.close)
  460. v.close = nil
  461. }
  462. if v.UDPConn != nil {
  463. err := v.UDPConn.Close()
  464. if err != nil {
  465. fmt.Println("error closing udp connection: ", err)
  466. }
  467. v.UDPConn = nil
  468. }
  469. if v.wsConn != nil {
  470. err := v.wsConn.Close()
  471. if err != nil {
  472. fmt.Println("error closing websocket connection: ", err)
  473. }
  474. v.wsConn = nil
  475. }
  476. }