72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import { randomUUID } from 'crypto';
|
|
|
|
import { mediaConfig } from '../config';
|
|
import type {
|
|
SfuPublishTransportRequest,
|
|
SfuPublishTransportResult,
|
|
SfuService,
|
|
SfuSessionDescriptor,
|
|
SfuSessionStartInput,
|
|
SfuSubscribeTransportRequest,
|
|
SfuSubscribeTransportResult,
|
|
} from './types';
|
|
|
|
const toIceServers = (): Array<{ urls: string; username?: string; credential?: string }> => {
|
|
if (mediaConfig.turn.urls.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
return mediaConfig.turn.urls.map((urls) => ({
|
|
urls,
|
|
...(mediaConfig.turn.username ? { username: mediaConfig.turn.username } : {}),
|
|
...(mediaConfig.turn.credential ? { credential: mediaConfig.turn.credential } : {}),
|
|
}));
|
|
};
|
|
|
|
export class NoopSfuService implements SfuService {
|
|
mode: 'single_server_sfu' = 'single_server_sfu';
|
|
private readonly sessions = new Map<string, SfuSessionDescriptor>();
|
|
|
|
async startSession(input: SfuSessionStartInput): Promise<SfuSessionDescriptor> {
|
|
const now = new Date().toISOString();
|
|
const existing = this.sessions.get(input.streamSessionId);
|
|
if (existing) return existing;
|
|
|
|
const descriptor: SfuSessionDescriptor = {
|
|
streamSessionId: input.streamSessionId,
|
|
ownerUserId: input.ownerUserId,
|
|
cameraDeviceId: input.cameraDeviceId,
|
|
requesterDeviceId: input.requesterDeviceId,
|
|
state: 'starting',
|
|
createdAt: now,
|
|
};
|
|
this.sessions.set(input.streamSessionId, descriptor);
|
|
return descriptor;
|
|
}
|
|
|
|
async endSession(streamSessionId: string): Promise<void> {
|
|
const existing = this.sessions.get(streamSessionId);
|
|
if (!existing) return;
|
|
this.sessions.set(streamSessionId, { ...existing, state: 'ended' });
|
|
}
|
|
|
|
async getSession(streamSessionId: string): Promise<SfuSessionDescriptor | null> {
|
|
return this.sessions.get(streamSessionId) ?? null;
|
|
}
|
|
|
|
async createPublishTransport(_input: SfuPublishTransportRequest): Promise<SfuPublishTransportResult> {
|
|
return {
|
|
transportId: `pub_${randomUUID()}`,
|
|
iceServers: toIceServers(),
|
|
};
|
|
}
|
|
|
|
async createSubscribeTransport(_input: SfuSubscribeTransportRequest): Promise<SfuSubscribeTransportResult> {
|
|
return {
|
|
transportId: `sub_${randomUUID()}`,
|
|
iceServers: toIceServers(),
|
|
};
|
|
}
|
|
}
|
|
|