# rtp — 12-Byte Voice/Video Media Streams & DSCP EF Marking (`rtp`)

> **FrameAssembly v3.2.2** · Protocol 58 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/rtp

- **Full Protocol Name:** Real-time Transport Protocol Audio & Video Streams
- **Keyword:** `rtp`
- **OSI Layer & Transport:** Layer 7 · UDP 5004
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 3550, RFC 3551, RFC 4733
- **Wireshark Display Filter:** `rtp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 5004 -> RTP (Media)`

## Overview

SIP sets up the call and `rtp` carries the actual voice packets — tack `dscp 46` (`EF`) and `wait 20ms` onto G.711 `pt 0` frames to verify your WAN routers aren’t dropping voice into best-effort queues.

Real-time Transport Protocol (`rtp`, RFC 3550) carries live VoIP voice (`G.711`, `Opus`), WebRTC video (`VP9`, `H.264`), and IP camera streams over **UDP** (default `dstport 5004`). Every RTP packet begins with a 12-byte RTP v2 (`0x80`) header containing the 1-bit `marker`, 7-bit Payload Type (`pt`), 16-bit `seq`, 32-bit media sample `timestamp`, and 32-bit Synchronization Source (`ssrc`).

Combining `rtp` with FrameAssembly’s precision `wait` clock (`wait 20ms` for standard 50-pps G.711 audio, or variable delays to simulate WAN jitter) and `dscp 46` (Expedited Forwarding) lets you test QoS queues and Wireshark RTP Stream Analysis with microsecond accuracy.

## Minimal Quickstart (`rtp_quickstart.frasm`)

```frameassembly
host handset_a { ip 10.30.1.101 }
host handset_b { ip 10.30.1.102 }

compile {
    handset_a -> handset_b rtp pt 0 seq 1 timestamp 160 ssrc 0x12345678 marker 1 dscp 46 payload "frame-1" wait 20ms
    handset_a -> handset_b rtp pt 0 seq 2 timestamp 320 ssrc 0x12345678 marker 0 dscp 46 payload "frame-2" wait 20ms
    handset_a -> handset_b rtp pt 0 seq 3 timestamp 480 ssrc 0x12345678 marker 0 dscp 46 payload "frame-3"
}
```

### Automatic Wire Calculations

- Defaults to UDP destination port `5004` and writes RTP Version `2` (`0x80`) in the upper bits of byte 0.
- Packs the 1-bit `marker` and 7-bit Payload Type (`pt`) cleanly into byte 1 (`(marker << 7) | pt`).
- Treats `seq` on an `rtp` line as the 16-bit RTP header Sequence Number.

## Practical Lab Example: G.711 VoIP Stream With Simulated WAN Jitter & Single-Packet Drop (`rtp_qos_jitter_lab.frasm`)

Emits a G.711 PCMU (`pt 0`, 8 kHz = `+160` timestamp ticks per `20ms`) voice burst containing a skipped sequence number (`seq 3` dropped) and a `55ms` jitter spike.

```frameassembly
host branch_phone { ip 172.16.30.21 }
host hq_gateway   { ip 172.16.1.10 }

compile {
    // Normal 20ms pacing (+160 samples at 8000 Hz)
    branch_phone -> hq_gateway rtp srcport 16384 dstport 16400 pt 0 seq 1 timestamp 160 ssrc 0xdeadbeef marker 1 dscp 46 payload "audio-pkt-1" wait 20ms
    branch_phone -> hq_gateway rtp pt 0 seq 2 timestamp 320 ssrc 0xdeadbeef marker 0 dscp 46 payload "audio-pkt-2" wait 55ms
    // seq 3 intentionally omitted to test 1-packet loss + 55ms jitter detection!
    branch_phone -> hq_gateway rtp pt 0 seq 4 timestamp 640 ssrc 0xdeadbeef marker 0 dscp 46 payload "audio-pkt-4" wait 5ms
    branch_phone -> hq_gateway rtp pt 0 seq 5 timestamp 800 ssrc 0xdeadbeef marker 0 dscp 46 payload "audio-pkt-5"
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show rtp_qos_jitter_lab.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly rtp_qos_jitter_lab.frasm rtp_qos_jitter_lab.pcap
frameassembly rtp_qos_jitter_lab.frasm rtp_qos_jitter_lab.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse rtp_qos_jitter_lab.pcap rtp_qos_jitter_lab_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r rtp_qos_jitter_lab.pcap
```

## All `rtp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host rtp [pt <0..127>] [seq <0..65535>] [timestamp <number>] [ssrc <hex_or_num>] [marker <0|1>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `pt <0..127>` | 7-bit RTP Payload Type (`0` PCMU, `8` PCMA, `9` G.722, `96` Opus/H.264, `101` DTMF) | `0 (G.711 PCMU)` | Sets the lower 7 bits of byte 1 in the 12-byte RTP header. |
| `seq <0..65535>` | 16-bit RTP Sequence Number | `1` | Sets bytes 2..3 of the RTP header used by receivers to detect packet loss and reordering. |
| `timestamp <number>` | 32-bit media sampling clock timestamp | `160` | Sets bytes 4..7 of the RTP header (`+160` per 20ms for 8kHz voice; `+1800` per frame for 90kHz 50fps video). |
| `ssrc <hex_or_num>` | 32-bit Synchronization Source ID | `0x12345678` | Uniquely identifies the unidirectional RTP media stream in bytes 8..11. |
| `marker <0\|1>` | `1` (talkspurt start / end-of-video-frame) or `0` | `0` | Sets the 1-bit `M` flag at the top of byte 1. |

## Cross-Layer Options Valid with `rtp`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), UDP 5004` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |

## Common Mistakes & Quick Fixes

### Enable Wireshark "Try to decode RTP outside of conversations" for Standalone RTP Captures

- **Valid v3.2.2 Syntax:** `Pair rtp with sip invite + SDP, or enable RTP heuristic dissection in Wireshark`

Because RTP uses dynamic even UDP ports without a fixed magic header, Wireshark auto-detects RTP when preceded by `sip` / `rtsp` setup—or on any UDP port when *Analyze -> Enabled Protocols -> rtp_udp* is checked.

### pt Maximum Value Is 127 (7 Bits)

- **Avoid:** `handset_a -> handset_b rtp pt 200`
- **Valid v3.2.2 Syntax:** `handset_a -> handset_b rtp pt 96`

The RTP Payload Type shares byte 1 with the 1-bit `marker` flag, so `pt` must be `0..127`.
