voice.go 17 KB

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