Skip to content

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.

UdpTransport.udpSocket  :: UdpSocket?
UdpTransport.udpReceiveThread  :: thread?
UdpTransport.keepaliveThread  :: thread?
UdpTransport.keepaliveSequence  :: number
UdpTransport.unreportedDecryptFailures  :: number
UdpTransport.lastDecryptWarningClock  :: number
UdpTransport.pendingSends  :: {
{ [stdPolyfills.UdpSocket]: number? } }

How many sends each socket has in flight; a socket is closed only once it has none.

UdpTransport.ssrc  :: number?
UdpTransport.udpIp  :: string?
UdpTransport.udpPort  :: number?
UdpTransport.modes  :: { string }?
UdpTransport.streams  :: {
{ { [string]: any } }
UdpTransport.experiments  :: { string }?
UdpTransport.substitutingSilence  :: boolean

Whether the latest audio send went out as DAVE silence, so each gap is logged once.

UdpTransport.decryptPacket  :: ((packet: buffer) -> (buffer?, string?))?
UdpTransport.onAudioPacket  :: ((ssrc: number, sequence: number, frame: buffer) -> ())?
UdpTransport.logger  :: Logger
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.

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.

UdpTransport : sendKeepalive ( )  -> ()

Sends a single UDP keepalive packet, which keeps the NAT mapping for the voice socket open.

UdpTransport : startKeepaliveLoop ( )  -> ()

Starts the keepalive loop that fires every five seconds while the UDP socket is open.

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.

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
UdpTransport . new ( loggerInstance )  -> UdpTransport

Responsible for constructing this class.

Parameters

loggerInstance: Logger

Returns

UdpTransport