-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add TURN-TCP support with server-driven config #112
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7c27596
feat: add TURN-TCP support and E2E tests for WebRTC fallback
nagar-decart 99e6b84
feat: accept server-driven TURN config via signaling
nagar-decart 1bec2aa
fix: ICE restart when turn_config arrives after PC creation
nagar-decart 0d2be20
fix: ignore stale answers after ICE restart
nagar-decart 9348290
fix: wait for turn_config before creating PeerConnection
nagar-decart d5cd0be
feat: add iceTransportPolicy connect option
nagar-decart 9bebe2d
fix: skip turn_config wait when TURN not requested, fix handler leak
nagar-decart 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
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
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
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
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
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,176 @@ | ||
| declare const __DECART_API_KEY__: string; | ||
| declare const __WEBRTC_BASE_URL__: string; | ||
|
|
||
| import { | ||
| createDecartClient, | ||
| type CustomModelDefinition, | ||
| type DecartSDKError, | ||
| type SelectedCandidatePairEvent, | ||
| } from "@decartai/sdk"; | ||
| import { beforeAll, describe, expect, it } from "vitest"; | ||
|
|
||
| function createSyntheticStream(fps: number, width: number, height: number): MediaStream { | ||
| const canvas = document.createElement("canvas"); | ||
| canvas.width = width; | ||
| canvas.height = height; | ||
| return canvas.captureStream(fps); | ||
| } | ||
|
|
||
| const BIT_INVERT_MODEL: CustomModelDefinition = { | ||
| name: "bit_invert", | ||
| urlPath: "/ws", | ||
| fps: 25, | ||
| width: 512, | ||
| height: 512, | ||
| }; | ||
|
|
||
| const TURN_ICE_SERVERS: RTCIceServer[] = [ | ||
| { urls: "stun:stun.l.google.com:19302" }, | ||
| { urls: "turn:127.0.0.1:3478?transport=tcp", username: "turn", credential: "turn" }, | ||
| ]; | ||
|
|
||
| const TIMEOUT = 2 * 60 * 1000; // 2 minutes | ||
|
|
||
| /** | ||
| * Wraps the global RTCPeerConnection so every new instance uses | ||
| * the given iceTransportPolicy. Returns a cleanup function to restore. | ||
| */ | ||
| function overrideIceTransportPolicy(policy: RTCIceTransportPolicy): () => void { | ||
| const OriginalPC = globalThis.RTCPeerConnection; | ||
| globalThis.RTCPeerConnection = class extends OriginalPC { | ||
| constructor(config?: RTCConfiguration) { | ||
| super({ ...config, iceTransportPolicy: policy }); | ||
| } | ||
| } as typeof RTCPeerConnection; | ||
| return () => { | ||
| globalThis.RTCPeerConnection = OriginalPC; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Collects the selectedCandidatePair diagnostic from a realtime client. | ||
| * The event is buffered during connect() and flushed via setTimeout(0) | ||
| * after connect() resolves, so registering immediately catches it. | ||
| */ | ||
| function collectSelectedCandidatePair( | ||
| realtimeClient: { on: (event: "diagnostic", handler: (e: { name: string; data: unknown }) => void) => void }, | ||
| ): Promise<SelectedCandidatePairEvent | null> { | ||
| return new Promise((resolve) => { | ||
| const handler = (event: { name: string; data: unknown }) => { | ||
| if (event.name === "selectedCandidatePair") { | ||
| resolve(event.data as SelectedCandidatePairEvent); | ||
| } | ||
| }; | ||
| realtimeClient.on("diagnostic", handler); | ||
| // Fallback: if the event was already emitted before we registered, resolve after a delay | ||
| setTimeout(() => resolve(null), 5000); | ||
| }); | ||
| } | ||
|
|
||
| describe("TURN-TCP E2E Tests", { timeout: TIMEOUT, retry: 2 }, () => { | ||
| let apiKey: string; | ||
| let webrtcBaseUrl: string; | ||
|
|
||
| beforeAll(() => { | ||
| apiKey = __DECART_API_KEY__; | ||
| webrtcBaseUrl = __WEBRTC_BASE_URL__; | ||
| if (!apiKey) { | ||
| throw new Error( | ||
| "DECART_API_KEY environment variable not set. Run with: DECART_API_KEY=your_key pnpm test:e2e:turn-tcp", | ||
| ); | ||
| } | ||
| if (!webrtcBaseUrl) { | ||
| throw new Error( | ||
| "WEBRTC_BASE_URL environment variable not set. Set it to your local k8s WebSocket URL.", | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| // Requires server-side aioice TURN-TCP allocation to work (server must produce relay candidates). | ||
| // Skip until server-side TURN candidate generation is verified. | ||
| it.skip("TURN-TCP relay only (iceTransportPolicy=relay)", async () => { | ||
| const restore = overrideIceTransportPolicy("relay"); | ||
|
|
||
| try { | ||
| const client = createDecartClient({ apiKey, realtimeBaseUrl: webrtcBaseUrl }); | ||
| const stream = createSyntheticStream(BIT_INVERT_MODEL.fps, BIT_INVERT_MODEL.width, BIT_INVERT_MODEL.height); | ||
|
|
||
| let remoteStreamReceived = false; | ||
|
|
||
| const realtimeClient = await client.realtime.connect(stream, { | ||
| model: BIT_INVERT_MODEL, | ||
| onRemoteStream: () => { | ||
| remoteStreamReceived = true; | ||
| }, | ||
| iceServers: TURN_ICE_SERVERS, | ||
| }); | ||
|
|
||
| // Register diagnostic listener immediately - buffered events flush on next macrotask | ||
| const candidatePairPromise = collectSelectedCandidatePair(realtimeClient); | ||
|
|
||
| const errors: DecartSDKError[] = []; | ||
| realtimeClient.on("error", (err) => errors.push(err)); | ||
|
|
||
| try { | ||
| expect(["connected", "generating"]).toContain(realtimeClient.getConnectionState()); | ||
| expect(realtimeClient.sessionId).toBeTruthy(); | ||
| expect(remoteStreamReceived).toBe(true); | ||
| expect(errors).toEqual([]); | ||
|
|
||
| // With relay-only policy, the selected candidate must be a relay (TURN) | ||
| const pair = await candidatePairPromise; | ||
| if (pair) { | ||
| expect(pair.local.candidateType).toBe("relay"); | ||
| } | ||
| } finally { | ||
| realtimeClient.disconnect(); | ||
| for (const track of stream.getTracks()) track.stop(); | ||
| } | ||
|
|
||
| expect(realtimeClient.getConnectionState()).toBe("disconnected"); | ||
| } finally { | ||
| restore(); | ||
| } | ||
| }); | ||
|
|
||
| it("Both UDP + TURN available (default iceTransportPolicy=all)", async () => { | ||
| const client = createDecartClient({ apiKey, realtimeBaseUrl: webrtcBaseUrl }); | ||
| const stream = createSyntheticStream(BIT_INVERT_MODEL.fps, BIT_INVERT_MODEL.width, BIT_INVERT_MODEL.height); | ||
|
|
||
| let remoteStreamReceived = false; | ||
|
|
||
| const realtimeClient = await client.realtime.connect(stream, { | ||
| model: BIT_INVERT_MODEL, | ||
| onRemoteStream: () => { | ||
| remoteStreamReceived = true; | ||
| }, | ||
| iceServers: TURN_ICE_SERVERS, | ||
| }); | ||
|
|
||
| // Register diagnostic listener immediately | ||
| const candidatePairPromise = collectSelectedCandidatePair(realtimeClient); | ||
|
|
||
| const errors: DecartSDKError[] = []; | ||
| realtimeClient.on("error", (err) => errors.push(err)); | ||
|
|
||
| try { | ||
| expect(["connected", "generating"]).toContain(realtimeClient.getConnectionState()); | ||
| expect(realtimeClient.sessionId).toBeTruthy(); | ||
| expect(remoteStreamReceived).toBe(true); | ||
| expect(errors).toEqual([]); | ||
|
|
||
| // With default policy, ICE should prefer direct UDP over relay. | ||
| // In Docker/NAT environments, the local candidate may appear as "prflx" | ||
| // (peer-reflexive) rather than "host", but it should NOT be "relay". | ||
| const pair = await candidatePairPromise; | ||
| if (pair) { | ||
| expect(pair.local.candidateType).not.toBe("relay"); | ||
| } | ||
| } finally { | ||
| realtimeClient.disconnect(); | ||
| for (const track of stream.getTracks()) track.stop(); | ||
| } | ||
|
|
||
| expect(realtimeClient.getConnectionState()).toBe("disconnected"); | ||
| }); | ||
| }); |
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,18 @@ | ||
| import { playwright } from "@vitest/browser-playwright"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| define: { | ||
| __DECART_API_KEY__: JSON.stringify(process.env.DECART_API_KEY), | ||
| __WEBRTC_BASE_URL__: JSON.stringify(process.env.WEBRTC_BASE_URL || "wss://slim-bit-invert.dev.localhost"), | ||
| }, | ||
| test: { | ||
| include: ["tests/e2e-turn-tcp.test.ts"], | ||
| browser: { | ||
| enabled: true, | ||
| provider: playwright(), | ||
| headless: true, | ||
| instances: [{ browser: "chromium" }], | ||
| }, | ||
| }, | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.