MPEG TS and metadata channels
MPEG-TS in brief
MPEG Transport Stream (MPEG-TS) is a container format designed for streaming audio, video, and metadata over unreliable networks or broadcast links. The stream is split into small fixed-size packets, usually 188 bytes, where each packet carries a packet identifier (PID). A receiver uses these PIDs and the program tables in the stream to find the elementary streams that belong to a program, such as a video track, audio track, or metadata track.
MPEG-TS is commonly used for live video because it can be decoded progressively and can recover from packet loss better than formats that depend on a complete file structure.
Simple video-only UDP pipeline
The following pipeline generates a test video pattern, encodes it as H.264, muxes it into MPEG-TS, and streams it over UDP:
mpegtsmux alignment=7
mpegtsmux alignment=7 tells GStreamer how many MPEG-TS packets to group into each output buffer. MPEG-TS packets are normally 188 bytes each.
h264parse config-interval=-1
tells h264parse to insert the H.264 configuration data into the stream with every IDR keyframe. For UDP/live streaming, -1 is useful because a receiver can join the stream later and still get the required decoder configuration at the next keyframe. Without SPS/PPS appearing in-band often enough, a late receiver may fail to decode until it somehow receives that config data.
IDR: Instantaneous Decoder Refresh frame. A keyframe that can be decoded without needing earlier frames. After an IDR, the decoder can start fresh.
This sends an MPEG-TS stream with only one video elementary stream and no audio or metadata channels.
Run this receiver pipeline in another terminal to receive the MPEG-TS stream, extract the H.264 video, decode it, and display it:
This version is tuned for low latency. tsdemux latency=0 removes the default
demux smoothing delay, leaky queues prevent old frames from building up, and
autovideosink sync=false displays frames as soon as they are decoded. If the
machine cannot decode fast enough, frames may be dropped instead of increasing
the visible delay.
- queue before tsdemux: absorb UDP jitter (absorb means “take in and smooth out.”)
- queue after tsdemux: separate demux from decode
- h264parse: clean/prepare H.264 for decoder
- queue before sink: prevent display slowness from causing latency buildup
Metadata-only UDP pipeline
MPEG-TS can carry a metadata elementary stream without video or audio. In
GStreamer, mpegtsmux accepts this kind of metadata on a meta/x-klv sink pad.
For a real interoperable system, wrap the JSON in valid KLV. For a simple lab
pipeline, the JSON bytes can be sent on that private metadata channel.
Run this sender pipeline to stream only JSON metadata over UDP:
Run this receiver pipeline in another terminal to receive the MPEG-TS stream and print the metadata channel to stdout:
The final branch handles only the demuxed metadata stream:
queuedecouplestsdemuxfrom the metadata consumer so parsing and printing metadata does not block demuxing."meta/x-klv"filters the branch to KLV metadata buffers from the transport stream.identity silent=false dump=truelogs each buffer and dumps its bytes, which makes the JSON payload visible in the terminal.fakesink sync=falsediscards the buffers after inspection and does not wait on the pipeline clock.
Demo:
Sender
json_sender.py creates a live MPEG-TS sender with two synchronized inputs.
For every timer tick it generates one OpenCV video frame and one UTF-8 JSON
payload, gives both buffers the same PTS/DTS/duration, muxes them with
mpegtsmux, and sends the transport stream to udp://HOST:PORT.
flowchart LR
timer["GLib timer<br/>fps interval"] --> push["push_frame_and_json()"]
push --> frame["make_counter_frame()<br/>BGR video bytes"]
frame --> videosrc["appsrc videosrc<br/>video/x-raw, BGR"]
videosrc --> vqueue1["queue<br/>leaky downstream"]
vqueue1 --> tee["tee name=video_tee"]
tee --> vqueue2["queue"]
vqueue2 --> convert1["videoconvert"]
convert1 --> enc["x264enc<br/>zerolatency, ultrafast"]
enc --> parse["h264parse<br/>config-interval=1"]
parse --> vqueue3["queue"]
vqueue3 --> mux["mpegtsmux<br/>alignment=7"]
tee --> previewq["queue"]
previewq --> convert2["videoconvert"]
convert2 --> preview["autovideosink<br/>local preview"]
push --> json["make_json_payload()<br/>UTF-8 JSON bytes"]
json --> jsonsrc["appsrc jsonsrc<br/>meta/x-klv, parsed=true"]
jsonsrc --> mqueue["queue<br/>leaky downstream"]
mqueue --> mux
mux --> outq["queue"]
outq --> udp["udpsink<br/>sync=false async=false"]
Sender code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
Receiver
json_receiver.py listens for an MPEG-TS stream on UDP port 5000 by default. tsdemux
creates dynamic pads for the incoming streams: the H.264 pad is linked to the video
decode/display branch, while the first non-video pad is treated as the private JSON
metadata stream and linked to an appsink.
For every metadata sample, the receiver decodes the bytes as UTF-8 JSON, prints the
payload with timing information, compares metadata PTS with the latest decoded video
PTS, and updates a textoverlay on the video output. Invalid JSON is logged as raw
bytes so transport or payload issues are still visible.
flowchart LR
udp["udpsrc<br/>port=5000<br/>video/mpegts packetsize=188"] --> demux["tsdemux<br/>latency=0"]
demux -- "video/x-h264 dynamic pad" --> vqueue["queue<br/>leaky downstream"]
vqueue --> parse["h264parse"]
parse --> decoder["avdec_h264<br/>max-threads=1"]
decoder --> convert["videoconvert"]
convert --> probe["video PTS pad probe"]
probe --> overlay["textoverlay<br/>RX JSON status"]
overlay --> vsink["autovideosink<br/>or fakesink with --no-video<br/>sync=false"]
demux -- "private metadata dynamic pad" --> mqueue["queue<br/>leaky downstream"]
mqueue --> appsink["appsink meta_sink<br/>emit-signals=true<br/>drop=true"]
appsink --> sample["on_metadata_sample()"]
sample --> decode["decode UTF-8 JSON"]
decode --> metrics["counter, wall latency,<br/>metadata/video PTS deltas"]
metrics --> log["print payload and timing"]
metrics --> overlay
Receiver code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |