diff --git a/.changeset/egress-stream-destinations.md b/.changeset/egress-stream-destinations.md new file mode 100644 index 00000000..f87f09f6 --- /dev/null +++ b/.changeset/egress-stream-destinations.md @@ -0,0 +1,5 @@ +--- +'livekit-server-sdk': patch +--- + +Add `addDestinations` to `EgressClient.updateStream` and export `StreamDestination`, so a stream started with `StreamOutput.destinations` or added to a running egress carries an id that egress reports back on `StreamInfo.id` (livekit/egress#1462). Update @livekit/protocol to 1.53.1. diff --git a/packages/livekit-server-sdk/src/EgressClient.test.ts b/packages/livekit-server-sdk/src/EgressClient.test.ts new file mode 100644 index 00000000..14f04a03 --- /dev/null +++ b/packages/livekit-server-sdk/src/EgressClient.test.ts @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2026 LiveKit, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +import { StreamDestination, StreamOutput, StreamProtocol } from '@livekit/protocol'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { EgressClient } from './EgressClient.js'; + +describe('EgressClient stream destinations', () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + const youtube = { id: 'youtube', url: 'rtmps://a.example.com/live/key' }; + + const sentBodies = () => { + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({ + ok: true, + status: 200, + json: async () => ({ egressId: 'EG_abc123' }), + } as unknown as Response); + return () => fetchSpy.mock.calls.map(([, init]) => JSON.parse(init!.body as string)); + }; + + const client = () => + new EgressClient('https://test.livekit.cloud', 'devkey', 'secret', { failover: false }); + + it('adds destinations, with their ids, to a running egress', async () => { + const bodies = sentBodies(); + + await client().updateStream('EG_abc123', [], [], [new StreamDestination(youtube)]); + + expect(bodies()).toEqual([{ egressId: 'EG_abc123', addDestinations: [youtube] }]); + }); + + it('starts an egress streaming to destinations', async () => { + const bodies = sentBodies(); + + await client().startRoomCompositeEgress( + 'test-room', + new StreamOutput({ + protocol: StreamProtocol.RTMP, + destinations: [new StreamDestination(youtube)], + }), + ); + + expect(bodies()[0].streamOutputs).toEqual([{ protocol: 'RTMP', destinations: [youtube] }]); + }); +}); diff --git a/packages/livekit-server-sdk/src/EgressClient.ts b/packages/livekit-server-sdk/src/EgressClient.ts index 6f01e038..6f925ddd 100644 --- a/packages/livekit-server-sdk/src/EgressClient.ts +++ b/packages/livekit-server-sdk/src/EgressClient.ts @@ -8,6 +8,7 @@ import type { EncodingOptionsPreset, ImageOutput, SegmentedFileOutput, + StreamDestination, StreamOutput, WebhookConfig, } from '@livekit/protocol'; @@ -614,19 +615,28 @@ export class EgressClient extends ServiceBase { * @param egressId - * @param addOutputUrls - * @param removeOutputUrls - + * @param addDestinations - stream urls to add, each with an id of your choice that the + * resulting `StreamInfo` carries back in `id`. Remove them with `removeOutputUrls`. */ async updateStream( egressId: string, addOutputUrls?: string[], removeOutputUrls?: string[], + addDestinations?: StreamDestination[], ): Promise { addOutputUrls ??= []; removeOutputUrls ??= []; + addDestinations ??= []; const data = await this.rpc.request( svc, 'UpdateStream', - new UpdateStreamRequest({ egressId, addOutputUrls, removeOutputUrls }).toJson(), + new UpdateStreamRequest({ + egressId, + addOutputUrls, + removeOutputUrls, + addDestinations, + }).toJson(), await this.authHeader({ roomRecord: true }), ); return EgressInfo.fromJson(data, { ignoreUnknownFields: true }); diff --git a/packages/livekit-server-sdk/src/index.ts b/packages/livekit-server-sdk/src/index.ts index ef6b0d25..9e2b8cf7 100644 --- a/packages/livekit-server-sdk/src/index.ts +++ b/packages/livekit-server-sdk/src/index.ts @@ -68,6 +68,7 @@ export { SegmentedFileProtocol, StartEgressRequest, StorageConfig, + StreamDestination, StreamOutput, StreamProtocol, TemplateSource, diff --git a/packages/livekit-server-sdk/test/api/livekitapi.test.ts b/packages/livekit-server-sdk/test/api/livekitapi.test.ts index 86f967c5..021d43ee 100644 --- a/packages/livekit-server-sdk/test/api/livekitapi.test.ts +++ b/packages/livekit-server-sdk/test/api/livekitapi.test.ts @@ -37,6 +37,7 @@ import { SessionDescription, StartEgressRequest, StorageConfig, + StreamDestination, StreamOutput, StreamProtocol, WebSource, @@ -182,6 +183,7 @@ d('LiveKitAPI', () => { 'EG_abc123', ['rtmps://b.example.com/live/key'], ['rtmps://a.example.com/live/key'], + [new StreamDestination({ id: 'youtube', url: 'rtmps://c.example.com/live/key' })], )); it('listEgress', () => api.egress.listEgress({ roomName: 'test-room', egressId: 'EG_abc123', active: true }));