-
Notifications
You must be signed in to change notification settings - Fork 269
Data streams v2 #1985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
1egoman
wants to merge
36
commits into
main
Choose a base branch
from
data-streams-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Data streams v2 #1985
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
c049e4c
refactor: rename constant
1egoman 4df4fbd
fix: make rpc use datastream.sendText, NOT streamText
1egoman fea1f2c
fix: convert rpc tests from streamText -> sendText
1egoman b7f9d00
feat: add initial support for single packet data streams via hacky me…
1egoman 15c4a1a
feat: add backwards compatibility back in for old multi packet v1 dat…
1egoman 9811524
feat: check all participants in room before broadcasting a v2 data st…
1egoman 54971eb
feat: add initial data stream benchmark app
1egoman 69bd59e
feat: add missing constants file
1egoman 2f80a8a
feat: add initial data stream compression implementation
1egoman f1fa99b
feat: migrate compression to get rid of "pump" and compress per packe…
1egoman 3ec5e6e
fix: swap else if -> switch/case
1egoman 4fb1efd
feat: add initial take at streaming through CompressionStream for data
1egoman b1e0794
feat: commit hand written data streams compression approach
1egoman ec6e1d0
feat: try out using fflate to share a single compression stream acros…
1egoman 8eb8ac4
refactor: move to deflate-raw for all compression, move to Compressio…
1egoman a2f0ede
fix: get rid of singleWrite mode on streamText
1egoman 5a79e3a
fix: clean up code
1egoman 69b3681
feat: remove compression from streamText, in practice this doesn't re…
1egoman d93f017
feat: add compression into bytestreams too
1egoman 7a708a5
refactor: move around outgoing data stream single packet / compressio…
1egoman e285f4b
fix: reduce needless diff churn
1egoman ee9dde7
feat: add max header size breaking change
1egoman 8f0720b
feat: advertise new CAP_COMPRESSION_DEFLATE_RAW capability to SFU
1egoman 8378001
feat: add proper OutgoingDataStreamManager tests
1egoman 8858e3d
feat: add initial IncomingDataStreamManager tests
1egoman 7af75f5
fix: ensure that compression only used when remote participants suppo…
1egoman 2317d5d
fix: fiz zlib Z_SYNC_FLUSH test
1egoman 80225e7
feat: add a few more IncomingDataStreamManager tests
1egoman a1de874
fix: reformat code
1egoman ec979e0
fix: remove unused import
1egoman 715c55d
fix: remove Z_SYNC_FLUSH test, it was not really testing the right thing
1egoman 6d5b6e7
fix: add participant disconnect test
1egoman 88cbff3
fix: add sendFile v1 outgoing data stream manager tests
1egoman 86b0369
feat: port over data streams implementation to use newly added proto …
1egoman 4b904eb
feat: commit initial data streams spec
1egoman d77161f
fix: remove unused tests
1egoman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`. |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 failureCode scanning / CodeQL Missing rate limiting High
This route handler performs
a system command Error loading related location Loading This route handler performs a system command Error loading related location Loading |
||
|
1egoman marked this conversation as resolved.
Dismissed
|
||
|
|
||
| export const handler: Express = app; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
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.