RTCP
RTCP, the RTP Control Protocol (RFC 3550 §6), travels beside an RTP stream: each end reports what it sent and what it received, names itself, and says goodbye. The decoder was added in 13.3.0 (#354, PR #362). The module is packet_parser::parse::application::protocols::rtcp, the errors are RtcpError, in packet_parser::errors::application::rtcp, and the label is "RTCP".
#![allow(unused)] fn main() { pub struct RtcpCompound<'a> { /* the datagram, validated */ } pub struct RtcpPacket<'a> { pub padding: bool, // the body ends with padding pub count: u8, // five bits: what they count depends on the type pub packet_type: RtcpPacketType, pub body: &'a [u8], // what follows the four-byte header } pub struct RtcpSenderInfo { pub ntp_timestamp: u64, // wallclock time of the report pub rtp_timestamp: u32, // the same instant on the clock of the stream pub packet_count: u32, pub octet_count: u32, } }
A datagram carries one RTCP packet or several, back to back: a compound packet (§6.1). RtcpCompound::try_from validates the whole datagram once; packets() then gives the packets in wire order without allocating, and as_bytes() the datagram. The types are #[non_exhaustive], and every body borrows from the datagram.
| Type | RtcpPacketType | count is | Defined in |
|---|---|---|---|
| 200 | SenderReport | the number of report blocks | RFC 3550 §6.4.1 |
| 201 | ReceiverReport | the number of report blocks | RFC 3550 §6.4.2 |
| 202 | SourceDescription | the number of chunks | RFC 3550 §6.5 |
| 203 | Goodbye | the number of sources | RFC 3550 §6.6 |
| 204 | ApplicationDefined | a subtype | RFC 3550 §6.7 |
| 205 | TransportFeedback | a feedback message type | RFC 4585 §6.2 |
| 206 | PayloadFeedback | a feedback message type | RFC 4585 §6.3 |
| 207 | ExtendedReport | reserved | RFC 3611 |
Two methods read what a consumer most often wants. sender_ssrc() is the SSRC that opens the body of every packet but a Source Description and a Goodbye, whose body is a list of sources. sender_info() is Some for a Sender Report: the NTP timestamp, the RTP timestamp and the two counters. The report blocks, the SDES items and the feedback messages stay bytes. Two constants are public: RTCP_VERSION and RTCP_HEADER_LENGTH, 4.
The last datagram a phone sends at the end of a call (tested):
#![allow(unused)] fn main() { #[test] fn a_compound_rtcp_datagram() { let raw = hex::decode(RTCP_SR_SDES_BYE_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); // No port, no Decode As: the blind rule reads the content. assert_eq!(label(&flow), Some("RTCP")); let compound = RtcpCompound::try_from(payload(&flow)).unwrap(); let packets: Vec<_> = compound.packets().collect(); let types: Vec<_> = packets.iter().map(|packet| packet.packet_type).collect(); assert_eq!( types, [ RtcpPacketType::SenderReport, RtcpPacketType::SourceDescription, RtcpPacketType::Goodbye, ] ); // 28, 48 and 28 bytes: the three packets tile the 104 bytes. let lengths: Vec<_> = packets.iter().map(|packet| 4 + packet.body.len()).collect(); assert_eq!(lengths, [28, 48, 28]); assert_eq!(compound.as_bytes().len(), 104); // The Sender Report: no report block, the sender and its counters. let report = &packets[0]; assert_eq!(report.count, 0); assert_eq!(report.sender_ssrc(), Some(0x3796_cb71)); let info = report.sender_info().unwrap(); assert_eq!(info.ntp_timestamp >> 32, 1_120_470_986); assert_eq!(info.rtp_timestamp, 9411); assert_eq!((info.packet_count, info.octet_count), (9, 1548)); // SDES and BYE carry a list of sources, not a sender: one each. assert_eq!(packets[1].count, 1); assert_eq!(packets[1].sender_ssrc(), None); assert_eq!(packets[1].sender_info(), None); assert_eq!(packets[2].count, 1); // The bodies are borrowed, not decoded: the reason of the Goodbye // is still in its bytes. assert!(packets[2].body.ends_with(b"session shutdown\0\0\0")); } }
Detection: any UDP port
| Where | Rule | Probe | Accepts |
|---|---|---|---|
| any UDP port | blind rule 75 | ProbeId::RtcpBlind | a datagram RtcpCompound::try_from accepts, opened by a Sender Report or a Receiver Report, or made of one feedback packet alone |
a UDP port declared with ParseConfig::decode_as(port, DecodeAsProtocol::Rtcp) | tried before the table | ProbeId::Rtcp | any datagram RtcpCompound::try_from accepts |
RTCP has no port of its own: by convention it takes the port after the one of its RTP stream, and RFC 5761 lets both share one. Rule 75 is the last UDP rule of the table: every probe with a stronger signature has had the datagram first.
RTCP has what RTP lacks. Each packet says its length, and the lengths must tile the datagram to its last byte. The packet type is one of eight values. Each type has a minimum size, which grows with its count: a Sender Report with two report blocks is at least 76 bytes. On top of the decoder, the blind rule asks for the opening RFC 3550 §6.1 imposes, a Sender Report or a Receiver Report first. It accepts one exception, the reduced-size RTCP of RFC 5506: a single feedback packet, type 205 or 206, alone in its datagram.
DecodeAsProtocol::Rtcp, new in 13.3.0 and the last of the 34 variants, resolves to the label RTCP, the guard Udp and the probe Rtcp: on a declared port, any well-formed datagram is RTCP, whatever its first packet.
Validations
| Part | Check | Error |
|---|---|---|
| Packet | at least the four bytes of its header | RtcpError::TooShort { expected, actual } |
| Version | 2 | InvalidVersion |
| Packet type | 200 to 207 | UnknownPacketType |
| Length | the packet it announces fits in what remains of the datagram | TruncatedPacket { declared, available } |
| Padding, with the P bit | the last byte of the body is a multiple of four, not zero, and not longer than the body | InvalidPadding |
| Size | the body, without its padding, holds what the type and the count require | PacketTooShortForType { packet_type, count, expected, actual } |
| Padding | only on the last packet of the datagram | InvalidPadding |
The checks run in this order on each packet, from the first to the last, and the first failure is the error. Nothing may follow the last packet: a byte left over is a packet too short for its header.
| Type | Minimum body |
|---|---|
| Sender Report | 24 bytes, the SSRC and the sender information, plus 24 for each report block |
| Receiver Report | 4 bytes, the SSRC, plus 24 for each report block |
| Source Description | 8 bytes for each chunk |
| Goodbye | 4 bytes for each source |
| Application-defined | 8 bytes, the SSRC and a four-character name |
| Feedback, 205 and 206 | 8 bytes, the SSRC of the sender and the SSRC of the media source |
| Extended Report | 4 bytes, the SSRC |
Two refusals on the bytes of the corpus (tested):
#![allow(unused)] fn main() { #[test] fn what_rtcp_refuses() { let raw = hex::decode(RTCP_SR_SDES_BYE_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); let datagram = payload(&flow); // The lengths must tile the datagram: one byte less, and the // Goodbye runs past the end. assert_eq!( RtcpCompound::try_from(&datagram[..103]).unwrap_err(), RtcpError::TruncatedPacket { declared: 28, available: 27 } ); // An RTP packet of the same call: its second byte, 8, is no RTCP // packet type. let raw = hex::decode(RTP_PCMA_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); assert_eq!( RtcpCompound::try_from(payload(&flow)).unwrap_err(), RtcpError::UnknownPacketType(8) ); } }
What is not RTCP
Each row was checked against the crate, on a UDP port without a rule of its own, then on the same port declared Rtcp:
| Payload | Blind | Declared | Why |
|---|---|---|---|
| the Sender Report, SDES and BYE of the example | "RTCP" | "RTCP" | |
| its Sender Report alone, 28 bytes | "RTCP" | "RTCP" | a report may come alone |
| its SDES and BYE, without the report | "Unknown" | "RTCP" | well formed, but a compound packet opens with a report |
| its BYE alone | "Unknown" | "RTCP" | the same |
| one feedback packet of the LINE call | "RTCP" | "RTCP" | reduced-size RTCP |
| that feedback packet followed by a BYE | "Unknown" | "RTCP" | a feedback packet opens no compound packet |
| the example with one byte more | "Unknown" | "Unknown" | the lengths no longer tile the datagram |
| an RTP packet of the same call | "Unknown" | "Unknown" | its second byte, 8, is no packet type |
| the example, on TCP | "Unknown" | "Unknown" | the guard is UDP |
The seventh row is what makes the rule safe to run blind, and what RTP has no equivalent of. A datagram is RTCP only if its first byte is 0x80 to 0xBF, its second one of two values, or of two others when the packet is alone, and its length field, 16 bits, matches the size of the datagram or leads to another header that passes the same tests.
Oracle: 52 datagrams
The golden test tests/rtcp_golden.rs compares the crate with tshark 4.6.9. tools/rtcp_oracle.sh freezes, in tests/data/rtcp_tshark_oracle.tsv, one line per datagram that tshark dissects as RTCP: the types and the lengths of its packets, the SSRC of each sender, and the sender information of a Sender Report.
| Capture | RTCP | What |
|---|---|---|
sip/wireshark_aaa.pcap | 1 | the Sender Report, SDES and BYE of the example |
ntp/ndpi_line.pcap | 11 | feedback packets, each alone in its datagram |
rtcp/terrain_rtcp_censure.pcap | 40 | 20 Sender Reports and 20 Receiver Reports, each with one report block and followed by an SDES |
The third capture is new. The corpus had no Receiver Report, and no public capture with one was found, so 40 datagrams were taken from a field capture of the maintainer and censored by tools/censor_capture.py before they were published. Addresses, ports, NTP timestamps and the text of the SDES items were rewritten; packet types, lengths, counts and SSRC are the original ones. Its SOURCE.md lists each rewritten field.
The crate and tshark give RTCP the same 52 datagrams, and no other frame of the corpus gets the label. In the golden histogram "RTCP" went from 0 to 52.
What is left out
The limits every protocol shares are on the Limits page. The ones below are this protocol's own.
- The bodies. Report blocks, SDES items, the reason of a Goodbye and feedback messages are returned as bytes.
- SRTCP (RFC 3711) encrypts the packets and adds an index and an authentication tag after the last one. The lengths no longer tile the datagram, which stays
"Unknown", even on a declared port. - A compound packet that opens with another type than a report is named on a declared port only.
- RTCP multiplexed with RTP on one port (RFC 5761) is named packet by packet: the RTCP datagrams get their label, the RTP ones do not, unless the port is declared
Rtp. - RTCP over TCP is not read.