voice.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. Opus chan []byte // Chan for sending opus audio
  29. // FrameRate int // This can be used to set the FrameRate of Opus data
  30. // FrameSize int // This can be used to set the FrameSize of Opus data
  31. wsConn *websocket.Conn
  32. UDPConn *net.UDPConn // this will become unexported soon.
  33. sessionID string
  34. token string
  35. endpoint string
  36. guildID string
  37. channelID string
  38. userID string
  39. }
  40. // ------------------------------------------------------------------------------------------------
  41. // Code related to the Voice websocket connection
  42. // ------------------------------------------------------------------------------------------------
  43. // A voiceOP2 stores the data for the voice operation 2 websocket event
  44. // which is sort of like the voice READY packet
  45. type voiceOP2 struct {
  46. SSRC uint32 `json:"ssrc"`
  47. Port int `json:"port"`
  48. Modes []string `json:"modes"`
  49. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  50. }
  51. type voiceHandshakeData struct {
  52. ServerID string `json:"server_id"`
  53. UserID string `json:"user_id"`
  54. SessionID string `json:"session_id"`
  55. Token string `json:"token"`
  56. }
  57. type voiceHandshakeOp struct {
  58. Op int `json:"op"` // Always 0
  59. Data voiceHandshakeData `json:"d"`
  60. }
  61. // Open opens a voice connection. This should be called
  62. // after VoiceChannelJoin is used and the data VOICE websocket events
  63. // are captured.
  64. func (v *Voice) Open() (err error) {
  65. // TODO: How do we handle changing channels?
  66. // Don't open a websocket if one is already open
  67. if v.wsConn != nil {
  68. return
  69. }
  70. // Connect to Voice Websocket
  71. vg := fmt.Sprintf("wss://%s", strings.TrimSuffix(v.endpoint, ":80"))
  72. v.wsConn, _, err = websocket.DefaultDialer.Dial(vg, nil)
  73. if err != nil {
  74. fmt.Println("VOICE error opening websocket:", err)
  75. return
  76. }
  77. data := voiceHandshakeOp{0, voiceHandshakeData{v.guildID, v.userID, v.sessionID, v.token}}
  78. err = v.wsConn.WriteJSON(data)
  79. if err != nil {
  80. fmt.Println("VOICE error sending init packet:", err)
  81. return
  82. }
  83. // Start a listening for voice websocket events
  84. // TODO add a check here to make sure Listen worked by monitoring
  85. // a chan or bool?
  86. go v.wsListen()
  87. return
  88. }
  89. // Close closes the voice connection
  90. func (v *Voice) Close() {
  91. if v.UDPConn != nil {
  92. err := v.UDPConn.Close()
  93. if err != nil {
  94. fmt.Println("error closing udp connection: ", err)
  95. }
  96. }
  97. if v.wsConn != nil {
  98. err := v.wsConn.Close()
  99. if err != nil {
  100. fmt.Println("error closing websocket connection: ", err)
  101. }
  102. }
  103. }
  104. // wsListen listens on the voice websocket for messages and passes them
  105. // to the voice event handler. This is automatically called by the Open func
  106. func (v *Voice) wsListen() {
  107. for {
  108. messageType, message, err := v.wsConn.ReadMessage()
  109. if err != nil {
  110. // TODO: Handle this problem better.
  111. // TODO: needs proper logging
  112. fmt.Println("Voice Listen Error:", err)
  113. break
  114. }
  115. // Pass received message to voice event handler
  116. go v.wsEvent(messageType, message)
  117. }
  118. return
  119. }
  120. // wsEvent handles any voice websocket events. This is only called by the
  121. // wsListen() function.
  122. func (v *Voice) wsEvent(messageType int, message []byte) {
  123. if v.Debug {
  124. fmt.Println("wsEvent received: ", messageType)
  125. printJSON(message)
  126. }
  127. var e Event
  128. if err := json.Unmarshal(message, &e); err != nil {
  129. fmt.Println("wsEvent Unmarshall error: ", err)
  130. return
  131. }
  132. switch e.Operation {
  133. case 2: // READY
  134. v.OP2 = &voiceOP2{}
  135. if err := json.Unmarshal(e.RawData, v.OP2); err != nil {
  136. fmt.Println("voiceWS.onEvent OP2 Unmarshall error: ", err)
  137. printJSON(e.RawData) // TODO: Better error logging
  138. return
  139. }
  140. // Start the voice websocket heartbeat to keep the connection alive
  141. go v.wsHeartbeat(v.OP2.HeartbeatInterval)
  142. // TODO monitor a chan/bool to verify this was successful
  143. // Start the UDP connection
  144. err := v.udpOpen()
  145. if err != nil {
  146. fmt.Println("Error opening udp connection: ", err)
  147. return
  148. }
  149. // Start the opusSender.
  150. // TODO: Should we allow 48000/960 values to be user defined?
  151. v.Opus = make(chan []byte, 2)
  152. go v.opusSender(v.Opus, 48000, 960)
  153. return
  154. case 3: // HEARTBEAT response
  155. // add code to use this to track latency?
  156. return
  157. case 4:
  158. // TODO
  159. case 5:
  160. // SPEAKING TRUE/FALSE NOTIFICATION
  161. /*
  162. {
  163. "user_id": "1238921738912",
  164. "ssrc": 2,
  165. "speaking": false
  166. }
  167. */
  168. default:
  169. fmt.Println("UNKNOWN VOICE OP: ", e.Operation)
  170. printJSON(e.RawData)
  171. }
  172. return
  173. }
  174. type voiceHeartbeatOp struct {
  175. Op int `json:"op"` // Always 3
  176. Data int `json:"d"`
  177. }
  178. // wsHeartbeat sends regular heartbeats to voice Discord so it knows the client
  179. // is still connected. If you do not send these heartbeats Discord will
  180. // disconnect the websocket connection after a few seconds.
  181. func (v *Voice) wsHeartbeat(i time.Duration) {
  182. ticker := time.NewTicker(i * time.Millisecond)
  183. for {
  184. err := v.wsConn.WriteJSON(voiceHeartbeatOp{3, int(time.Now().Unix())})
  185. if err != nil {
  186. v.Ready = false
  187. fmt.Println("wsHeartbeat send error: ", err)
  188. return // TODO better logging
  189. }
  190. <-ticker.C
  191. }
  192. }
  193. type voiceSpeakingData struct {
  194. Speaking bool `json:"speaking"`
  195. Delay int `json:"delay"`
  196. }
  197. type voiceSpeakingOp struct {
  198. Op int `json:"op"` // Always 5
  199. Data voiceSpeakingData `json:"d"`
  200. }
  201. // Speaking sends a speaking notification to Discord over the voice websocket.
  202. // This must be sent as true prior to sending audio and should be set to false
  203. // once finished sending audio.
  204. // b : Send true if speaking, false if not.
  205. func (v *Voice) Speaking(b bool) (err error) {
  206. if v.wsConn == nil {
  207. return fmt.Errorf("No Voice websocket.")
  208. }
  209. data := voiceSpeakingOp{5, voiceSpeakingData{b, 0}}
  210. err = v.wsConn.WriteJSON(data)
  211. if err != nil {
  212. fmt.Println("Speaking() write json error:", err)
  213. return
  214. }
  215. return
  216. }
  217. // ------------------------------------------------------------------------------------------------
  218. // Code related to the Voice UDP connection
  219. // ------------------------------------------------------------------------------------------------
  220. type voiceUDPData struct {
  221. Address string `json:"address"` // Public IP of machine running this code
  222. Port uint16 `json:"port"` // UDP Port of machine running this code
  223. Mode string `json:"mode"` // plain or ? (plain or encrypted)
  224. }
  225. type voiceUDPD struct {
  226. Protocol string `json:"protocol"` // Always "udp" ?
  227. Data voiceUDPData `json:"data"`
  228. }
  229. type voiceUDPOp struct {
  230. Op int `json:"op"` // Always 1
  231. Data voiceUDPD `json:"d"`
  232. }
  233. // udpOpen opens a UDP connection to the voice server and completes the
  234. // initial required handshake. This connection is left open in the session
  235. // and can be used to send or receive audio. This should only be called
  236. // from voice.wsEvent OP2
  237. func (v *Voice) udpOpen() (err error) {
  238. host := fmt.Sprintf("%s:%d", strings.TrimSuffix(v.endpoint, ":80"), v.OP2.Port)
  239. addr, err := net.ResolveUDPAddr("udp", host)
  240. if err != nil {
  241. fmt.Println("udpOpen resolve addr error: ", err)
  242. // TODO better logging
  243. return
  244. }
  245. v.UDPConn, err = net.DialUDP("udp", nil, addr)
  246. if err != nil {
  247. fmt.Println("udpOpen dial udp error: ", err)
  248. // TODO better logging
  249. return
  250. }
  251. // Create a 70 byte array and put the SSRC code from the Op 2 Voice event
  252. // into it. Then send that over the UDP connection to Discord
  253. sb := make([]byte, 70)
  254. binary.BigEndian.PutUint32(sb, v.OP2.SSRC)
  255. _, err = v.UDPConn.Write(sb)
  256. if err != nil {
  257. fmt.Println("udpOpen udp write error : ", err)
  258. // TODO better logging
  259. return
  260. }
  261. // Create a 70 byte array and listen for the initial handshake response
  262. // from Discord. Once we get it parse the IP and PORT information out
  263. // of the response. This should be our public IP and PORT as Discord
  264. // saw us.
  265. rb := make([]byte, 70)
  266. rlen, _, err := v.UDPConn.ReadFromUDP(rb)
  267. if err != nil {
  268. fmt.Println("udpOpen udp read error : ", err)
  269. // TODO better logging
  270. return
  271. }
  272. if rlen < 70 {
  273. fmt.Println("Voice RLEN should be 70 but isn't")
  274. }
  275. // Loop over position 4 though 20 to grab the IP address
  276. // Should never be beyond position 20.
  277. var ip string
  278. for i := 4; i < 20; i++ {
  279. if rb[i] == 0 {
  280. break
  281. }
  282. ip += string(rb[i])
  283. }
  284. // Grab port from position 68 and 69
  285. port := binary.LittleEndian.Uint16(rb[68:70])
  286. // Take the data from above and send it back to Discord to finalize
  287. // the UDP connection handshake.
  288. data := voiceUDPOp{1, voiceUDPD{"udp", voiceUDPData{ip, port, "plain"}}}
  289. err = v.wsConn.WriteJSON(data)
  290. if err != nil {
  291. fmt.Println("udpOpen write json error:", err)
  292. return
  293. }
  294. // start udpKeepAlive
  295. go v.udpKeepAlive(5 * time.Second)
  296. // TODO: find a way to check that it fired off okay
  297. return
  298. }
  299. // udpKeepAlive sends a udp packet to keep the udp connection open
  300. // This is still a bit of a "proof of concept"
  301. func (v *Voice) udpKeepAlive(i time.Duration) {
  302. var err error
  303. var sequence uint64
  304. packet := make([]byte, 8)
  305. ticker := time.NewTicker(i)
  306. for {
  307. // TODO: Add a way to break from loop
  308. binary.LittleEndian.PutUint64(packet, sequence)
  309. sequence++
  310. _, err = v.UDPConn.Write(packet)
  311. if err != nil {
  312. fmt.Println("udpKeepAlive udp write error : ", err)
  313. return
  314. }
  315. <-ticker.C
  316. }
  317. }
  318. // opusSender will listen on the given channel and send any
  319. // pre-encoded opus audio to Discord. Supposedly.
  320. func (v *Voice) opusSender(opus <-chan []byte, rate, size int) {
  321. // TODO: Better checking to prevent this from running more than
  322. // one instance at a time.
  323. v.Lock()
  324. if opus == nil {
  325. v.Unlock()
  326. return
  327. }
  328. v.Unlock()
  329. runtime.LockOSThread()
  330. // Voice is now ready to receive audio packets
  331. // TODO: this needs reviewed as I think there must be a better way.
  332. v.Ready = true
  333. defer func() { v.Ready = false }()
  334. var sequence uint16
  335. var timestamp uint32
  336. udpHeader := make([]byte, 12)
  337. // build the parts that don't change in the udpHeader
  338. udpHeader[0] = 0x80
  339. udpHeader[1] = 0x78
  340. binary.BigEndian.PutUint32(udpHeader[8:], v.OP2.SSRC)
  341. // start a send loop that loops until buf chan is closed
  342. ticker := time.NewTicker(time.Millisecond * time.Duration(size/(rate/1000)))
  343. for {
  344. // Add sequence and timestamp to udpPacket
  345. binary.BigEndian.PutUint16(udpHeader[2:], sequence)
  346. binary.BigEndian.PutUint32(udpHeader[4:], timestamp)
  347. // Get data from chan. If chan is closed, return.
  348. recvbuf, ok := <-opus
  349. if !ok {
  350. return
  351. }
  352. // Combine the UDP Header and the opus data
  353. sendbuf := append(udpHeader, recvbuf...)
  354. // block here until we're exactly at the right time :)
  355. // Then send rtp audio packet to Discord over UDP
  356. <-ticker.C
  357. _, err := v.UDPConn.Write(sendbuf)
  358. if err != nil {
  359. fmt.Println("error writing to udp connection: ", err)
  360. }
  361. if (sequence) == 0xFFFF {
  362. sequence = 0
  363. } else {
  364. sequence++
  365. }
  366. if (timestamp + uint32(size)) >= 0xFFFFFFFF {
  367. timestamp = 0
  368. } else {
  369. timestamp += uint32(size)
  370. }
  371. }
  372. }