Files
siprouter/ts/codec.ts
Juergen Kunz f3e1c96872 initial commit — SIP B2BUA + WebRTC bridge with Rust codec engine
Full-featured SIP router with multi-provider trunking, browser softphone
via WebRTC, real-time Opus/G.722/PCM transcoding in Rust, RNNoise ML
noise suppression, Kokoro neural TTS announcements, and a Lit-based
web dashboard with live call monitoring and REST API.
2026-04-09 23:03:55 +00:00

41 lines
1.3 KiB
TypeScript

/**
* Audio codec translation layer for bridging between WebRTC and SIP.
*
* All actual codec work (Opus, G.722, PCMU, PCMA) is done in Rust via
* the smartrust bridge. This module provides the RTP-level transcoding
* interface used by the webrtcbridge.
*/
import { Buffer } from 'node:buffer';
import { transcode, isCodecReady } from './opusbridge.ts';
/** Opus dynamic payload type (standard WebRTC assignment). */
export const OPUS_PT = 111;
export interface IRtpTranscoder {
/** Transcode an RTP payload. Always async (Rust IPC). */
payload(data: Buffer): Promise<Buffer>;
fromPT: number;
toPT: number;
}
/**
* Create a transcoder that converts RTP payloads between two codecs.
* Returns null if the codecs are the same or the Rust bridge isn't ready.
*
* @param sessionId - optional Rust codec session for isolated state per call
*/
export function createTranscoder(fromPT: number, toPT: number, sessionId?: string, direction?: string): IRtpTranscoder | null {
if (fromPT === toPT) return null;
if (!isCodecReady()) return null;
return {
fromPT,
toPT,
async payload(data: Buffer): Promise<Buffer> {
const result = await transcode(data, fromPT, toPT, sessionId, direction);
return result || Buffer.alloc(0); // return empty on failure — never pass raw codec bytes
},
};
}