Skip to content

Jitter

Per-SSRC jitter buffer for the voice receive pipeline.

Holds incoming Opus packets indexed by RTP sequence number and replays them at a steady 20ms rate after an initial four-frame delay. Gaps in the sequence are filled via FEC (if the next packet is already buffered) or Opus PLC, so the consumer receives a continuous PCM stream while the speaker talks.

The tick loop runs only while audio is arriving: after five empty slots with nothing buffered it stops, and the next pushed packet resyncs and restarts it. A packet that arrives after a pause, far ahead of the expected sequence, or into an empty buffer resyncs the stream; any other late packet is dropped, so playback never rewinds.

Jitter.packets  :: {
{ [number]: buffer } }
Jitter.nextSequence  :: number
Jitter.stopped  :: boolean

True once destroy has been called.

Jitter.running  :: boolean

True while the tick loop is producing frames.

Jitter.clockAnchor  :: number
Jitter.tickCount  :: number
Jitter.emptyTicks  :: number

Consecutive slots that had no packet.

Jitter.lastPushClock  :: number
Jitter.thread  :: thread?
Jitter.decoder  :: Decoder
Jitter.shouldDecode  :: (() -> boolean)?

Asked once per slot whether to decode it; nil decodes every slot.

Jitter.decoding  :: boolean

The latest answer from shouldDecode, so each change is logged once.

Jitter.decoderStale  :: boolean

True from the slot decoding resumes until a new decoder has replaced the one that sat idle.

Jitter.logger  :: Logger
Jitter.onRawFrame  :: ((sequence: number, packet: buffer) -> ())?
Jitter.onPcmFrame  :: ((pcm: buffer) -> ())?
Jitter : push ( sequence ,  packet )  -> ()

Inserts an Opus packet for the given RTP sequence number, resyncing and restarting the tick loop when the stream was idle, jumped far ahead, or ran dry. Other late packets are dropped.

Parameters

sequence: number
packet: buffer
Jitter : start ( onRawFrame ,  onPcmFrame ,  shouldDecode )  -> ()

Starts producing frames. onRawFrame(sequence, packet) fires for each packet that was present in its slot and onPcmFrame(pcm) for every slot while audio is arriving, filling gaps with FEC from the next buffered packet or Opus PLC. shouldDecode, when given, is asked once per slot: while it returns false the slot skips decoding, FEC and PLC, so onPcmFrame does not fire while onRawFrame still does, and when it returns true again a new decoder takes over. shouldDecode must return without yielding; one that yields is closed and warned about, and the slot decodes. Errors raised by any callback are logged at warn and do not stop the loop.

Parameters

onRawFrame: (sequence: number, packet: buffer) -> ()
onPcmFrame: (pcm: buffer) -> ()
shouldDecode: (() -> boolean)?
Jitter : destroy ( )  -> ()

Stops the tick loop and frees the Opus decoder. Safe to call more than once, and from inside a frame callback, where the loop stops as soon as the callback returns.

Jitter . new ( firstSequence ,  jitterLogger )  -> Jitter

Responsible for constructing this class. jitterLogger receives warnings about callback errors and undecodable packets, and debug lines for dropped packets; without one, the jitter logs as Jitter.

Parameters

firstSequence: number
jitterLogger: Logger?

Returns

Jitter