feat(streams): add phase-2 SFU transport handshake and produce/consume APIs

This commit is contained in:
2026-02-08 16:35:00 +00:00
parent 8eed0df577
commit 8fc7302a58
5 changed files with 529 additions and 33 deletions

View File

@@ -3,6 +3,12 @@ import { randomUUID } from 'crypto';
import { mediaConfig } from '../config';
import { SfuSessionRegistry } from './registry';
import type {
SfuConnectTransportInput,
SfuConsumeInput,
SfuConsumerDescriptor,
SfuIceServer,
SfuProduceInput,
SfuProducerDescriptor,
SfuPublishTransportRequest,
SfuPublishTransportResult,
SfuService,
@@ -10,9 +16,10 @@ import type {
SfuSessionStartInput,
SfuSubscribeTransportRequest,
SfuSubscribeTransportResult,
SfuTransportDescriptor,
} from './types';
const toIceServers = (): Array<{ urls: string; username?: string; credential?: string }> => {
const toIceServers = (): SfuIceServer[] => {
if (mediaConfig.turn.urls.length === 0) {
return [];
}
@@ -61,17 +68,115 @@ export class NoopSfuService implements SfuService {
return this.registry.list();
}
async createPublishTransport(_input: SfuPublishTransportRequest): Promise<SfuPublishTransportResult> {
async listTransports(streamSessionId: string): Promise<SfuTransportDescriptor[]> {
return this.registry.listTransports(streamSessionId);
}
async listProducers(streamSessionId: string): Promise<SfuProducerDescriptor[]> {
return this.registry.listProducers(streamSessionId);
}
async listConsumers(streamSessionId: string): Promise<SfuConsumerDescriptor[]> {
return this.registry.listConsumers(streamSessionId);
}
async createPublishTransport(input: SfuPublishTransportRequest): Promise<SfuPublishTransportResult> {
const transportId = `pub_${randomUUID()}`;
this.registry.addTransport({
transportId,
streamSessionId: input.streamSessionId,
ownerDeviceId: input.cameraDeviceId,
direction: 'publish',
});
return {
transportId: `pub_${randomUUID()}`,
transportId,
iceServers: toIceServers(),
};
}
async createSubscribeTransport(_input: SfuSubscribeTransportRequest): Promise<SfuSubscribeTransportResult> {
async createSubscribeTransport(input: SfuSubscribeTransportRequest): Promise<SfuSubscribeTransportResult> {
const transportId = `sub_${randomUUID()}`;
this.registry.addTransport({
transportId,
streamSessionId: input.streamSessionId,
ownerDeviceId: input.viewerDeviceId,
direction: 'subscribe',
});
return {
transportId: `sub_${randomUUID()}`,
transportId,
iceServers: toIceServers(),
};
}
async connectPublishTransport(input: SfuConnectTransportInput): Promise<SfuTransportDescriptor> {
const transport = this.registry.getTransport(input.transportId);
if (!transport) throw new Error('Publish transport not found');
if (transport.streamSessionId !== input.streamSessionId) throw new Error('Transport does not belong to stream');
if (transport.direction !== 'publish') throw new Error('Transport is not a publish transport');
if (transport.ownerDeviceId !== input.deviceId) throw new Error('Device does not own this publish transport');
const connected = this.registry.connectTransport(input.transportId);
if (!connected) throw new Error('Publish transport connect failed');
return connected;
}
async connectSubscribeTransport(input: SfuConnectTransportInput): Promise<SfuTransportDescriptor> {
const transport = this.registry.getTransport(input.transportId);
if (!transport) throw new Error('Subscribe transport not found');
if (transport.streamSessionId !== input.streamSessionId) throw new Error('Transport does not belong to stream');
if (transport.direction !== 'subscribe') throw new Error('Transport is not a subscribe transport');
if (transport.ownerDeviceId !== input.deviceId) throw new Error('Device does not own this subscribe transport');
const connected = this.registry.connectTransport(input.transportId);
if (!connected) throw new Error('Subscribe transport connect failed');
return connected;
}
async produce(input: SfuProduceInput): Promise<SfuProducerDescriptor> {
const transport = this.registry.getTransport(input.transportId);
if (!transport) throw new Error('Publish transport not found');
if (transport.streamSessionId !== input.streamSessionId) throw new Error('Transport does not belong to stream');
if (transport.direction !== 'publish') throw new Error('Transport is not a publish transport');
if (transport.ownerDeviceId !== input.cameraDeviceId) throw new Error('Device does not own this publish transport');
if (transport.state !== 'connected') throw new Error('Publish transport must be connected before producing');
return this.registry.addProducer({
producerId: `prod_${randomUUID()}`,
streamSessionId: input.streamSessionId,
transportId: input.transportId,
cameraDeviceId: input.cameraDeviceId,
kind: input.kind,
rtpParameters: input.rtpParameters,
});
}
async consume(input: SfuConsumeInput): Promise<SfuConsumerDescriptor> {
const transport = this.registry.getTransport(input.transportId);
if (!transport) throw new Error('Subscribe transport not found');
if (transport.streamSessionId !== input.streamSessionId) throw new Error('Transport does not belong to stream');
if (transport.direction !== 'subscribe') throw new Error('Transport is not a subscribe transport');
if (transport.ownerDeviceId !== input.viewerDeviceId) throw new Error('Device does not own this subscribe transport');
if (transport.state !== 'connected') throw new Error('Subscribe transport must be connected before consuming');
const selectedProducer =
(input.producerId ? this.registry.getProducer(input.producerId) : null) ??
this.registry
.listProducers(input.streamSessionId)
.slice()
.reverse()
.find((producer) => producer.kind === 'video');
if (!selectedProducer) throw new Error('No producer available for consume');
if (selectedProducer.streamSessionId !== input.streamSessionId) throw new Error('Producer does not belong to stream');
return this.registry.addConsumer({
consumerId: `cons_${randomUUID()}`,
streamSessionId: input.streamSessionId,
transportId: input.transportId,
viewerDeviceId: input.viewerDeviceId,
producerId: selectedProducer.producerId,
kind: selectedProducer.kind,
rtpParameters: selectedProducer.rtpParameters,
});
}
}