webrtc_rtp_seq (webrtc v0.1.0)

View Source

Wrapping arithmetic for RTP sequence numbers (16 bits) and timestamps (32 bits), RFC 3550 §5.1 and Appendix A.1.

Comparisons use serial number arithmetic: A is before B when the forward distance from A to B is less than half the space. A distance of exactly half the space counts as negative, so that seq_diff/2 stays in -32768..32767.

seq_unwrap/2 and ts_unwrap/2 extend a wrapped value to the unbounded integer closest to a previous extended value, which is how receivers keep a monotonic index across wraparound.

Summary

Functions

Seq + N modulo 2^16 (N may be negative).

Signed distance from B to A (A - B), in -32768..32767.

True when A comes strictly before B.

Extend Seq to the extended sequence number closest to Last (itself extended). Use undefined for the first packet.

Signed distance A - B modulo 2^32.

Types

seq/0

-type seq() :: 0..65535.

ts/0

-type ts() :: 0..4294967295.

Functions

seq_add(Seq, N)

-spec seq_add(seq(), integer()) -> seq().

Seq + N modulo 2^16 (N may be negative).

seq_diff(A, B)

-spec seq_diff(seq(), seq()) -> -32768..32767.

Signed distance from B to A (A - B), in -32768..32767.

seq_gt(A, B)

-spec seq_gt(seq(), seq()) -> boolean().

seq_le(A, B)

-spec seq_le(seq(), seq()) -> boolean().

seq_lt(A, B)

-spec seq_lt(seq(), seq()) -> boolean().

True when A comes strictly before B.

seq_unwrap(Seq, Last)

-spec seq_unwrap(seq(), integer() | undefined) -> integer().

Extend Seq to the extended sequence number closest to Last (itself extended). Use undefined for the first packet.

ts_add(Ts, N)

-spec ts_add(ts(), integer()) -> ts().

ts_diff(A, B)

-spec ts_diff(ts(), ts()) -> integer().

Signed distance A - B modulo 2^32.

ts_gt(A, B)

-spec ts_gt(ts(), ts()) -> boolean().

ts_lt(A, B)

-spec ts_lt(ts(), ts()) -> boolean().

ts_unwrap(Ts, Last)

-spec ts_unwrap(ts(), integer() | undefined) -> integer().