-
Notifications
You must be signed in to change notification settings - Fork 270
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
44
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
44 commits
Select commit
Hold shift + click to select a range
9c4a51d
refactor: rename constant
1egoman 7a13f50
fix: make rpc use datastream.sendText, NOT streamText
1egoman 6820330
fix: convert rpc tests from streamText -> sendText
1egoman 846173f
feat: add initial support for single packet data streams via hacky me…
1egoman db5547b
feat: add backwards compatibility back in for old multi packet v1 dat…
1egoman 64042a0
feat: check all participants in room before broadcasting a v2 data st…
1egoman 759b8e6
feat: add initial data stream benchmark app
1egoman 42ae43f
feat: add missing constants file
1egoman 9f8a983
feat: add initial data stream compression implementation
1egoman 4ada1a1
feat: migrate compression to get rid of "pump" and compress per packe…
1egoman 434c932
fix: swap else if -> switch/case
1egoman c9ed60a
feat: add initial take at streaming through CompressionStream for data
1egoman 1ee48e8
feat: commit hand written data streams compression approach
1egoman ea65ece
feat: try out using fflate to share a single compression stream acros…
1egoman ba4a28e
refactor: move to deflate-raw for all compression, move to Compressio…
1egoman 3c7b6a8
fix: get rid of singleWrite mode on streamText
1egoman a721255
fix: clean up code
1egoman d6da60d
feat: remove compression from streamText, in practice this doesn't re…
1egoman 0f2831d
feat: add compression into bytestreams too
1egoman a3c60ad
refactor: move around outgoing data stream single packet / compressio…
1egoman 837aacf
fix: reduce needless diff churn
1egoman 21c2544
feat: add max header size breaking change
1egoman 520c381
feat: advertise new CAP_COMPRESSION_DEFLATE_RAW capability to SFU
1egoman 88adfa0
feat: add proper OutgoingDataStreamManager tests
1egoman 5990dbb
feat: add initial IncomingDataStreamManager tests
1egoman d690e42
fix: ensure that compression only used when remote participants suppo…
1egoman ad14a39
fix: fiz zlib Z_SYNC_FLUSH test
1egoman ce342ec
feat: add a few more IncomingDataStreamManager tests
1egoman cb247a9
fix: reformat code
1egoman 3d312d5
fix: remove unused import
1egoman 4e43e86
fix: remove Z_SYNC_FLUSH test, it was not really testing the right thing
1egoman fe033cd
fix: add participant disconnect test
1egoman 69084c9
fix: add sendFile v1 outgoing data stream manager tests
1egoman f4652e3
feat: port over data streams implementation to use newly added proto …
1egoman bd74e39
feat: commit initial data streams spec
1egoman b41875e
fix: remove unused tests
1egoman 2fc2bbd
fix: remove earlier drafts mention
1egoman 949a8b1
fix: remove more out of date E2EE stuff and note about utf-8 chunk bo…
1egoman a1780ac
fix: update misleading comments
1egoman f4eab35
fix: add sendBytes method as the main entrypoint for sending inline b…
1egoman deb8b57
fix: make sendFile optional in spec
1egoman 0f13573
fix: run npm run format
1egoman 42c7ee1
fix: address remaining lint errors
1egoman 76f9866
fix: add missing changeset
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
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,5 @@ | ||
| --- | ||
| 'livekit-client': patch | ||
| --- | ||
|
|
||
| Add support for data streams v2 |
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)}` }); | ||
| } | ||
| }); | ||
|
1egoman marked this conversation as resolved.
Dismissed
|
||
|
|
||
| export const handler: Express = app; | ||
Oops, something went wrong.
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.