AWS Builder Center
Benchmarking glass-to-glass latency for Amazon IVS real-time streaming

Benchmarking glass-to-glass latency for Amazon IVS real-time streaming

Measure real glass-to-glass latency on Amazon IVS Real-Time Streaming using a simple QR-code timestamp trick. Full code, live results, and the gotchas.

Benchmarking glass-to-glass latency on Amazon IVS Real-Time Streaming

Amazon IVS Real-Time says it can deliver live video in "under 300 ms from host to viewer." That is a great number. But how do you actually check it for your own app, on your own network?
This post shows a simple way to measure it. The idea is easy: the person broadcasting puts a live clock (as a QR code) on their video. The person watching reads that clock off the screen and compares it to their own. The difference is the delay. That delay, from camera to screen, is what we call glass-to-glass latency, and it is the number your users actually feel.
I built a small demo that does exactly this, and the full code is on GitHub:  aws-samples/sample-glass-to-glass-latency-benchmarking-amazon-ivs . On a normal laptop over office Wi-Fi at 720p, the viewer measured about 225 ms on average, with the slowest frames (p95) around 320 ms. Well under 300 ms most of the time, and easy to reproduce.
The one thing to remember: you can measure real end-to-end video latency with nothing more than a QR code and two clocks that agree. Get the clocks right and you get a trustworthy number. Get them wrong and every result is silently off.
This is for anyone evaluating IVS Real-Time for a live, interactive app (auctions, live shopping, betting, two-way video) who wants to verify the latency themselves instead of trusting a spec sheet.

Why "glass-to-glass"?

There are lots of ways to measure "latency," and most of them only look at one piece: how long the encoder takes, or how long the network hop takes, or how long the video player takes to draw a frame.
The problem is that no single piece tells you what the viewer experiences. The real delay is the whole chain added up: camera, encoding, network, the viewer's buffer, decoding, and finally drawing it on screen.
Glass-to-glass measures all of it at once. If the broadcaster stamps each video frame with the exact time, and the viewer notes the time it sees that stamp, the gap between them is the total delay. No need to measure each step. You just need both sides to agree on what time it is (more on that later, because it is the tricky part).

A quick primer on IVS Real-Time

Amazon IVS has two modes. We want the second one.
Mode
Typical latency
Good for
Low-Latency
3 to 5 seconds
broadcasting to big audiences
Real-Time
under 300 ms
live, interactive, two-way video
A few things worth knowing:
  • Real-Time is built on WebRTC (the same tech video calls use).
  • Everyone joins a "stage," which is basically a room. Up to 12 people can broadcast and up to 10,000 can watch.
  • The broadcaster can send a few quality levels at once (180p / 360p / 720p) so each viewer gets the best one their connection can handle.
One small but important note: you are measuring frame by frame, and at 30 frames per second, frames are about 33 ms apart. So a single measurement is only accurate to about one frame. Don't read too much into a 10 ms difference.

Step 1: Connect the app to the backend

Get the code: everything in this post (broadcaster, viewer, and the backend stack) is in the  aws-samples/sample-glass-to-glass-latency-benchmarking-amazon-ivs  repo. Clone it, deploy the stack, and follow along.
The demo has a small backend that creates the stage and hands out access tokens. The app connects to it using two values, a Domain ID and an API key, which you copy from the setup output. You enter the same two values on the broadcaster and on every viewer, so they all talk to the same backend.
Configure domain name and api key

Figure 1 - The setup screen. Both values come from the demo stack's output, and the same pair goes on every device.
That is the whole setup. Now for the two interesting parts: the broadcaster and the viewer.

Step 2: The broadcaster stamps each frame with a clock

Here is the key trick. Instead of sending the raw camera video, we wrap the camera track: we draw each camera frame onto a canvas, burn on the current time (both as readable digits and as a QR code), and then publish that canvas as the video track. In the demo there is a "Burn timestamp + QR" switch that turns this wrapping on. Turn it off, and the viewer has no clock to read, so nothing can be measured.
The whole thing is one function,  withTimecode , that takes the raw video track and returns a new track with the timecode burned in:
/**
* Wraps an incoming video MediaStreamTrack and returns a new track whose
* frames carry a `Date.now()` timestamp burned into the pixels:
* - human-readable monospace digits, top-left
* - a small QR code encoding the same value, just below
*
* Consumed by viewers (see G2GMeter.vue) to compute glass-to-glass latency.
*
* Usage:
* const tcTrack = withTimecode(rawVideoTrack);
*
* Notes:
* - The QR encoder runs synchronously per frame at ~30 fps. Cost is a few
* percent of one core on a modern laptop.
* - Audio tracks are not touched.
*/
import QRCode from 'qrcode';

export function withTimecode(videoTrack, options = {}) {
const { fps = 30 } = options;
const settings = videoTrack.getSettings();
const width = settings.width || 1280;
const height = settings.height || 720;

// Source: a hidden <video> playing the raw camera track
const inputVideo = document.createElement('video');
inputVideo.srcObject = new MediaStream([videoTrack]);
inputVideo.muted = true;
inputVideo.playsInline = true;
inputVideo.autoplay = true;
inputVideo.play().catch(() => {
/* will recover once a frame is ready */
});

// Output canvas: receives the raw frame plus the overlay
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d', { alpha: false });

// Offscreen QR canvas, rendered each frame
const qrCanvas = document.createElement('canvas');
const qrSize = Math.max(160, Math.round(height * 0.18));
qrCanvas.width = qrSize;
qrCanvas.height = qrSize;

let stopped = false;
const frameIntervalMs = 1000 / fps;
let lastFrameTime = 0;

const renderLoop = (now) => {
if (stopped || videoTrack.readyState === 'ended') return;
// Throttle to the target fps so we don't waste CPU at 60Hz when the
// output stream is 30 fps.
if (now - lastFrameTime >= frameIntervalMs && inputVideo.readyState >= 2) {
lastFrameTime = now;
const ts = Date.now();

// 1. Draw the raw camera frame
ctx.drawImage(inputVideo, 0, 0, width, height);

// 2. Readable timestamp for the demo audience
const padX = Math.round(width * 0.015);
const padY = Math.round(height * 0.025);
const fontPx = Math.max(28, Math.round(height * 0.05));
ctx.fillStyle = 'rgba(0, 0, 0, 0.75)';
ctx.fillRect(padX, padY, fontPx * 9, fontPx + 16);
ctx.fillStyle = '#00ff66';
ctx.font = `bold ${fontPx}px ui-monospace, SFMono-Regular, Menlo, monospace`;
ctx.textBaseline = 'top';
ctx.fillText(String(ts), padX + 12, padY + 8);

// 3. QR for machine reading. toCanvas resolves a Promise but renders
// synchronously, so drawing right after is safe.
QRCode.toCanvas(qrCanvas, String(ts), {
width: qrSize,
margin: 1,
errorCorrectionLevel: 'L',
color: { dark: '#000000', light: '#ffffff' },
}).catch(() => {
/* transient encode errors are fine; keep last frame */
});
ctx.drawImage(qrCanvas, padX, padY + fontPx + 24);
}
requestAnimationFrame(renderLoop);
};
requestAnimationFrame(renderLoop);

// captureStream produces the wrapped track
const stream = canvas.captureStream(fps);
const outTrack = stream.getVideoTracks()[0];

// When the camera track ends, end the wrapped track too
videoTrack.addEventListener('ended', () => {
stopped = true;
outTrack.stop();
});

return outTrack;
}
Then you publish the wrapped track to the stage with the Web broadcast SDK, exactly as you would any camera track:
import { Stage, LocalStageStream } from 'amazon-ivs-web-broadcast';

const tcTrack = withTimecode(rawVideoTrack); // wrap the camera track
const localStream = new LocalStageStream(tcTrack);
const strategy = {
stageStreamsToPublish: () => [localStream],
shouldPublishParticipant: () => true,
shouldSubscribeToParticipant: () => 'NONE',
};
await new Stage(BROADCASTER_TOKEN, strategy).join();
A few things worth calling out in that function:
  • It throttles to the target frame rate. The render loop runs on  requestAnimationFrame , which can fire at 60 Hz, but the output is 30 fps. The  now - lastFrameTime >= frameIntervalMs  check skips the extra calls so you don't burn CPU drawing frames nobody sends.
  • The QR renders synchronously.  QRCode.toCanvas  returns a Promise, but it actually paints the canvas before it resolves, so drawing the QR onto the frame right afterward is safe. The  .catch()  just keeps the last good frame if an encode ever hiccups.
  • The QR is sized to the frame, not fixed.  Math.max(160, height * 0.18)  keeps it at least 160 px and scales it up for larger frames, so it stays readable after the video is compressed. It uses error-correction level  L , which is enough because the QR sits on a clean background.

Step 3: The viewer reads the clock and does the math

The viewer watches the stream, reads the QR code off each frame, and subtracts that time from the current time. That difference is the latency. The one important optimization: only look at the corner of the frame where the QR actually is, because reading pixels is the expensive part.
function tick() {
const v = props.videoEl;
if (v && v.videoWidth && v.videoHeight) {
// Only sample the top-left quadrant where the QR lives. This is the
// single biggest performance win — getImageData is O(pixels).
const cropW = Math.min(v.videoWidth, Math.round(v.videoWidth * 0.4));
const cropH = Math.min(v.videoHeight, Math.round(v.videoHeight * 0.4));
if (canvas.width !== cropW) canvas.width = cropW;
if (canvas.height !== cropH) canvas.height = cropH;
try {
// Draw only the source's top-left region into the canvas.
ctx.drawImage(v, 0, 0, cropW, cropH, 0, 0, cropW, cropH);
const img = ctx.getImageData(0, 0, cropW, cropH);
const code = jsQR(img.data, img.width, img.height);
if (code && code.data) {
const tSrc = Number(code.data);
if (Number.isFinite(tSrc) && tSrc !== lastTs) {
lastTs = tSrc;
const latency = Date.now() - tSrc;
if (latency >= 0 && latency < 30000) pushSample(latency);
}
}
} catch {
/* drawImage may throw if the video isn't ready yet */
}
}
}
Three small details in there do a lot of work:
  • It crops to the top-left 40% of the frame.  getImageData  cost scales with the number of pixels, so reading only the corner where the QR lives is the single biggest speed-up. There is no point scanning the whole 720p frame.
  • It skips repeated frames. The loop runs faster than new frames arrive, so it often sees the same timestamp twice. The  tSrc !== lastTs  check means each real frame is only counted once.
  • It throws out garbage readings. The  latency >= 0 && latency < 30000  bound rejects impossible values (a misread QR, or a frame from before the clocks lined up) so one bad decode doesn't wreck your stats.
Here is the demo running, broadcaster on the left, viewer on the right:
Viewer and publisher

Figure 2 - The viewer's live readout: about 229 ms on average, 225 ms typical (p50), 318 ms for the slowest frames (p95), over 200 samples. Notice the two QR clocks show slightly different times. That gap, about 194 ms here, is the latency you can literally see.
The panel on the right also shows connection health (frame rate, bitrate, jitter, packet loss). Those numbers explain the latency: a clean connection gives steady, low latency. A bad connection makes both the latency and the jitter jump together.

The tricky part: both clocks must agree

The whole method is one clock minus another. So if the two devices disagree about the time by 40 ms, your result is off by 40 ms, and nothing will warn you. This is the easiest way to get a wrong answer, so it is worth getting right.
Three ways to handle it, from simplest to most accurate:
  1. One computer, two browser tabs. Both use the same clock, so there is no disagreement. Great for a first test, but the video never really travels over a network, so it is not realistic.
  1. Two computers, both synced to internet time (NTP). This is the normal setup. They will usually agree within a few to a few tens of milliseconds. Good enough for most cases, as long as you know that small gap is baked into your number.
  1. Measure the gap and subtract it. Before streaming, have the two sides ping each other to figure out how far apart their clocks are, then correct for it. This is the most accurate, and it is what you want if you need to trust the number precisely.
Simple rule: whenever you report a latency number, also say how you synced the clocks. "225 ms" alone doesn't mean much. "225 ms, with both machines synced to internet time" is something people can trust.

Turning readings into a real result

One measurement is just noise. Take a few hundred over a steady stream and look at the spread, because the occasional slow frame is what users notice, and an average hides those.
function summarize(samples) {
const xs = samples.map(s => s.latencyMs).sort((a, b) => a - b);
const pct = p => xs[Math.min(xs.length - 1, Math.floor((p / 100) * xs.length))];
const mean = Math.round(xs.reduce((a, b) => a + b, 0) / xs.length);
return { n: xs.length, min: xs[0], mean, p50: pct(50), p95: pct(95), p99: pct(99), max: xs.at(-1) };
}
The demo has a "Download CSV" button that saves every reading so you can chart it. Here is a 20-second run:
A sample analysis of glass-to-glass latency

Figure 3 - A typical run: around 265 ms on average, with the slowest frames near 345 ms. Everything stays far below the 1-second mark. The occasional spikes on the left are the small tail you see on the right.
When you report your results, include: the typical value (p50), the slow frames (p95/p99), and the conditions (resolution, frame rate, network, distance, device, and how you synced the clocks).
Why don't Figure 2 and Figure 3 match? Because they are two different runs (229 ms vs 265 ms average). That is normal. Latency shifts a bit with the network from moment to moment. That is exactly why you report a range with its conditions, instead of one perfect-looking number.

Test the things that actually change the result

The latency on office Wi-Fi is not what your users will get. So test the conditions that matter:
  • Slowing down the network. Careful here: throttling the network in Chrome's developer tools does NOT affect this kind of video. I tried it, saw no change, and almost believed the connection was magic. To really slow the link, use a proper tool: Network Link Conditioner on Mac,  tc  on Linux, or clumsy on Windows.
  • Distance. Broadcaster and viewer in different regions vs the same city. Distance adds delay you cannot code away.
  • Video quality. Force a lower or higher quality level and see how it trades off against latency.
  • Device. An older phone takes longer to decode video, and that shows up in the total.
  • Load. More viewers, or a busy broadcaster device.
Line up the typical and slow numbers for each case side by side. That comparison is what people actually want to see.

Things that can throw off your numbers

Problem
What it does
How to handle it
Clocks disagree
Every reading is off by the same amount
Sync to internet time, or measure and subtract the gap. Always say which.
Frame rate limit
Accurate only to about one frame (~33 ms at 30 fps)
Use a higher frame rate; don't over-read small differences
QR won't scan after compression
You lose readings
Size the QR to the frame (min 160 px); keep it on a clean background
Checking faster than frames arrive
Same frame counted many times
Skip repeats (shown above)
Reading the whole frame costs time
Slightly inflates the number
Only read the corner where the QR lives
Garbage QR reads
One bad decode skews your stats
Reject impossible values (latency < 0 or > 30 s)
Chrome throttling
Doesn't affect this video at all
Slow the network with a real tool instead
Camera and screen delay
Part of the real total, hard to separate
Accept it, it is genuinely part of the experience

Results, and what I would do differently

On one laptop pair, at 720p over office Wi-Fi with clocks synced to internet time, IVS Real-Time gave a typical latency of around 225 to 265 ms, and slow frames around 318 to 345 ms, on a clean connection. That is comfortably in real-time territory and plenty fast for the kind of interactive apps this mode is built for.
Honest caveats: this is one place, one network, one pair of devices. I have not tested across regions or on a bad connection yet, and that is exactly where the slow-frame numbers will climb. And all of it depends on the clocks being honest.
What worked well and I would keep: the QR-on-video trick, and skipping repeated frames when counting. Both are simple and held up in practice. What I would do differently: measure the clock gap from the start instead of trusting internet time, and script the network slowdown so my test conditions are repeatable instead of "office Wi-Fi at 3pm."
If you are looking at IVS Real-Time, grab the code from  aws-samples/sample-glass-to-glass-latency-benchmarking-amazon-ivs , try it on your own network, and share your typical and slow-frame numbers along with your setup. I would especially love to see cross-region and bad-connection results, since that is the part that decides whether an interactive app really works.


Ideas for later: a cleaner version of the latency chart, and adding cross-region and bad-connection benchmark runs.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article