/** * 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; 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 { const result = await transcode(data, fromPT, toPT, sessionId, direction); return result || Buffer.alloc(0); // return empty on failure — never pass raw codec bytes }, }; }