Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c049e4c
refactor: rename constant
1egoman Jun 4, 2026
4df4fbd
fix: make rpc use datastream.sendText, NOT streamText
1egoman Jun 4, 2026
fea1f2c
fix: convert rpc tests from streamText -> sendText
1egoman Jun 4, 2026
b7f9d00
feat: add initial support for single packet data streams via hacky me…
1egoman Jun 4, 2026
15c4a1a
feat: add backwards compatibility back in for old multi packet v1 dat…
1egoman Jun 4, 2026
9811524
feat: check all participants in room before broadcasting a v2 data st…
1egoman Jun 4, 2026
54971eb
feat: add initial data stream benchmark app
1egoman Jun 5, 2026
69bd59e
feat: add missing constants file
1egoman Jun 5, 2026
2f80a8a
feat: add initial data stream compression implementation
1egoman Jun 5, 2026
f1fa99b
feat: migrate compression to get rid of "pump" and compress per packe…
1egoman Jun 5, 2026
3ec5e6e
fix: swap else if -> switch/case
1egoman Jun 5, 2026
4fb1efd
feat: add initial take at streaming through CompressionStream for data
1egoman Jun 8, 2026
b1e0794
feat: commit hand written data streams compression approach
1egoman Jun 9, 2026
ec6e1d0
feat: try out using fflate to share a single compression stream acros…
1egoman Jun 10, 2026
8eb8ac4
refactor: move to deflate-raw for all compression, move to Compressio…
1egoman Jun 10, 2026
a2f0ede
fix: get rid of singleWrite mode on streamText
1egoman Jun 10, 2026
5a79e3a
fix: clean up code
1egoman Jun 11, 2026
69b3681
feat: remove compression from streamText, in practice this doesn't re…
1egoman Jun 11, 2026
d93f017
feat: add compression into bytestreams too
1egoman Jun 12, 2026
7a708a5
refactor: move around outgoing data stream single packet / compressio…
1egoman Jun 12, 2026
e285f4b
fix: reduce needless diff churn
1egoman Jun 12, 2026
ee9dde7
feat: add max header size breaking change
1egoman Jun 12, 2026
8f0720b
feat: advertise new CAP_COMPRESSION_DEFLATE_RAW capability to SFU
1egoman Jun 15, 2026
8378001
feat: add proper OutgoingDataStreamManager tests
1egoman Jun 15, 2026
8858e3d
feat: add initial IncomingDataStreamManager tests
1egoman Jun 16, 2026
7af75f5
fix: ensure that compression only used when remote participants suppo…
1egoman Jun 16, 2026
2317d5d
fix: fiz zlib Z_SYNC_FLUSH test
1egoman Jun 16, 2026
80225e7
feat: add a few more IncomingDataStreamManager tests
1egoman Jun 16, 2026
a1de874
fix: reformat code
1egoman Jun 17, 2026
ec979e0
fix: remove unused import
1egoman Jun 17, 2026
715c55d
fix: remove Z_SYNC_FLUSH test, it was not really testing the right thing
1egoman Jun 17, 2026
6d5b6e7
fix: add participant disconnect test
1egoman Jun 17, 2026
88cbff3
fix: add sendFile v1 outgoing data stream manager tests
1egoman Jun 17, 2026
86b0369
feat: port over data streams implementation to use newly added proto …
1egoman Jun 23, 2026
4b904eb
feat: commit initial data streams spec
1egoman Jun 23, 2026
d77161f
fix: remove unused tests
1egoman Jun 23, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
684 changes: 684 additions & 0 deletions DATA_STREAMS_SPEC.md

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers: here is the (mostly complete) spec for this feature.

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions examples/data-stream-benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Data Stream Benchmark

Measures the end-to-end latency of LiveKit **v2 data streams** across a grid of network conditions
(X axis) and payload sizes (Y axis).

Two participants (`bench-sender` and `bench-receiver`) join a shared room in the same browser tab.
For each box in the grid the sender sends a fixed number of data streams (default 10) of random,
realistic JSON data to the receiver. Each stream header carries a `checksum` (XOR of the payload
bytes) and a `sendTs` timestamp; the receiver verifies the checksum and computes the end-to-end
latency. Each cell shows the average latency over the checksum-matching streams, or `N/A`.

## Running

1. Create `.env.local` with `LIVEKIT_API_KEY`, `LIVEKIT_API_SECRET`, and `LIVEKIT_URL`.
2. Install dependencies: `pnpm install`
3. Start the server: `pnpm dev`
4. Open the local URL (typically http://localhost:5173).
5. Click **Connect**, then **Run Benchmark**. Click **Disconnect** when done.

## Network conditioning (macOS only)

The **X axis** (Edge / 3G / LTE / Wi-Fi / None) is driven by the macOS **Network Link Conditioner**.
The server (`api.ts`) toggles it via `osascript` through `POST /api/network-condition`.

Prerequisites:

- The **Network Link Conditioner** preference pane must be installed
(`/Library/PreferencePanes/Network Link Conditioner.prefPane`, from Apple's "Additional Tools for
Xcode").
- The process running `vite`/node needs macOS **Accessibility** permission (System Settings →
Privacy & Security → Accessibility) so it can drive System Settings.
- The preset names must match the conditioner's menu exactly: `Edge`, `3G`, `LTE`, `Wi-Fi`.

The benchmark always resets the conditioner to **off** when it finishes, errors, or you disconnect.

## Notes

- The v2 single-packet (inline) optimization only changes behavior for payloads under the ~15 KB
header budget (10 B–10 KB); 100 KB and 1 MB are multi-packet regardless.
- `Edge`/`3G` × `1 MB` can be slow; each send has a 60 s timeout (`SEND_TIMEOUT_MS` in
`benchmark.ts`) and slow boxes show `N/A`. Repeat count and timeout are constants at the top of
`benchmark.ts`.
121 changes: 121 additions & 0 deletions examples/data-stream-benchmark/api.ts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: The diff is quite large, and a big reason why it is is because I have both this example app AND the spec checked in. I'll remove these before an eventual merge which will make the diff a lot more reasonable.

Feel free to ignore this examples/data-stream-benchmark directory when reviewing.

Image

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { exec } from 'child_process';
import dotenv from 'dotenv';
import express from 'express';
import { AccessToken } from 'livekit-server-sdk';
import { promisify } from 'util';
import type { Express } from 'express';

dotenv.config({ path: '.env.local' });

const execAsync = promisify(exec);

const LIVEKIT_API_KEY = process.env.LIVEKIT_API_KEY;
const LIVEKIT_API_SECRET = process.env.LIVEKIT_API_SECRET;
const LIVEKIT_URL = process.env.LIVEKIT_URL;

/** Network Link Conditioner presets we allow the client to request. */
const ALLOWED_PRESETS = ['Edge', '3G', 'LTE', 'Wi-Fi', 'Very Bad Network'];

const app = express();
app.use(express.json());

app.post('/api/get-token', async (req, res) => {
const { identity, roomName } = req.body;

if (!LIVEKIT_API_KEY || !LIVEKIT_API_SECRET) {
res.status(500).json({ error: 'Server misconfigured' });
return;
}

const token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, {
identity,
});
token.addGrant({
room: roomName,
roomJoin: true,
canPublish: true,
canSubscribe: true,
});

res.json({
token: await token.toJwt(),
url: LIVEKIT_URL,
});
});

/**
* Enables the macOS Network Link Conditioner at the given preset. The preset name is read from the
* `mode` environment variable via `system attribute "mode"`.
*/
const ENABLE_SCRIPT = `osascript <<'EOF'
set mode to system attribute "mode"
do shell script "open '/Library/PreferencePanes/Network Link Conditioner.prefPane'"
delay 2
tell application "System Settings" to activate
tell application "System Events"
tell process "System Settings"
tell window "Network Link Conditioner"
tell scroll area 1 of group 3 of splitter group 1 of group 1
tell group 1
click pop up button 1
delay 0.3
click menu item mode of menu 1 of pop up button 1
end tell
delay 0.3
click button "ON"
end tell
end tell
end tell
end tell
EOF`;

/** Disables the macOS Network Link Conditioner, returning to the host network speed. */
const DISABLE_SCRIPT = `osascript <<'EOF'
do shell script "open '/Library/PreferencePanes/Network Link Conditioner.prefPane'"
delay 2
tell application "System Settings" to activate
tell application "System Events"
tell process "System Settings"
tell window "Network Link Conditioner"
tell scroll area 1 of group 3 of splitter group 1 of group 1
click button "OFF"
end tell
end tell
end tell
end tell
EOF`;

app.post('/api/network-condition', async (req, res) => {
const { preset } = req.body as { preset?: string };

if (preset === 'off') {
try {
await execAsync(DISABLE_SCRIPT, { timeout: 30_000 });
res.json({ ok: true, preset: 'off' });
} catch (error) {
res.status(500).json({ error: `Failed to disable network conditioner: ${String(error)}` });
}
return;
}

if (!preset || !ALLOWED_PRESETS.includes(preset)) {
res
.status(400)
.json({ error: `Invalid preset "${preset}". Allowed: ${ALLOWED_PRESETS.join(', ')}, off` });
return;
}

try {
await execAsync(ENABLE_SCRIPT, {
timeout: 30_000,
env: { ...process.env, mode: preset },
});
res.json({ ok: true, preset });
} catch (error) {
res
.status(500)
.json({ error: `Failed to set network conditioner to ${preset}: ${String(error)}` });
}
});

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a system command
, but is not rate-limited.
This route handler performs
a system command
, but is not rate-limited.
Comment thread
1egoman marked this conversation as resolved.
Dismissed

export const handler: Express = app;
Loading