voice.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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.UDPConn != nil {
  258. return fmt.Errorf("udp connection already open")
  259. }
  260. if v.close == nil {
  261. return fmt.Errorf("nil close channel")
  262. }
  263. if v.endpoint == "" {
  264. return fmt.Errorf("empty endpoint")
  265. }
  266. host := fmt.Sprintf("%s:%d", strings.TrimSuffix(v.endpoint, ":80"), v.OP2.Port)
  267. addr, err := net.ResolveUDPAddr("udp", host)
  268. if err != nil {
  269. fmt.Println("udpOpen resolve addr error: ", err)
  270. // TODO better logging
  271. return
  272. }
  273. v.UDPConn, err = net.DialUDP("udp", nil, addr)
  274. if err != nil {
  275. fmt.Println("udpOpen dial udp error: ", err)
  276. // TODO better logging
  277. return
  278. }
  279. // Create a 70 byte array and put the SSRC code from the Op 2 Voice event
  280. // into it. Then send that over the UDP connection to Discord
  281. sb := make([]byte, 70)
  282. binary.BigEndian.PutUint32(sb, v.OP2.SSRC)
  283. _, err = v.UDPConn.Write(sb)
  284. if err != nil {
  285. fmt.Println("udpOpen udp write error : ", err)
  286. // TODO better logging
  287. return
  288. }
  289. // Create a 70 byte array and listen for the initial handshake response
  290. // from Discord. Once we get it parse the IP and PORT information out
  291. // of the response. This should be our public IP and PORT as Discord
  292. // saw us.
  293. rb := make([]byte, 70)
  294. rlen, _, err := v.UDPConn.ReadFromUDP(rb)
  295. if err != nil {
  296. fmt.Println("udpOpen udp read error : ", err)
  297. // TODO better logging
  298. return
  299. }
  300. if rlen < 70 {
  301. fmt.Println("Voice RLEN should be 70 but isn't")
  302. }
  303. // Loop over position 4 though 20 to grab the IP address
  304. // Should never be beyond position 20.
  305. var ip string
  306. for i := 4; i < 20; i++ {
  307. if rb[i] == 0 {
  308. break
  309. }
  310. ip += string(rb[i])
  311. }
  312. // Grab port from position 68 and 69
  313. port := binary.LittleEndian.Uint16(rb[68:70])
  314. // Take the data from above and send it back to Discord to finalize
  315. // the UDP connection handshake.
  316. data := voiceUDPOp{1, voiceUDPD{"udp", voiceUDPData{ip, port, "plain"}}}
  317. err = v.wsConn.WriteJSON(data)
  318. if err != nil {
  319. fmt.Println("udpOpen write json error:", err)
  320. return
  321. }
  322. // start udpKeepAlive
  323. go v.udpKeepAlive(v.UDPConn, v.close, 5*time.Second)
  324. // TODO: find a way to check that it fired off okay
  325. return
  326. }
  327. // udpKeepAlive sends a udp packet to keep the udp connection open
  328. // This is still a bit of a "proof of concept"
  329. func (v *Voice) udpKeepAlive(UDPConn *net.UDPConn, close <-chan struct{}, i time.Duration) {
  330. if UDPConn == nil || close == nil {
  331. return
  332. }
  333. var err error
  334. var sequence uint64
  335. packet := make([]byte, 8)
  336. ticker := time.NewTicker(i)
  337. for {
  338. binary.LittleEndian.PutUint64(packet, sequence)
  339. sequence++
  340. _, err = UDPConn.Write(packet)
  341. if err != nil {
  342. fmt.Println("udpKeepAlive udp write error : ", err)
  343. return
  344. }
  345. select {
  346. case <-ticker.C:
  347. // continue loop and send keepalive
  348. case <-close:
  349. return
  350. }
  351. }
  352. }
  353. // opusSender will listen on the given channel and send any
  354. // pre-encoded opus audio to Discord. Supposedly.
  355. func (v *Voice) opusSender(UDPConn *net.UDPConn, close <-chan struct{}, opus <-chan []byte, rate, size int) {
  356. if UDPConn == nil || close == nil {
  357. return
  358. }
  359. runtime.LockOSThread()
  360. // Voice is now ready to receive audio packets
  361. // TODO: this needs reviewed as I think there must be a better way.
  362. v.Ready = true
  363. defer func() { v.Ready = false }()
  364. var sequence uint16
  365. var timestamp uint32
  366. var recvbuf []byte
  367. var ok bool
  368. udpHeader := make([]byte, 12)
  369. // build the parts that don't change in the udpHeader
  370. udpHeader[0] = 0x80
  371. udpHeader[1] = 0x78
  372. binary.BigEndian.PutUint32(udpHeader[8:], v.OP2.SSRC)
  373. // start a send loop that loops until buf chan is closed
  374. ticker := time.NewTicker(time.Millisecond * time.Duration(size/(rate/1000)))
  375. for {
  376. // Get data from chan. If chan is closed, return.
  377. select {
  378. case <-close:
  379. return
  380. case recvbuf, ok = <-opus:
  381. if !ok {
  382. return
  383. }
  384. // else, continue loop
  385. }
  386. // Add sequence and timestamp to udpPacket
  387. binary.BigEndian.PutUint16(udpHeader[2:], sequence)
  388. binary.BigEndian.PutUint32(udpHeader[4:], timestamp)
  389. // Combine the UDP Header and the opus data
  390. sendbuf := append(udpHeader, recvbuf...)
  391. // block here until we're exactly at the right time :)
  392. // Then send rtp audio packet to Discord over UDP
  393. select {
  394. case <-close:
  395. return
  396. case <-ticker.C:
  397. // continue
  398. }
  399. _, err := UDPConn.Write(sendbuf)
  400. if err != nil {
  401. fmt.Println("error writing to udp connection: ", err)
  402. return
  403. }
  404. if (sequence) == 0xFFFF {
  405. sequence = 0
  406. } else {
  407. sequence++
  408. }
  409. if (timestamp + uint32(size)) >= 0xFFFFFFFF {
  410. timestamp = 0
  411. } else {
  412. timestamp += uint32(size)
  413. }
  414. }
  415. }
  416. // A Packet contains the headers and content of a received voice packet.
  417. type Packet struct {
  418. SSRC uint32
  419. Sequence uint16
  420. Timestamp uint32
  421. Type []byte
  422. Opus []byte
  423. PCM []int16
  424. }
  425. // opusReceiver listens on the UDP socket for incoming packets
  426. // and sends them across the given channel
  427. // NOTE :: This function may change names later.
  428. func (v *Voice) opusReceiver(UDPConn *net.UDPConn, close <-chan struct{}, c chan *Packet) {
  429. if UDPConn == nil || close == nil {
  430. return
  431. }
  432. p := Packet{}
  433. recvbuf := make([]byte, 1024)
  434. for {
  435. rlen, err := UDPConn.Read(recvbuf)
  436. if err != nil {
  437. fmt.Println("opusReceiver UDP Read error:", err)
  438. return
  439. }
  440. select {
  441. case <-close:
  442. return
  443. default:
  444. // continue loop
  445. }
  446. // For now, skip anything except audio.
  447. if rlen < 12 || recvbuf[0] != 0x80 {
  448. continue
  449. }
  450. p.Type = recvbuf[0:2]
  451. p.Sequence = binary.BigEndian.Uint16(recvbuf[2:4])
  452. p.Timestamp = binary.BigEndian.Uint32(recvbuf[4:8])
  453. p.SSRC = binary.BigEndian.Uint32(recvbuf[8:12])
  454. p.Opus = recvbuf[12:rlen]
  455. if c != nil {
  456. c <- &p
  457. }
  458. }
  459. }
  460. // Close closes the voice ws and udp connections
  461. func (v *Voice) Close() {
  462. v.Lock()
  463. defer v.Unlock()
  464. v.Ready = false
  465. if v.close != nil {
  466. close(v.close)
  467. v.close = nil
  468. }
  469. if v.UDPConn != nil {
  470. err := v.UDPConn.Close()
  471. if err != nil {
  472. fmt.Println("error closing udp connection: ", err)
  473. }
  474. v.UDPConn = nil
  475. }
  476. if v.wsConn != nil {
  477. err := v.wsConn.Close()
  478. if err != nil {
  479. fmt.Println("error closing websocket connection: ", err)
  480. }
  481. v.wsConn = nil
  482. }
  483. }