voice.go 16 KB

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