UdpTransport
Manages the UDP socket for a voice connection: IP discovery, keepalive, RTP receive, and audio send.
decryptPacket and onAudioPacket must be set by the owner before starting the receive loop.
Summary
Properties
UdpTransport.udpSocket :: stdPolyfills.UdpSocket?UdpTransport.udpReceiveThread :: thread?UdpTransport.keepaliveThread :: thread?UdpTransport.keepaliveSequence :: numberUdpTransport.unreportedDecryptFailures :: numberUdpTransport.lastDecryptWarningClock :: numberUdpTransport.pendingSends :: { [stdPolyfills.UdpSocket]: number? }UdpTransport.ssrc :: number?UdpTransport.udpIp :: string?UdpTransport.udpPort :: number?UdpTransport.modes :: { string }?UdpTransport.streams :: { { [string]: any } }?UdpTransport.experiments :: { string }?UdpTransport.substitutingSilence :: booleanUdpTransport.decryptPacket :: ((packet: buffer) -> (buffer?, string?))?UdpTransport.onAudioPacket :: ((ssrc: number, sequence: number, frame: buffer) -> ())?UdpTransport.logger :: logger.LoggerProperties
Section titled “Properties”udpSocket
Section titled “udpSocket”udpReceiveThread
Section titled “udpReceiveThread”keepaliveThread
Section titled “keepaliveThread”keepaliveSequence
Section titled “keepaliveSequence”unreportedDecryptFailures
Section titled “unreportedDecryptFailures”lastDecryptWarningClock
Section titled “lastDecryptWarningClock”pendingSends
Section titled “pendingSends”{ [stdPolyfills.UdpSocket]: number? } }
How many sends each socket has in flight; a socket is closed only once it has none.
udpPort
Section titled “udpPort”streams
Section titled “streams”{ { [string]: any } }
experiments
Section titled “experiments”substitutingSilence
Section titled “substitutingSilence”Whether the latest audio send went out as DAVE silence, so each gap is logged once.
decryptPacket
Section titled “decryptPacket”onAudioPacket
Section titled “onAudioPacket”logger
Section titled “logger”Methods
Section titled “Methods”cleanup
Section titled “cleanup”UdpTransport : cleanup ( ) -> ()
Closes the UDP socket and stops both the receive loop and keepalive loop, without yielding. Neither thread is closed, since either can be parked in a socket operation: closing the socket wakes the receive loop's pending read with "Canceled", and both loops end at their next check because the socket is no longer this transport's.
discoverIpAsync
Section titled “discoverIpAsync”UdpTransport : discoverIpAsync ( ) -> ()
Performs UDP IP discovery via the voice server and returns the bot's external IP and port. A reader thread waits for a reply that is 74 bytes, of type 0x0002 and for this ssrc; each request waits up to two seconds for it, three requests in all. Fails, closing the socket, when none is answered or the socket cannot be opened.
sendKeepalive
Section titled “sendKeepalive”UdpTransport : sendKeepalive ( ) -> ()
Sends a single UDP keepalive packet, which keeps the NAT mapping for the voice socket open.
startKeepaliveLoop
Section titled “startKeepaliveLoop”UdpTransport : startKeepaliveLoop ( ) -> ()
Starts the keepalive loop that fires every five seconds while the UDP socket is open.
startUdpReceiveLoop
Section titled “startUdpReceiveLoop”UdpTransport : startUdpReceiveLoop ( ) -> ()
Starts the UDP receive loop. Routes RTP audio packets to onAudioPacket after decryption via decryptPacket;
anything else is skipped with a debug line.
sendOpusAsync
Section titled “sendOpusAsync”UdpTransport : sendOpusAsync ( audioFrame , rtpCrypto , daveManager ) -> ()
Builds an RTP packet from the given Opus frame, encrypts it and sends it over UDP. The RTP timestamp advances by
the samples the frame's TOC byte describes (opusSamples), and an invalid frame fails before a sequence number or
timestamp is reserved. daveManager's encryptor, mediaProtocolVersion and mediaReady are read when the future
runs, not when it is created, so a send that waits through a DAVE group reset never touches the destroyed
encryptor. Fails while mediaProtocolVersion is unknown, because the frame could otherwise leave without DAVE in a
DAVE channel. Under DAVE until media is ready, Opus silence goes out in place of the frame, logged at debug once per
gap, and the timestamp still advances by the frame's samples; the gap is marked before the send yields, so a send
still in flight when cleanup runs cannot carry its gap into the next session. Fails with audio send failed: <reason> when the socket send fails; the sequence number and timestamp stay used, so the receiver sees plain loss.
Parameters
| audioFrame: buffer | |
| rtpCrypto: RtpCrypto | |
| daveManager: DaveManager |
Functions
Section titled “Functions”UdpTransport . new ( loggerInstance ) -> UdpTransport
Responsible for constructing this class.
Parameters
| loggerInstance: Logger |
Returns
| UdpTransport |