voice.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. "log"
  13. "net"
  14. "runtime"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "time"
  19. "github.com/gorilla/websocket"
  20. "golang.org/x/crypto/nacl/secretbox"
  21. )
  22. // ------------------------------------------------------------------------------------------------
  23. // Code related to both VoiceConnection Websocket and UDP connections.
  24. // ------------------------------------------------------------------------------------------------
  25. // A VoiceConnection struct holds all the data and functions related to a Discord Voice Connection.
  26. type VoiceConnection struct {
  27. sync.RWMutex
  28. Debug bool // If true, print extra logging -- DEPRECATED
  29. LogLevel int
  30. Ready bool // If true, voice is ready to send/receive audio
  31. UserID string
  32. GuildID string
  33. ChannelID string
  34. deaf bool
  35. mute bool
  36. speaking bool
  37. reconnecting bool // If true, voice connection is trying to reconnect
  38. OpusSend chan []byte // Chan for sending opus audio
  39. OpusRecv chan *Packet // Chan for receiving opus audio
  40. wsConn *websocket.Conn
  41. wsMutex sync.Mutex
  42. udpConn *net.UDPConn
  43. session *Session
  44. sessionID string
  45. token string
  46. endpoint string
  47. // Used to send a close signal to goroutines
  48. close chan struct{}
  49. // Used to allow blocking until connected
  50. connected chan bool
  51. // Used to pass the sessionid from onVoiceStateUpdate
  52. // sessionRecv chan string UNUSED ATM
  53. op4 voiceOP4
  54. op2 voiceOP2
  55. voiceSpeakingUpdateHandlers []VoiceSpeakingUpdateHandler
  56. }
  57. // VoiceSpeakingUpdateHandler type provides a function defination for the
  58. // VoiceSpeakingUpdate event
  59. type VoiceSpeakingUpdateHandler func(vc *VoiceConnection, vs *VoiceSpeakingUpdate)
  60. // Speaking sends a speaking notification to Discord over the voice websocket.
  61. // This must be sent as true prior to sending audio and should be set to false
  62. // once finished sending audio.
  63. // b : Send true if speaking, false if not.
  64. func (v *VoiceConnection) Speaking(b bool) (err error) {
  65. v.log(LogDebug, "called (%t)", b)
  66. type voiceSpeakingData struct {
  67. Speaking bool `json:"speaking"`
  68. Delay int `json:"delay"`
  69. }
  70. type voiceSpeakingOp struct {
  71. Op int `json:"op"` // Always 5
  72. Data voiceSpeakingData `json:"d"`
  73. }
  74. if v.wsConn == nil {
  75. return fmt.Errorf("no VoiceConnection websocket")
  76. }
  77. data := voiceSpeakingOp{5, voiceSpeakingData{b, 0}}
  78. v.wsMutex.Lock()
  79. err = v.wsConn.WriteJSON(data)
  80. v.wsMutex.Unlock()
  81. v.Lock()
  82. defer v.Unlock()
  83. if err != nil {
  84. v.speaking = false
  85. log.Println("Speaking() write json error:", err)
  86. return
  87. }
  88. v.speaking = b
  89. return
  90. }
  91. // ChangeChannel sends Discord a request to change channels within a Guild
  92. // !!! NOTE !!! This function may be removed in favour of just using ChannelVoiceJoin
  93. func (v *VoiceConnection) ChangeChannel(channelID string, mute, deaf bool) (err error) {
  94. v.log(LogInformational, "called")
  95. data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, &channelID, mute, deaf}}
  96. v.wsMutex.Lock()
  97. err = v.session.wsConn.WriteJSON(data)
  98. v.wsMutex.Unlock()
  99. if err != nil {
  100. return
  101. }
  102. v.ChannelID = channelID
  103. v.deaf = deaf
  104. v.mute = mute
  105. v.speaking = false
  106. return
  107. }
  108. // Disconnect disconnects from this voice channel and closes the websocket
  109. // and udp connections to Discord.
  110. // !!! NOTE !!! this function may be removed in favour of ChannelVoiceLeave
  111. func (v *VoiceConnection) Disconnect() (err error) {
  112. // Send a OP4 with a nil channel to disconnect
  113. if v.sessionID != "" {
  114. data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, nil, true, true}}
  115. v.session.wsMutex.Lock()
  116. err = v.session.wsConn.WriteJSON(data)
  117. v.session.wsMutex.Unlock()
  118. v.sessionID = ""
  119. }
  120. // Close websocket and udp connections
  121. v.Close()
  122. v.log(LogInformational, "Deleting VoiceConnection %s", v.GuildID)
  123. v.session.Lock()
  124. delete(v.session.VoiceConnections, v.GuildID)
  125. v.session.Unlock()
  126. return
  127. }
  128. // Close closes the voice ws and udp connections
  129. func (v *VoiceConnection) Close() {
  130. v.log(LogInformational, "called")
  131. v.Lock()
  132. defer v.Unlock()
  133. v.Ready = false
  134. v.speaking = false
  135. if v.close != nil {
  136. v.log(LogInformational, "closing v.close")
  137. close(v.close)
  138. v.close = nil
  139. }
  140. if v.udpConn != nil {
  141. v.log(LogInformational, "closing udp")
  142. err := v.udpConn.Close()
  143. if err != nil {
  144. log.Println("error closing udp connection: ", err)
  145. }
  146. v.udpConn = nil
  147. }
  148. if v.wsConn != nil {
  149. v.log(LogInformational, "sending close frame")
  150. // To cleanly close a connection, a client should send a close
  151. // frame and wait for the server to close the connection.
  152. v.wsMutex.Lock()
  153. err := v.wsConn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
  154. v.wsMutex.Unlock()
  155. if err != nil {
  156. v.log(LogError, "error closing websocket, %s", err)
  157. }
  158. // TODO: Wait for Discord to actually close the connection.
  159. time.Sleep(1 * time.Second)
  160. v.log(LogInformational, "closing websocket")
  161. err = v.wsConn.Close()
  162. if err != nil {
  163. v.log(LogError, "error closing websocket, %s", err)
  164. }
  165. v.wsConn = nil
  166. }
  167. }
  168. // AddHandler adds a Handler for VoiceSpeakingUpdate events.
  169. func (v *VoiceConnection) AddHandler(h VoiceSpeakingUpdateHandler) {
  170. v.Lock()
  171. defer v.Unlock()
  172. v.voiceSpeakingUpdateHandlers = append(v.voiceSpeakingUpdateHandlers, h)
  173. }
  174. // VoiceSpeakingUpdate is a struct for a VoiceSpeakingUpdate event.
  175. type VoiceSpeakingUpdate struct {
  176. UserID string `json:"user_id"`
  177. SSRC int `json:"ssrc"`
  178. Speaking bool `json:"speaking"`
  179. }
  180. // ------------------------------------------------------------------------------------------------
  181. // Unexported Internal Functions Below.
  182. // ------------------------------------------------------------------------------------------------
  183. // A voiceOP4 stores the data for the voice operation 4 websocket event
  184. // which provides us with the NaCl SecretBox encryption key
  185. type voiceOP4 struct {
  186. SecretKey [32]byte `json:"secret_key"`
  187. Mode string `json:"mode"`
  188. }
  189. // A voiceOP2 stores the data for the voice operation 2 websocket event
  190. // which is sort of like the voice READY packet
  191. type voiceOP2 struct {
  192. SSRC uint32 `json:"ssrc"`
  193. Port int `json:"port"`
  194. Modes []string `json:"modes"`
  195. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  196. }
  197. // WaitUntilConnected waits for the Voice Connection to
  198. // become ready, if it does not become ready it retuns an err
  199. func (v *VoiceConnection) waitUntilConnected() error {
  200. v.log(LogInformational, "called")
  201. i := 0
  202. for {
  203. v.RLock()
  204. ready := v.Ready
  205. v.RUnlock()
  206. if ready {
  207. return nil
  208. }
  209. if i > 10 {
  210. return fmt.Errorf("timeout waiting for voice")
  211. }
  212. time.Sleep(1 * time.Second)
  213. i++
  214. }
  215. }
  216. // Open opens a voice connection. This should be called
  217. // after VoiceChannelJoin is used and the data VOICE websocket events
  218. // are captured.
  219. func (v *VoiceConnection) open() (err error) {
  220. v.log(LogInformational, "called")
  221. v.Lock()
  222. defer v.Unlock()
  223. // Don't open a websocket if one is already open
  224. if v.wsConn != nil {
  225. v.log(LogWarning, "refusing to overwrite non-nil websocket")
  226. return
  227. }
  228. // TODO temp? loop to wait for the SessionID
  229. i := 0
  230. for {
  231. if v.sessionID != "" {
  232. break
  233. }
  234. if i > 20 { // only loop for up to 1 second total
  235. return fmt.Errorf("did not receive voice Session ID in time")
  236. }
  237. time.Sleep(50 * time.Millisecond)
  238. i++
  239. }
  240. // Connect to VoiceConnection Websocket
  241. vg := "wss://" + strings.TrimSuffix(v.endpoint, ":80")
  242. v.log(LogInformational, "connecting to voice endpoint %s", vg)
  243. v.wsConn, _, err = websocket.DefaultDialer.Dial(vg, nil)
  244. if err != nil {
  245. v.log(LogWarning, "error connecting to voice endpoint %s, %s", vg, err)
  246. v.log(LogDebug, "voice struct: %#v\n", v)
  247. return
  248. }
  249. type voiceHandshakeData struct {
  250. ServerID string `json:"server_id"`
  251. UserID string `json:"user_id"`
  252. SessionID string `json:"session_id"`
  253. Token string `json:"token"`
  254. }
  255. type voiceHandshakeOp struct {
  256. Op int `json:"op"` // Always 0
  257. Data voiceHandshakeData `json:"d"`
  258. }
  259. data := voiceHandshakeOp{0, voiceHandshakeData{v.GuildID, v.UserID, v.sessionID, v.token}}
  260. err = v.wsConn.WriteJSON(data)
  261. if err != nil {
  262. v.log(LogWarning, "error sending init packet, %s", err)
  263. return
  264. }
  265. v.close = make(chan struct{})
  266. go v.wsListen(v.wsConn, v.close)
  267. // add loop/check for Ready bool here?
  268. // then return false if not ready?
  269. // but then wsListen will also err.
  270. return
  271. }
  272. // wsListen listens on the voice websocket for messages and passes them
  273. // to the voice event handler. This is automatically called by the Open func
  274. func (v *VoiceConnection) wsListen(wsConn *websocket.Conn, close <-chan struct{}) {
  275. v.log(LogInformational, "called")
  276. for {
  277. _, message, err := v.wsConn.ReadMessage()
  278. if err != nil {
  279. // Detect if we have been closed manually. If a Close() has already
  280. // happened, the websocket we are listening on will be different to the
  281. // current session.
  282. v.RLock()
  283. sameConnection := v.wsConn == wsConn
  284. v.RUnlock()
  285. if sameConnection {
  286. v.log(LogError, "voice endpoint %s websocket closed unexpectantly, %s", v.endpoint, err)
  287. // Start reconnect goroutine then exit.
  288. go v.reconnect()
  289. }
  290. return
  291. }
  292. // Pass received message to voice event handler
  293. select {
  294. case <-close:
  295. return
  296. default:
  297. go v.onEvent(message)
  298. }
  299. }
  300. }
  301. // wsEvent handles any voice websocket events. This is only called by the
  302. // wsListen() function.
  303. func (v *VoiceConnection) onEvent(message []byte) {
  304. v.log(LogDebug, "received: %s", string(message))
  305. var e Event
  306. if err := json.Unmarshal(message, &e); err != nil {
  307. v.log(LogError, "unmarshall error, %s", err)
  308. return
  309. }
  310. switch e.Operation {
  311. case 2: // READY
  312. if err := json.Unmarshal(e.RawData, &v.op2); err != nil {
  313. v.log(LogError, "OP2 unmarshall error, %s, %s", err, string(e.RawData))
  314. return
  315. }
  316. // Start the voice websocket heartbeat to keep the connection alive
  317. go v.wsHeartbeat(v.wsConn, v.close, v.op2.HeartbeatInterval)
  318. // TODO monitor a chan/bool to verify this was successful
  319. // Start the UDP connection
  320. err := v.udpOpen()
  321. if err != nil {
  322. v.log(LogError, "error opening udp connection, %s", err)
  323. return
  324. }
  325. // Start the opusSender.
  326. // TODO: Should we allow 48000/960 values to be user defined?
  327. if v.OpusSend == nil {
  328. v.OpusSend = make(chan []byte, 2)
  329. }
  330. go v.opusSender(v.udpConn, v.close, v.OpusSend, 48000, 960)
  331. // Start the opusReceiver
  332. if !v.deaf {
  333. if v.OpusRecv == nil {
  334. v.OpusRecv = make(chan *Packet, 2)
  335. }
  336. go v.opusReceiver(v.udpConn, v.close, v.OpusRecv)
  337. }
  338. return
  339. case 3: // HEARTBEAT response
  340. // add code to use this to track latency?
  341. return
  342. case 4: // udp encryption secret key
  343. v.Lock()
  344. defer v.Unlock()
  345. v.op4 = voiceOP4{}
  346. if err := json.Unmarshal(e.RawData, &v.op4); err != nil {
  347. v.log(LogError, "OP4 unmarshall error, %s, %s", err, string(e.RawData))
  348. return
  349. }
  350. return
  351. case 5:
  352. if len(v.voiceSpeakingUpdateHandlers) == 0 {
  353. return
  354. }
  355. voiceSpeakingUpdate := &VoiceSpeakingUpdate{}
  356. if err := json.Unmarshal(e.RawData, voiceSpeakingUpdate); err != nil {
  357. v.log(LogError, "OP5 unmarshall error, %s, %s", err, string(e.RawData))
  358. return
  359. }
  360. for _, h := range v.voiceSpeakingUpdateHandlers {
  361. h(v, voiceSpeakingUpdate)
  362. }
  363. default:
  364. v.log(LogDebug, "unknown voice operation, %d, %s", e.Operation, string(e.RawData))
  365. }
  366. return
  367. }
  368. type voiceHeartbeatOp struct {
  369. Op int `json:"op"` // Always 3
  370. Data int `json:"d"`
  371. }
  372. // NOTE :: When a guild voice server changes how do we shut this down
  373. // properly, so a new connection can be setup without fuss?
  374. //
  375. // wsHeartbeat sends regular heartbeats to voice Discord so it knows the client
  376. // is still connected. If you do not send these heartbeats Discord will
  377. // disconnect the websocket connection after a few seconds.
  378. func (v *VoiceConnection) wsHeartbeat(wsConn *websocket.Conn, close <-chan struct{}, i time.Duration) {
  379. if close == nil || wsConn == nil {
  380. return
  381. }
  382. var err error
  383. ticker := time.NewTicker(i * time.Millisecond)
  384. defer ticker.Stop()
  385. for {
  386. v.log(LogDebug, "sending heartbeat packet")
  387. v.wsMutex.Lock()
  388. err = wsConn.WriteJSON(voiceHeartbeatOp{3, int(time.Now().Unix())})
  389. v.wsMutex.Unlock()
  390. if err != nil {
  391. v.log(LogError, "error sending heartbeat to voice endpoint %s, %s", v.endpoint, err)
  392. return
  393. }
  394. select {
  395. case <-ticker.C:
  396. // continue loop and send heartbeat
  397. case <-close:
  398. return
  399. }
  400. }
  401. }
  402. // ------------------------------------------------------------------------------------------------
  403. // Code related to the VoiceConnection UDP connection
  404. // ------------------------------------------------------------------------------------------------
  405. type voiceUDPData struct {
  406. Address string `json:"address"` // Public IP of machine running this code
  407. Port uint16 `json:"port"` // UDP Port of machine running this code
  408. Mode string `json:"mode"` // always "xsalsa20_poly1305"
  409. }
  410. type voiceUDPD struct {
  411. Protocol string `json:"protocol"` // Always "udp" ?
  412. Data voiceUDPData `json:"data"`
  413. }
  414. type voiceUDPOp struct {
  415. Op int `json:"op"` // Always 1
  416. Data voiceUDPD `json:"d"`
  417. }
  418. // udpOpen opens a UDP connection to the voice server and completes the
  419. // initial required handshake. This connection is left open in the session
  420. // and can be used to send or receive audio. This should only be called
  421. // from voice.wsEvent OP2
  422. func (v *VoiceConnection) udpOpen() (err error) {
  423. v.Lock()
  424. defer v.Unlock()
  425. if v.wsConn == nil {
  426. return fmt.Errorf("nil voice websocket")
  427. }
  428. if v.udpConn != nil {
  429. return fmt.Errorf("udp connection already open")
  430. }
  431. if v.close == nil {
  432. return fmt.Errorf("nil close channel")
  433. }
  434. if v.endpoint == "" {
  435. return fmt.Errorf("empty endpoint")
  436. }
  437. host := strings.TrimSuffix(v.endpoint, ":80") + ":" + strconv.Itoa(v.op2.Port)
  438. addr, err := net.ResolveUDPAddr("udp", host)
  439. if err != nil {
  440. v.log(LogWarning, "error resolving udp host %s, %s", host, err)
  441. return
  442. }
  443. v.log(LogInformational, "connecting to udp addr %s", addr.String())
  444. v.udpConn, err = net.DialUDP("udp", nil, addr)
  445. if err != nil {
  446. v.log(LogWarning, "error connecting to udp addr %s, %s", addr.String(), err)
  447. return
  448. }
  449. // Create a 70 byte array and put the SSRC code from the Op 2 VoiceConnection event
  450. // into it. Then send that over the UDP connection to Discord
  451. sb := make([]byte, 70)
  452. binary.BigEndian.PutUint32(sb, v.op2.SSRC)
  453. _, err = v.udpConn.Write(sb)
  454. if err != nil {
  455. v.log(LogWarning, "udp write error to %s, %s", addr.String(), err)
  456. return
  457. }
  458. // Create a 70 byte array and listen for the initial handshake response
  459. // from Discord. Once we get it parse the IP and PORT information out
  460. // of the response. This should be our public IP and PORT as Discord
  461. // saw us.
  462. rb := make([]byte, 70)
  463. rlen, _, err := v.udpConn.ReadFromUDP(rb)
  464. if err != nil {
  465. v.log(LogWarning, "udp read error, %s, %s", addr.String(), err)
  466. return
  467. }
  468. if rlen < 70 {
  469. v.log(LogWarning, "received udp packet too small")
  470. return fmt.Errorf("received udp packet too small")
  471. }
  472. // Loop over position 4 through 20 to grab the IP address
  473. // Should never be beyond position 20.
  474. var ip string
  475. for i := 4; i < 20; i++ {
  476. if rb[i] == 0 {
  477. break
  478. }
  479. ip += string(rb[i])
  480. }
  481. // Grab port from position 68 and 69
  482. port := binary.LittleEndian.Uint16(rb[68:70])
  483. // Take the data from above and send it back to Discord to finalize
  484. // the UDP connection handshake.
  485. data := voiceUDPOp{1, voiceUDPD{"udp", voiceUDPData{ip, port, "xsalsa20_poly1305"}}}
  486. v.wsMutex.Lock()
  487. err = v.wsConn.WriteJSON(data)
  488. v.wsMutex.Unlock()
  489. if err != nil {
  490. v.log(LogWarning, "udp write error, %#v, %s", data, err)
  491. return
  492. }
  493. // start udpKeepAlive
  494. go v.udpKeepAlive(v.udpConn, v.close, 5*time.Second)
  495. // TODO: find a way to check that it fired off okay
  496. return
  497. }
  498. // udpKeepAlive sends a udp packet to keep the udp connection open
  499. // This is still a bit of a "proof of concept"
  500. func (v *VoiceConnection) udpKeepAlive(udpConn *net.UDPConn, close <-chan struct{}, i time.Duration) {
  501. if udpConn == nil || close == nil {
  502. return
  503. }
  504. var err error
  505. var sequence uint64
  506. packet := make([]byte, 8)
  507. ticker := time.NewTicker(i)
  508. defer ticker.Stop()
  509. for {
  510. binary.LittleEndian.PutUint64(packet, sequence)
  511. sequence++
  512. _, err = udpConn.Write(packet)
  513. if err != nil {
  514. v.log(LogError, "write error, %s", err)
  515. return
  516. }
  517. select {
  518. case <-ticker.C:
  519. // continue loop and send keepalive
  520. case <-close:
  521. return
  522. }
  523. }
  524. }
  525. // opusSender will listen on the given channel and send any
  526. // pre-encoded opus audio to Discord. Supposedly.
  527. func (v *VoiceConnection) opusSender(udpConn *net.UDPConn, close <-chan struct{}, opus <-chan []byte, rate, size int) {
  528. if udpConn == nil || close == nil {
  529. return
  530. }
  531. runtime.LockOSThread()
  532. // VoiceConnection is now ready to receive audio packets
  533. // TODO: this needs reviewed as I think there must be a better way.
  534. v.Lock()
  535. v.Ready = true
  536. v.Unlock()
  537. defer func() {
  538. v.Lock()
  539. v.Ready = false
  540. v.Unlock()
  541. }()
  542. var sequence uint16
  543. var timestamp uint32
  544. var recvbuf []byte
  545. var ok bool
  546. udpHeader := make([]byte, 12)
  547. var nonce [24]byte
  548. // build the parts that don't change in the udpHeader
  549. udpHeader[0] = 0x80
  550. udpHeader[1] = 0x78
  551. binary.BigEndian.PutUint32(udpHeader[8:], v.op2.SSRC)
  552. // start a send loop that loops until buf chan is closed
  553. ticker := time.NewTicker(time.Millisecond * time.Duration(size/(rate/1000)))
  554. defer ticker.Stop()
  555. for {
  556. // Get data from chan. If chan is closed, return.
  557. select {
  558. case <-close:
  559. return
  560. case recvbuf, ok = <-opus:
  561. if !ok {
  562. return
  563. }
  564. // else, continue loop
  565. }
  566. v.RLock()
  567. speaking := v.speaking
  568. v.RUnlock()
  569. if !speaking {
  570. err := v.Speaking(true)
  571. if err != nil {
  572. v.log(LogError, "error sending speaking packet, %s", err)
  573. }
  574. }
  575. // Add sequence and timestamp to udpPacket
  576. binary.BigEndian.PutUint16(udpHeader[2:], sequence)
  577. binary.BigEndian.PutUint32(udpHeader[4:], timestamp)
  578. // encrypt the opus data
  579. copy(nonce[:], udpHeader)
  580. v.RLock()
  581. sendbuf := secretbox.Seal(udpHeader, recvbuf, &nonce, &v.op4.SecretKey)
  582. v.RUnlock()
  583. // block here until we're exactly at the right time :)
  584. // Then send rtp audio packet to Discord over UDP
  585. select {
  586. case <-close:
  587. return
  588. case <-ticker.C:
  589. // continue
  590. }
  591. _, err := udpConn.Write(sendbuf)
  592. if err != nil {
  593. v.log(LogError, "udp write error, %s", err)
  594. v.log(LogDebug, "voice struct: %#v\n", v)
  595. return
  596. }
  597. if (sequence) == 0xFFFF {
  598. sequence = 0
  599. } else {
  600. sequence++
  601. }
  602. if (timestamp + uint32(size)) >= 0xFFFFFFFF {
  603. timestamp = 0
  604. } else {
  605. timestamp += uint32(size)
  606. }
  607. }
  608. }
  609. // A Packet contains the headers and content of a received voice packet.
  610. type Packet struct {
  611. SSRC uint32
  612. Sequence uint16
  613. Timestamp uint32
  614. Type []byte
  615. Opus []byte
  616. PCM []int16
  617. }
  618. // opusReceiver listens on the UDP socket for incoming packets
  619. // and sends them across the given channel
  620. // NOTE :: This function may change names later.
  621. func (v *VoiceConnection) opusReceiver(udpConn *net.UDPConn, close <-chan struct{}, c chan *Packet) {
  622. if udpConn == nil || close == nil {
  623. return
  624. }
  625. recvbuf := make([]byte, 1024)
  626. var nonce [24]byte
  627. for {
  628. rlen, err := udpConn.Read(recvbuf)
  629. if err != nil {
  630. // Detect if we have been closed manually. If a Close() has already
  631. // happened, the udp connection we are listening on will be different
  632. // to the current session.
  633. v.RLock()
  634. sameConnection := v.udpConn == udpConn
  635. v.RUnlock()
  636. if sameConnection {
  637. v.log(LogError, "udp read error, %s, %s", v.endpoint, err)
  638. v.log(LogDebug, "voice struct: %#v\n", v)
  639. go v.reconnect()
  640. }
  641. return
  642. }
  643. select {
  644. case <-close:
  645. return
  646. default:
  647. // continue loop
  648. }
  649. // For now, skip anything except audio.
  650. if rlen < 12 || recvbuf[0] != 0x80 {
  651. continue
  652. }
  653. // build a audio packet struct
  654. p := Packet{}
  655. p.Type = recvbuf[0:2]
  656. p.Sequence = binary.BigEndian.Uint16(recvbuf[2:4])
  657. p.Timestamp = binary.BigEndian.Uint32(recvbuf[4:8])
  658. p.SSRC = binary.BigEndian.Uint32(recvbuf[8:12])
  659. // decrypt opus data
  660. copy(nonce[:], recvbuf[0:12])
  661. p.Opus, _ = secretbox.Open(nil, recvbuf[12:rlen], &nonce, &v.op4.SecretKey)
  662. if c != nil {
  663. select {
  664. case c <- &p:
  665. case <-close:
  666. return
  667. }
  668. }
  669. }
  670. }
  671. // Reconnect will close down a voice connection then immediately try to
  672. // reconnect to that session.
  673. // NOTE : This func is messy and a WIP while I find what works.
  674. // It will be cleaned up once a proven stable option is flushed out.
  675. // aka: this is ugly shit code, please don't judge too harshly.
  676. func (v *VoiceConnection) reconnect() {
  677. v.log(LogInformational, "called")
  678. v.Lock()
  679. if v.reconnecting {
  680. v.log(LogInformational, "already reconnecting to channel %s, exiting", v.ChannelID)
  681. v.Unlock()
  682. return
  683. }
  684. v.reconnecting = true
  685. v.Unlock()
  686. defer func() { v.reconnecting = false }()
  687. // Close any currently open connections
  688. v.Close()
  689. wait := time.Duration(1)
  690. for {
  691. <-time.After(wait * time.Second)
  692. wait *= 2
  693. if wait > 600 {
  694. wait = 600
  695. }
  696. if v.session.DataReady == false || v.session.wsConn == nil {
  697. v.log(LogInformational, "cannot reconenct to channel %s with unready session", v.ChannelID)
  698. continue
  699. }
  700. v.log(LogInformational, "trying to reconnect to channel %s", v.ChannelID)
  701. _, err := v.session.ChannelVoiceJoin(v.GuildID, v.ChannelID, v.mute, v.deaf)
  702. if err == nil {
  703. v.log(LogInformational, "successfully reconnected to channel %s", v.ChannelID)
  704. return
  705. }
  706. v.log(LogInformational, "error reconnecting to channel %s, %s", v.ChannelID, err)
  707. // if the reconnect above didn't work lets just send a disconnect
  708. // packet to reset things.
  709. // Send a OP4 with a nil channel to disconnect
  710. data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, nil, true, true}}
  711. v.session.wsMutex.Lock()
  712. err = v.session.wsConn.WriteJSON(data)
  713. v.session.wsMutex.Unlock()
  714. if err != nil {
  715. v.log(LogError, "error sending disconnect packet, %s", err)
  716. }
  717. }
  718. }