Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c9ae747c95 | |||
| 45f9b9c15c | |||
| 7d59361352 | |||
| 6a130db7c7 | |||
| 93f671f1f9 | |||
| 36eab44e28 | |||
| 9e5aa35fee | |||
| 82f2742db5 | |||
| 239e2ac81d |
33
changelog.md
33
changelog.md
@@ -1,5 +1,38 @@
|
||||
# Changelog
|
||||
|
||||
## 2026-04-10 - 1.15.0 - feat(proxy-engine)
|
||||
add device leg, leg transfer, and leg replacement call controls
|
||||
|
||||
- adds proxy-engine commands and call manager support for inviting a registered SIP device into an active call
|
||||
- supports transferring an existing leg between calls while preserving the active connection and updating mixer routing
|
||||
- supports replacing a call leg by removing the current leg and dialing a new outbound destination
|
||||
- wires the frontend add-leg API and TypeScript bridge to the new device leg and leg control commands
|
||||
|
||||
## 2026-04-10 - 1.14.0 - feat(proxy-engine)
|
||||
add multiparty call mixing with dynamic SIP and WebRTC leg management
|
||||
|
||||
- replace passthrough call handling with a mixer-backed call model that tracks multiple legs and exposes leg status in call state output
|
||||
- add mixer and leg I/O infrastructure to bridge SIP RTP and WebRTC audio through channel-based mix-minus processing
|
||||
- introduce add_leg and remove_leg proxy commands and wire frontend bridge APIs to manage external call legs
|
||||
- emit leg lifecycle events for observability and mark unimplemented device-leg and transfer HTTP endpoints with 501 responses
|
||||
|
||||
## 2026-04-10 - 1.13.0 - feat(proxy-engine,webrtc)
|
||||
add B2BUA SIP leg handling and WebRTC call bridging for outbound calls
|
||||
|
||||
- introduce a new SipLeg module to manage outbound provider dialogs, including INVITE lifecycle, digest auth retries, ACK handling, media endpoint tracking, and termination
|
||||
- store outbound dashboard calls as B2BUA calls in the call manager and emit provider media details on call_answered for bridge setup
|
||||
- separate SIP and WebRTC engine locking to avoid contention and deadlocks while linking sessions to call RTP sockets
|
||||
- add bidirectional RTP bridging between provider SIP media and browser WebRTC audio using the allocated RTP socket
|
||||
- wire browser webrtc-accept events in the frontend and sipproxy so session-to-call linking can occur when media and acceptance arrive in either order
|
||||
|
||||
## 2026-04-10 - 1.12.0 - feat(proxy-engine)
|
||||
add Rust-based outbound calling, WebRTC bridging, and voicemail handling
|
||||
|
||||
- adds outbound call origination through the Rust proxy engine with dashboard make_call support
|
||||
- routes unanswered inbound calls to voicemail, including greeting playback, beep generation, and WAV message recording
|
||||
- introduces Rust WebRTC session handling and SIP audio bridging, replacing the previous TypeScript WebRTC path
|
||||
- moves SIP registration and routing responsibilities further into the Rust proxy engine and removes legacy TypeScript call/SIP modules
|
||||
|
||||
## 2026-04-10 - 1.11.0 - feat(rust-proxy-engine)
|
||||
add a Rust SIP proxy engine with shared SIP and codec libraries
|
||||
|
||||
|
||||
BIN
nogit/voicemail/default/msg-1775825168199.wav
Normal file
BIN
nogit/voicemail/default/msg-1775825168199.wav
Normal file
Binary file not shown.
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "siprouter",
|
||||
"version": "1.11.0",
|
||||
"version": "1.15.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
@@ -14,7 +14,6 @@
|
||||
"@design.estate/dees-element": "^2.2.4",
|
||||
"@push.rocks/smartrust": "^1.3.2",
|
||||
"@push.rocks/smartstate": "^2.3.0",
|
||||
"werift": "^0.22.9",
|
||||
"ws": "^8.20.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
1839
rust/Cargo.lock
generated
1839
rust/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -15,3 +15,6 @@ serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
base64 = "0.22"
|
||||
regex-lite = "0.1"
|
||||
webrtc = "0.8"
|
||||
rand = "0.8"
|
||||
hound = "3.5"
|
||||
|
||||
240
rust/crates/proxy-engine/src/audio_player.rs
Normal file
240
rust/crates/proxy-engine/src/audio_player.rs
Normal file
@@ -0,0 +1,240 @@
|
||||
//! Audio player — reads a WAV file and streams it as RTP packets.
|
||||
//! Also provides prompt preparation for the leg interaction system.
|
||||
|
||||
use crate::rtp::{build_rtp_header, rtp_clock_increment};
|
||||
use codec_lib::{codec_sample_rate, TranscodeState};
|
||||
use std::net::SocketAddr;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::UdpSocket;
|
||||
use tokio::time::{self, Duration};
|
||||
|
||||
/// Mixing sample rate used by the mixer (must stay in sync with mixer::MIX_RATE).
|
||||
const MIX_RATE: u32 = 16000;
|
||||
/// Samples per 20ms frame at the mixing rate.
|
||||
const MIX_FRAME_SIZE: usize = 320;
|
||||
|
||||
/// Play a WAV file as RTP to a destination.
|
||||
/// Returns when playback is complete.
|
||||
pub async fn play_wav_file(
|
||||
file_path: &str,
|
||||
socket: Arc<UdpSocket>,
|
||||
dest: SocketAddr,
|
||||
codec_pt: u8,
|
||||
ssrc: u32,
|
||||
) -> Result<u32, String> {
|
||||
let path = Path::new(file_path);
|
||||
if !path.exists() {
|
||||
return Err(format!("WAV file not found: {file_path}"));
|
||||
}
|
||||
|
||||
// Read WAV file.
|
||||
let mut reader =
|
||||
hound::WavReader::open(path).map_err(|e| format!("open WAV {file_path}: {e}"))?;
|
||||
let spec = reader.spec();
|
||||
let wav_rate = spec.sample_rate;
|
||||
|
||||
// Read all samples as i16.
|
||||
let samples: Vec<i16> = if spec.bits_per_sample == 16 {
|
||||
reader
|
||||
.samples::<i16>()
|
||||
.filter_map(|s| s.ok())
|
||||
.collect()
|
||||
} else if spec.bits_per_sample == 32 && spec.sample_format == hound::SampleFormat::Float {
|
||||
reader
|
||||
.samples::<f32>()
|
||||
.filter_map(|s| s.ok())
|
||||
.map(|s| (s * 32767.0).round().clamp(-32768.0, 32767.0) as i16)
|
||||
.collect()
|
||||
} else {
|
||||
return Err(format!(
|
||||
"unsupported WAV format: {}bit {:?}",
|
||||
spec.bits_per_sample, spec.sample_format
|
||||
));
|
||||
};
|
||||
|
||||
if samples.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
// Create codec state for encoding.
|
||||
let mut transcoder = TranscodeState::new().map_err(|e| format!("codec init: {e}"))?;
|
||||
|
||||
// Resample to target codec rate.
|
||||
let target_rate = codec_sample_rate(codec_pt);
|
||||
let resampled = if wav_rate != target_rate {
|
||||
transcoder
|
||||
.resample(&samples, wav_rate, target_rate)
|
||||
.map_err(|e| format!("resample: {e}"))?
|
||||
} else {
|
||||
samples
|
||||
};
|
||||
|
||||
// Calculate frame size (20ms of audio at target rate).
|
||||
let frame_samples = (target_rate as usize) / 50; // 20ms = 1/50 second
|
||||
|
||||
// Stream as RTP at 20ms intervals.
|
||||
let mut seq: u16 = 0;
|
||||
let mut ts: u32 = 0;
|
||||
let mut offset = 0;
|
||||
let mut interval = time::interval(Duration::from_millis(20));
|
||||
let mut frames_sent = 0u32;
|
||||
|
||||
while offset < resampled.len() {
|
||||
interval.tick().await;
|
||||
|
||||
let end = (offset + frame_samples).min(resampled.len());
|
||||
let frame = &resampled[offset..end];
|
||||
|
||||
// Pad short final frame with silence.
|
||||
let frame_data = if frame.len() < frame_samples {
|
||||
let mut padded = frame.to_vec();
|
||||
padded.resize(frame_samples, 0);
|
||||
padded
|
||||
} else {
|
||||
frame.to_vec()
|
||||
};
|
||||
|
||||
// Encode to target codec.
|
||||
let encoded = match transcoder.encode_from_pcm(&frame_data, codec_pt) {
|
||||
Ok(e) if !e.is_empty() => e,
|
||||
_ => {
|
||||
offset += frame_samples;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// Build RTP packet.
|
||||
let header = build_rtp_header(codec_pt, seq, ts, ssrc);
|
||||
let mut packet = header.to_vec();
|
||||
packet.extend_from_slice(&encoded);
|
||||
|
||||
let _ = socket.send_to(&packet, dest).await;
|
||||
|
||||
seq = seq.wrapping_add(1);
|
||||
ts = ts.wrapping_add(rtp_clock_increment(codec_pt));
|
||||
offset += frame_samples;
|
||||
frames_sent += 1;
|
||||
}
|
||||
|
||||
Ok(frames_sent)
|
||||
}
|
||||
|
||||
/// Generate and play a beep tone (sine wave) as RTP.
|
||||
pub async fn play_beep(
|
||||
socket: Arc<UdpSocket>,
|
||||
dest: SocketAddr,
|
||||
codec_pt: u8,
|
||||
ssrc: u32,
|
||||
start_seq: u16,
|
||||
start_ts: u32,
|
||||
freq_hz: u32,
|
||||
duration_ms: u32,
|
||||
) -> Result<(u16, u32), String> {
|
||||
let mut transcoder = TranscodeState::new().map_err(|e| format!("codec init: {e}"))?;
|
||||
let target_rate = codec_sample_rate(codec_pt);
|
||||
let frame_samples = (target_rate as usize) / 50;
|
||||
let total_samples = (target_rate as usize * duration_ms as usize) / 1000;
|
||||
|
||||
// Generate sine wave.
|
||||
let amplitude = 16000i16;
|
||||
let sine: Vec<i16> = (0..total_samples)
|
||||
.map(|i| {
|
||||
let t = i as f64 / target_rate as f64;
|
||||
(amplitude as f64 * (2.0 * std::f64::consts::PI * freq_hz as f64 * t).sin()) as i16
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut seq = start_seq;
|
||||
let mut ts = start_ts;
|
||||
let mut offset = 0;
|
||||
let mut interval = time::interval(Duration::from_millis(20));
|
||||
|
||||
while offset < sine.len() {
|
||||
interval.tick().await;
|
||||
|
||||
let end = (offset + frame_samples).min(sine.len());
|
||||
let mut frame = sine[offset..end].to_vec();
|
||||
frame.resize(frame_samples, 0);
|
||||
|
||||
let encoded = match transcoder.encode_from_pcm(&frame, codec_pt) {
|
||||
Ok(e) if !e.is_empty() => e,
|
||||
_ => {
|
||||
offset += frame_samples;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let header = build_rtp_header(codec_pt, seq, ts, ssrc);
|
||||
let mut packet = header.to_vec();
|
||||
packet.extend_from_slice(&encoded);
|
||||
let _ = socket.send_to(&packet, dest).await;
|
||||
|
||||
seq = seq.wrapping_add(1);
|
||||
ts = ts.wrapping_add(rtp_clock_increment(codec_pt));
|
||||
offset += frame_samples;
|
||||
}
|
||||
|
||||
Ok((seq, ts))
|
||||
}
|
||||
|
||||
/// Load a WAV file and split it into 20ms PCM frames at 16kHz.
|
||||
/// Used by the leg interaction system to prepare prompt audio for the mixer.
|
||||
pub fn load_prompt_pcm_frames(wav_path: &str) -> Result<Vec<Vec<i16>>, String> {
|
||||
let path = Path::new(wav_path);
|
||||
if !path.exists() {
|
||||
return Err(format!("WAV file not found: {wav_path}"));
|
||||
}
|
||||
|
||||
let mut reader =
|
||||
hound::WavReader::open(path).map_err(|e| format!("open WAV {wav_path}: {e}"))?;
|
||||
let spec = reader.spec();
|
||||
let wav_rate = spec.sample_rate;
|
||||
|
||||
// Read all samples as i16.
|
||||
let samples: Vec<i16> = if spec.bits_per_sample == 16 {
|
||||
reader
|
||||
.samples::<i16>()
|
||||
.filter_map(|s| s.ok())
|
||||
.collect()
|
||||
} else if spec.bits_per_sample == 32 && spec.sample_format == hound::SampleFormat::Float {
|
||||
reader
|
||||
.samples::<f32>()
|
||||
.filter_map(|s| s.ok())
|
||||
.map(|s| (s * 32767.0).round().clamp(-32768.0, 32767.0) as i16)
|
||||
.collect()
|
||||
} else {
|
||||
return Err(format!(
|
||||
"unsupported WAV format: {}bit {:?}",
|
||||
spec.bits_per_sample, spec.sample_format
|
||||
));
|
||||
};
|
||||
|
||||
if samples.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
// Resample to MIX_RATE (16kHz) if needed.
|
||||
let resampled = if wav_rate != MIX_RATE {
|
||||
let mut transcoder = TranscodeState::new().map_err(|e| format!("codec init: {e}"))?;
|
||||
transcoder
|
||||
.resample(&samples, wav_rate, MIX_RATE)
|
||||
.map_err(|e| format!("resample: {e}"))?
|
||||
} else {
|
||||
samples
|
||||
};
|
||||
|
||||
// Split into MIX_FRAME_SIZE (320) sample frames.
|
||||
let mut frames = Vec::new();
|
||||
let mut offset = 0;
|
||||
while offset < resampled.len() {
|
||||
let end = (offset + MIX_FRAME_SIZE).min(resampled.len());
|
||||
let mut frame = resampled[offset..end].to_vec();
|
||||
// Pad short final frame with silence.
|
||||
frame.resize(MIX_FRAME_SIZE, 0);
|
||||
frames.push(frame);
|
||||
offset += MIX_FRAME_SIZE;
|
||||
}
|
||||
|
||||
Ok(frames)
|
||||
}
|
||||
@@ -1,12 +1,20 @@
|
||||
//! Call hub — owns legs and bridges media.
|
||||
//! Call hub — owns N legs and a mixer task.
|
||||
//!
|
||||
//! Each Call has a unique ID and tracks its state, direction, and associated
|
||||
//! SIP Call-IDs for message routing.
|
||||
//! Every call has a central mixer that provides mix-minus audio to all
|
||||
//! participants. Legs can be added and removed dynamically mid-call.
|
||||
|
||||
use crate::mixer::{MixerCommand, RtpPacket};
|
||||
use crate::sip_leg::SipLeg;
|
||||
use sip_proto::message::SipMessage;
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
use tokio::net::UdpSocket;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
pub type LegId = String;
|
||||
|
||||
/// Call state machine.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -15,8 +23,6 @@ pub enum CallState {
|
||||
Ringing,
|
||||
Connected,
|
||||
Voicemail,
|
||||
Ivr,
|
||||
Terminating,
|
||||
Terminated,
|
||||
}
|
||||
|
||||
@@ -27,8 +33,6 @@ impl CallState {
|
||||
Self::Ringing => "ringing",
|
||||
Self::Connected => "connected",
|
||||
Self::Voicemail => "voicemail",
|
||||
Self::Ivr => "ivr",
|
||||
Self::Terminating => "terminating",
|
||||
Self::Terminated => "terminated",
|
||||
}
|
||||
}
|
||||
@@ -49,43 +53,191 @@ impl CallDirection {
|
||||
}
|
||||
}
|
||||
|
||||
/// A passthrough call — both sides share the same SIP Call-ID.
|
||||
/// The proxy rewrites SDP/Contact/Request-URI and relays RTP.
|
||||
pub struct PassthroughCall {
|
||||
/// The type of a call leg.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LegKind {
|
||||
SipProvider,
|
||||
SipDevice,
|
||||
WebRtc,
|
||||
Media, // voicemail playback, IVR, recording
|
||||
Tool, // observer leg for recording, transcription, etc.
|
||||
}
|
||||
|
||||
impl LegKind {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::SipProvider => "sip-provider",
|
||||
Self::SipDevice => "sip-device",
|
||||
Self::WebRtc => "webrtc",
|
||||
Self::Media => "media",
|
||||
Self::Tool => "tool",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-leg state.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LegState {
|
||||
Inviting,
|
||||
Ringing,
|
||||
Connected,
|
||||
Terminated,
|
||||
}
|
||||
|
||||
impl LegState {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Inviting => "inviting",
|
||||
Self::Ringing => "ringing",
|
||||
Self::Connected => "connected",
|
||||
Self::Terminated => "terminated",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Information about a single leg in a call.
|
||||
pub struct LegInfo {
|
||||
pub id: LegId,
|
||||
pub kind: LegKind,
|
||||
pub state: LegState,
|
||||
pub codec_pt: u8,
|
||||
|
||||
/// For SIP legs: the SIP dialog manager (handles 407 auth, BYE, etc).
|
||||
pub sip_leg: Option<SipLeg>,
|
||||
/// For SIP legs: the SIP Call-ID for message routing.
|
||||
pub sip_call_id: Option<String>,
|
||||
/// For WebRTC legs: the session ID in WebRtcEngine.
|
||||
pub webrtc_session_id: Option<String>,
|
||||
/// The RTP socket allocated for this leg.
|
||||
pub rtp_socket: Option<Arc<UdpSocket>>,
|
||||
/// The RTP port number.
|
||||
pub rtp_port: u16,
|
||||
/// The remote media endpoint (learned from SDP or address learning).
|
||||
pub remote_media: Option<SocketAddr>,
|
||||
/// SIP signaling address (provider or device).
|
||||
pub signaling_addr: Option<SocketAddr>,
|
||||
|
||||
/// Flexible key-value metadata (consent state, tool config, etc.).
|
||||
/// Persisted into call history on call end.
|
||||
pub metadata: HashMap<String, serde_json::Value>,
|
||||
}
|
||||
|
||||
/// A multiparty call with N legs and a central mixer.
|
||||
pub struct Call {
|
||||
pub id: String,
|
||||
pub sip_call_id: String,
|
||||
pub state: CallState,
|
||||
pub direction: CallDirection,
|
||||
pub created_at: Instant,
|
||||
|
||||
// Call metadata.
|
||||
// Metadata.
|
||||
pub caller_number: Option<String>,
|
||||
pub callee_number: Option<String>,
|
||||
pub provider_id: String,
|
||||
|
||||
// Provider side.
|
||||
pub provider_addr: SocketAddr,
|
||||
pub provider_media: Option<SocketAddr>,
|
||||
/// Original INVITE from the device (for device-originated outbound calls).
|
||||
/// Used to construct proper 180/200/error responses back to the device.
|
||||
pub device_invite: Option<SipMessage>,
|
||||
|
||||
// Device side.
|
||||
pub device_addr: SocketAddr,
|
||||
pub device_media: Option<SocketAddr>,
|
||||
/// All legs in this call, keyed by leg ID.
|
||||
pub legs: HashMap<LegId, LegInfo>,
|
||||
|
||||
// RTP relay.
|
||||
pub rtp_port: u16,
|
||||
pub rtp_socket: Arc<UdpSocket>,
|
||||
/// Channel to send commands to the mixer task.
|
||||
pub mixer_cmd_tx: mpsc::Sender<MixerCommand>,
|
||||
|
||||
// Packet counters.
|
||||
pub pkt_from_device: u64,
|
||||
pub pkt_from_provider: u64,
|
||||
/// Handle to the mixer task (aborted on call teardown).
|
||||
mixer_task: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl PassthroughCall {
|
||||
impl Call {
|
||||
pub fn new(
|
||||
id: String,
|
||||
direction: CallDirection,
|
||||
provider_id: String,
|
||||
mixer_cmd_tx: mpsc::Sender<MixerCommand>,
|
||||
mixer_task: JoinHandle<()>,
|
||||
) -> Self {
|
||||
Self {
|
||||
id,
|
||||
state: CallState::SettingUp,
|
||||
direction,
|
||||
created_at: Instant::now(),
|
||||
caller_number: None,
|
||||
callee_number: None,
|
||||
provider_id,
|
||||
device_invite: None,
|
||||
legs: HashMap::new(),
|
||||
mixer_cmd_tx,
|
||||
mixer_task: Some(mixer_task),
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a leg to the mixer. Sends the AddLeg command with channel endpoints.
|
||||
pub async fn add_leg_to_mixer(
|
||||
&self,
|
||||
leg_id: &str,
|
||||
codec_pt: u8,
|
||||
inbound_rx: mpsc::Receiver<RtpPacket>,
|
||||
outbound_tx: mpsc::Sender<Vec<u8>>,
|
||||
) {
|
||||
let _ = self
|
||||
.mixer_cmd_tx
|
||||
.send(MixerCommand::AddLeg {
|
||||
leg_id: leg_id.to_string(),
|
||||
codec_pt,
|
||||
inbound_rx,
|
||||
outbound_tx,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Remove a leg from the mixer.
|
||||
pub async fn remove_leg_from_mixer(&self, leg_id: &str) {
|
||||
let _ = self
|
||||
.mixer_cmd_tx
|
||||
.send(MixerCommand::RemoveLeg {
|
||||
leg_id: leg_id.to_string(),
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
pub fn duration_secs(&self) -> u64 {
|
||||
self.created_at.elapsed().as_secs()
|
||||
}
|
||||
|
||||
/// Shut down the mixer and abort its task.
|
||||
pub async fn shutdown_mixer(&mut self) {
|
||||
let _ = self.mixer_cmd_tx.send(MixerCommand::Shutdown).await;
|
||||
if let Some(handle) = self.mixer_task.take() {
|
||||
handle.abort();
|
||||
}
|
||||
}
|
||||
|
||||
/// Produce a JSON status snapshot for the dashboard.
|
||||
pub fn to_status_json(&self) -> serde_json::Value {
|
||||
let legs: Vec<serde_json::Value> = self
|
||||
.legs
|
||||
.values()
|
||||
.filter(|l| l.state != LegState::Terminated)
|
||||
.map(|l| {
|
||||
let metadata: serde_json::Value = if l.metadata.is_empty() {
|
||||
serde_json::json!({})
|
||||
} else {
|
||||
serde_json::Value::Object(
|
||||
l.metadata.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
|
||||
)
|
||||
};
|
||||
serde_json::json!({
|
||||
"id": l.id,
|
||||
"type": l.kind.as_str(),
|
||||
"state": l.state.as_str(),
|
||||
"codec": sip_proto::helpers::codec_name(l.codec_pt),
|
||||
"rtpPort": l.rtp_port,
|
||||
"remoteMedia": l.remote_media.map(|a| format!("{}:{}", a.ip(), a.port())),
|
||||
"metadata": metadata,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
serde_json::json!({
|
||||
"id": self.id,
|
||||
"state": self.state.as_str(),
|
||||
@@ -93,11 +245,8 @@ impl PassthroughCall {
|
||||
"callerNumber": self.caller_number,
|
||||
"calleeNumber": self.callee_number,
|
||||
"providerUsed": self.provider_id,
|
||||
"createdAt": self.created_at.elapsed().as_millis(),
|
||||
"duration": self.duration_secs(),
|
||||
"rtpPort": self.rtp_port,
|
||||
"pktFromDevice": self.pkt_from_device,
|
||||
"pktFromProvider": self.pkt_from_provider,
|
||||
"legs": legs,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -14,8 +14,18 @@ pub struct Endpoint {
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
/// Resolve to a SocketAddr. Handles both IP addresses and hostnames.
|
||||
pub fn to_socket_addr(&self) -> Option<SocketAddr> {
|
||||
format!("{}:{}", self.address, self.port).parse().ok()
|
||||
// Try direct parse first (IP address).
|
||||
if let Ok(addr) = format!("{}:{}", self.address, self.port).parse() {
|
||||
return Some(addr);
|
||||
}
|
||||
// DNS resolution for hostnames.
|
||||
use std::net::ToSocketAddrs;
|
||||
format!("{}:{}", self.address, self.port)
|
||||
.to_socket_addrs()
|
||||
.ok()
|
||||
.and_then(|mut addrs| addrs.next())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
82
rust/crates/proxy-engine/src/leg_io.rs
Normal file
82
rust/crates/proxy-engine/src/leg_io.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
//! Leg I/O task spawners.
|
||||
//!
|
||||
//! Each SIP leg gets two tasks:
|
||||
//! - Inbound: recv_from on RTP socket → strip header → send RtpPacket to mixer channel
|
||||
//! - Outbound: recv encoded RTP from mixer channel → send_to remote media endpoint
|
||||
//!
|
||||
//! WebRTC leg I/O is handled inside webrtc_engine.rs (on_track + track.write).
|
||||
|
||||
use crate::mixer::RtpPacket;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::UdpSocket;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
/// Channel pair for connecting a leg to the mixer.
|
||||
pub struct LegChannels {
|
||||
/// Mixer receives decoded packets from this leg.
|
||||
pub inbound_tx: mpsc::Sender<RtpPacket>,
|
||||
pub inbound_rx: mpsc::Receiver<RtpPacket>,
|
||||
/// Mixer sends encoded RTP to this leg.
|
||||
pub outbound_tx: mpsc::Sender<Vec<u8>>,
|
||||
pub outbound_rx: mpsc::Receiver<Vec<u8>>,
|
||||
}
|
||||
|
||||
/// Create a channel pair for a leg.
|
||||
pub fn create_leg_channels() -> LegChannels {
|
||||
let (inbound_tx, inbound_rx) = mpsc::channel::<RtpPacket>(64);
|
||||
let (outbound_tx, outbound_rx) = mpsc::channel::<Vec<u8>>(8);
|
||||
LegChannels {
|
||||
inbound_tx,
|
||||
inbound_rx,
|
||||
outbound_tx,
|
||||
outbound_rx,
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn the inbound I/O task for a SIP leg.
|
||||
/// Reads RTP from the socket, strips the 12-byte header, sends payload to the mixer.
|
||||
/// Returns the JoinHandle (exits when the inbound_tx channel is dropped).
|
||||
pub fn spawn_sip_inbound(
|
||||
rtp_socket: Arc<UdpSocket>,
|
||||
inbound_tx: mpsc::Sender<RtpPacket>,
|
||||
) -> tokio::task::JoinHandle<()> {
|
||||
tokio::spawn(async move {
|
||||
let mut buf = vec![0u8; 1500];
|
||||
loop {
|
||||
match rtp_socket.recv_from(&mut buf).await {
|
||||
Ok((n, _from)) => {
|
||||
if n < 12 {
|
||||
continue; // Too small for RTP header.
|
||||
}
|
||||
let pt = buf[1] & 0x7F;
|
||||
let marker = (buf[1] & 0x80) != 0;
|
||||
let timestamp = u32::from_be_bytes([buf[4], buf[5], buf[6], buf[7]]);
|
||||
let payload = buf[12..n].to_vec();
|
||||
if payload.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if inbound_tx.send(RtpPacket { payload, payload_type: pt, marker, timestamp }).await.is_err() {
|
||||
break; // Channel closed — leg removed.
|
||||
}
|
||||
}
|
||||
Err(_) => break, // Socket error.
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Spawn the outbound I/O task for a SIP leg.
|
||||
/// Reads encoded RTP packets from the mixer and sends them to the remote media endpoint.
|
||||
/// Returns the JoinHandle (exits when the outbound_rx channel is closed).
|
||||
pub fn spawn_sip_outbound(
|
||||
rtp_socket: Arc<UdpSocket>,
|
||||
remote_media: SocketAddr,
|
||||
mut outbound_rx: mpsc::Receiver<Vec<u8>>,
|
||||
) -> tokio::task::JoinHandle<()> {
|
||||
tokio::spawn(async move {
|
||||
while let Some(rtp_data) = outbound_rx.recv().await {
|
||||
let _ = rtp_socket.send_to(&rtp_data, remote_media).await;
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -6,15 +6,23 @@
|
||||
///
|
||||
/// No raw SIP ever touches TypeScript.
|
||||
|
||||
mod audio_player;
|
||||
mod call;
|
||||
mod call_manager;
|
||||
mod config;
|
||||
mod dtmf;
|
||||
mod ipc;
|
||||
mod leg_io;
|
||||
mod mixer;
|
||||
mod provider;
|
||||
mod recorder;
|
||||
mod registrar;
|
||||
mod rtp;
|
||||
mod sip_leg;
|
||||
mod sip_transport;
|
||||
mod tool_leg;
|
||||
mod voicemail;
|
||||
mod webrtc_engine;
|
||||
|
||||
use crate::call_manager::CallManager;
|
||||
use crate::config::AppConfig;
|
||||
@@ -23,6 +31,7 @@ use crate::provider::ProviderManager;
|
||||
use crate::registrar::Registrar;
|
||||
use crate::rtp::RtpPortPool;
|
||||
use crate::sip_transport::SipTransport;
|
||||
use crate::webrtc_engine::WebRtcEngine;
|
||||
use sip_proto::message::SipMessage;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
@@ -30,7 +39,9 @@ use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::UdpSocket;
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
|
||||
/// Shared mutable state for the proxy engine.
|
||||
/// Shared mutable state for the proxy engine (SIP side).
|
||||
/// WebRTC is intentionally kept in a separate lock to avoid contention
|
||||
/// between SIP packet handlers and WebRTC command handlers.
|
||||
struct ProxyEngine {
|
||||
config: Option<AppConfig>,
|
||||
transport: Option<SipTransport>,
|
||||
@@ -76,9 +87,12 @@ async fn main() {
|
||||
// Emit ready event.
|
||||
emit_event(&out_tx, "ready", serde_json::json!({}));
|
||||
|
||||
// Shared engine state.
|
||||
// Shared engine state (SIP side).
|
||||
let engine = Arc::new(Mutex::new(ProxyEngine::new(out_tx.clone())));
|
||||
|
||||
// WebRTC engine — separate lock to avoid deadlock with SIP handlers.
|
||||
let webrtc = Arc::new(Mutex::new(WebRtcEngine::new(out_tx.clone())));
|
||||
|
||||
// Read commands from stdin.
|
||||
let stdin = tokio::io::stdin();
|
||||
let reader = BufReader::new(stdin);
|
||||
@@ -98,20 +112,44 @@ async fn main() {
|
||||
};
|
||||
|
||||
let engine = engine.clone();
|
||||
let webrtc = webrtc.clone();
|
||||
let out_tx = out_tx.clone();
|
||||
|
||||
// Handle commands — some are async, so we spawn.
|
||||
tokio::spawn(async move {
|
||||
handle_command(engine, &out_tx, cmd).await;
|
||||
handle_command(engine, webrtc, &out_tx, cmd).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_command(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd: Command) {
|
||||
async fn handle_command(
|
||||
engine: Arc<Mutex<ProxyEngine>>,
|
||||
webrtc: Arc<Mutex<WebRtcEngine>>,
|
||||
out_tx: &OutTx,
|
||||
cmd: Command,
|
||||
) {
|
||||
match cmd.method.as_str() {
|
||||
// SIP commands — lock engine only.
|
||||
"configure" => handle_configure(engine, out_tx, &cmd).await,
|
||||
"hangup" => handle_hangup(engine, out_tx, &cmd).await,
|
||||
"make_call" => handle_make_call(engine, out_tx, &cmd).await,
|
||||
"get_status" => handle_get_status(engine, out_tx, &cmd).await,
|
||||
"add_leg" => handle_add_leg(engine, out_tx, &cmd).await,
|
||||
"remove_leg" => handle_remove_leg(engine, out_tx, &cmd).await,
|
||||
// WebRTC commands — lock webrtc only (no engine contention).
|
||||
"webrtc_offer" => handle_webrtc_offer(webrtc, out_tx, &cmd).await,
|
||||
"webrtc_ice" => handle_webrtc_ice(webrtc, out_tx, &cmd).await,
|
||||
"webrtc_close" => handle_webrtc_close(webrtc, out_tx, &cmd).await,
|
||||
// webrtc_link needs both: engine (for mixer channels) and webrtc (for session).
|
||||
"webrtc_link" => handle_webrtc_link(engine, webrtc, out_tx, &cmd).await,
|
||||
"add_device_leg" => handle_add_device_leg(engine, out_tx, &cmd).await,
|
||||
"transfer_leg" => handle_transfer_leg(engine, out_tx, &cmd).await,
|
||||
"replace_leg" => handle_replace_leg(engine, out_tx, &cmd).await,
|
||||
// Leg interaction and tool leg commands.
|
||||
"start_interaction" => handle_start_interaction(engine, out_tx, &cmd).await,
|
||||
"add_tool_leg" => handle_add_tool_leg(engine, out_tx, &cmd).await,
|
||||
"remove_tool_leg" => handle_remove_tool_leg(engine, out_tx, &cmd).await,
|
||||
"set_leg_metadata" => handle_set_leg_metadata(engine, out_tx, &cmd).await,
|
||||
_ => respond_err(out_tx, &cmd.id, &format!("unknown command: {}", cmd.method)),
|
||||
}
|
||||
}
|
||||
@@ -234,14 +272,11 @@ async fn handle_sip_packet(
|
||||
}
|
||||
|
||||
// 3. Route to existing call by SIP Call-ID.
|
||||
// Check if this Call-ID belongs to an active call (avoids borrow conflict).
|
||||
if eng.call_mgr.has_call(msg.call_id()) {
|
||||
let config_ref = eng.config.as_ref().unwrap().clone();
|
||||
// Temporarily take registrar to avoid overlapping borrows.
|
||||
let registrar_dummy = Registrar::new(eng.out_tx.clone());
|
||||
if eng
|
||||
.call_mgr
|
||||
.route_sip_message(&msg, from_addr, socket, &config_ref, ®istrar_dummy)
|
||||
.route_sip_message(&msg, from_addr, socket, &config_ref)
|
||||
.await
|
||||
{
|
||||
return;
|
||||
@@ -347,11 +382,14 @@ async fn handle_sip_packet(
|
||||
);
|
||||
|
||||
if let Some(route) = route_result {
|
||||
let public_ip = if let Some(ps_arc) = eng.provider_mgr.find_by_address(&from_addr).await {
|
||||
// Look up provider state by config ID (not by device address).
|
||||
let (public_ip, registered_aor) = if let Some(ps_arc) =
|
||||
eng.provider_mgr.find_by_provider_id(&route.provider.id).await
|
||||
{
|
||||
let ps = ps_arc.lock().await;
|
||||
ps.public_ip.clone()
|
||||
(ps.public_ip.clone(), ps.registered_aor.clone())
|
||||
} else {
|
||||
None
|
||||
(None, format!("sip:{}@{}", route.provider.username, route.provider.domain))
|
||||
};
|
||||
|
||||
let ProxyEngine {
|
||||
@@ -361,7 +399,7 @@ async fn handle_sip_packet(
|
||||
} = *eng;
|
||||
let rtp_pool = rtp_pool.as_mut().unwrap();
|
||||
let call_id = call_mgr
|
||||
.create_outbound_passthrough(
|
||||
.create_device_outbound_call(
|
||||
&msg,
|
||||
from_addr,
|
||||
&route.provider,
|
||||
@@ -369,6 +407,7 @@ async fn handle_sip_packet(
|
||||
rtp_pool,
|
||||
socket,
|
||||
public_ip.as_deref(),
|
||||
®istered_aor,
|
||||
)
|
||||
.await;
|
||||
|
||||
@@ -413,6 +452,78 @@ async fn handle_get_status(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd:
|
||||
respond_ok(out_tx, &cmd.id, serde_json::json!({ "calls": calls }));
|
||||
}
|
||||
|
||||
/// Handle `make_call` — initiate an outbound call to a number via a provider.
|
||||
async fn handle_make_call(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let number = match cmd.params.get("number").and_then(|v| v.as_str()) {
|
||||
Some(n) => n.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing number"); return; }
|
||||
};
|
||||
let provider_id = cmd.params.get("provider_id").and_then(|v| v.as_str());
|
||||
|
||||
let mut eng = engine.lock().await;
|
||||
let config_ref = match &eng.config {
|
||||
Some(c) => c.clone(),
|
||||
None => { respond_err(out_tx, &cmd.id, "not configured"); return; }
|
||||
};
|
||||
|
||||
// Resolve provider.
|
||||
let provider_config = if let Some(pid) = provider_id {
|
||||
config_ref.providers.iter().find(|p| p.id == pid).cloned()
|
||||
} else {
|
||||
// Use route resolution or first provider.
|
||||
let route = config_ref.resolve_outbound_route(&number, None, &|_| true);
|
||||
route.map(|r| r.provider)
|
||||
};
|
||||
|
||||
let provider_config = match provider_config {
|
||||
Some(p) => p,
|
||||
None => { respond_err(out_tx, &cmd.id, "no provider available"); return; }
|
||||
};
|
||||
|
||||
// Get public IP and registered AOR from provider state.
|
||||
let (public_ip, registered_aor) = if let Some(ps_arc) = eng.provider_mgr.find_by_address(
|
||||
&provider_config.outbound_proxy.to_socket_addr().unwrap_or_else(|| "0.0.0.0:0".parse().unwrap())
|
||||
).await {
|
||||
let ps = ps_arc.lock().await;
|
||||
(ps.public_ip.clone(), ps.registered_aor.clone())
|
||||
} else {
|
||||
// Fallback — construct AOR from config.
|
||||
(None, format!("sip:{}@{}", provider_config.username, provider_config.domain))
|
||||
};
|
||||
|
||||
let socket = match &eng.transport {
|
||||
Some(t) => t.socket(),
|
||||
None => { respond_err(out_tx, &cmd.id, "not initialized"); return; }
|
||||
};
|
||||
|
||||
let ProxyEngine { ref mut call_mgr, ref mut rtp_pool, .. } = *eng;
|
||||
let rtp_pool = rtp_pool.as_mut().unwrap();
|
||||
|
||||
let call_id = call_mgr.make_outbound_call(
|
||||
&number,
|
||||
&provider_config,
|
||||
&config_ref,
|
||||
rtp_pool,
|
||||
&socket,
|
||||
public_ip.as_deref(),
|
||||
®istered_aor,
|
||||
).await;
|
||||
|
||||
match call_id {
|
||||
Some(id) => {
|
||||
emit_event(out_tx, "outbound_call_started", serde_json::json!({
|
||||
"call_id": id,
|
||||
"number": number,
|
||||
"provider_id": provider_config.id,
|
||||
}));
|
||||
respond_ok(out_tx, &cmd.id, serde_json::json!({ "call_id": id }));
|
||||
}
|
||||
None => {
|
||||
respond_err(out_tx, &cmd.id, "call origination failed — provider not registered or no ports available");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle the `hangup` command.
|
||||
async fn handle_hangup(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
@@ -438,3 +549,672 @@ async fn handle_hangup(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd: &Co
|
||||
respond_err(out_tx, &cmd.id, &format!("call {call_id} not found"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle `webrtc_offer` — browser sends SDP offer, we create PeerConnection and return answer.
|
||||
/// Uses only the WebRTC lock — no contention with SIP handlers.
|
||||
async fn handle_webrtc_offer(webrtc: Arc<Mutex<WebRtcEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let session_id = match cmd.params.get("session_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing session_id"); return; }
|
||||
};
|
||||
let offer_sdp = match cmd.params.get("sdp").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing sdp"); return; }
|
||||
};
|
||||
|
||||
let mut wrtc = webrtc.lock().await;
|
||||
match wrtc.handle_offer(&session_id, &offer_sdp).await {
|
||||
Ok(answer_sdp) => {
|
||||
respond_ok(out_tx, &cmd.id, serde_json::json!({
|
||||
"session_id": session_id,
|
||||
"sdp": answer_sdp,
|
||||
}));
|
||||
}
|
||||
Err(e) => respond_err(out_tx, &cmd.id, &e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle `webrtc_ice` — forward ICE candidate from browser to Rust PeerConnection.
|
||||
/// Uses only the WebRTC lock.
|
||||
async fn handle_webrtc_ice(webrtc: Arc<Mutex<WebRtcEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let session_id = match cmd.params.get("session_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing session_id"); return; }
|
||||
};
|
||||
let candidate = cmd.params.get("candidate").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let sdp_mid = cmd.params.get("sdp_mid").and_then(|v| v.as_str());
|
||||
let sdp_mline_index = cmd.params.get("sdp_mline_index").and_then(|v| v.as_u64()).map(|v| v as u16);
|
||||
|
||||
let wrtc = webrtc.lock().await;
|
||||
match wrtc.add_ice_candidate(&session_id, candidate, sdp_mid, sdp_mline_index).await {
|
||||
Ok(()) => respond_ok(out_tx, &cmd.id, serde_json::json!({})),
|
||||
Err(e) => respond_err(out_tx, &cmd.id, &e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle `webrtc_link` — link a WebRTC session to a call's mixer for audio bridging.
|
||||
/// Creates channels, adds WebRTC leg to the call, wires the WebRTC engine.
|
||||
/// Locks are never held simultaneously — no deadlock possible.
|
||||
async fn handle_webrtc_link(
|
||||
engine: Arc<Mutex<ProxyEngine>>,
|
||||
webrtc: Arc<Mutex<WebRtcEngine>>,
|
||||
out_tx: &OutTx,
|
||||
cmd: &Command,
|
||||
) {
|
||||
let session_id = match cmd.params.get("session_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing session_id"); return; }
|
||||
};
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing call_id"); return; }
|
||||
};
|
||||
|
||||
// Create channels for the WebRTC leg.
|
||||
let channels = crate::leg_io::create_leg_channels();
|
||||
|
||||
// Briefly lock engine to add the WebRTC leg to the call's mixer.
|
||||
{
|
||||
let eng = engine.lock().await;
|
||||
let call = match eng.call_mgr.calls.get(&call_id) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
respond_err(out_tx, &cmd.id, &format!("call {call_id} not found"));
|
||||
return;
|
||||
}
|
||||
};
|
||||
// Add to mixer via channel.
|
||||
call.add_leg_to_mixer(
|
||||
&session_id,
|
||||
codec_lib::PT_OPUS,
|
||||
channels.inbound_rx,
|
||||
channels.outbound_tx,
|
||||
)
|
||||
.await;
|
||||
} // engine lock released
|
||||
|
||||
// Lock webrtc to wire the channels.
|
||||
let mut wrtc = webrtc.lock().await;
|
||||
if wrtc
|
||||
.link_to_mixer(&session_id, &call_id, channels.inbound_tx, channels.outbound_rx)
|
||||
.await
|
||||
{
|
||||
// Also store the WebRTC leg info in the call.
|
||||
drop(wrtc); // Release webrtc lock before re-acquiring engine.
|
||||
{
|
||||
let mut eng = engine.lock().await;
|
||||
if let Some(call) = eng.call_mgr.calls.get_mut(&call_id) {
|
||||
call.legs.insert(
|
||||
session_id.clone(),
|
||||
crate::call::LegInfo {
|
||||
id: session_id.clone(),
|
||||
kind: crate::call::LegKind::WebRtc,
|
||||
state: crate::call::LegState::Connected,
|
||||
codec_pt: codec_lib::PT_OPUS,
|
||||
sip_leg: None,
|
||||
sip_call_id: None,
|
||||
webrtc_session_id: Some(session_id.clone()),
|
||||
rtp_socket: None,
|
||||
rtp_port: 0,
|
||||
remote_media: None,
|
||||
signaling_addr: None,
|
||||
metadata: std::collections::HashMap::new(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
emit_event(out_tx, "leg_added", serde_json::json!({
|
||||
"call_id": call_id,
|
||||
"leg_id": session_id,
|
||||
"kind": "webrtc",
|
||||
"state": "connected",
|
||||
}));
|
||||
|
||||
respond_ok(out_tx, &cmd.id, serde_json::json!({
|
||||
"session_id": session_id,
|
||||
"call_id": call_id,
|
||||
"bridged": true,
|
||||
}));
|
||||
} else {
|
||||
respond_err(out_tx, &cmd.id, &format!("session {session_id} not found"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle `add_leg` — add a new SIP leg to an existing call.
|
||||
async fn handle_add_leg(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing call_id"); return; }
|
||||
};
|
||||
let number = match cmd.params.get("number").and_then(|v| v.as_str()) {
|
||||
Some(n) => n.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing number"); return; }
|
||||
};
|
||||
let provider_id = cmd.params.get("provider_id").and_then(|v| v.as_str());
|
||||
|
||||
let mut eng = engine.lock().await;
|
||||
let config_ref = match &eng.config {
|
||||
Some(c) => c.clone(),
|
||||
None => { respond_err(out_tx, &cmd.id, "not configured"); return; }
|
||||
};
|
||||
|
||||
// Resolve provider.
|
||||
let provider_config = if let Some(pid) = provider_id {
|
||||
config_ref.providers.iter().find(|p| p.id == pid).cloned()
|
||||
} else {
|
||||
config_ref.resolve_outbound_route(&number, None, &|_| true).map(|r| r.provider)
|
||||
};
|
||||
|
||||
let provider_config = match provider_config {
|
||||
Some(p) => p,
|
||||
None => { respond_err(out_tx, &cmd.id, "no provider available"); return; }
|
||||
};
|
||||
|
||||
// Get registered AOR.
|
||||
let registered_aor = if let Some(ps_arc) = eng.provider_mgr.find_by_address(
|
||||
&provider_config.outbound_proxy.to_socket_addr().unwrap_or_else(|| "0.0.0.0:0".parse().unwrap())
|
||||
).await {
|
||||
let ps = ps_arc.lock().await;
|
||||
ps.registered_aor.clone()
|
||||
} else {
|
||||
format!("sip:{}@{}", provider_config.username, provider_config.domain)
|
||||
};
|
||||
|
||||
let public_ip = if let Some(ps_arc) = eng.provider_mgr.find_by_address(
|
||||
&provider_config.outbound_proxy.to_socket_addr().unwrap_or_else(|| "0.0.0.0:0".parse().unwrap())
|
||||
).await {
|
||||
let ps = ps_arc.lock().await;
|
||||
ps.public_ip.clone()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let socket = match &eng.transport {
|
||||
Some(t) => t.socket(),
|
||||
None => { respond_err(out_tx, &cmd.id, "not initialized"); return; }
|
||||
};
|
||||
|
||||
let ProxyEngine { ref mut call_mgr, ref mut rtp_pool, .. } = *eng;
|
||||
let rtp_pool = rtp_pool.as_mut().unwrap();
|
||||
|
||||
let leg_id = call_mgr.add_external_leg(
|
||||
&call_id, &number, &provider_config, &config_ref,
|
||||
rtp_pool, &socket, public_ip.as_deref(), ®istered_aor,
|
||||
).await;
|
||||
|
||||
match leg_id {
|
||||
Some(lid) => respond_ok(out_tx, &cmd.id, serde_json::json!({ "leg_id": lid })),
|
||||
None => respond_err(out_tx, &cmd.id, "failed to add leg"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle `add_device_leg` — add a local SIP device to an existing call.
|
||||
async fn handle_add_device_leg(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing call_id"); return; }
|
||||
};
|
||||
let device_id = match cmd.params.get("device_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing device_id"); return; }
|
||||
};
|
||||
|
||||
let mut eng = engine.lock().await;
|
||||
let config_ref = match &eng.config {
|
||||
Some(c) => c.clone(),
|
||||
None => { respond_err(out_tx, &cmd.id, "not configured"); return; }
|
||||
};
|
||||
let socket = match &eng.transport {
|
||||
Some(t) => t.socket(),
|
||||
None => { respond_err(out_tx, &cmd.id, "not initialized"); return; }
|
||||
};
|
||||
|
||||
let ProxyEngine { ref registrar, ref mut call_mgr, ref mut rtp_pool, .. } = *eng;
|
||||
let rtp_pool = rtp_pool.as_mut().unwrap();
|
||||
|
||||
let leg_id = call_mgr.add_device_leg(
|
||||
&call_id, &device_id, registrar, &config_ref, rtp_pool, &socket,
|
||||
).await;
|
||||
|
||||
match leg_id {
|
||||
Some(lid) => respond_ok(out_tx, &cmd.id, serde_json::json!({ "leg_id": lid })),
|
||||
None => respond_err(out_tx, &cmd.id, "failed to add device leg — device not registered or call not found"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle `transfer_leg` — move a leg from one call to another.
|
||||
async fn handle_transfer_leg(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let source_call_id = match cmd.params.get("source_call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing source_call_id"); return; }
|
||||
};
|
||||
let leg_id = match cmd.params.get("leg_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing leg_id"); return; }
|
||||
};
|
||||
let target_call_id = match cmd.params.get("target_call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing target_call_id"); return; }
|
||||
};
|
||||
|
||||
let mut eng = engine.lock().await;
|
||||
if eng.call_mgr.transfer_leg(&source_call_id, &leg_id, &target_call_id).await {
|
||||
respond_ok(out_tx, &cmd.id, serde_json::json!({}));
|
||||
} else {
|
||||
respond_err(out_tx, &cmd.id, "transfer failed — call or leg not found");
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle `replace_leg` — terminate a leg and dial a replacement into the same call.
|
||||
async fn handle_replace_leg(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing call_id"); return; }
|
||||
};
|
||||
let old_leg_id = match cmd.params.get("old_leg_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing old_leg_id"); return; }
|
||||
};
|
||||
let number = match cmd.params.get("number").and_then(|v| v.as_str()) {
|
||||
Some(n) => n.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing number"); return; }
|
||||
};
|
||||
let provider_id = cmd.params.get("provider_id").and_then(|v| v.as_str());
|
||||
|
||||
let mut eng = engine.lock().await;
|
||||
let config_ref = match &eng.config {
|
||||
Some(c) => c.clone(),
|
||||
None => { respond_err(out_tx, &cmd.id, "not configured"); return; }
|
||||
};
|
||||
let socket = match &eng.transport {
|
||||
Some(t) => t.socket(),
|
||||
None => { respond_err(out_tx, &cmd.id, "not initialized"); return; }
|
||||
};
|
||||
|
||||
// Resolve provider.
|
||||
let provider_config = if let Some(pid) = provider_id {
|
||||
config_ref.providers.iter().find(|p| p.id == pid).cloned()
|
||||
} else {
|
||||
config_ref.resolve_outbound_route(&number, None, &|_| true).map(|r| r.provider)
|
||||
};
|
||||
let provider_config = match provider_config {
|
||||
Some(p) => p,
|
||||
None => { respond_err(out_tx, &cmd.id, "no provider available"); return; }
|
||||
};
|
||||
|
||||
let (public_ip, registered_aor) = if let Some(ps_arc) = eng.provider_mgr.find_by_provider_id(&provider_config.id).await {
|
||||
let ps = ps_arc.lock().await;
|
||||
(ps.public_ip.clone(), ps.registered_aor.clone())
|
||||
} else {
|
||||
(None, format!("sip:{}@{}", provider_config.username, provider_config.domain))
|
||||
};
|
||||
|
||||
let ProxyEngine { ref mut call_mgr, ref mut rtp_pool, .. } = *eng;
|
||||
let rtp_pool = rtp_pool.as_mut().unwrap();
|
||||
|
||||
let new_leg_id = call_mgr.replace_leg(
|
||||
&call_id, &old_leg_id, &number, &provider_config, &config_ref,
|
||||
rtp_pool, &socket, public_ip.as_deref(), ®istered_aor,
|
||||
).await;
|
||||
|
||||
match new_leg_id {
|
||||
Some(lid) => respond_ok(out_tx, &cmd.id, serde_json::json!({ "new_leg_id": lid })),
|
||||
None => respond_err(out_tx, &cmd.id, "replace failed — call ended or dial failed"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle `remove_leg` — remove a leg from a call.
|
||||
async fn handle_remove_leg(engine: Arc<Mutex<ProxyEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing call_id"); return; }
|
||||
};
|
||||
let leg_id = match cmd.params.get("leg_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing leg_id"); return; }
|
||||
};
|
||||
|
||||
let mut eng = engine.lock().await;
|
||||
let socket = match &eng.transport {
|
||||
Some(t) => t.socket(),
|
||||
None => { respond_err(out_tx, &cmd.id, "not initialized"); return; }
|
||||
};
|
||||
|
||||
if eng.call_mgr.remove_leg(&call_id, &leg_id, &socket).await {
|
||||
respond_ok(out_tx, &cmd.id, serde_json::json!({}));
|
||||
} else {
|
||||
respond_err(out_tx, &cmd.id, &format!("call/leg not found"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle `webrtc_close` — close a WebRTC session.
|
||||
/// Uses only the WebRTC lock.
|
||||
async fn handle_webrtc_close(webrtc: Arc<Mutex<WebRtcEngine>>, out_tx: &OutTx, cmd: &Command) {
|
||||
let session_id = match cmd.params.get("session_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing session_id"); return; }
|
||||
};
|
||||
|
||||
let mut wrtc = webrtc.lock().await;
|
||||
match wrtc.close_session(&session_id).await {
|
||||
Ok(()) => respond_ok(out_tx, &cmd.id, serde_json::json!({})),
|
||||
Err(e) => respond_err(out_tx, &cmd.id, &e),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Leg interaction & tool leg commands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Handle `start_interaction` — isolate a leg, play a prompt, collect DTMF.
|
||||
/// This command blocks until the interaction completes (digit, timeout, or cancel).
|
||||
async fn handle_start_interaction(
|
||||
engine: Arc<Mutex<ProxyEngine>>,
|
||||
out_tx: &OutTx,
|
||||
cmd: &Command,
|
||||
) {
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing call_id"); return; }
|
||||
};
|
||||
let leg_id = match cmd.params.get("leg_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing leg_id"); return; }
|
||||
};
|
||||
let prompt_wav = match cmd.params.get("prompt_wav").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing prompt_wav"); return; }
|
||||
};
|
||||
let expected_digits: Vec<char> = cmd
|
||||
.params
|
||||
.get("expected_digits")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("12")
|
||||
.chars()
|
||||
.collect();
|
||||
let timeout_ms = cmd
|
||||
.params
|
||||
.get("timeout_ms")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(15000) as u32;
|
||||
|
||||
// Load prompt audio from WAV file.
|
||||
let prompt_frames = match crate::audio_player::load_prompt_pcm_frames(&prompt_wav) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
respond_err(out_tx, &cmd.id, &format!("prompt load failed: {e}"));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Create oneshot channel for the result.
|
||||
let (result_tx, result_rx) = tokio::sync::oneshot::channel();
|
||||
|
||||
// Send StartInteraction to the mixer.
|
||||
{
|
||||
let eng = engine.lock().await;
|
||||
let call = match eng.call_mgr.calls.get(&call_id) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
respond_err(out_tx, &cmd.id, &format!("call {call_id} not found"));
|
||||
return;
|
||||
}
|
||||
};
|
||||
let _ = call
|
||||
.mixer_cmd_tx
|
||||
.send(crate::mixer::MixerCommand::StartInteraction {
|
||||
leg_id: leg_id.clone(),
|
||||
prompt_pcm_frames: prompt_frames,
|
||||
expected_digits: expected_digits.clone(),
|
||||
timeout_ms,
|
||||
result_tx,
|
||||
})
|
||||
.await;
|
||||
} // engine lock released — we block on the oneshot, not the lock.
|
||||
|
||||
// Await the interaction result (blocks this task until complete).
|
||||
let safety_timeout = tokio::time::Duration::from_millis(timeout_ms as u64 + 30000);
|
||||
let result = match tokio::time::timeout(safety_timeout, result_rx).await {
|
||||
Ok(Ok(r)) => r,
|
||||
Ok(Err(_)) => crate::mixer::InteractionResult::Cancelled, // oneshot dropped
|
||||
Err(_) => crate::mixer::InteractionResult::Timeout, // safety timeout
|
||||
};
|
||||
|
||||
// Store consent result in leg metadata.
|
||||
let (result_str, digit_str) = match &result {
|
||||
crate::mixer::InteractionResult::Digit(d) => ("digit", Some(d.to_string())),
|
||||
crate::mixer::InteractionResult::Timeout => ("timeout", None),
|
||||
crate::mixer::InteractionResult::Cancelled => ("cancelled", None),
|
||||
};
|
||||
|
||||
{
|
||||
let mut eng = engine.lock().await;
|
||||
if let Some(call) = eng.call_mgr.calls.get_mut(&call_id) {
|
||||
if let Some(leg) = call.legs.get_mut(&leg_id) {
|
||||
leg.metadata.insert(
|
||||
"last_interaction_result".to_string(),
|
||||
serde_json::json!(result_str),
|
||||
);
|
||||
if let Some(ref d) = digit_str {
|
||||
leg.metadata.insert(
|
||||
"last_interaction_digit".to_string(),
|
||||
serde_json::json!(d),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut resp = serde_json::json!({ "result": result_str });
|
||||
if let Some(d) = digit_str {
|
||||
resp["digit"] = serde_json::json!(d);
|
||||
}
|
||||
respond_ok(out_tx, &cmd.id, resp);
|
||||
}
|
||||
|
||||
/// Handle `add_tool_leg` — add a recording or transcription tool leg to a call.
|
||||
async fn handle_add_tool_leg(
|
||||
engine: Arc<Mutex<ProxyEngine>>,
|
||||
out_tx: &OutTx,
|
||||
cmd: &Command,
|
||||
) {
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing call_id"); return; }
|
||||
};
|
||||
let tool_type_str = match cmd.params.get("tool_type").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing tool_type"); return; }
|
||||
};
|
||||
|
||||
let tool_type = match tool_type_str.as_str() {
|
||||
"recording" => crate::mixer::ToolType::Recording,
|
||||
"transcription" => crate::mixer::ToolType::Transcription,
|
||||
other => {
|
||||
respond_err(out_tx, &cmd.id, &format!("unknown tool_type: {other}"));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let tool_leg_id = format!("{call_id}-tool-{}", rand::random::<u32>());
|
||||
|
||||
// Spawn the appropriate background task.
|
||||
let (audio_tx, _task_handle) = match tool_type {
|
||||
crate::mixer::ToolType::Recording => {
|
||||
let base_dir = cmd
|
||||
.params
|
||||
.get("config")
|
||||
.and_then(|c| c.get("base_dir"))
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or(".nogit/recordings")
|
||||
.to_string();
|
||||
crate::tool_leg::spawn_recording_tool(
|
||||
tool_leg_id.clone(),
|
||||
call_id.clone(),
|
||||
base_dir,
|
||||
out_tx.clone(),
|
||||
)
|
||||
}
|
||||
crate::mixer::ToolType::Transcription => {
|
||||
crate::tool_leg::spawn_transcription_tool(
|
||||
tool_leg_id.clone(),
|
||||
call_id.clone(),
|
||||
out_tx.clone(),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Send AddToolLeg to the mixer and register in call.
|
||||
{
|
||||
let mut eng = engine.lock().await;
|
||||
let call = match eng.call_mgr.calls.get_mut(&call_id) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
respond_err(out_tx, &cmd.id, &format!("call {call_id} not found"));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let _ = call
|
||||
.mixer_cmd_tx
|
||||
.send(crate::mixer::MixerCommand::AddToolLeg {
|
||||
leg_id: tool_leg_id.clone(),
|
||||
tool_type,
|
||||
audio_tx,
|
||||
})
|
||||
.await;
|
||||
|
||||
// Register tool leg in the call's leg map.
|
||||
let mut metadata = std::collections::HashMap::new();
|
||||
metadata.insert(
|
||||
"tool_type".to_string(),
|
||||
serde_json::json!(tool_type_str),
|
||||
);
|
||||
call.legs.insert(
|
||||
tool_leg_id.clone(),
|
||||
crate::call::LegInfo {
|
||||
id: tool_leg_id.clone(),
|
||||
kind: crate::call::LegKind::Tool,
|
||||
state: crate::call::LegState::Connected,
|
||||
codec_pt: 0,
|
||||
sip_leg: None,
|
||||
sip_call_id: None,
|
||||
webrtc_session_id: None,
|
||||
rtp_socket: None,
|
||||
rtp_port: 0,
|
||||
remote_media: None,
|
||||
signaling_addr: None,
|
||||
metadata,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
emit_event(
|
||||
out_tx,
|
||||
"leg_added",
|
||||
serde_json::json!({
|
||||
"call_id": call_id,
|
||||
"leg_id": tool_leg_id,
|
||||
"kind": "tool",
|
||||
"tool_type": tool_type_str,
|
||||
"state": "connected",
|
||||
}),
|
||||
);
|
||||
|
||||
respond_ok(
|
||||
out_tx,
|
||||
&cmd.id,
|
||||
serde_json::json!({ "tool_leg_id": tool_leg_id }),
|
||||
);
|
||||
}
|
||||
|
||||
/// Handle `remove_tool_leg` — remove a tool leg from a call.
|
||||
async fn handle_remove_tool_leg(
|
||||
engine: Arc<Mutex<ProxyEngine>>,
|
||||
out_tx: &OutTx,
|
||||
cmd: &Command,
|
||||
) {
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing call_id"); return; }
|
||||
};
|
||||
let tool_leg_id = match cmd.params.get("tool_leg_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing tool_leg_id"); return; }
|
||||
};
|
||||
|
||||
let mut eng = engine.lock().await;
|
||||
let call = match eng.call_mgr.calls.get_mut(&call_id) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
respond_err(out_tx, &cmd.id, &format!("call {call_id} not found"));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Remove from mixer (drops audio_tx → background task finalizes).
|
||||
let _ = call
|
||||
.mixer_cmd_tx
|
||||
.send(crate::mixer::MixerCommand::RemoveToolLeg {
|
||||
leg_id: tool_leg_id.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
// Remove from call's leg map.
|
||||
call.legs.remove(&tool_leg_id);
|
||||
|
||||
emit_event(
|
||||
out_tx,
|
||||
"leg_removed",
|
||||
serde_json::json!({
|
||||
"call_id": call_id,
|
||||
"leg_id": tool_leg_id,
|
||||
}),
|
||||
);
|
||||
|
||||
respond_ok(out_tx, &cmd.id, serde_json::json!({}));
|
||||
}
|
||||
|
||||
/// Handle `set_leg_metadata` — set a metadata key on a leg.
|
||||
async fn handle_set_leg_metadata(
|
||||
engine: Arc<Mutex<ProxyEngine>>,
|
||||
out_tx: &OutTx,
|
||||
cmd: &Command,
|
||||
) {
|
||||
let call_id = match cmd.params.get("call_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing call_id"); return; }
|
||||
};
|
||||
let leg_id = match cmd.params.get("leg_id").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing leg_id"); return; }
|
||||
};
|
||||
let key = match cmd.params.get("key").and_then(|v| v.as_str()) {
|
||||
Some(s) => s.to_string(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing key"); return; }
|
||||
};
|
||||
let value = match cmd.params.get("value") {
|
||||
Some(v) => v.clone(),
|
||||
None => { respond_err(out_tx, &cmd.id, "missing value"); return; }
|
||||
};
|
||||
|
||||
let mut eng = engine.lock().await;
|
||||
let call = match eng.call_mgr.calls.get_mut(&call_id) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
respond_err(out_tx, &cmd.id, &format!("call {call_id} not found"));
|
||||
return;
|
||||
}
|
||||
};
|
||||
let leg = match call.legs.get_mut(&leg_id) {
|
||||
Some(l) => l,
|
||||
None => {
|
||||
respond_err(out_tx, &cmd.id, &format!("leg {leg_id} not found"));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
leg.metadata.insert(key, value);
|
||||
respond_ok(out_tx, &cmd.id, serde_json::json!({}));
|
||||
}
|
||||
|
||||
578
rust/crates/proxy-engine/src/mixer.rs
Normal file
578
rust/crates/proxy-engine/src/mixer.rs
Normal file
@@ -0,0 +1,578 @@
|
||||
//! Audio mixer — mix-minus engine for multiparty calls.
|
||||
//!
|
||||
//! Each Call spawns one mixer task. Legs communicate with the mixer via
|
||||
//! tokio mpsc channels — no shared mutable state, no lock contention.
|
||||
//!
|
||||
//! The mixer runs a 20ms tick loop:
|
||||
//! 1. Drain inbound channels, decode to PCM, resample to 16kHz
|
||||
//! 2. Compute total mix (sum of all **participant** legs' PCM as i32)
|
||||
//! 3. For each participant leg: mix-minus = total - own, resample to leg codec rate, encode, send
|
||||
//! 4. For each isolated leg: play prompt frame or silence, check DTMF
|
||||
//! 5. For each tool leg: send per-source unmerged audio batch
|
||||
//! 6. Forward DTMF between participant legs only
|
||||
|
||||
use crate::ipc::{emit_event, OutTx};
|
||||
use crate::rtp::{build_rtp_header, rtp_clock_increment};
|
||||
use codec_lib::{codec_sample_rate, TranscodeState};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio::time::{self, Duration, MissedTickBehavior};
|
||||
|
||||
/// Mixing sample rate — 16kHz. G.722 is native, G.711 needs 2× upsample, Opus needs 3× downsample.
|
||||
const MIX_RATE: u32 = 16000;
|
||||
/// Samples per 20ms frame at the mixing rate.
|
||||
const MIX_FRAME_SIZE: usize = 320; // 16000 * 0.020
|
||||
|
||||
/// A raw RTP payload received from a leg (no RTP header).
|
||||
pub struct RtpPacket {
|
||||
pub payload: Vec<u8>,
|
||||
pub payload_type: u8,
|
||||
/// RTP marker bit (first packet of a DTMF event, etc.).
|
||||
pub marker: bool,
|
||||
/// RTP timestamp from the original packet header.
|
||||
pub timestamp: u32,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Leg roles
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// What role a leg currently plays in the mixer.
|
||||
enum LegRole {
|
||||
/// Normal participant: contributes to mix, receives mix-minus.
|
||||
Participant,
|
||||
/// Temporarily isolated for IVR/consent interaction.
|
||||
Isolated(IsolationState),
|
||||
}
|
||||
|
||||
struct IsolationState {
|
||||
/// PCM frames at MIX_RATE (320 samples each) queued for playback.
|
||||
prompt_frames: VecDeque<Vec<i16>>,
|
||||
/// Digits that complete the interaction (e.g., ['1', '2']).
|
||||
expected_digits: Vec<char>,
|
||||
/// Ticks remaining before timeout (decremented each tick after prompt ends).
|
||||
timeout_ticks_remaining: u32,
|
||||
/// Whether we've finished playing the prompt.
|
||||
prompt_done: bool,
|
||||
/// Channel to send the result back to the command handler.
|
||||
result_tx: Option<oneshot::Sender<InteractionResult>>,
|
||||
}
|
||||
|
||||
/// Result of a leg interaction (consent prompt, IVR, etc.).
|
||||
pub enum InteractionResult {
|
||||
/// The participant pressed one of the expected digits.
|
||||
Digit(char),
|
||||
/// No digit was received within the timeout.
|
||||
Timeout,
|
||||
/// The leg was removed or the call tore down before completion.
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tool legs
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Type of tool leg.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ToolType {
|
||||
Recording,
|
||||
Transcription,
|
||||
}
|
||||
|
||||
/// Per-source audio delivered to a tool leg each mixer tick.
|
||||
pub struct ToolAudioBatch {
|
||||
pub sources: Vec<ToolAudioSource>,
|
||||
}
|
||||
|
||||
/// One participant's 20ms audio frame.
|
||||
pub struct ToolAudioSource {
|
||||
pub leg_id: String,
|
||||
/// PCM at 16kHz, MIX_FRAME_SIZE (320) samples.
|
||||
pub pcm_16k: Vec<i16>,
|
||||
}
|
||||
|
||||
/// Internal storage for a tool leg inside the mixer.
|
||||
struct ToolLegSlot {
|
||||
#[allow(dead_code)]
|
||||
tool_type: ToolType,
|
||||
audio_tx: mpsc::Sender<ToolAudioBatch>,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Commands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Commands sent to the mixer task via a control channel.
|
||||
pub enum MixerCommand {
|
||||
/// Add a new participant leg to the mix.
|
||||
AddLeg {
|
||||
leg_id: String,
|
||||
codec_pt: u8,
|
||||
inbound_rx: mpsc::Receiver<RtpPacket>,
|
||||
outbound_tx: mpsc::Sender<Vec<u8>>,
|
||||
},
|
||||
/// Remove a leg from the mix (channels are dropped, I/O tasks exit).
|
||||
RemoveLeg { leg_id: String },
|
||||
/// Shut down the mixer.
|
||||
Shutdown,
|
||||
|
||||
/// Isolate a leg and start an interaction (consent prompt, IVR).
|
||||
/// The leg is removed from the mix and hears the prompt instead.
|
||||
/// DTMF from the leg is checked against expected_digits.
|
||||
StartInteraction {
|
||||
leg_id: String,
|
||||
/// PCM frames at MIX_RATE (16kHz), each 320 samples.
|
||||
prompt_pcm_frames: Vec<Vec<i16>>,
|
||||
expected_digits: Vec<char>,
|
||||
timeout_ms: u32,
|
||||
result_tx: oneshot::Sender<InteractionResult>,
|
||||
},
|
||||
/// Cancel an in-progress interaction (e.g., leg being removed).
|
||||
CancelInteraction { leg_id: String },
|
||||
|
||||
/// Add a tool leg that receives per-source unmerged audio.
|
||||
AddToolLeg {
|
||||
leg_id: String,
|
||||
tool_type: ToolType,
|
||||
audio_tx: mpsc::Sender<ToolAudioBatch>,
|
||||
},
|
||||
/// Remove a tool leg (drops the channel, background task finalizes).
|
||||
RemoveToolLeg { leg_id: String },
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mixer internals
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Internal per-leg state inside the mixer.
|
||||
struct MixerLegSlot {
|
||||
codec_pt: u8,
|
||||
transcoder: TranscodeState,
|
||||
inbound_rx: mpsc::Receiver<RtpPacket>,
|
||||
outbound_tx: mpsc::Sender<Vec<u8>>,
|
||||
/// Last decoded PCM frame at MIX_RATE (320 samples). Used for mix-minus.
|
||||
last_pcm_frame: Vec<i16>,
|
||||
/// Number of consecutive ticks with no inbound packet.
|
||||
silent_ticks: u32,
|
||||
// RTP output state.
|
||||
rtp_seq: u16,
|
||||
rtp_ts: u32,
|
||||
rtp_ssrc: u32,
|
||||
/// Current role of this leg in the mixer.
|
||||
role: LegRole,
|
||||
}
|
||||
|
||||
/// Spawn the mixer task for a call. Returns the command sender and task handle.
|
||||
pub fn spawn_mixer(
|
||||
call_id: String,
|
||||
out_tx: OutTx,
|
||||
) -> (mpsc::Sender<MixerCommand>, JoinHandle<()>) {
|
||||
let (cmd_tx, cmd_rx) = mpsc::channel::<MixerCommand>(32);
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
mixer_loop(call_id, cmd_rx, out_tx).await;
|
||||
});
|
||||
|
||||
(cmd_tx, handle)
|
||||
}
|
||||
|
||||
/// The 20ms mixing loop.
|
||||
async fn mixer_loop(
|
||||
call_id: String,
|
||||
mut cmd_rx: mpsc::Receiver<MixerCommand>,
|
||||
out_tx: OutTx,
|
||||
) {
|
||||
let mut legs: HashMap<String, MixerLegSlot> = HashMap::new();
|
||||
let mut tool_legs: HashMap<String, ToolLegSlot> = HashMap::new();
|
||||
let mut interval = time::interval(Duration::from_millis(20));
|
||||
interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
|
||||
// ── 1. Process control commands (non-blocking). ─────────────
|
||||
loop {
|
||||
match cmd_rx.try_recv() {
|
||||
Ok(MixerCommand::AddLeg {
|
||||
leg_id,
|
||||
codec_pt,
|
||||
inbound_rx,
|
||||
outbound_tx,
|
||||
}) => {
|
||||
let transcoder = match TranscodeState::new() {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"mixer_error",
|
||||
serde_json::json!({
|
||||
"call_id": call_id,
|
||||
"leg_id": leg_id,
|
||||
"error": format!("codec init: {e}"),
|
||||
}),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
legs.insert(
|
||||
leg_id,
|
||||
MixerLegSlot {
|
||||
codec_pt,
|
||||
transcoder,
|
||||
inbound_rx,
|
||||
outbound_tx,
|
||||
last_pcm_frame: vec![0i16; MIX_FRAME_SIZE],
|
||||
silent_ticks: 0,
|
||||
rtp_seq: 0,
|
||||
rtp_ts: 0,
|
||||
rtp_ssrc: rand::random(),
|
||||
role: LegRole::Participant,
|
||||
},
|
||||
);
|
||||
}
|
||||
Ok(MixerCommand::RemoveLeg { leg_id }) => {
|
||||
// If the leg is isolated, send Cancelled before dropping.
|
||||
if let Some(slot) = legs.get_mut(&leg_id) {
|
||||
if let LegRole::Isolated(ref mut state) = slot.role {
|
||||
if let Some(tx) = state.result_tx.take() {
|
||||
let _ = tx.send(InteractionResult::Cancelled);
|
||||
}
|
||||
}
|
||||
}
|
||||
legs.remove(&leg_id);
|
||||
// Channels drop → I/O tasks exit cleanly.
|
||||
}
|
||||
Ok(MixerCommand::Shutdown) => {
|
||||
// Cancel all outstanding interactions before shutting down.
|
||||
for slot in legs.values_mut() {
|
||||
if let LegRole::Isolated(ref mut state) = slot.role {
|
||||
if let Some(tx) = state.result_tx.take() {
|
||||
let _ = tx.send(InteractionResult::Cancelled);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
Ok(MixerCommand::StartInteraction {
|
||||
leg_id,
|
||||
prompt_pcm_frames,
|
||||
expected_digits,
|
||||
timeout_ms,
|
||||
result_tx,
|
||||
}) => {
|
||||
if let Some(slot) = legs.get_mut(&leg_id) {
|
||||
// Cancel any existing interaction first.
|
||||
if let LegRole::Isolated(ref mut old_state) = slot.role {
|
||||
if let Some(tx) = old_state.result_tx.take() {
|
||||
let _ = tx.send(InteractionResult::Cancelled);
|
||||
}
|
||||
}
|
||||
let timeout_ticks = timeout_ms / 20;
|
||||
slot.role = LegRole::Isolated(IsolationState {
|
||||
prompt_frames: VecDeque::from(prompt_pcm_frames),
|
||||
expected_digits,
|
||||
timeout_ticks_remaining: timeout_ticks,
|
||||
prompt_done: false,
|
||||
result_tx: Some(result_tx),
|
||||
});
|
||||
} else {
|
||||
// Leg not found — immediately cancel.
|
||||
let _ = result_tx.send(InteractionResult::Cancelled);
|
||||
}
|
||||
}
|
||||
Ok(MixerCommand::CancelInteraction { leg_id }) => {
|
||||
if let Some(slot) = legs.get_mut(&leg_id) {
|
||||
if let LegRole::Isolated(ref mut state) = slot.role {
|
||||
if let Some(tx) = state.result_tx.take() {
|
||||
let _ = tx.send(InteractionResult::Cancelled);
|
||||
}
|
||||
}
|
||||
slot.role = LegRole::Participant;
|
||||
}
|
||||
}
|
||||
Ok(MixerCommand::AddToolLeg {
|
||||
leg_id,
|
||||
tool_type,
|
||||
audio_tx,
|
||||
}) => {
|
||||
tool_legs.insert(leg_id, ToolLegSlot { tool_type, audio_tx });
|
||||
}
|
||||
Ok(MixerCommand::RemoveToolLeg { leg_id }) => {
|
||||
tool_legs.remove(&leg_id);
|
||||
// Dropping the ToolLegSlot drops audio_tx → background task sees channel close.
|
||||
}
|
||||
Err(mpsc::error::TryRecvError::Empty) => break,
|
||||
Err(mpsc::error::TryRecvError::Disconnected) => return,
|
||||
}
|
||||
}
|
||||
|
||||
if legs.is_empty() && tool_legs.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// ── 2. Drain inbound packets, decode to 16kHz PCM. ─────────
|
||||
// DTMF (PT 101) packets are collected separately.
|
||||
let leg_ids: Vec<String> = legs.keys().cloned().collect();
|
||||
let mut dtmf_forward: Vec<(String, RtpPacket)> = Vec::new();
|
||||
|
||||
for lid in &leg_ids {
|
||||
let slot = legs.get_mut(lid).unwrap();
|
||||
|
||||
// Drain channel — collect DTMF packets separately, keep latest audio.
|
||||
let mut latest_audio: Option<RtpPacket> = None;
|
||||
loop {
|
||||
match slot.inbound_rx.try_recv() {
|
||||
Ok(pkt) => {
|
||||
if pkt.payload_type == 101 {
|
||||
// DTMF telephone-event: collect for processing.
|
||||
dtmf_forward.push((lid.clone(), pkt));
|
||||
} else {
|
||||
latest_audio = Some(pkt);
|
||||
}
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(pkt) = latest_audio {
|
||||
slot.silent_ticks = 0;
|
||||
match slot.transcoder.decode_to_pcm(&pkt.payload, pkt.payload_type) {
|
||||
Ok((pcm, rate)) => {
|
||||
// Resample to mixing rate if needed.
|
||||
let pcm_mix = if rate == MIX_RATE {
|
||||
pcm
|
||||
} else {
|
||||
slot.transcoder
|
||||
.resample(&pcm, rate, MIX_RATE)
|
||||
.unwrap_or_else(|_| vec![0i16; MIX_FRAME_SIZE])
|
||||
};
|
||||
// Pad or truncate to exactly MIX_FRAME_SIZE.
|
||||
let mut frame = pcm_mix;
|
||||
frame.resize(MIX_FRAME_SIZE, 0);
|
||||
slot.last_pcm_frame = frame;
|
||||
}
|
||||
Err(_) => {
|
||||
// Decode failed — use silence.
|
||||
slot.last_pcm_frame = vec![0i16; MIX_FRAME_SIZE];
|
||||
}
|
||||
}
|
||||
} else if dtmf_forward.iter().any(|(src, _)| src == lid) {
|
||||
// Got DTMF but no audio — don't bump silent_ticks (DTMF counts as activity).
|
||||
slot.silent_ticks = 0;
|
||||
} else {
|
||||
slot.silent_ticks += 1;
|
||||
// After 150 ticks (3 seconds) of silence, zero out to avoid stale audio.
|
||||
if slot.silent_ticks > 150 {
|
||||
slot.last_pcm_frame = vec![0i16; MIX_FRAME_SIZE];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── 3. Compute total mix from PARTICIPANT legs only. ────────
|
||||
let mut total_mix = vec![0i32; MIX_FRAME_SIZE];
|
||||
for slot in legs.values() {
|
||||
if matches!(slot.role, LegRole::Participant) {
|
||||
for (i, &s) in slot.last_pcm_frame.iter().enumerate().take(MIX_FRAME_SIZE) {
|
||||
total_mix[i] += s as i32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── 4. Per-leg output. ──────────────────────────────────────
|
||||
// Collect interaction completions to apply after the loop
|
||||
// (can't mutate role while iterating mutably for encode).
|
||||
let mut completed_interactions: Vec<(String, InteractionResult)> = Vec::new();
|
||||
|
||||
for (lid, slot) in legs.iter_mut() {
|
||||
match &mut slot.role {
|
||||
LegRole::Participant => {
|
||||
// Mix-minus: total minus this leg's own contribution.
|
||||
let mut mix_minus = Vec::with_capacity(MIX_FRAME_SIZE);
|
||||
for i in 0..MIX_FRAME_SIZE {
|
||||
let sample = (total_mix[i] - slot.last_pcm_frame[i] as i32)
|
||||
.clamp(-32768, 32767) as i16;
|
||||
mix_minus.push(sample);
|
||||
}
|
||||
|
||||
// Resample from 16kHz to the leg's codec native rate.
|
||||
let target_rate = codec_sample_rate(slot.codec_pt);
|
||||
let resampled = if target_rate == MIX_RATE {
|
||||
mix_minus
|
||||
} else {
|
||||
slot.transcoder
|
||||
.resample(&mix_minus, MIX_RATE, target_rate)
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
// Encode to the leg's codec.
|
||||
let encoded =
|
||||
match slot.transcoder.encode_from_pcm(&resampled, slot.codec_pt) {
|
||||
Ok(e) if !e.is_empty() => e,
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
// Build RTP packet with header.
|
||||
let header =
|
||||
build_rtp_header(slot.codec_pt, slot.rtp_seq, slot.rtp_ts, slot.rtp_ssrc);
|
||||
let mut rtp = header.to_vec();
|
||||
rtp.extend_from_slice(&encoded);
|
||||
|
||||
slot.rtp_seq = slot.rtp_seq.wrapping_add(1);
|
||||
slot.rtp_ts = slot.rtp_ts.wrapping_add(rtp_clock_increment(slot.codec_pt));
|
||||
|
||||
// Non-blocking send — drop frame if channel is full.
|
||||
let _ = slot.outbound_tx.try_send(rtp);
|
||||
}
|
||||
LegRole::Isolated(state) => {
|
||||
// Check for DTMF digit from this leg.
|
||||
let mut matched_digit: Option<char> = None;
|
||||
for (src_lid, dtmf_pkt) in &dtmf_forward {
|
||||
if src_lid == lid && dtmf_pkt.payload.len() >= 4 {
|
||||
let event_id = dtmf_pkt.payload[0];
|
||||
let end_bit = (dtmf_pkt.payload[1] & 0x80) != 0;
|
||||
if end_bit {
|
||||
const EVENT_CHARS: &[char] = &[
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '#',
|
||||
'A', 'B', 'C', 'D',
|
||||
];
|
||||
if let Some(&ch) = EVENT_CHARS.get(event_id as usize) {
|
||||
if state.expected_digits.contains(&ch) {
|
||||
matched_digit = Some(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(digit) = matched_digit {
|
||||
// Interaction complete — digit matched.
|
||||
completed_interactions
|
||||
.push((lid.clone(), InteractionResult::Digit(digit)));
|
||||
} else {
|
||||
// Play prompt frame or silence.
|
||||
let pcm_frame = if let Some(frame) = state.prompt_frames.pop_front() {
|
||||
frame
|
||||
} else {
|
||||
state.prompt_done = true;
|
||||
vec![0i16; MIX_FRAME_SIZE]
|
||||
};
|
||||
|
||||
// Encode prompt frame to the leg's codec (reuses existing encode path).
|
||||
let target_rate = codec_sample_rate(slot.codec_pt);
|
||||
let resampled = if target_rate == MIX_RATE {
|
||||
pcm_frame
|
||||
} else {
|
||||
slot.transcoder
|
||||
.resample(&pcm_frame, MIX_RATE, target_rate)
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
if let Ok(encoded) =
|
||||
slot.transcoder.encode_from_pcm(&resampled, slot.codec_pt)
|
||||
{
|
||||
if !encoded.is_empty() {
|
||||
let header = build_rtp_header(
|
||||
slot.codec_pt,
|
||||
slot.rtp_seq,
|
||||
slot.rtp_ts,
|
||||
slot.rtp_ssrc,
|
||||
);
|
||||
let mut rtp = header.to_vec();
|
||||
rtp.extend_from_slice(&encoded);
|
||||
slot.rtp_seq = slot.rtp_seq.wrapping_add(1);
|
||||
slot.rtp_ts = slot
|
||||
.rtp_ts
|
||||
.wrapping_add(rtp_clock_increment(slot.codec_pt));
|
||||
let _ = slot.outbound_tx.try_send(rtp);
|
||||
}
|
||||
}
|
||||
|
||||
// Check timeout (only after prompt finishes).
|
||||
if state.prompt_done {
|
||||
if state.timeout_ticks_remaining == 0 {
|
||||
completed_interactions
|
||||
.push((lid.clone(), InteractionResult::Timeout));
|
||||
} else {
|
||||
state.timeout_ticks_remaining -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply completed interactions — revert legs to Participant.
|
||||
for (lid, result) in completed_interactions {
|
||||
if let Some(slot) = legs.get_mut(&lid) {
|
||||
if let LegRole::Isolated(ref mut state) = slot.role {
|
||||
if let Some(tx) = state.result_tx.take() {
|
||||
let _ = tx.send(result);
|
||||
}
|
||||
}
|
||||
slot.role = LegRole::Participant;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 5. Distribute per-source audio to tool legs. ────────────
|
||||
if !tool_legs.is_empty() {
|
||||
// Collect participant PCM frames (computed in step 2).
|
||||
let sources: Vec<ToolAudioSource> = legs
|
||||
.iter()
|
||||
.filter(|(_, s)| matches!(s.role, LegRole::Participant))
|
||||
.map(|(lid, s)| ToolAudioSource {
|
||||
leg_id: lid.clone(),
|
||||
pcm_16k: s.last_pcm_frame.clone(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
for tool in tool_legs.values() {
|
||||
let batch = ToolAudioBatch {
|
||||
sources: sources
|
||||
.iter()
|
||||
.map(|s| ToolAudioSource {
|
||||
leg_id: s.leg_id.clone(),
|
||||
pcm_16k: s.pcm_16k.clone(),
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
// Non-blocking send — drop batch if tool can't keep up.
|
||||
let _ = tool.audio_tx.try_send(batch);
|
||||
}
|
||||
}
|
||||
|
||||
// ── 6. Forward DTMF packets between participant legs only. ──
|
||||
for (source_lid, dtmf_pkt) in &dtmf_forward {
|
||||
// Skip if the source is an isolated leg (its DTMF was handled in step 4).
|
||||
if let Some(src_slot) = legs.get(source_lid) {
|
||||
if matches!(src_slot.role, LegRole::Isolated(_)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
for (target_lid, target_slot) in legs.iter_mut() {
|
||||
if target_lid == source_lid {
|
||||
continue; // Don't echo DTMF back to sender.
|
||||
}
|
||||
// Don't forward to isolated legs.
|
||||
if matches!(target_slot.role, LegRole::Isolated(_)) {
|
||||
continue;
|
||||
}
|
||||
let mut header = build_rtp_header(
|
||||
101,
|
||||
target_slot.rtp_seq,
|
||||
target_slot.rtp_ts,
|
||||
target_slot.rtp_ssrc,
|
||||
);
|
||||
if dtmf_pkt.marker {
|
||||
header[1] |= 0x80; // Set marker bit.
|
||||
}
|
||||
let mut rtp_out = header.to_vec();
|
||||
rtp_out.extend_from_slice(&dtmf_pkt.payload);
|
||||
target_slot.rtp_seq = target_slot.rtp_seq.wrapping_add(1);
|
||||
// Don't increment rtp_ts for DTMF — it shares timestamp context with audio.
|
||||
let _ = target_slot.outbound_tx.try_send(rtp_out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -321,6 +321,17 @@ impl ProviderManager {
|
||||
None
|
||||
}
|
||||
|
||||
/// Find a provider by its config ID (e.g. "easybell").
|
||||
pub async fn find_by_provider_id(&self, provider_id: &str) -> Option<Arc<Mutex<ProviderState>>> {
|
||||
for ps_arc in &self.providers {
|
||||
let ps = ps_arc.lock().await;
|
||||
if ps.config.id == provider_id {
|
||||
return Some(ps_arc.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Check if a provider is currently registered.
|
||||
pub async fn is_registered(&self, provider_id: &str) -> bool {
|
||||
for ps_arc in &self.providers {
|
||||
|
||||
182
rust/crates/proxy-engine/src/recorder.rs
Normal file
182
rust/crates/proxy-engine/src/recorder.rs
Normal file
@@ -0,0 +1,182 @@
|
||||
//! Audio recorder — receives RTP packets and writes a WAV file.
|
||||
|
||||
use codec_lib::TranscodeState;
|
||||
use std::path::Path;
|
||||
|
||||
/// Active recording session.
|
||||
pub struct Recorder {
|
||||
writer: hound::WavWriter<std::io::BufWriter<std::fs::File>>,
|
||||
transcoder: TranscodeState,
|
||||
source_pt: u8,
|
||||
total_samples: u64,
|
||||
sample_rate: u32,
|
||||
max_samples: Option<u64>,
|
||||
file_path: String,
|
||||
}
|
||||
|
||||
impl Recorder {
|
||||
/// Create a new recorder that writes to a WAV file.
|
||||
/// `source_pt` is the RTP payload type of the incoming audio.
|
||||
/// `max_duration_ms` optionally limits the recording length.
|
||||
pub fn new(
|
||||
file_path: &str,
|
||||
source_pt: u8,
|
||||
max_duration_ms: Option<u64>,
|
||||
) -> Result<Self, String> {
|
||||
// Ensure parent directory exists.
|
||||
if let Some(parent) = Path::new(file_path).parent() {
|
||||
std::fs::create_dir_all(parent)
|
||||
.map_err(|e| format!("create dir: {e}"))?;
|
||||
}
|
||||
|
||||
let sample_rate = 8000u32; // Record at 8kHz (standard telephony)
|
||||
let spec = hound::WavSpec {
|
||||
channels: 1,
|
||||
sample_rate,
|
||||
bits_per_sample: 16,
|
||||
sample_format: hound::SampleFormat::Int,
|
||||
};
|
||||
|
||||
let writer = hound::WavWriter::create(file_path, spec)
|
||||
.map_err(|e| format!("create WAV {file_path}: {e}"))?;
|
||||
|
||||
let transcoder = TranscodeState::new().map_err(|e| format!("codec init: {e}"))?;
|
||||
|
||||
let max_samples = max_duration_ms.map(|ms| (sample_rate as u64 * ms) / 1000);
|
||||
|
||||
Ok(Self {
|
||||
writer,
|
||||
transcoder,
|
||||
source_pt,
|
||||
total_samples: 0,
|
||||
sample_rate,
|
||||
max_samples,
|
||||
file_path: file_path.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a recorder that writes raw PCM at a given sample rate.
|
||||
/// Used by tool legs that already have decoded PCM (no RTP processing needed).
|
||||
pub fn new_pcm(file_path: &str, sample_rate: u32, max_duration_ms: Option<u64>) -> Result<Self, String> {
|
||||
if let Some(parent) = Path::new(file_path).parent() {
|
||||
std::fs::create_dir_all(parent)
|
||||
.map_err(|e| format!("create dir: {e}"))?;
|
||||
}
|
||||
|
||||
let spec = hound::WavSpec {
|
||||
channels: 1,
|
||||
sample_rate,
|
||||
bits_per_sample: 16,
|
||||
sample_format: hound::SampleFormat::Int,
|
||||
};
|
||||
|
||||
let writer = hound::WavWriter::create(file_path, spec)
|
||||
.map_err(|e| format!("create WAV {file_path}: {e}"))?;
|
||||
|
||||
// source_pt is unused for PCM recording; set to 0.
|
||||
let transcoder = TranscodeState::new().map_err(|e| format!("codec init: {e}"))?;
|
||||
let max_samples = max_duration_ms.map(|ms| (sample_rate as u64 * ms) / 1000);
|
||||
|
||||
Ok(Self {
|
||||
writer,
|
||||
transcoder,
|
||||
source_pt: 0,
|
||||
total_samples: 0,
|
||||
sample_rate,
|
||||
max_samples,
|
||||
file_path: file_path.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Write raw PCM samples directly (no RTP decoding).
|
||||
/// Returns true if recording should continue, false if max duration reached.
|
||||
pub fn write_pcm(&mut self, samples: &[i16]) -> bool {
|
||||
for &sample in samples {
|
||||
if self.writer.write_sample(sample).is_err() {
|
||||
return false;
|
||||
}
|
||||
self.total_samples += 1;
|
||||
if let Some(max) = self.max_samples {
|
||||
if self.total_samples >= max {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Process an incoming RTP packet (full packet with header).
|
||||
/// Returns true if recording should continue, false if max duration reached.
|
||||
pub fn process_rtp(&mut self, data: &[u8]) -> bool {
|
||||
if data.len() <= 12 {
|
||||
return true; // Too short, skip.
|
||||
}
|
||||
|
||||
let pt = data[1] & 0x7F;
|
||||
// Skip telephone-event (DTMF) packets.
|
||||
if pt == 101 {
|
||||
return true;
|
||||
}
|
||||
|
||||
let payload = &data[12..];
|
||||
if payload.is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Decode to PCM.
|
||||
let (pcm, rate) = match self.transcoder.decode_to_pcm(payload, self.source_pt) {
|
||||
Ok(result) => result,
|
||||
Err(_) => return true, // Decode failed, skip packet.
|
||||
};
|
||||
|
||||
// Resample to 8kHz if needed.
|
||||
let pcm_8k = if rate != self.sample_rate {
|
||||
match self.transcoder.resample(&pcm, rate, self.sample_rate) {
|
||||
Ok(r) => r,
|
||||
Err(_) => return true,
|
||||
}
|
||||
} else {
|
||||
pcm
|
||||
};
|
||||
|
||||
// Write samples.
|
||||
for &sample in &pcm_8k {
|
||||
if let Err(_) = self.writer.write_sample(sample) {
|
||||
return false;
|
||||
}
|
||||
self.total_samples += 1;
|
||||
|
||||
if let Some(max) = self.max_samples {
|
||||
if self.total_samples >= max {
|
||||
return false; // Max duration reached.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
/// Stop recording and finalize the WAV file.
|
||||
pub fn stop(self) -> RecordingResult {
|
||||
let duration_ms = if self.sample_rate > 0 {
|
||||
(self.total_samples * 1000) / self.sample_rate as u64
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
// Writer is finalized on drop (writes RIFF header sizes).
|
||||
drop(self.writer);
|
||||
|
||||
RecordingResult {
|
||||
file_path: self.file_path,
|
||||
duration_ms,
|
||||
total_samples: self.total_samples,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RecordingResult {
|
||||
pub file_path: String,
|
||||
pub duration_ms: u64,
|
||||
pub total_samples: u64,
|
||||
}
|
||||
475
rust/crates/proxy-engine/src/sip_leg.rs
Normal file
475
rust/crates/proxy-engine/src/sip_leg.rs
Normal file
@@ -0,0 +1,475 @@
|
||||
//! SipLeg — manages one side of a B2BUA call.
|
||||
//!
|
||||
//! Handles the full INVITE lifecycle:
|
||||
//! - Send INVITE with SDP
|
||||
//! - Handle 407 Proxy Authentication (digest auth retry)
|
||||
//! - Handle 200 OK (ACK, learn media endpoint)
|
||||
//! - Handle BYE/CANCEL (teardown)
|
||||
//! - Track SIP dialog state (early → confirmed → terminated)
|
||||
//!
|
||||
//! Ported from ts/call/sip-leg.ts.
|
||||
|
||||
use sip_proto::dialog::{DialogState, SipDialog};
|
||||
use sip_proto::helpers::{
|
||||
build_sdp, compute_digest_auth, generate_branch, generate_tag, parse_digest_challenge,
|
||||
parse_sdp_endpoint, SdpOptions,
|
||||
};
|
||||
use sip_proto::message::{RequestOptions, SipMessage};
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::UdpSocket;
|
||||
|
||||
/// State of a SIP leg.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LegState {
|
||||
Inviting,
|
||||
Ringing,
|
||||
Connected,
|
||||
Terminating,
|
||||
Terminated,
|
||||
}
|
||||
|
||||
/// Configuration for creating a SIP leg.
|
||||
pub struct SipLegConfig {
|
||||
/// Proxy LAN IP (for Via, Contact, SDP).
|
||||
pub lan_ip: String,
|
||||
/// Proxy LAN port.
|
||||
pub lan_port: u16,
|
||||
/// Public IP (for provider-facing legs).
|
||||
pub public_ip: Option<String>,
|
||||
/// SIP target endpoint (provider outbound proxy or device address).
|
||||
pub sip_target: SocketAddr,
|
||||
/// Provider credentials (for 407 auth).
|
||||
pub username: Option<String>,
|
||||
pub password: Option<String>,
|
||||
pub registered_aor: Option<String>,
|
||||
/// Codec payload types to offer.
|
||||
pub codecs: Vec<u8>,
|
||||
/// Our RTP port for SDP.
|
||||
pub rtp_port: u16,
|
||||
}
|
||||
|
||||
/// A SIP leg with full dialog management.
|
||||
pub struct SipLeg {
|
||||
pub id: String,
|
||||
pub state: LegState,
|
||||
pub config: SipLegConfig,
|
||||
pub dialog: Option<SipDialog>,
|
||||
|
||||
/// The INVITE we sent (needed for CANCEL and 407 ACK).
|
||||
invite: Option<SipMessage>,
|
||||
/// Original unauthenticated INVITE (for re-ACKing retransmitted 407s).
|
||||
orig_invite: Option<SipMessage>,
|
||||
/// Whether we've attempted digest auth.
|
||||
auth_attempted: bool,
|
||||
|
||||
/// Remote media endpoint (learned from SDP in 200 OK).
|
||||
pub remote_media: Option<SocketAddr>,
|
||||
}
|
||||
|
||||
impl SipLeg {
|
||||
pub fn new(id: String, config: SipLegConfig) -> Self {
|
||||
Self {
|
||||
id,
|
||||
state: LegState::Inviting,
|
||||
config,
|
||||
dialog: None,
|
||||
invite: None,
|
||||
orig_invite: None,
|
||||
auth_attempted: false,
|
||||
remote_media: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build and send an INVITE to establish this leg.
|
||||
pub async fn send_invite(
|
||||
&mut self,
|
||||
from_uri: &str,
|
||||
to_uri: &str,
|
||||
sip_call_id: &str,
|
||||
socket: &UdpSocket,
|
||||
) {
|
||||
let ip = self
|
||||
.config
|
||||
.public_ip
|
||||
.as_deref()
|
||||
.unwrap_or(&self.config.lan_ip);
|
||||
|
||||
let sdp = build_sdp(&SdpOptions {
|
||||
ip,
|
||||
port: self.config.rtp_port,
|
||||
payload_types: &self.config.codecs,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let invite = SipMessage::create_request(
|
||||
"INVITE",
|
||||
to_uri,
|
||||
RequestOptions {
|
||||
via_host: ip.to_string(),
|
||||
via_port: self.config.lan_port,
|
||||
via_transport: None,
|
||||
via_branch: Some(generate_branch()),
|
||||
from_uri: from_uri.to_string(),
|
||||
from_display_name: None,
|
||||
from_tag: Some(generate_tag()),
|
||||
to_uri: to_uri.to_string(),
|
||||
to_display_name: None,
|
||||
to_tag: None,
|
||||
call_id: Some(sip_call_id.to_string()),
|
||||
cseq: Some(1),
|
||||
contact: Some(format!("<sip:{ip}:{}>", self.config.lan_port)),
|
||||
max_forwards: Some(70),
|
||||
body: Some(sdp),
|
||||
content_type: Some("application/sdp".to_string()),
|
||||
extra_headers: Some(vec![
|
||||
("User-Agent".to_string(), "SipRouter/1.0".to_string()),
|
||||
]),
|
||||
},
|
||||
);
|
||||
|
||||
self.dialog = Some(SipDialog::from_uac_invite(&invite, ip, self.config.lan_port));
|
||||
self.invite = Some(invite.clone());
|
||||
self.state = LegState::Inviting;
|
||||
|
||||
let _ = socket.send_to(&invite.serialize(), self.config.sip_target).await;
|
||||
}
|
||||
|
||||
/// Handle an incoming SIP message routed to this leg.
|
||||
/// Returns an optional reply to send (e.g. ACK, auth retry INVITE).
|
||||
pub fn handle_message(&mut self, msg: &SipMessage) -> SipLegAction {
|
||||
if msg.is_response() {
|
||||
self.handle_response(msg)
|
||||
} else {
|
||||
self.handle_request(msg)
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_response(&mut self, msg: &SipMessage) -> SipLegAction {
|
||||
let code = msg.status_code().unwrap_or(0);
|
||||
let cseq_method = msg.cseq_method().unwrap_or("").to_uppercase();
|
||||
|
||||
if cseq_method != "INVITE" {
|
||||
return SipLegAction::None;
|
||||
}
|
||||
|
||||
// Handle retransmitted 407 for the original unauthenticated INVITE.
|
||||
if self.auth_attempted {
|
||||
if let Some(dialog) = &self.dialog {
|
||||
let response_cseq: u32 = msg
|
||||
.get_header("CSeq")
|
||||
.and_then(|s| s.split_whitespace().next())
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(0);
|
||||
if response_cseq < dialog.local_cseq && code >= 400 {
|
||||
// ACK the retransmitted error response.
|
||||
if let Some(orig) = &self.orig_invite {
|
||||
let ack = build_non_2xx_ack(orig, msg);
|
||||
return SipLegAction::Send(ack.serialize());
|
||||
}
|
||||
return SipLegAction::None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle 407 Proxy Authentication Required.
|
||||
if code == 407 {
|
||||
return self.handle_auth_challenge(msg);
|
||||
}
|
||||
|
||||
// Update dialog state.
|
||||
if let Some(dialog) = &mut self.dialog {
|
||||
dialog.process_response(msg);
|
||||
}
|
||||
|
||||
if code == 180 || code == 183 {
|
||||
self.state = LegState::Ringing;
|
||||
SipLegAction::StateChange(LegState::Ringing)
|
||||
} else if code >= 200 && code < 300 {
|
||||
// ACK the 200 OK.
|
||||
let ack_buf = if let Some(dialog) = &self.dialog {
|
||||
let ack = dialog.create_ack();
|
||||
Some(ack.serialize())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// If already connected (200 retransmit), just re-ACK.
|
||||
if self.state == LegState::Connected {
|
||||
return match ack_buf {
|
||||
Some(buf) => SipLegAction::Send(buf),
|
||||
None => SipLegAction::None,
|
||||
};
|
||||
}
|
||||
|
||||
// Learn media endpoint from SDP.
|
||||
if msg.has_sdp_body() {
|
||||
if let Some(ep) = parse_sdp_endpoint(&msg.body) {
|
||||
if let Ok(addr) = format!("{}:{}", ep.address, ep.port).parse() {
|
||||
self.remote_media = Some(addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.state = LegState::Connected;
|
||||
|
||||
match ack_buf {
|
||||
Some(buf) => SipLegAction::ConnectedWithAck(buf),
|
||||
None => SipLegAction::StateChange(LegState::Connected),
|
||||
}
|
||||
} else if code >= 300 {
|
||||
self.state = LegState::Terminated;
|
||||
if let Some(dialog) = &mut self.dialog {
|
||||
dialog.terminate();
|
||||
}
|
||||
SipLegAction::Terminated(format!("rejected_{code}"))
|
||||
} else {
|
||||
SipLegAction::None // 1xx provisional
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_auth_challenge(&mut self, msg: &SipMessage) -> SipLegAction {
|
||||
if self.auth_attempted {
|
||||
self.state = LegState::Terminated;
|
||||
if let Some(dialog) = &mut self.dialog {
|
||||
dialog.terminate();
|
||||
}
|
||||
return SipLegAction::Terminated("auth_rejected".to_string());
|
||||
}
|
||||
self.auth_attempted = true;
|
||||
|
||||
let challenge_header = match msg.get_header("Proxy-Authenticate") {
|
||||
Some(h) => h,
|
||||
None => {
|
||||
self.state = LegState::Terminated;
|
||||
return SipLegAction::Terminated("407_no_challenge".to_string());
|
||||
}
|
||||
};
|
||||
|
||||
let challenge = match parse_digest_challenge(challenge_header) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
self.state = LegState::Terminated;
|
||||
return SipLegAction::Terminated("407_bad_challenge".to_string());
|
||||
}
|
||||
};
|
||||
|
||||
let password = match &self.config.password {
|
||||
Some(p) => p.clone(),
|
||||
None => {
|
||||
self.state = LegState::Terminated;
|
||||
return SipLegAction::Terminated("407_no_password".to_string());
|
||||
}
|
||||
};
|
||||
|
||||
let aor = match &self.config.registered_aor {
|
||||
Some(a) => a.clone(),
|
||||
None => {
|
||||
self.state = LegState::Terminated;
|
||||
return SipLegAction::Terminated("407_no_aor".to_string());
|
||||
}
|
||||
};
|
||||
|
||||
let username = aor
|
||||
.trim_start_matches("sip:")
|
||||
.trim_start_matches("sips:")
|
||||
.split('@')
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
let dest_uri = self
|
||||
.invite
|
||||
.as_ref()
|
||||
.and_then(|i| i.request_uri())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
let auth_value = compute_digest_auth(
|
||||
&username,
|
||||
&password,
|
||||
&challenge.realm,
|
||||
&challenge.nonce,
|
||||
"INVITE",
|
||||
&dest_uri,
|
||||
challenge.algorithm.as_deref(),
|
||||
challenge.opaque.as_deref(),
|
||||
);
|
||||
|
||||
// ACK the 407.
|
||||
let mut ack_buf = None;
|
||||
if let Some(invite) = &self.invite {
|
||||
let ack = build_non_2xx_ack(invite, msg);
|
||||
ack_buf = Some(ack.serialize());
|
||||
}
|
||||
|
||||
// Save original INVITE for retransmission handling.
|
||||
self.orig_invite = self.invite.clone();
|
||||
|
||||
// Build authenticated INVITE with same From tag, CSeq=2.
|
||||
let ip = self
|
||||
.config
|
||||
.public_ip
|
||||
.as_deref()
|
||||
.unwrap_or(&self.config.lan_ip);
|
||||
let from_tag = self
|
||||
.dialog
|
||||
.as_ref()
|
||||
.map(|d| d.local_tag.clone())
|
||||
.unwrap_or_else(generate_tag);
|
||||
|
||||
let sdp = build_sdp(&SdpOptions {
|
||||
ip,
|
||||
port: self.config.rtp_port,
|
||||
payload_types: &self.config.codecs,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let call_id = self
|
||||
.dialog
|
||||
.as_ref()
|
||||
.map(|d| d.call_id.clone())
|
||||
.unwrap_or_default();
|
||||
|
||||
let invite_auth = SipMessage::create_request(
|
||||
"INVITE",
|
||||
&dest_uri,
|
||||
RequestOptions {
|
||||
via_host: ip.to_string(),
|
||||
via_port: self.config.lan_port,
|
||||
via_transport: None,
|
||||
via_branch: Some(generate_branch()),
|
||||
from_uri: aor,
|
||||
from_display_name: None,
|
||||
from_tag: Some(from_tag),
|
||||
to_uri: dest_uri.clone(),
|
||||
to_display_name: None,
|
||||
to_tag: None,
|
||||
call_id: Some(call_id),
|
||||
cseq: Some(2),
|
||||
contact: Some(format!("<sip:{ip}:{}>", self.config.lan_port)),
|
||||
max_forwards: Some(70),
|
||||
body: Some(sdp),
|
||||
content_type: Some("application/sdp".to_string()),
|
||||
extra_headers: Some(vec![
|
||||
("Proxy-Authorization".to_string(), auth_value),
|
||||
("User-Agent".to_string(), "SipRouter/1.0".to_string()),
|
||||
]),
|
||||
},
|
||||
);
|
||||
|
||||
self.invite = Some(invite_auth.clone());
|
||||
if let Some(dialog) = &mut self.dialog {
|
||||
dialog.local_cseq = 2;
|
||||
}
|
||||
|
||||
// Return both the ACK for the 407 and the new authenticated INVITE.
|
||||
let invite_buf = invite_auth.serialize();
|
||||
SipLegAction::AuthRetry {
|
||||
ack_407: ack_buf,
|
||||
invite_with_auth: invite_buf,
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_request(&mut self, msg: &SipMessage) -> SipLegAction {
|
||||
let method = msg.method().unwrap_or("");
|
||||
|
||||
if method == "BYE" {
|
||||
let ok = SipMessage::create_response(200, "OK", msg, None);
|
||||
self.state = LegState::Terminated;
|
||||
if let Some(dialog) = &mut self.dialog {
|
||||
dialog.terminate();
|
||||
}
|
||||
return SipLegAction::SendAndTerminate(ok.serialize(), "bye".to_string());
|
||||
}
|
||||
|
||||
if method == "INFO" {
|
||||
let ok = SipMessage::create_response(200, "OK", msg, None);
|
||||
return SipLegAction::Send(ok.serialize());
|
||||
}
|
||||
|
||||
SipLegAction::None
|
||||
}
|
||||
|
||||
/// Build a BYE or CANCEL to tear down this leg.
|
||||
pub fn build_hangup(&mut self) -> Option<Vec<u8>> {
|
||||
let dialog = self.dialog.as_mut()?;
|
||||
|
||||
let msg = if dialog.state == DialogState::Confirmed {
|
||||
dialog.create_request("BYE", None, None, None)
|
||||
} else if dialog.state == DialogState::Early {
|
||||
if let Some(invite) = &self.invite {
|
||||
dialog.create_cancel(invite)
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
self.state = LegState::Terminating;
|
||||
dialog.terminate();
|
||||
Some(msg.serialize())
|
||||
}
|
||||
|
||||
/// Get the SIP Call-ID for routing.
|
||||
pub fn sip_call_id(&self) -> Option<&str> {
|
||||
self.dialog.as_ref().map(|d| d.call_id.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
/// Actions produced by the SipLeg message handler.
|
||||
pub enum SipLegAction {
|
||||
/// No action needed.
|
||||
None,
|
||||
/// Send a SIP message (ACK, 200 OK to INFO, etc.).
|
||||
Send(Vec<u8>),
|
||||
/// Leg state changed.
|
||||
StateChange(LegState),
|
||||
/// Connected — send this ACK.
|
||||
ConnectedWithAck(Vec<u8>),
|
||||
/// Terminated with a reason.
|
||||
Terminated(String),
|
||||
/// Send 200 OK and terminate.
|
||||
SendAndTerminate(Vec<u8>, String),
|
||||
/// 407 auth retry — send ACK for 407, then send new INVITE with auth.
|
||||
AuthRetry {
|
||||
ack_407: Option<Vec<u8>>,
|
||||
invite_with_auth: Vec<u8>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Build an ACK for a non-2xx response (same transaction as the INVITE).
|
||||
fn build_non_2xx_ack(original_invite: &SipMessage, response: &SipMessage) -> SipMessage {
|
||||
let via = original_invite.get_header("Via").unwrap_or("").to_string();
|
||||
let from = original_invite
|
||||
.get_header("From")
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let to = response.get_header("To").unwrap_or("").to_string();
|
||||
let call_id = original_invite.call_id().to_string();
|
||||
let cseq_num: u32 = original_invite
|
||||
.get_header("CSeq")
|
||||
.and_then(|s| s.split_whitespace().next())
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(1);
|
||||
|
||||
let ruri = original_invite
|
||||
.request_uri()
|
||||
.unwrap_or("sip:unknown")
|
||||
.to_string();
|
||||
|
||||
SipMessage::new(
|
||||
format!("ACK {ruri} SIP/2.0"),
|
||||
vec![
|
||||
("Via".to_string(), via),
|
||||
("From".to_string(), from),
|
||||
("To".to_string(), to),
|
||||
("Call-ID".to_string(), call_id),
|
||||
("CSeq".to_string(), format!("{cseq_num} ACK")),
|
||||
("Max-Forwards".to_string(), "70".to_string()),
|
||||
("Content-Length".to_string(), "0".to_string()),
|
||||
],
|
||||
String::new(),
|
||||
)
|
||||
}
|
||||
138
rust/crates/proxy-engine/src/tool_leg.rs
Normal file
138
rust/crates/proxy-engine/src/tool_leg.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
//! Tool leg consumers — background tasks that process per-source unmerged audio.
|
||||
//!
|
||||
//! Tool legs are observer legs that receive individual audio streams from each
|
||||
//! participant in a call. The mixer pipes `ToolAudioBatch` every 20ms containing
|
||||
//! each participant's decoded PCM@16kHz tagged with source leg ID.
|
||||
//!
|
||||
//! Consumers:
|
||||
//! - **Recording**: writes per-source WAV files for speaker-separated recording.
|
||||
//! - **Transcription**: stub for future Whisper integration (accumulates audio in Rust).
|
||||
|
||||
use crate::ipc::{emit_event, OutTx};
|
||||
use crate::mixer::ToolAudioBatch;
|
||||
use crate::recorder::Recorder;
|
||||
use std::collections::HashMap;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Recording consumer
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Spawn a recording tool leg that writes per-source WAV files.
|
||||
///
|
||||
/// Returns the channel sender (for the mixer to send batches) and the task handle.
|
||||
/// When the channel is closed (tool leg removed), all WAV files are finalized
|
||||
/// and a `tool_recording_done` event is emitted.
|
||||
pub fn spawn_recording_tool(
|
||||
tool_leg_id: String,
|
||||
call_id: String,
|
||||
base_dir: String,
|
||||
out_tx: OutTx,
|
||||
) -> (mpsc::Sender<ToolAudioBatch>, JoinHandle<()>) {
|
||||
let (tx, mut rx) = mpsc::channel::<ToolAudioBatch>(64);
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
let mut recorders: HashMap<String, Recorder> = HashMap::new();
|
||||
|
||||
while let Some(batch) = rx.recv().await {
|
||||
for source in &batch.sources {
|
||||
// Skip silence-only frames (all zeros = no audio activity).
|
||||
let has_audio = source.pcm_16k.iter().any(|&s| s != 0);
|
||||
if !has_audio && !recorders.contains_key(&source.leg_id) {
|
||||
continue; // Don't create a file for silence-only sources.
|
||||
}
|
||||
|
||||
let recorder = recorders.entry(source.leg_id.clone()).or_insert_with(|| {
|
||||
let path = format!("{}/{}-{}.wav", base_dir, call_id, source.leg_id);
|
||||
Recorder::new_pcm(&path, 16000, None).unwrap_or_else(|e| {
|
||||
panic!("failed to create recorder for {}: {e}", source.leg_id);
|
||||
})
|
||||
});
|
||||
|
||||
if !recorder.write_pcm(&source.pcm_16k) {
|
||||
// Max duration reached — stop recording this source.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Channel closed — finalize all recordings.
|
||||
let mut files = Vec::new();
|
||||
for (leg_id, rec) in recorders {
|
||||
let result = rec.stop();
|
||||
files.push(serde_json::json!({
|
||||
"source_leg_id": leg_id,
|
||||
"file_path": result.file_path,
|
||||
"duration_ms": result.duration_ms,
|
||||
}));
|
||||
}
|
||||
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"tool_recording_done",
|
||||
serde_json::json!({
|
||||
"call_id": call_id,
|
||||
"tool_leg_id": tool_leg_id,
|
||||
"files": files,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
(tx, handle)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Transcription consumer (stub — real plumbing, stub consumer)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Spawn a transcription tool leg.
|
||||
///
|
||||
/// The plumbing is fully real: it receives per-source unmerged PCM@16kHz from
|
||||
/// the mixer every 20ms. The consumer is a stub that accumulates audio and
|
||||
/// reports metadata on close. Future: will stream to a Whisper HTTP endpoint.
|
||||
pub fn spawn_transcription_tool(
|
||||
tool_leg_id: String,
|
||||
call_id: String,
|
||||
out_tx: OutTx,
|
||||
) -> (mpsc::Sender<ToolAudioBatch>, JoinHandle<()>) {
|
||||
let (tx, mut rx) = mpsc::channel::<ToolAudioBatch>(64);
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
// Track per-source sample counts for duration reporting.
|
||||
let mut source_samples: HashMap<String, u64> = HashMap::new();
|
||||
|
||||
while let Some(batch) = rx.recv().await {
|
||||
for source in &batch.sources {
|
||||
*source_samples.entry(source.leg_id.clone()).or_insert(0) +=
|
||||
source.pcm_16k.len() as u64;
|
||||
|
||||
// TODO: Future — accumulate chunks and stream to Whisper endpoint.
|
||||
// For now, the audio is received and counted but not processed.
|
||||
}
|
||||
}
|
||||
|
||||
// Channel closed — report metadata.
|
||||
let sources: Vec<serde_json::Value> = source_samples
|
||||
.iter()
|
||||
.map(|(leg_id, samples)| {
|
||||
serde_json::json!({
|
||||
"source_leg_id": leg_id,
|
||||
"duration_ms": (samples * 1000) / 16000,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"tool_transcription_done",
|
||||
serde_json::json!({
|
||||
"call_id": call_id,
|
||||
"tool_leg_id": tool_leg_id,
|
||||
"sources": sources,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
(tx, handle)
|
||||
}
|
||||
137
rust/crates/proxy-engine/src/voicemail.rs
Normal file
137
rust/crates/proxy-engine/src/voicemail.rs
Normal file
@@ -0,0 +1,137 @@
|
||||
//! Voicemail session — answer → play greeting → beep → record → done.
|
||||
|
||||
use crate::audio_player::{play_beep, play_wav_file};
|
||||
use crate::ipc::{emit_event, OutTx};
|
||||
use crate::recorder::Recorder;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::UdpSocket;
|
||||
|
||||
/// Run a voicemail session on an RTP port.
|
||||
///
|
||||
/// 1. Plays the greeting WAV file to the caller
|
||||
/// 2. Plays a beep tone
|
||||
/// 3. Records the caller's message until BYE or max duration
|
||||
///
|
||||
/// The RTP receive loop is separate — it feeds packets to the recorder
|
||||
/// via the returned channel.
|
||||
pub async fn run_voicemail_session(
|
||||
rtp_socket: Arc<UdpSocket>,
|
||||
provider_media: SocketAddr,
|
||||
codec_pt: u8,
|
||||
greeting_wav: Option<String>,
|
||||
recording_path: String,
|
||||
max_recording_ms: u64,
|
||||
call_id: String,
|
||||
caller_number: String,
|
||||
out_tx: OutTx,
|
||||
) {
|
||||
let ssrc: u32 = rand::random();
|
||||
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"voicemail_started",
|
||||
serde_json::json!({
|
||||
"call_id": call_id,
|
||||
"caller_number": caller_number,
|
||||
}),
|
||||
);
|
||||
|
||||
// Step 1: Play greeting.
|
||||
let mut next_seq: u16 = 0;
|
||||
let mut next_ts: u32 = 0;
|
||||
|
||||
if let Some(wav_path) = &greeting_wav {
|
||||
match play_wav_file(wav_path, rtp_socket.clone(), provider_media, codec_pt, ssrc).await {
|
||||
Ok(frames) => {
|
||||
next_seq = frames as u16;
|
||||
next_ts = frames * crate::rtp::rtp_clock_increment(codec_pt);
|
||||
}
|
||||
Err(e) => {
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"voicemail_error",
|
||||
serde_json::json!({ "call_id": call_id, "error": format!("greeting: {e}") }),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Play beep (1kHz, 500ms).
|
||||
match play_beep(
|
||||
rtp_socket.clone(),
|
||||
provider_media,
|
||||
codec_pt,
|
||||
ssrc,
|
||||
next_seq,
|
||||
next_ts,
|
||||
1000,
|
||||
500,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok((_seq, _ts)) => {}
|
||||
Err(e) => {
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"voicemail_error",
|
||||
serde_json::json!({ "call_id": call_id, "error": format!("beep: {e}") }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: Record incoming audio.
|
||||
let recorder = match Recorder::new(&recording_path, codec_pt, Some(max_recording_ms)) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"voicemail_error",
|
||||
serde_json::json!({ "call_id": call_id, "error": format!("recorder: {e}") }),
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Receive RTP and feed to recorder.
|
||||
let result = record_from_socket(rtp_socket, recorder, max_recording_ms).await;
|
||||
|
||||
// Step 4: Done — emit recording result.
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"recording_done",
|
||||
serde_json::json!({
|
||||
"call_id": call_id,
|
||||
"file_path": result.file_path,
|
||||
"duration_ms": result.duration_ms,
|
||||
"caller_number": caller_number,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/// Read RTP packets from the socket and feed them to the recorder.
|
||||
/// Returns when the socket errors out (BYE closes the call/socket)
|
||||
/// or max duration is reached.
|
||||
async fn record_from_socket(
|
||||
socket: Arc<UdpSocket>,
|
||||
mut recorder: Recorder,
|
||||
max_ms: u64,
|
||||
) -> crate::recorder::RecordingResult {
|
||||
let mut buf = vec![0u8; 65535];
|
||||
let deadline = tokio::time::Instant::now() + tokio::time::Duration::from_millis(max_ms + 2000);
|
||||
|
||||
loop {
|
||||
let timeout = tokio::time::timeout_at(deadline, socket.recv_from(&mut buf));
|
||||
match timeout.await {
|
||||
Ok(Ok((n, _addr))) => {
|
||||
if !recorder.process_rtp(&buf[..n]) {
|
||||
break; // Max duration reached.
|
||||
}
|
||||
}
|
||||
Ok(Err(_)) => break, // Socket error (closed).
|
||||
Err(_) => break, // Timeout (max duration + grace).
|
||||
}
|
||||
}
|
||||
|
||||
recorder.stop()
|
||||
}
|
||||
326
rust/crates/proxy-engine/src/webrtc_engine.rs
Normal file
326
rust/crates/proxy-engine/src/webrtc_engine.rs
Normal file
@@ -0,0 +1,326 @@
|
||||
//! WebRTC engine — manages browser PeerConnections.
|
||||
//!
|
||||
//! Audio bridging is now channel-based:
|
||||
//! - Browser Opus audio → on_track → mixer inbound channel
|
||||
//! - Mixer outbound channel → Opus RTP → TrackLocalStaticRTP → browser
|
||||
//!
|
||||
//! The mixer handles all transcoding. The WebRTC engine just shuttles raw Opus.
|
||||
|
||||
use crate::ipc::{emit_event, OutTx};
|
||||
use crate::mixer::RtpPacket;
|
||||
use codec_lib::PT_OPUS;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
use webrtc::api::media_engine::MediaEngine;
|
||||
use webrtc::api::APIBuilder;
|
||||
use webrtc::ice_transport::ice_candidate::RTCIceCandidateInit;
|
||||
use webrtc::peer_connection::configuration::RTCConfiguration;
|
||||
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
|
||||
use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;
|
||||
use webrtc::peer_connection::RTCPeerConnection;
|
||||
use webrtc::rtp_transceiver::rtp_codec::RTCRtpCodecCapability;
|
||||
use webrtc::track::track_local::track_local_static_rtp::TrackLocalStaticRTP;
|
||||
use webrtc::track::track_local::{TrackLocal, TrackLocalWriter};
|
||||
|
||||
/// A managed WebRTC session.
|
||||
struct WebRtcSession {
|
||||
pc: Arc<RTCPeerConnection>,
|
||||
local_track: Arc<TrackLocalStaticRTP>,
|
||||
call_id: Option<String>,
|
||||
/// Channel sender for forwarding browser Opus audio to the mixer.
|
||||
/// Set when the session is linked to a call via link_to_mixer().
|
||||
mixer_tx: Arc<Mutex<Option<mpsc::Sender<RtpPacket>>>>,
|
||||
}
|
||||
|
||||
/// Manages all WebRTC sessions.
|
||||
pub struct WebRtcEngine {
|
||||
sessions: HashMap<String, WebRtcSession>,
|
||||
out_tx: OutTx,
|
||||
}
|
||||
|
||||
impl WebRtcEngine {
|
||||
pub fn new(out_tx: OutTx) -> Self {
|
||||
Self {
|
||||
sessions: HashMap::new(),
|
||||
out_tx,
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle a WebRTC offer from a browser — create PeerConnection, return SDP answer.
|
||||
pub async fn handle_offer(
|
||||
&mut self,
|
||||
session_id: &str,
|
||||
offer_sdp: &str,
|
||||
) -> Result<String, String> {
|
||||
let mut media_engine = MediaEngine::default();
|
||||
media_engine
|
||||
.register_default_codecs()
|
||||
.map_err(|e| format!("register codecs: {e}"))?;
|
||||
|
||||
let api = APIBuilder::new()
|
||||
.with_media_engine(media_engine)
|
||||
.build();
|
||||
|
||||
let config = RTCConfiguration {
|
||||
ice_servers: vec![],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let pc = api
|
||||
.new_peer_connection(config)
|
||||
.await
|
||||
.map_err(|e| format!("create peer connection: {e}"))?;
|
||||
let pc = Arc::new(pc);
|
||||
|
||||
// Local audio track for sending audio to browser (Opus).
|
||||
let local_track = Arc::new(TrackLocalStaticRTP::new(
|
||||
RTCRtpCodecCapability {
|
||||
mime_type: "audio/opus".to_string(),
|
||||
clock_rate: 48000,
|
||||
channels: 1,
|
||||
..Default::default()
|
||||
},
|
||||
"audio".to_string(),
|
||||
"siprouter".to_string(),
|
||||
));
|
||||
|
||||
let _sender = pc
|
||||
.add_track(local_track.clone() as Arc<dyn TrackLocal + Send + Sync>)
|
||||
.await
|
||||
.map_err(|e| format!("add track: {e}"))?;
|
||||
|
||||
// Shared mixer channel sender (populated when linked to a call).
|
||||
let mixer_tx: Arc<Mutex<Option<mpsc::Sender<RtpPacket>>>> =
|
||||
Arc::new(Mutex::new(None));
|
||||
|
||||
// ICE candidate handler.
|
||||
let out_tx_ice = self.out_tx.clone();
|
||||
let sid_ice = session_id.to_string();
|
||||
pc.on_ice_candidate(Box::new(move |candidate| {
|
||||
let out_tx = out_tx_ice.clone();
|
||||
let sid = sid_ice.clone();
|
||||
Box::pin(async move {
|
||||
if let Some(c) = candidate {
|
||||
if let Ok(json) = c.to_json() {
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"webrtc_ice_candidate",
|
||||
serde_json::json!({
|
||||
"session_id": sid,
|
||||
"candidate": json.candidate,
|
||||
"sdp_mid": json.sdp_mid,
|
||||
"sdp_mline_index": json.sdp_mline_index,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
})
|
||||
}));
|
||||
|
||||
// Connection state handler.
|
||||
let out_tx_state = self.out_tx.clone();
|
||||
let sid_state = session_id.to_string();
|
||||
pc.on_peer_connection_state_change(Box::new(move |state| {
|
||||
let out_tx = out_tx_state.clone();
|
||||
let sid = sid_state.clone();
|
||||
Box::pin(async move {
|
||||
let state_str = match state {
|
||||
RTCPeerConnectionState::Connected => "connected",
|
||||
RTCPeerConnectionState::Disconnected => "disconnected",
|
||||
RTCPeerConnectionState::Failed => "failed",
|
||||
RTCPeerConnectionState::Closed => "closed",
|
||||
RTCPeerConnectionState::New => "new",
|
||||
RTCPeerConnectionState::Connecting => "connecting",
|
||||
_ => "unknown",
|
||||
};
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"webrtc_state",
|
||||
serde_json::json!({ "session_id": sid, "state": state_str }),
|
||||
);
|
||||
})
|
||||
}));
|
||||
|
||||
// Track handler — receives Opus audio from the browser.
|
||||
// Forwards raw Opus payload to the mixer channel (when linked).
|
||||
let out_tx_track = self.out_tx.clone();
|
||||
let sid_track = session_id.to_string();
|
||||
let mixer_tx_for_track = mixer_tx.clone();
|
||||
pc.on_track(Box::new(move |track, _receiver, _transceiver| {
|
||||
let out_tx = out_tx_track.clone();
|
||||
let sid = sid_track.clone();
|
||||
let mixer_tx = mixer_tx_for_track.clone();
|
||||
Box::pin(async move {
|
||||
let codec_info = track.codec();
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"webrtc_track",
|
||||
serde_json::json!({
|
||||
"session_id": sid,
|
||||
"kind": track.kind().to_string(),
|
||||
"codec": codec_info.capability.mime_type,
|
||||
}),
|
||||
);
|
||||
|
||||
// Spawn browser→mixer forwarding task.
|
||||
tokio::spawn(browser_to_mixer_loop(track, mixer_tx, out_tx, sid));
|
||||
})
|
||||
}));
|
||||
|
||||
// Set remote offer.
|
||||
let offer = RTCSessionDescription::offer(offer_sdp.to_string())
|
||||
.map_err(|e| format!("parse offer: {e}"))?;
|
||||
pc.set_remote_description(offer)
|
||||
.await
|
||||
.map_err(|e| format!("set remote description: {e}"))?;
|
||||
|
||||
// Create answer.
|
||||
let answer = pc
|
||||
.create_answer(None)
|
||||
.await
|
||||
.map_err(|e| format!("create answer: {e}"))?;
|
||||
let answer_sdp = answer.sdp.clone();
|
||||
pc.set_local_description(answer)
|
||||
.await
|
||||
.map_err(|e| format!("set local description: {e}"))?;
|
||||
|
||||
self.sessions.insert(
|
||||
session_id.to_string(),
|
||||
WebRtcSession {
|
||||
pc,
|
||||
local_track,
|
||||
call_id: None,
|
||||
mixer_tx,
|
||||
},
|
||||
);
|
||||
|
||||
Ok(answer_sdp)
|
||||
}
|
||||
|
||||
/// Link a WebRTC session to a call's mixer via channels.
|
||||
/// - `inbound_tx`: browser audio goes TO the mixer through this channel
|
||||
/// - `outbound_rx`: mixed audio comes FROM the mixer through this channel
|
||||
pub async fn link_to_mixer(
|
||||
&mut self,
|
||||
session_id: &str,
|
||||
call_id: &str,
|
||||
inbound_tx: mpsc::Sender<RtpPacket>,
|
||||
outbound_rx: mpsc::Receiver<Vec<u8>>,
|
||||
) -> bool {
|
||||
let session = match self.sessions.get_mut(session_id) {
|
||||
Some(s) => s,
|
||||
None => return false,
|
||||
};
|
||||
|
||||
session.call_id = Some(call_id.to_string());
|
||||
|
||||
// Set the mixer sender so the on_track loop starts forwarding.
|
||||
{
|
||||
let mut tx = session.mixer_tx.lock().await;
|
||||
*tx = Some(inbound_tx);
|
||||
}
|
||||
|
||||
// Spawn mixer→browser outbound task.
|
||||
let local_track = session.local_track.clone();
|
||||
tokio::spawn(mixer_to_browser_loop(outbound_rx, local_track));
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub async fn add_ice_candidate(
|
||||
&self,
|
||||
session_id: &str,
|
||||
candidate: &str,
|
||||
sdp_mid: Option<&str>,
|
||||
sdp_mline_index: Option<u16>,
|
||||
) -> Result<(), String> {
|
||||
let session = self
|
||||
.sessions
|
||||
.get(session_id)
|
||||
.ok_or_else(|| format!("session {session_id} not found"))?;
|
||||
|
||||
let init = RTCIceCandidateInit {
|
||||
candidate: candidate.to_string(),
|
||||
sdp_mid: sdp_mid.map(|s| s.to_string()),
|
||||
sdp_mline_index,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
session
|
||||
.pc
|
||||
.add_ice_candidate(init)
|
||||
.await
|
||||
.map_err(|e| format!("add ICE: {e}"))
|
||||
}
|
||||
|
||||
pub async fn close_session(&mut self, session_id: &str) -> Result<(), String> {
|
||||
if let Some(session) = self.sessions.remove(session_id) {
|
||||
session.pc.close().await.map_err(|e| format!("close: {e}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Browser → Mixer audio forwarding loop.
|
||||
/// Reads Opus RTP from the browser track, sends raw Opus payload to the mixer channel.
|
||||
async fn browser_to_mixer_loop(
|
||||
track: Arc<webrtc::track::track_remote::TrackRemote>,
|
||||
mixer_tx: Arc<Mutex<Option<mpsc::Sender<RtpPacket>>>>,
|
||||
out_tx: OutTx,
|
||||
session_id: String,
|
||||
) {
|
||||
let mut buf = vec![0u8; 1500];
|
||||
let mut count = 0u64;
|
||||
|
||||
loop {
|
||||
match track.read(&mut buf).await {
|
||||
Ok((rtp_packet, _attributes)) => {
|
||||
count += 1;
|
||||
|
||||
let payload = &rtp_packet.payload;
|
||||
if payload.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Send raw Opus payload to mixer (if linked).
|
||||
let tx = mixer_tx.lock().await;
|
||||
if let Some(ref tx) = *tx {
|
||||
let _ = tx
|
||||
.send(RtpPacket {
|
||||
payload: payload.to_vec(),
|
||||
payload_type: PT_OPUS,
|
||||
marker: false,
|
||||
timestamp: 0,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
drop(tx);
|
||||
|
||||
if count == 1 || count == 50 || count % 500 == 0 {
|
||||
emit_event(
|
||||
&out_tx,
|
||||
"webrtc_audio_rx",
|
||||
serde_json::json!({
|
||||
"session_id": session_id,
|
||||
"direction": "browser_to_mixer",
|
||||
"packet_count": count,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(_) => break, // Track ended.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Mixer → Browser audio forwarding loop.
|
||||
/// Reads Opus-encoded RTP packets from the mixer and writes to the WebRTC track.
|
||||
async fn mixer_to_browser_loop(
|
||||
mut outbound_rx: mpsc::Receiver<Vec<u8>>,
|
||||
local_track: Arc<TrackLocalStaticRTP>,
|
||||
) {
|
||||
while let Some(rtp_data) = outbound_rx.recv().await {
|
||||
let _ = local_track.write(&rtp_data).await;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: 'siprouter',
|
||||
version: '1.11.0',
|
||||
version: '1.15.0',
|
||||
description: 'undefined'
|
||||
}
|
||||
|
||||
@@ -14,9 +14,26 @@ import { execSync } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { Buffer } from 'node:buffer';
|
||||
import { buildRtpHeader, rtpClockIncrement } from './call/leg.ts';
|
||||
import { encodePcm, isCodecReady } from './opusbridge.ts';
|
||||
|
||||
/** RTP clock increment per 20ms frame for each codec. */
|
||||
function rtpClockIncrement(pt: number): number {
|
||||
if (pt === 111) return 960;
|
||||
if (pt === 9) return 160;
|
||||
return 160;
|
||||
}
|
||||
|
||||
/** Build a fresh RTP header. */
|
||||
function buildRtpHeader(pt: number, seq: number, ts: number, ssrc: number, marker: boolean): Buffer {
|
||||
const hdr = Buffer.alloc(12);
|
||||
hdr[0] = 0x80;
|
||||
hdr[1] = (marker ? 0x80 : 0) | (pt & 0x7f);
|
||||
hdr.writeUInt16BE(seq & 0xffff, 2);
|
||||
hdr.writeUInt32BE(ts >>> 0, 4);
|
||||
hdr.writeUInt32BE(ssrc >>> 0, 8);
|
||||
return hdr;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -1,323 +0,0 @@
|
||||
/**
|
||||
* Audio recorder — captures RTP packets from a single direction,
|
||||
* decodes them to PCM, and writes a WAV file.
|
||||
*
|
||||
* Uses the Rust codec bridge to transcode incoming audio (G.722, Opus,
|
||||
* PCMU, PCMA) to PCMU, then decodes mu-law to 16-bit PCM in TypeScript.
|
||||
* Output: 8kHz 16-bit mono WAV (standard telephony quality).
|
||||
*
|
||||
* Supports:
|
||||
* - Max recording duration limit
|
||||
* - Silence detection (stop after N seconds of silence)
|
||||
* - Manual stop
|
||||
* - DTMF packets (PT 101) are automatically skipped
|
||||
*/
|
||||
|
||||
import { Buffer } from 'node:buffer';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { WavWriter } from './wav-writer.ts';
|
||||
import type { IWavWriterResult } from './wav-writer.ts';
|
||||
import { transcode, createSession, destroySession } from '../opusbridge.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface IRecordingOptions {
|
||||
/** Output directory for WAV files. */
|
||||
outputDir: string;
|
||||
/** Target sample rate for the WAV output (default 8000). */
|
||||
sampleRate?: number;
|
||||
/** Maximum recording duration in seconds. 0 = unlimited. Default 120. */
|
||||
maxDurationSec?: number;
|
||||
/** Stop after this many consecutive seconds of silence. 0 = disabled. Default 5. */
|
||||
silenceTimeoutSec?: number;
|
||||
/** Silence threshold: max PCM amplitude below this is "silent". Default 200. */
|
||||
silenceThreshold?: number;
|
||||
/** Logging function. */
|
||||
log: (msg: string) => void;
|
||||
}
|
||||
|
||||
export interface IRecordingResult {
|
||||
/** Full path to the WAV file. */
|
||||
filePath: string;
|
||||
/** Duration in milliseconds. */
|
||||
durationMs: number;
|
||||
/** Sample rate of the WAV. */
|
||||
sampleRate: number;
|
||||
/** Size of the WAV file in bytes. */
|
||||
fileSize: number;
|
||||
/** Why the recording was stopped. */
|
||||
stopReason: TRecordingStopReason;
|
||||
}
|
||||
|
||||
export type TRecordingStopReason = 'manual' | 'max-duration' | 'silence' | 'cancelled';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mu-law decode table (ITU-T G.711)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Pre-computed mu-law → 16-bit linear PCM lookup table (256 entries). */
|
||||
const MULAW_DECODE: Int16Array = buildMulawDecodeTable();
|
||||
|
||||
function buildMulawDecodeTable(): Int16Array {
|
||||
const table = new Int16Array(256);
|
||||
for (let i = 0; i < 256; i++) {
|
||||
// Invert all bits per mu-law standard.
|
||||
let mu = ~i & 0xff;
|
||||
const sign = mu & 0x80;
|
||||
const exponent = (mu >> 4) & 0x07;
|
||||
const mantissa = mu & 0x0f;
|
||||
let magnitude = ((mantissa << 1) + 33) << (exponent + 2);
|
||||
magnitude -= 0x84; // Bias adjustment
|
||||
table[i] = sign ? -magnitude : magnitude;
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
/** Decode a PCMU payload to 16-bit LE PCM. */
|
||||
function decodeMulaw(mulaw: Buffer): Buffer {
|
||||
const pcm = Buffer.alloc(mulaw.length * 2);
|
||||
for (let i = 0; i < mulaw.length; i++) {
|
||||
pcm.writeInt16LE(MULAW_DECODE[mulaw[i]], i * 2);
|
||||
}
|
||||
return pcm;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// AudioRecorder
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export class AudioRecorder {
|
||||
/** Current state. */
|
||||
state: 'idle' | 'recording' | 'stopped' = 'idle';
|
||||
|
||||
/** Called when recording stops automatically (silence or max duration). */
|
||||
onStopped: ((result: IRecordingResult) => void) | null = null;
|
||||
|
||||
private outputDir: string;
|
||||
private sampleRate: number;
|
||||
private maxDurationSec: number;
|
||||
private silenceTimeoutSec: number;
|
||||
private silenceThreshold: number;
|
||||
private log: (msg: string) => void;
|
||||
|
||||
private wavWriter: WavWriter | null = null;
|
||||
private filePath: string = '';
|
||||
private codecSessionId: string | null = null;
|
||||
private stopReason: TRecordingStopReason = 'manual';
|
||||
|
||||
// Silence detection.
|
||||
private consecutiveSilentFrames = 0;
|
||||
/** Number of 20ms frames that constitute silence timeout. */
|
||||
private silenceFrameThreshold = 0;
|
||||
|
||||
// Max duration timer.
|
||||
private maxDurationTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
// Processing queue to avoid concurrent transcodes.
|
||||
private processQueue: Promise<void> = Promise.resolve();
|
||||
|
||||
constructor(options: IRecordingOptions) {
|
||||
this.outputDir = options.outputDir;
|
||||
this.sampleRate = options.sampleRate ?? 8000;
|
||||
this.maxDurationSec = options.maxDurationSec ?? 120;
|
||||
this.silenceTimeoutSec = options.silenceTimeoutSec ?? 5;
|
||||
this.silenceThreshold = options.silenceThreshold ?? 200;
|
||||
this.log = options.log;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start recording. Creates the output directory, WAV file, and codec session.
|
||||
* @param fileId - unique ID for the recording file name
|
||||
*/
|
||||
async start(fileId?: string): Promise<void> {
|
||||
if (this.state !== 'idle') return;
|
||||
|
||||
// Ensure output directory exists.
|
||||
if (!fs.existsSync(this.outputDir)) {
|
||||
fs.mkdirSync(this.outputDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Generate file path.
|
||||
const id = fileId ?? `rec-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
this.filePath = path.join(this.outputDir, `${id}.wav`);
|
||||
|
||||
// Create a codec session for isolated decoding.
|
||||
this.codecSessionId = `recorder-${id}`;
|
||||
await createSession(this.codecSessionId);
|
||||
|
||||
// Open WAV writer.
|
||||
this.wavWriter = new WavWriter({
|
||||
filePath: this.filePath,
|
||||
sampleRate: this.sampleRate,
|
||||
});
|
||||
this.wavWriter.open();
|
||||
|
||||
// Silence detection threshold: frames in timeout period.
|
||||
this.silenceFrameThreshold = this.silenceTimeoutSec > 0
|
||||
? Math.ceil((this.silenceTimeoutSec * 1000) / 20)
|
||||
: 0;
|
||||
this.consecutiveSilentFrames = 0;
|
||||
|
||||
// Max duration timer.
|
||||
if (this.maxDurationSec > 0) {
|
||||
this.maxDurationTimer = setTimeout(() => {
|
||||
if (this.state === 'recording') {
|
||||
this.stopReason = 'max-duration';
|
||||
this.log(`[recorder] max duration reached (${this.maxDurationSec}s)`);
|
||||
this.stop().then((result) => this.onStopped?.(result));
|
||||
}
|
||||
}, this.maxDurationSec * 1000);
|
||||
}
|
||||
|
||||
this.state = 'recording';
|
||||
this.stopReason = 'manual';
|
||||
this.log(`[recorder] started → ${this.filePath}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Feed an RTP packet. Strips the 12-byte header, transcodes the payload
|
||||
* to PCMU via the Rust bridge, decodes to PCM, and writes to WAV.
|
||||
* Skips telephone-event (DTMF) and comfort noise packets.
|
||||
*/
|
||||
processRtp(data: Buffer): void {
|
||||
if (this.state !== 'recording') return;
|
||||
if (data.length < 13) return; // too short
|
||||
|
||||
const pt = data[1] & 0x7f;
|
||||
|
||||
// Skip DTMF (telephone-event) and comfort noise.
|
||||
if (pt === 101 || pt === 13) return;
|
||||
|
||||
const payload = data.subarray(12);
|
||||
if (payload.length === 0) return;
|
||||
|
||||
// Queue processing to avoid concurrent transcodes corrupting codec state.
|
||||
this.processQueue = this.processQueue.then(() => this.decodeAndWrite(payload, pt));
|
||||
}
|
||||
|
||||
/** Decode a single RTP payload to PCM and write to WAV. */
|
||||
private async decodeAndWrite(payload: Buffer, pt: number): Promise<void> {
|
||||
if (this.state !== 'recording' || !this.wavWriter) return;
|
||||
|
||||
let pcm: Buffer;
|
||||
|
||||
if (pt === 0) {
|
||||
// PCMU: decode directly in TypeScript (no Rust round-trip needed).
|
||||
pcm = decodeMulaw(payload);
|
||||
} else {
|
||||
// All other codecs: transcode to PCMU via Rust, then decode mu-law.
|
||||
const mulaw = await transcode(payload, pt, 0, this.codecSessionId ?? undefined);
|
||||
if (!mulaw) return;
|
||||
pcm = decodeMulaw(mulaw);
|
||||
}
|
||||
|
||||
// Silence detection.
|
||||
if (this.silenceFrameThreshold > 0) {
|
||||
if (isSilent(pcm, this.silenceThreshold)) {
|
||||
this.consecutiveSilentFrames++;
|
||||
if (this.consecutiveSilentFrames >= this.silenceFrameThreshold) {
|
||||
this.stopReason = 'silence';
|
||||
this.log(`[recorder] silence detected (${this.silenceTimeoutSec}s)`);
|
||||
this.stop().then((result) => this.onStopped?.(result));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
this.consecutiveSilentFrames = 0;
|
||||
}
|
||||
}
|
||||
|
||||
this.wavWriter.write(pcm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop recording and finalize the WAV file.
|
||||
*/
|
||||
async stop(): Promise<IRecordingResult> {
|
||||
if (this.state === 'stopped' || this.state === 'idle') {
|
||||
return {
|
||||
filePath: this.filePath,
|
||||
durationMs: 0,
|
||||
sampleRate: this.sampleRate,
|
||||
fileSize: 0,
|
||||
stopReason: this.stopReason,
|
||||
};
|
||||
}
|
||||
|
||||
this.state = 'stopped';
|
||||
|
||||
// Wait for pending decode operations to finish.
|
||||
await this.processQueue;
|
||||
|
||||
// Clear timers.
|
||||
if (this.maxDurationTimer) {
|
||||
clearTimeout(this.maxDurationTimer);
|
||||
this.maxDurationTimer = null;
|
||||
}
|
||||
|
||||
// Finalize WAV.
|
||||
let wavResult: IWavWriterResult | null = null;
|
||||
if (this.wavWriter) {
|
||||
wavResult = this.wavWriter.close();
|
||||
this.wavWriter = null;
|
||||
}
|
||||
|
||||
// Destroy codec session.
|
||||
if (this.codecSessionId) {
|
||||
await destroySession(this.codecSessionId);
|
||||
this.codecSessionId = null;
|
||||
}
|
||||
|
||||
const result: IRecordingResult = {
|
||||
filePath: this.filePath,
|
||||
durationMs: wavResult?.durationMs ?? 0,
|
||||
sampleRate: this.sampleRate,
|
||||
fileSize: wavResult?.fileSize ?? 0,
|
||||
stopReason: this.stopReason,
|
||||
};
|
||||
|
||||
this.log(`[recorder] stopped (${result.stopReason}): ${result.durationMs}ms → ${this.filePath}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Cancel recording — stops and deletes the WAV file. */
|
||||
async cancel(): Promise<void> {
|
||||
this.stopReason = 'cancelled';
|
||||
await this.stop();
|
||||
|
||||
// Delete the incomplete file.
|
||||
try {
|
||||
if (fs.existsSync(this.filePath)) {
|
||||
fs.unlinkSync(this.filePath);
|
||||
this.log(`[recorder] cancelled — deleted ${this.filePath}`);
|
||||
}
|
||||
} catch { /* best effort */ }
|
||||
}
|
||||
|
||||
/** Clean up all resources. */
|
||||
destroy(): void {
|
||||
if (this.state === 'recording') {
|
||||
this.cancel();
|
||||
}
|
||||
this.onStopped = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Check if a PCM buffer is "silent" (max amplitude below threshold). */
|
||||
function isSilent(pcm: Buffer, threshold: number): boolean {
|
||||
let maxAmp = 0;
|
||||
for (let i = 0; i < pcm.length - 1; i += 2) {
|
||||
const sample = pcm.readInt16LE(i);
|
||||
const abs = sample < 0 ? -sample : sample;
|
||||
if (abs > maxAmp) maxAmp = abs;
|
||||
// Early exit: already above threshold.
|
||||
if (maxAmp >= threshold) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
265
ts/call/call.ts
265
ts/call/call.ts
@@ -1,265 +0,0 @@
|
||||
/**
|
||||
* Call — the hub entity in the hub model.
|
||||
*
|
||||
* A Call owns N legs and bridges their media. For 2-party calls, RTP packets
|
||||
* from leg A are forwarded to leg B and vice versa. For N>2 party calls,
|
||||
* packets from each leg are forwarded to all other legs (fan-out).
|
||||
*
|
||||
* Transcoding is applied per-leg when codecs differ.
|
||||
*/
|
||||
|
||||
import { Buffer } from 'node:buffer';
|
||||
import type { ILeg } from './leg.ts';
|
||||
import type { TCallState, TCallDirection, ICallStatus } from './types.ts';
|
||||
import { RtpPortPool } from './rtp-port-pool.ts';
|
||||
import type { SipLeg } from './sip-leg.ts';
|
||||
|
||||
export class Call {
|
||||
readonly id: string;
|
||||
state: TCallState = 'setting-up';
|
||||
direction: TCallDirection;
|
||||
readonly createdAt: number;
|
||||
|
||||
callerNumber: string | null = null;
|
||||
calleeNumber: string | null = null;
|
||||
providerUsed: string | null = null;
|
||||
|
||||
/** All legs in this call. */
|
||||
private legs = new Map<string, ILeg>();
|
||||
|
||||
/** Codec payload type for the "native" audio in the call (usually the first SIP leg's codec). */
|
||||
private nativeCodec: number | null = null;
|
||||
|
||||
/** Port pool reference for cleanup. */
|
||||
private portPool: RtpPortPool;
|
||||
private log: (msg: string) => void;
|
||||
private onChange: ((call: Call) => void) | null = null;
|
||||
|
||||
constructor(options: {
|
||||
id: string;
|
||||
direction: TCallDirection;
|
||||
portPool: RtpPortPool;
|
||||
log: (msg: string) => void;
|
||||
onChange?: (call: Call) => void;
|
||||
}) {
|
||||
this.id = options.id;
|
||||
this.direction = options.direction;
|
||||
this.createdAt = Date.now();
|
||||
this.portPool = options.portPool;
|
||||
this.log = options.log;
|
||||
this.onChange = options.onChange ?? null;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Leg management
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Add a leg to this call and wire up media forwarding. */
|
||||
addLeg(leg: ILeg): void {
|
||||
this.legs.set(leg.id, leg);
|
||||
|
||||
// Wire up RTP forwarding: when this leg receives a packet, forward to all other legs.
|
||||
leg.onRtpReceived = (data: Buffer) => {
|
||||
this.forwardRtp(leg.id, data);
|
||||
};
|
||||
|
||||
this.log(`[call:${this.id}] added leg ${leg.id} (${leg.type}), total=${this.legs.size}`);
|
||||
this.updateState();
|
||||
}
|
||||
|
||||
/** Remove a leg from this call, tear it down, and release its port. */
|
||||
removeLeg(legId: string): void {
|
||||
const leg = this.legs.get(legId);
|
||||
if (!leg) return;
|
||||
|
||||
leg.onRtpReceived = null;
|
||||
leg.teardown();
|
||||
if (leg.rtpPort) {
|
||||
this.portPool.release(leg.rtpPort);
|
||||
}
|
||||
this.legs.delete(legId);
|
||||
|
||||
this.log(`[call:${this.id}] removed leg ${legId}, total=${this.legs.size}`);
|
||||
this.updateState();
|
||||
}
|
||||
|
||||
getLeg(legId: string): ILeg | null {
|
||||
return this.legs.get(legId) ?? null;
|
||||
}
|
||||
|
||||
getLegs(): ILeg[] {
|
||||
return [...this.legs.values()];
|
||||
}
|
||||
|
||||
getLegByType(type: string): ILeg | null {
|
||||
for (const leg of this.legs.values()) {
|
||||
if (leg.type === type) return leg;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
getLegBySipCallId(sipCallId: string): ILeg | null {
|
||||
for (const leg of this.legs.values()) {
|
||||
if (leg.sipCallId === sipCallId) return leg;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
get legCount(): number {
|
||||
return this.legs.size;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Media forwarding (the hub)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private forwardRtp(fromLegId: string, data: Buffer): void {
|
||||
for (const [id, leg] of this.legs) {
|
||||
if (id === fromLegId) continue;
|
||||
if (leg.state !== 'connected') continue;
|
||||
|
||||
// For WebRTC legs, sendRtp calls forwardToBrowser which handles transcoding internally.
|
||||
// For SIP legs, forward the raw packet (same codec path) or let the leg handle it.
|
||||
// The Call hub does NOT transcode — that's the leg's responsibility.
|
||||
leg.sendRtp(data);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// State management
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private updateState(): void {
|
||||
if (this.state === 'terminated' || this.state === 'terminating') return;
|
||||
|
||||
const legs = [...this.legs.values()];
|
||||
if (legs.length === 0) {
|
||||
this.state = 'terminated';
|
||||
} else if (legs.every((l) => l.state === 'terminated')) {
|
||||
this.state = 'terminated';
|
||||
} else if (legs.some((l) => l.state === 'connected') && legs.filter((l) => l.state !== 'terminated').length >= 2) {
|
||||
// If a system leg is connected, report voicemail/ivr state for the dashboard.
|
||||
const systemLeg = legs.find((l) => l.type === 'system');
|
||||
if (systemLeg) {
|
||||
// Keep voicemail/ivr state if already set; otherwise set connected.
|
||||
if (this.state !== 'voicemail' && this.state !== 'ivr') {
|
||||
this.state = 'connected';
|
||||
}
|
||||
} else {
|
||||
this.state = 'connected';
|
||||
}
|
||||
} else if (legs.some((l) => l.state === 'ringing')) {
|
||||
this.state = 'ringing';
|
||||
} else {
|
||||
this.state = 'setting-up';
|
||||
}
|
||||
|
||||
this.onChange?.(this);
|
||||
}
|
||||
|
||||
/** Notify the call that a leg's state has changed. */
|
||||
notifyLegStateChange(_leg: ILeg): void {
|
||||
this.updateState();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Hangup
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Tear down all legs and terminate the call. */
|
||||
hangup(): void {
|
||||
if (this.state === 'terminated' || this.state === 'terminating') return;
|
||||
this.state = 'terminating';
|
||||
this.log(`[call:${this.id}] hanging up (${this.legs.size} legs)`);
|
||||
|
||||
for (const [id, leg] of this.legs) {
|
||||
// Send BYE/CANCEL for SIP legs (system legs have no SIP signaling).
|
||||
if (leg.type === 'sip-device' || leg.type === 'sip-provider') {
|
||||
(leg as SipLeg).sendHangup();
|
||||
}
|
||||
leg.teardown();
|
||||
if (leg.rtpPort) {
|
||||
this.portPool.release(leg.rtpPort);
|
||||
}
|
||||
}
|
||||
this.legs.clear();
|
||||
|
||||
this.state = 'terminated';
|
||||
this.onChange?.(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a BYE from one leg — tear down the other legs.
|
||||
* Called by CallManager when a SipLeg receives a BYE.
|
||||
*/
|
||||
handleLegTerminated(terminatedLegId: string): void {
|
||||
const terminatedLeg = this.legs.get(terminatedLegId);
|
||||
if (!terminatedLeg) return;
|
||||
|
||||
// Remove the terminated leg.
|
||||
terminatedLeg.onRtpReceived = null;
|
||||
if (terminatedLeg.rtpPort) {
|
||||
this.portPool.release(terminatedLeg.rtpPort);
|
||||
}
|
||||
this.legs.delete(terminatedLegId);
|
||||
|
||||
// If this is a 2-party call, hang up the other leg too.
|
||||
if (this.legs.size <= 1) {
|
||||
for (const [id, leg] of this.legs) {
|
||||
// Send BYE/CANCEL for SIP legs (system legs just get torn down).
|
||||
if (leg.type === 'sip-device' || leg.type === 'sip-provider') {
|
||||
(leg as SipLeg).sendHangup();
|
||||
}
|
||||
leg.teardown();
|
||||
if (leg.rtpPort) {
|
||||
this.portPool.release(leg.rtpPort);
|
||||
}
|
||||
}
|
||||
this.legs.clear();
|
||||
this.state = 'terminated';
|
||||
this.log(`[call:${this.id}] terminated`);
|
||||
this.onChange?.(this);
|
||||
} else {
|
||||
this.log(`[call:${this.id}] leg ${terminatedLegId} removed, ${this.legs.size} remaining`);
|
||||
this.updateState();
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Transfer
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Detach a leg from this call (without tearing it down).
|
||||
* The leg can then be added to another call.
|
||||
*/
|
||||
detachLeg(legId: string): ILeg | null {
|
||||
const leg = this.legs.get(legId);
|
||||
if (!leg) return null;
|
||||
|
||||
leg.onRtpReceived = null;
|
||||
this.legs.delete(legId);
|
||||
|
||||
this.log(`[call:${this.id}] detached leg ${legId}`);
|
||||
this.updateState();
|
||||
return leg;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Status
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
getStatus(): ICallStatus {
|
||||
return {
|
||||
id: this.id,
|
||||
state: this.state,
|
||||
direction: this.direction,
|
||||
callerNumber: this.callerNumber,
|
||||
calleeNumber: this.calleeNumber,
|
||||
providerUsed: this.providerUsed,
|
||||
createdAt: this.createdAt,
|
||||
duration: Math.floor((Date.now() - this.createdAt) / 1000),
|
||||
legs: [...this.legs.values()].map((l) => l.getStatus()),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,272 +0,0 @@
|
||||
/**
|
||||
* DTMF detection — parses RFC 2833 telephone-event RTP packets
|
||||
* and SIP INFO (application/dtmf-relay) messages.
|
||||
*
|
||||
* Designed to be attached to any leg or RTP stream. The detector
|
||||
* deduplicates repeated telephone-event packets (same digit is sent
|
||||
* multiple times with increasing duration) and fires a callback
|
||||
* once per detected digit.
|
||||
*/
|
||||
|
||||
import { Buffer } from 'node:buffer';
|
||||
import type { SipMessage } from '../sip/index.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** A single detected DTMF digit. */
|
||||
export interface IDtmfDigit {
|
||||
/** The digit character: '0'-'9', '*', '#', 'A'-'D'. */
|
||||
digit: string;
|
||||
/** Duration in milliseconds. */
|
||||
durationMs: number;
|
||||
/** Detection source. */
|
||||
source: 'rfc2833' | 'sip-info';
|
||||
/** Wall-clock timestamp when the digit was detected. */
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
/** Callback fired once per detected DTMF digit. */
|
||||
export type TDtmfCallback = (digit: IDtmfDigit) => void;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Constants
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** RFC 2833 event ID → character mapping. */
|
||||
const EVENT_CHARS: string[] = [
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'*', '#', 'A', 'B', 'C', 'D',
|
||||
];
|
||||
|
||||
/** Safety timeout: report digit if no End packet arrives within this many ms. */
|
||||
const SAFETY_TIMEOUT_MS = 200;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// DtmfDetector
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Detects DTMF digits from RFC 2833 RTP packets and SIP INFO messages.
|
||||
*
|
||||
* Usage:
|
||||
* ```
|
||||
* const detector = new DtmfDetector(log);
|
||||
* detector.onDigit = (d) => console.log('DTMF:', d.digit);
|
||||
* // Feed every RTP packet (detector checks PT internally):
|
||||
* detector.processRtp(rtpPacket);
|
||||
* // Or feed a SIP INFO message:
|
||||
* detector.processSipInfo(sipMsg);
|
||||
* ```
|
||||
*/
|
||||
export class DtmfDetector {
|
||||
/** Callback fired once per detected digit. */
|
||||
onDigit: TDtmfCallback | null = null;
|
||||
|
||||
/** Negotiated telephone-event payload type (default 101). */
|
||||
private telephoneEventPt: number;
|
||||
|
||||
/** Clock rate for duration calculation (default 8000 Hz). */
|
||||
private clockRate: number;
|
||||
|
||||
// -- Deduplication state for RFC 2833 --
|
||||
/** Event ID of the digit currently being received. */
|
||||
private currentEventId: number | null = null;
|
||||
/** RTP timestamp of the first packet for the current event. */
|
||||
private currentEventTs: number | null = null;
|
||||
/** Whether the current event has already been reported. */
|
||||
private currentEventReported = false;
|
||||
/** Latest duration value seen (in clock ticks). */
|
||||
private currentEventDuration = 0;
|
||||
/** Latest volume value seen (dBm0, 0 = loudest). */
|
||||
private currentEventVolume = 0;
|
||||
/** Safety timer: fires if no End packet arrives. */
|
||||
private safetyTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
private log: (msg: string) => void;
|
||||
|
||||
constructor(
|
||||
log: (msg: string) => void,
|
||||
telephoneEventPt = 101,
|
||||
clockRate = 8000,
|
||||
) {
|
||||
this.log = log;
|
||||
this.telephoneEventPt = telephoneEventPt;
|
||||
this.clockRate = clockRate;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// RFC 2833 RTP processing
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Feed an RTP packet. Checks PT; ignores non-DTMF packets.
|
||||
* Expects the full RTP packet (12-byte header + payload).
|
||||
*/
|
||||
processRtp(data: Buffer): void {
|
||||
if (data.length < 16) return; // 12-byte header + 4-byte telephone-event payload minimum
|
||||
|
||||
const pt = data[1] & 0x7f;
|
||||
if (pt !== this.telephoneEventPt) return;
|
||||
|
||||
// Parse RTP header fields we need.
|
||||
const marker = (data[1] & 0x80) !== 0;
|
||||
const rtpTimestamp = data.readUInt32BE(4);
|
||||
|
||||
// Parse telephone-event payload (4 bytes starting at offset 12).
|
||||
const eventId = data[12];
|
||||
const endBit = (data[13] & 0x80) !== 0;
|
||||
const volume = data[13] & 0x3f;
|
||||
const duration = data.readUInt16BE(14);
|
||||
|
||||
// Validate event ID.
|
||||
if (eventId >= EVENT_CHARS.length) return;
|
||||
|
||||
// Detect new event: marker bit, different event ID, or different RTP timestamp.
|
||||
const isNewEvent =
|
||||
marker ||
|
||||
eventId !== this.currentEventId ||
|
||||
rtpTimestamp !== this.currentEventTs;
|
||||
|
||||
if (isNewEvent) {
|
||||
// If there was an unreported previous event, report it now (fallback).
|
||||
this.reportPendingEvent();
|
||||
|
||||
// Start tracking the new event.
|
||||
this.currentEventId = eventId;
|
||||
this.currentEventTs = rtpTimestamp;
|
||||
this.currentEventReported = false;
|
||||
this.currentEventDuration = duration;
|
||||
this.currentEventVolume = volume;
|
||||
|
||||
// Start safety timer.
|
||||
this.clearSafetyTimer();
|
||||
this.safetyTimer = setTimeout(() => {
|
||||
this.reportPendingEvent();
|
||||
}, SAFETY_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
// Update duration (it increases with each retransmission).
|
||||
if (duration > this.currentEventDuration) {
|
||||
this.currentEventDuration = duration;
|
||||
}
|
||||
|
||||
// Report on End bit (first time only).
|
||||
if (endBit && !this.currentEventReported) {
|
||||
this.currentEventReported = true;
|
||||
this.clearSafetyTimer();
|
||||
|
||||
const digit = EVENT_CHARS[eventId];
|
||||
const durationMs = (this.currentEventDuration / this.clockRate) * 1000;
|
||||
|
||||
this.log(`[dtmf] RFC 2833 digit '${digit}' (${Math.round(durationMs)}ms)`);
|
||||
this.onDigit?.({
|
||||
digit,
|
||||
durationMs,
|
||||
source: 'rfc2833',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Report a pending (unreported) event — called by safety timer or on new event start. */
|
||||
private reportPendingEvent(): void {
|
||||
if (
|
||||
this.currentEventId !== null &&
|
||||
!this.currentEventReported &&
|
||||
this.currentEventId < EVENT_CHARS.length
|
||||
) {
|
||||
this.currentEventReported = true;
|
||||
this.clearSafetyTimer();
|
||||
|
||||
const digit = EVENT_CHARS[this.currentEventId];
|
||||
const durationMs = (this.currentEventDuration / this.clockRate) * 1000;
|
||||
|
||||
this.log(`[dtmf] RFC 2833 digit '${digit}' (${Math.round(durationMs)}ms, safety timeout)`);
|
||||
this.onDigit?.({
|
||||
digit,
|
||||
durationMs,
|
||||
source: 'rfc2833',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private clearSafetyTimer(): void {
|
||||
if (this.safetyTimer) {
|
||||
clearTimeout(this.safetyTimer);
|
||||
this.safetyTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// SIP INFO processing
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Parse a SIP INFO message carrying DTMF.
|
||||
* Supports Content-Type: application/dtmf-relay (Signal=X / Duration=Y).
|
||||
*/
|
||||
processSipInfo(msg: SipMessage): void {
|
||||
const ct = (msg.getHeader('Content-Type') || '').toLowerCase();
|
||||
if (!ct.includes('application/dtmf-relay') && !ct.includes('application/dtmf')) return;
|
||||
|
||||
const body = msg.body || '';
|
||||
|
||||
if (ct.includes('application/dtmf-relay')) {
|
||||
// Format: "Signal= 5\r\nDuration= 160\r\n"
|
||||
const signalMatch = body.match(/Signal\s*=\s*(\S+)/i);
|
||||
const durationMatch = body.match(/Duration\s*=\s*(\d+)/i);
|
||||
if (!signalMatch) return;
|
||||
|
||||
const signal = signalMatch[1];
|
||||
const durationTicks = durationMatch ? parseInt(durationMatch[1], 10) : 160;
|
||||
|
||||
// Validate digit.
|
||||
if (signal.length !== 1 || !/[0-9*#A-Da-d]/.test(signal)) return;
|
||||
const digit = signal.toUpperCase();
|
||||
const durationMs = (durationTicks / this.clockRate) * 1000;
|
||||
|
||||
this.log(`[dtmf] SIP INFO digit '${digit}' (${Math.round(durationMs)}ms)`);
|
||||
this.onDigit?.({
|
||||
digit,
|
||||
durationMs,
|
||||
source: 'sip-info',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
} else if (ct.includes('application/dtmf')) {
|
||||
// Simple format: just the digit character in the body.
|
||||
const digit = body.trim().toUpperCase();
|
||||
if (digit.length !== 1 || !/[0-9*#A-D]/.test(digit)) return;
|
||||
|
||||
this.log(`[dtmf] SIP INFO digit '${digit}' (application/dtmf)`);
|
||||
this.onDigit?.({
|
||||
digit,
|
||||
durationMs: 250, // default duration
|
||||
source: 'sip-info',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Lifecycle
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Reset detection state (e.g., between calls). */
|
||||
reset(): void {
|
||||
this.currentEventId = null;
|
||||
this.currentEventTs = null;
|
||||
this.currentEventReported = false;
|
||||
this.currentEventDuration = 0;
|
||||
this.currentEventVolume = 0;
|
||||
this.clearSafetyTimer();
|
||||
}
|
||||
|
||||
/** Clean up timers and references. */
|
||||
destroy(): void {
|
||||
this.clearSafetyTimer();
|
||||
this.onDigit = null;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
export type { TCallState, TLegState, TLegType, TCallDirection, ICallStatus, ILegStatus, ICallHistoryEntry } from './types.ts';
|
||||
export type { ILeg } from './leg.ts';
|
||||
export { rtpClockIncrement, buildRtpHeader, codecDisplayName } from './leg.ts';
|
||||
export { RtpPortPool } from './rtp-port-pool.ts';
|
||||
export type { IRtpAllocation } from './rtp-port-pool.ts';
|
||||
export { SipLeg } from './sip-leg.ts';
|
||||
export type { ISipLegConfig } from './sip-leg.ts';
|
||||
export { WebRtcLeg } from './webrtc-leg.ts';
|
||||
export type { IWebRtcLegConfig } from './webrtc-leg.ts';
|
||||
export { Call } from './call.ts';
|
||||
export { CallManager } from './call-manager.ts';
|
||||
export type { ICallManagerConfig } from './call-manager.ts';
|
||||
104
ts/call/leg.ts
104
ts/call/leg.ts
@@ -1,104 +0,0 @@
|
||||
/**
|
||||
* ILeg interface — abstract connection from a Call hub to an endpoint.
|
||||
*
|
||||
* Concrete implementations: SipLeg (SIP devices + providers) and WebRtcLeg (browsers).
|
||||
* Shared RTP utilities (header building, clock rates) are also defined here.
|
||||
*/
|
||||
|
||||
import { Buffer } from 'node:buffer';
|
||||
import type dgram from 'node:dgram';
|
||||
import type { IEndpoint } from '../sip/index.ts';
|
||||
import type { TLegState, TLegType, ILegStatus } from './types.ts';
|
||||
import type { IRtpTranscoder } from '../codec.ts';
|
||||
import type { SipDialog } from '../sip/index.ts';
|
||||
import type { SipMessage } from '../sip/index.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ILeg interface
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface ILeg {
|
||||
readonly id: string;
|
||||
readonly type: TLegType;
|
||||
state: TLegState;
|
||||
|
||||
/** The SIP Call-ID used by this leg (for CallManager routing). */
|
||||
readonly sipCallId: string;
|
||||
|
||||
/** Where this leg sends/receives RTP. */
|
||||
readonly rtpPort: number | null;
|
||||
readonly rtpSock: dgram.Socket | null;
|
||||
remoteMedia: IEndpoint | null;
|
||||
|
||||
/** Negotiated codec payload type (e.g. 9 = G.722, 111 = Opus). */
|
||||
codec: number | null;
|
||||
|
||||
/** Transcoder for converting to this leg's codec (set by Call when codecs differ). */
|
||||
transcoder: IRtpTranscoder | null;
|
||||
|
||||
/** Packet counters. */
|
||||
pktSent: number;
|
||||
pktReceived: number;
|
||||
|
||||
/** SIP dialog (SipLegs only, null for WebRtcLegs). */
|
||||
readonly dialog: SipDialog | null;
|
||||
|
||||
/**
|
||||
* Send an RTP packet toward this leg's remote endpoint.
|
||||
* If a transcoder is set, the Call should transcode before calling this.
|
||||
*/
|
||||
sendRtp(data: Buffer): void;
|
||||
|
||||
/**
|
||||
* Callback set by the owning Call — invoked when this leg receives an RTP packet.
|
||||
* The Call uses this to forward to other legs.
|
||||
*/
|
||||
onRtpReceived: ((data: Buffer) => void) | null;
|
||||
|
||||
/**
|
||||
* Handle an incoming SIP message routed to this leg (SipLegs only).
|
||||
* Returns a SipMessage response if one needs to be sent, or null.
|
||||
*/
|
||||
handleSipMessage(msg: SipMessage, rinfo: IEndpoint): void;
|
||||
|
||||
/** Release all resources (sockets, peer connections, etc.). */
|
||||
teardown(): void;
|
||||
|
||||
/** Status snapshot for the dashboard. */
|
||||
getStatus(): ILegStatus;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Shared RTP utilities
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** RTP clock increment per 20ms frame for each codec. */
|
||||
export function rtpClockIncrement(pt: number): number {
|
||||
if (pt === 111) return 960; // Opus: 48000 Hz x 0.02s
|
||||
if (pt === 9) return 160; // G.722: 8000 Hz x 0.02s (SDP clock rate quirk)
|
||||
return 160; // PCMU/PCMA: 8000 Hz x 0.02s
|
||||
}
|
||||
|
||||
/** Build a fresh RTP header with correct PT, timestamp, seq, SSRC. */
|
||||
export function buildRtpHeader(pt: number, seq: number, ts: number, ssrc: number, marker: boolean): Buffer {
|
||||
const hdr = Buffer.alloc(12);
|
||||
hdr[0] = 0x80; // V=2
|
||||
hdr[1] = (marker ? 0x80 : 0) | (pt & 0x7f);
|
||||
hdr.writeUInt16BE(seq & 0xffff, 2);
|
||||
hdr.writeUInt32BE(ts >>> 0, 4);
|
||||
hdr.writeUInt32BE(ssrc >>> 0, 8);
|
||||
return hdr;
|
||||
}
|
||||
|
||||
/** Codec name for status display. */
|
||||
export function codecDisplayName(pt: number | null): string | null {
|
||||
if (pt === null) return null;
|
||||
switch (pt) {
|
||||
case 0: return 'PCMU';
|
||||
case 8: return 'PCMA';
|
||||
case 9: return 'G.722';
|
||||
case 111: return 'Opus';
|
||||
case 101: return 'telephone-event';
|
||||
default: return `PT${pt}`;
|
||||
}
|
||||
}
|
||||
@@ -17,9 +17,26 @@ import { execSync } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { Buffer } from 'node:buffer';
|
||||
import { buildRtpHeader, rtpClockIncrement } from './leg.ts';
|
||||
import { encodePcm, isCodecReady } from '../opusbridge.ts';
|
||||
|
||||
/** RTP clock increment per 20ms frame for each codec. */
|
||||
function rtpClockIncrement(pt: number): number {
|
||||
if (pt === 111) return 960;
|
||||
if (pt === 9) return 160;
|
||||
return 160;
|
||||
}
|
||||
|
||||
/** Build a fresh RTP header. */
|
||||
function buildRtpHeader(pt: number, seq: number, ts: number, ssrc: number, marker: boolean): Buffer {
|
||||
const hdr = Buffer.alloc(12);
|
||||
hdr[0] = 0x80;
|
||||
hdr[1] = (marker ? 0x80 : 0) | (pt & 0x7f);
|
||||
hdr.writeUInt16BE(seq & 0xffff, 2);
|
||||
hdr.writeUInt32BE(ts >>> 0, 4);
|
||||
hdr.writeUInt32BE(ssrc >>> 0, 8);
|
||||
return hdr;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
/**
|
||||
* Unified RTP port pool — replaces the three separate allocators
|
||||
* in sipproxy.ts, calloriginator.ts, and webrtcbridge.ts.
|
||||
*
|
||||
* Allocates even-numbered UDP ports from a configured range.
|
||||
* Each allocation binds a dgram socket and returns it ready to use.
|
||||
*/
|
||||
|
||||
import dgram from 'node:dgram';
|
||||
|
||||
export interface IRtpAllocation {
|
||||
port: number;
|
||||
sock: dgram.Socket;
|
||||
}
|
||||
|
||||
export class RtpPortPool {
|
||||
private min: number;
|
||||
private max: number;
|
||||
private allocated = new Map<number, dgram.Socket>();
|
||||
private log: (msg: string) => void;
|
||||
|
||||
constructor(min: number, max: number, log: (msg: string) => void) {
|
||||
this.min = min % 2 === 0 ? min : min + 1; // ensure even start
|
||||
this.max = max;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate an even-numbered port and bind a UDP socket to it.
|
||||
* Returns null if the pool is exhausted.
|
||||
*/
|
||||
allocate(): IRtpAllocation | null {
|
||||
for (let port = this.min; port < this.max; port += 2) {
|
||||
if (this.allocated.has(port)) continue;
|
||||
|
||||
const sock = dgram.createSocket('udp4');
|
||||
try {
|
||||
sock.bind(port, '0.0.0.0');
|
||||
} catch {
|
||||
try { sock.close(); } catch { /* ignore */ }
|
||||
continue;
|
||||
}
|
||||
this.allocated.set(port, sock);
|
||||
this.log(`[rtp-pool] allocated port ${port} (${this.allocated.size} in use)`);
|
||||
return { port, sock };
|
||||
}
|
||||
this.log('[rtp-pool] WARN: port pool exhausted');
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release a port back to the pool and close its socket.
|
||||
*/
|
||||
release(port: number): void {
|
||||
const sock = this.allocated.get(port);
|
||||
if (!sock) return;
|
||||
try { sock.close(); } catch { /* ignore */ }
|
||||
this.allocated.delete(port);
|
||||
this.log(`[rtp-pool] released port ${port} (${this.allocated.size} in use)`);
|
||||
}
|
||||
|
||||
/** Number of currently allocated ports. */
|
||||
get size(): number {
|
||||
return this.allocated.size;
|
||||
}
|
||||
|
||||
/** Total capacity (number of even ports in range). */
|
||||
get capacity(): number {
|
||||
return Math.floor((this.max - this.min) / 2);
|
||||
}
|
||||
}
|
||||
@@ -1,633 +0,0 @@
|
||||
/**
|
||||
* SipLeg — a SIP connection from the Call hub to a device or provider.
|
||||
*
|
||||
* Wraps a SipDialog and an RTP socket. Handles:
|
||||
* - INVITE/ACK/BYE/CANCEL lifecycle
|
||||
* - SDP rewriting (LAN IP for devices, public IP for providers)
|
||||
* - Digest auth for provider legs (407/401)
|
||||
* - Early-media silence for providers with quirks
|
||||
* - Record-Route insertion for dialog-establishing requests
|
||||
*/
|
||||
|
||||
import dgram from 'node:dgram';
|
||||
import { Buffer } from 'node:buffer';
|
||||
import {
|
||||
SipMessage,
|
||||
SipDialog,
|
||||
buildSdp,
|
||||
parseSdpEndpoint,
|
||||
rewriteSdp,
|
||||
rewriteSipUri,
|
||||
parseDigestChallenge,
|
||||
computeDigestAuth,
|
||||
generateTag,
|
||||
} from '../sip/index.ts';
|
||||
import type { IEndpoint } from '../sip/index.ts';
|
||||
import type { IProviderConfig, IQuirks } from '../config.ts';
|
||||
import type { TLegState, TLegType, ILegStatus } from './types.ts';
|
||||
import type { ILeg } from './leg.ts';
|
||||
import { codecDisplayName } from './leg.ts';
|
||||
import type { IRtpTranscoder } from '../codec.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SipLeg config
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface ISipLegConfig {
|
||||
/** Whether this leg faces a device (LAN) or a provider (WAN). */
|
||||
role: 'device' | 'provider';
|
||||
|
||||
/** Proxy LAN IP (for SDP rewriting toward devices). */
|
||||
lanIp: string;
|
||||
/** Proxy LAN port (for Via, Contact, Record-Route). */
|
||||
lanPort: number;
|
||||
|
||||
/** Public IP (for SDP rewriting toward providers). */
|
||||
getPublicIp: () => string | null;
|
||||
|
||||
/** Send a SIP message via the main UDP socket. */
|
||||
sendSip: (buf: Buffer, dest: IEndpoint) => void;
|
||||
/** Logging function. */
|
||||
log: (msg: string) => void;
|
||||
|
||||
/** Provider config (for provider legs: auth, codecs, quirks, outbound proxy). */
|
||||
provider?: IProviderConfig;
|
||||
|
||||
/** The endpoint to send SIP messages to (device address or provider outbound proxy). */
|
||||
sipTarget: IEndpoint;
|
||||
|
||||
/** RTP port and socket (pre-allocated from the pool). */
|
||||
rtpPort: number;
|
||||
rtpSock: dgram.Socket;
|
||||
|
||||
/** Payload types to offer in SDP. */
|
||||
payloadTypes?: number[];
|
||||
|
||||
/** Registered AOR (for From header in provider leg). */
|
||||
getRegisteredAor?: () => string | null;
|
||||
/** SIP password (for digest auth). */
|
||||
getSipPassword?: () => string | null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SipLeg
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export class SipLeg implements ILeg {
|
||||
readonly id: string;
|
||||
readonly type: TLegType;
|
||||
state: TLegState = 'inviting';
|
||||
readonly config: ISipLegConfig;
|
||||
|
||||
/** The SIP dialog for this leg. */
|
||||
dialog: SipDialog | null = null;
|
||||
|
||||
/** Original INVITE (needed for CANCEL). */
|
||||
invite: SipMessage | null = null;
|
||||
|
||||
/** Original unauthenticated INVITE (for re-ACKing retransmitted 407s). */
|
||||
private origInvite: SipMessage | null = null;
|
||||
|
||||
/** Whether we've attempted digest auth on this leg. */
|
||||
private authAttempted = false;
|
||||
|
||||
/** RTP socket and port. */
|
||||
readonly rtpPort: number;
|
||||
readonly rtpSock: dgram.Socket;
|
||||
|
||||
/** Remote media endpoint (learned from SDP). */
|
||||
remoteMedia: IEndpoint | null = null;
|
||||
|
||||
/** Negotiated codec. */
|
||||
codec: number | null = null;
|
||||
|
||||
/** Transcoder (set by Call when codecs differ between legs). */
|
||||
transcoder: IRtpTranscoder | null = null;
|
||||
|
||||
/** Stable SSRC for this leg (used for silence + forwarded audio). */
|
||||
readonly ssrc: number = (Math.random() * 0xffffffff) >>> 0;
|
||||
|
||||
/** Packet counters. */
|
||||
pktSent = 0;
|
||||
pktReceived = 0;
|
||||
|
||||
/** Callback set by Call to receive RTP. */
|
||||
onRtpReceived: ((data: Buffer) => void) | null = null;
|
||||
|
||||
/** Silence stream timer (for provider quirks). */
|
||||
private silenceTimer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
/** Callbacks for lifecycle events. */
|
||||
onStateChange: ((leg: SipLeg) => void) | null = null;
|
||||
onConnected: ((leg: SipLeg) => void) | null = null;
|
||||
onTerminated: ((leg: SipLeg) => void) | null = null;
|
||||
|
||||
/** Callback for SIP INFO messages (used for DTMF relay). */
|
||||
onInfoReceived: ((msg: SipMessage) => void) | null = null;
|
||||
|
||||
constructor(id: string, config: ISipLegConfig) {
|
||||
this.id = id;
|
||||
this.type = config.role === 'device' ? 'sip-device' : 'sip-provider';
|
||||
this.config = config;
|
||||
this.rtpPort = config.rtpPort;
|
||||
this.rtpSock = config.rtpSock;
|
||||
|
||||
// Set up RTP receive handler.
|
||||
this.rtpSock.on('message', (data: Buffer, rinfo: dgram.RemoteInfo) => {
|
||||
this.pktReceived++;
|
||||
|
||||
// Learn remote media endpoint from first packet if not yet known.
|
||||
if (!this.remoteMedia) {
|
||||
this.remoteMedia = { address: rinfo.address, port: rinfo.port };
|
||||
this.config.log(`[sip-leg:${this.id}] learned remote media: ${rinfo.address}:${rinfo.port}`);
|
||||
}
|
||||
|
||||
// Forward to the Call hub.
|
||||
if (this.onRtpReceived) {
|
||||
this.onRtpReceived(data);
|
||||
}
|
||||
});
|
||||
|
||||
this.rtpSock.on('error', (e: Error) => {
|
||||
this.config.log(`[sip-leg:${this.id}] rtp error: ${e.message}`);
|
||||
});
|
||||
}
|
||||
|
||||
get sipCallId(): string {
|
||||
return this.dialog?.callId || 'no-dialog';
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Outbound INVITE (B2BUA mode — create a new dialog)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Send an INVITE to establish this leg.
|
||||
* Creates a new SipDialog (UAC side).
|
||||
*/
|
||||
sendInvite(options: {
|
||||
fromUri: string;
|
||||
toUri: string;
|
||||
callId: string;
|
||||
fromTag?: string;
|
||||
fromDisplayName?: string;
|
||||
cseq?: number;
|
||||
extraHeaders?: [string, string][];
|
||||
}): void {
|
||||
const ip = this.type === 'sip-provider'
|
||||
? (this.config.getPublicIp() || this.config.lanIp)
|
||||
: this.config.lanIp;
|
||||
const pts = this.config.payloadTypes || [9, 0, 8, 101];
|
||||
|
||||
const sdp = buildSdp({ ip, port: this.rtpPort, payloadTypes: pts });
|
||||
|
||||
const invite = SipMessage.createRequest('INVITE', options.toUri, {
|
||||
via: { host: ip, port: this.config.lanPort },
|
||||
from: { uri: options.fromUri, displayName: options.fromDisplayName, tag: options.fromTag },
|
||||
to: { uri: options.toUri },
|
||||
callId: options.callId,
|
||||
cseq: options.cseq,
|
||||
contact: `<sip:${ip}:${this.config.lanPort}>`,
|
||||
body: sdp,
|
||||
contentType: 'application/sdp',
|
||||
extraHeaders: options.extraHeaders,
|
||||
});
|
||||
|
||||
this.invite = invite;
|
||||
this.dialog = SipDialog.fromUacInvite(invite, ip, this.config.lanPort);
|
||||
this.state = 'inviting';
|
||||
|
||||
this.config.log(`[sip-leg:${this.id}] INVITE -> ${this.config.sipTarget.address}:${this.config.sipTarget.port}`);
|
||||
this.config.sendSip(invite.serialize(), this.config.sipTarget);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Passthrough mode — forward a SIP message with rewriting
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Accept an incoming INVITE as a UAS (for passthrough inbound calls).
|
||||
* Creates a SipDialog on the UAS side.
|
||||
*/
|
||||
acceptIncoming(invite: SipMessage): void {
|
||||
const localTag = generateTag();
|
||||
this.dialog = SipDialog.fromUasInvite(invite, localTag, this.config.lanIp, this.config.lanPort);
|
||||
this.invite = invite;
|
||||
this.state = 'inviting';
|
||||
|
||||
// Learn remote media from SDP.
|
||||
if (invite.hasSdpBody) {
|
||||
const ep = parseSdpEndpoint(invite.body);
|
||||
if (ep) {
|
||||
this.remoteMedia = ep;
|
||||
this.config.log(`[sip-leg:${this.id}] media from SDP: ${ep.address}:${ep.port}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward a SIP message through this leg with SDP rewriting.
|
||||
* Used for passthrough calls where the proxy relays messages.
|
||||
*/
|
||||
forwardMessage(msg: SipMessage, dest: IEndpoint): void {
|
||||
const rewriteIp = this.type === 'sip-provider'
|
||||
? (this.config.getPublicIp() || this.config.lanIp)
|
||||
: this.config.lanIp;
|
||||
|
||||
// Rewrite SDP if present.
|
||||
if (msg.hasSdpBody) {
|
||||
const { body, original } = rewriteSdp(msg.body, rewriteIp, this.rtpPort);
|
||||
msg.body = body;
|
||||
msg.updateContentLength();
|
||||
if (original) {
|
||||
this.remoteMedia = original;
|
||||
this.config.log(`[sip-leg:${this.id}] media from SDP rewrite: ${original.address}:${original.port}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Record-Route for dialog-establishing requests.
|
||||
if (msg.isRequest && msg.isDialogEstablishing) {
|
||||
msg.prependHeader('Record-Route', `<sip:${this.config.lanIp}:${this.config.lanPort};lr>`);
|
||||
}
|
||||
|
||||
// Rewrite Contact.
|
||||
if (this.type === 'sip-provider') {
|
||||
const contact = msg.getHeader('Contact');
|
||||
if (contact) {
|
||||
const nc = rewriteSipUri(contact, rewriteIp, this.config.lanPort);
|
||||
if (nc !== contact) msg.setHeader('Contact', nc);
|
||||
}
|
||||
}
|
||||
|
||||
// Rewrite Request-URI for inbound messages going to device.
|
||||
if (this.type === 'sip-device' && msg.isRequest) {
|
||||
msg.setRequestUri(rewriteSipUri(msg.requestUri!, dest.address, dest.port));
|
||||
}
|
||||
|
||||
this.config.sendSip(msg.serialize(), dest);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// SIP message handling (routed by CallManager)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
handleSipMessage(msg: SipMessage, rinfo: IEndpoint): void {
|
||||
if (msg.isResponse) {
|
||||
this.handleResponse(msg, rinfo);
|
||||
} else {
|
||||
this.handleRequest(msg, rinfo);
|
||||
}
|
||||
}
|
||||
|
||||
private handleResponse(msg: SipMessage, _rinfo: IEndpoint): void {
|
||||
const code = msg.statusCode ?? 0;
|
||||
const method = msg.cseqMethod?.toUpperCase();
|
||||
|
||||
this.config.log(`[sip-leg:${this.id}] <- ${code} (${method})`);
|
||||
|
||||
if (method === 'INVITE') {
|
||||
this.handleInviteResponse(msg, code);
|
||||
}
|
||||
// BYE/CANCEL responses don't need action beyond logging.
|
||||
}
|
||||
|
||||
private handleInviteResponse(msg: SipMessage, code: number): void {
|
||||
// Handle retransmitted 407 for the original unauthenticated INVITE.
|
||||
if (this.authAttempted && this.dialog) {
|
||||
const responseCSeqNum = parseInt((msg.getHeader('CSeq') || '').split(/\s+/)[0], 10);
|
||||
if (responseCSeqNum < this.dialog.localCSeq && code >= 400) {
|
||||
if (this.origInvite) {
|
||||
const ack = buildNon2xxAck(this.origInvite, msg);
|
||||
this.config.sendSip(ack.serialize(), this.config.sipTarget);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle 407 Proxy Authentication Required.
|
||||
if (code === 407 && this.type === 'sip-provider') {
|
||||
this.handleAuthChallenge(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
// Update dialog state.
|
||||
if (this.dialog) {
|
||||
this.dialog.processResponse(msg);
|
||||
}
|
||||
|
||||
if (code === 180 || code === 183) {
|
||||
this.state = 'ringing';
|
||||
this.onStateChange?.(this);
|
||||
} else if (code >= 200 && code < 300) {
|
||||
// ACK the 200 OK.
|
||||
if (this.dialog) {
|
||||
const ack = this.dialog.createAck();
|
||||
this.config.sendSip(ack.serialize(), this.config.sipTarget);
|
||||
this.config.log(`[sip-leg:${this.id}] ACK sent`);
|
||||
}
|
||||
|
||||
// If already connected (200 retransmit), just re-ACK.
|
||||
if (this.state === 'connected') {
|
||||
this.config.log(`[sip-leg:${this.id}] re-ACK (200 retransmit)`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Learn media endpoint from SDP.
|
||||
if (msg.hasSdpBody) {
|
||||
const ep = parseSdpEndpoint(msg.body);
|
||||
if (ep) {
|
||||
this.remoteMedia = ep;
|
||||
this.config.log(`[sip-leg:${this.id}] media = ${ep.address}:${ep.port}`);
|
||||
}
|
||||
}
|
||||
|
||||
this.state = 'connected';
|
||||
this.config.log(`[sip-leg:${this.id}] CONNECTED`);
|
||||
|
||||
// Start silence for provider legs with early media quirks.
|
||||
if (this.type === 'sip-provider') {
|
||||
this.startSilence();
|
||||
}
|
||||
|
||||
// Prime the RTP path.
|
||||
if (this.remoteMedia) {
|
||||
this.primeRtp(this.remoteMedia);
|
||||
}
|
||||
|
||||
this.onConnected?.(this);
|
||||
this.onStateChange?.(this);
|
||||
} else if (code >= 300) {
|
||||
this.config.log(`[sip-leg:${this.id}] rejected ${code}`);
|
||||
this.state = 'terminated';
|
||||
if (this.dialog) this.dialog.terminate();
|
||||
this.onTerminated?.(this);
|
||||
this.onStateChange?.(this);
|
||||
}
|
||||
}
|
||||
|
||||
private handleAuthChallenge(msg: SipMessage): void {
|
||||
if (this.authAttempted) {
|
||||
this.config.log(`[sip-leg:${this.id}] 407 after auth attempt — credentials rejected`);
|
||||
this.state = 'terminated';
|
||||
if (this.dialog) this.dialog.terminate();
|
||||
this.onTerminated?.(this);
|
||||
return;
|
||||
}
|
||||
this.authAttempted = true;
|
||||
|
||||
const challenge = msg.getHeader('Proxy-Authenticate');
|
||||
if (!challenge) {
|
||||
this.config.log(`[sip-leg:${this.id}] 407 but no Proxy-Authenticate`);
|
||||
this.state = 'terminated';
|
||||
if (this.dialog) this.dialog.terminate();
|
||||
this.onTerminated?.(this);
|
||||
return;
|
||||
}
|
||||
|
||||
const parsed = parseDigestChallenge(challenge);
|
||||
if (!parsed) {
|
||||
this.config.log(`[sip-leg:${this.id}] could not parse digest challenge`);
|
||||
this.state = 'terminated';
|
||||
if (this.dialog) this.dialog.terminate();
|
||||
this.onTerminated?.(this);
|
||||
return;
|
||||
}
|
||||
|
||||
const password = this.config.getSipPassword?.();
|
||||
const aor = this.config.getRegisteredAor?.();
|
||||
if (!password || !aor) {
|
||||
this.config.log(`[sip-leg:${this.id}] 407 but no password or AOR`);
|
||||
this.state = 'terminated';
|
||||
if (this.dialog) this.dialog.terminate();
|
||||
this.onTerminated?.(this);
|
||||
return;
|
||||
}
|
||||
|
||||
const username = aor.replace(/^sips?:/, '').split('@')[0];
|
||||
const destUri = this.invite?.requestUri || '';
|
||||
|
||||
const authValue = computeDigestAuth({
|
||||
username,
|
||||
password,
|
||||
realm: parsed.realm,
|
||||
nonce: parsed.nonce,
|
||||
method: 'INVITE',
|
||||
uri: destUri,
|
||||
algorithm: parsed.algorithm,
|
||||
opaque: parsed.opaque,
|
||||
});
|
||||
|
||||
// ACK the 407.
|
||||
if (this.invite) {
|
||||
const ack407 = buildNon2xxAck(this.invite, msg);
|
||||
this.config.sendSip(ack407.serialize(), this.config.sipTarget);
|
||||
this.config.log(`[sip-leg:${this.id}] ACK-407 sent`);
|
||||
}
|
||||
|
||||
// Keep original INVITE for re-ACKing retransmitted 407s.
|
||||
this.origInvite = this.invite;
|
||||
|
||||
// Resend INVITE with auth, same From tag, incremented CSeq.
|
||||
const ip = this.config.getPublicIp() || this.config.lanIp;
|
||||
const fromTag = this.dialog!.localTag;
|
||||
const pts = this.config.payloadTypes || [9, 0, 8, 101];
|
||||
|
||||
const sdp = buildSdp({ ip, port: this.rtpPort, payloadTypes: pts });
|
||||
|
||||
const inviteAuth = SipMessage.createRequest('INVITE', destUri, {
|
||||
via: { host: ip, port: this.config.lanPort },
|
||||
from: { uri: aor, tag: fromTag },
|
||||
to: { uri: destUri },
|
||||
callId: this.dialog!.callId,
|
||||
cseq: 2,
|
||||
contact: `<sip:${ip}:${this.config.lanPort}>`,
|
||||
body: sdp,
|
||||
contentType: 'application/sdp',
|
||||
extraHeaders: [['Proxy-Authorization', authValue]],
|
||||
});
|
||||
|
||||
this.invite = inviteAuth;
|
||||
this.dialog!.localCSeq = 2;
|
||||
|
||||
this.config.log(`[sip-leg:${this.id}] resending INVITE with auth`);
|
||||
this.config.sendSip(inviteAuth.serialize(), this.config.sipTarget);
|
||||
}
|
||||
|
||||
private handleRequest(msg: SipMessage, rinfo: IEndpoint): void {
|
||||
const method = msg.method;
|
||||
this.config.log(`[sip-leg:${this.id}] <- ${method} from ${rinfo.address}:${rinfo.port}`);
|
||||
|
||||
if (method === 'BYE') {
|
||||
// Send 200 OK to the BYE.
|
||||
const ok = SipMessage.createResponse(200, 'OK', msg);
|
||||
this.config.sendSip(ok.serialize(), { address: rinfo.address, port: rinfo.port });
|
||||
|
||||
this.state = 'terminated';
|
||||
if (this.dialog) this.dialog.terminate();
|
||||
this.onTerminated?.(this);
|
||||
this.onStateChange?.(this);
|
||||
}
|
||||
if (method === 'INFO') {
|
||||
// Respond 200 OK to the INFO request.
|
||||
const ok = SipMessage.createResponse(200, 'OK', msg);
|
||||
this.config.sendSip(ok.serialize(), { address: rinfo.address, port: rinfo.port });
|
||||
|
||||
// Forward to DTMF handler (if attached).
|
||||
this.onInfoReceived?.(msg);
|
||||
}
|
||||
// Other in-dialog requests (re-INVITE, etc.) can be handled here in the future.
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Send BYE / CANCEL
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Send BYE (if confirmed) or CANCEL (if early) to tear down this leg. */
|
||||
sendHangup(): void {
|
||||
if (!this.dialog) return;
|
||||
|
||||
if (this.dialog.state === 'confirmed') {
|
||||
const bye = this.dialog.createRequest('BYE');
|
||||
this.config.sendSip(bye.serialize(), this.config.sipTarget);
|
||||
this.config.log(`[sip-leg:${this.id}] BYE sent`);
|
||||
} else if (this.dialog.state === 'early' && this.invite) {
|
||||
const cancel = this.dialog.createCancel(this.invite);
|
||||
this.config.sendSip(cancel.serialize(), this.config.sipTarget);
|
||||
this.config.log(`[sip-leg:${this.id}] CANCEL sent`);
|
||||
}
|
||||
|
||||
this.state = 'terminating';
|
||||
this.dialog.terminate();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// RTP
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
sendRtp(data: Buffer): void {
|
||||
if (!this.remoteMedia) return;
|
||||
this.rtpSock.send(data, this.remoteMedia.port, this.remoteMedia.address);
|
||||
this.pktSent++;
|
||||
}
|
||||
|
||||
/** Send a 1-byte UDP packet to punch NAT hole. */
|
||||
private primeRtp(peer: IEndpoint): void {
|
||||
try {
|
||||
this.rtpSock.send(Buffer.alloc(1), peer.port, peer.address);
|
||||
this.config.log(`[sip-leg:${this.id}] RTP primed -> ${peer.address}:${peer.port}`);
|
||||
} catch (e: any) {
|
||||
this.config.log(`[sip-leg:${this.id}] prime error: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Silence stream (provider quirks)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private startSilence(): void {
|
||||
if (this.silenceTimer) return;
|
||||
const quirks = this.config.provider?.quirks;
|
||||
if (!quirks?.earlyMediaSilence) return;
|
||||
if (!this.remoteMedia) return;
|
||||
|
||||
const PT = quirks.silencePayloadType ?? 9;
|
||||
const MAX = quirks.silenceMaxPackets ?? 250;
|
||||
const PAYLOAD = 160;
|
||||
let seq = Math.floor(Math.random() * 0xffff);
|
||||
let rtpTs = Math.floor(Math.random() * 0xffffffff);
|
||||
let count = 0;
|
||||
|
||||
// Use proper silence byte for the codec (0x00 is NOT silence for most codecs).
|
||||
const silenceByte = silenceByteForPT(PT);
|
||||
|
||||
this.silenceTimer = setInterval(() => {
|
||||
if (this.pktReceived > 0 || count >= MAX) {
|
||||
clearInterval(this.silenceTimer!);
|
||||
this.silenceTimer = null;
|
||||
this.config.log(`[sip-leg:${this.id}] silence stop after ${count} pkts`);
|
||||
return;
|
||||
}
|
||||
const pkt = Buffer.alloc(12 + PAYLOAD, silenceByte);
|
||||
// RTP header (first 12 bytes).
|
||||
pkt[0] = 0x80;
|
||||
pkt[1] = PT;
|
||||
pkt.writeUInt16BE(seq & 0xffff, 2);
|
||||
pkt.writeUInt32BE(rtpTs >>> 0, 4);
|
||||
pkt.writeUInt32BE(this.ssrc >>> 0, 8); // stable SSRC
|
||||
this.rtpSock.send(pkt, this.remoteMedia!.port, this.remoteMedia!.address);
|
||||
seq++;
|
||||
rtpTs += PAYLOAD;
|
||||
count++;
|
||||
}, 20);
|
||||
|
||||
this.config.log(`[sip-leg:${this.id}] silence start -> ${this.remoteMedia.address}:${this.remoteMedia.port} (ssrc=${this.ssrc})`);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Lifecycle
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
teardown(): void {
|
||||
if (this.silenceTimer) {
|
||||
clearInterval(this.silenceTimer);
|
||||
this.silenceTimer = null;
|
||||
}
|
||||
this.state = 'terminated';
|
||||
if (this.dialog) this.dialog.terminate();
|
||||
// Note: RTP socket is NOT closed here — the RtpPortPool manages that.
|
||||
}
|
||||
|
||||
getStatus(): ILegStatus {
|
||||
return {
|
||||
id: this.id,
|
||||
type: this.type,
|
||||
state: this.state,
|
||||
remoteMedia: this.remoteMedia,
|
||||
rtpPort: this.rtpPort,
|
||||
pktSent: this.pktSent,
|
||||
pktReceived: this.pktReceived,
|
||||
codec: codecDisplayName(this.codec),
|
||||
transcoding: this.transcoder !== null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helper: proper silence byte per codec
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Return the byte value representing digital silence for a given RTP payload type. */
|
||||
function silenceByteForPT(pt: number): number {
|
||||
switch (pt) {
|
||||
case 0: return 0xFF; // PCMU: μ-law silence (zero amplitude)
|
||||
case 8: return 0xD5; // PCMA: A-law silence (zero amplitude)
|
||||
case 9: return 0xD5; // G.722: sub-band silence (zero amplitude)
|
||||
default: return 0xFF; // safe default
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helper: ACK for non-2xx (same transaction)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function buildNon2xxAck(originalInvite: SipMessage, response: SipMessage): SipMessage {
|
||||
const via = originalInvite.getHeader('Via') || '';
|
||||
const from = originalInvite.getHeader('From') || '';
|
||||
const toFromResponse = response.getHeader('To') || '';
|
||||
const callId = originalInvite.callId;
|
||||
const cseqNum = parseInt((originalInvite.getHeader('CSeq') || '1').split(/\s+/)[0], 10);
|
||||
|
||||
return new SipMessage(
|
||||
`ACK ${originalInvite.requestUri} SIP/2.0`,
|
||||
[
|
||||
['Via', via],
|
||||
['From', from],
|
||||
['To', toFromResponse],
|
||||
['Call-ID', callId],
|
||||
['CSeq', `${cseqNum} ACK`],
|
||||
['Max-Forwards', '70'],
|
||||
['Content-Length', '0'],
|
||||
],
|
||||
'',
|
||||
);
|
||||
}
|
||||
@@ -1,336 +0,0 @@
|
||||
/**
|
||||
* SystemLeg — virtual ILeg for IVR menus and voicemail.
|
||||
*
|
||||
* Plugs into the Call hub exactly like SipLeg or WebRtcLeg:
|
||||
* - Receives caller audio via sendRtp() (called by Call.forwardRtp)
|
||||
* - Plays prompts by firing onRtpReceived (picked up by Call.forwardRtp → caller's leg)
|
||||
* - Detects DTMF from caller's audio (RFC 2833 telephone-event)
|
||||
* - Records caller's audio to WAV files (for voicemail)
|
||||
*
|
||||
* No UDP socket or SIP dialog needed — purely virtual.
|
||||
*/
|
||||
|
||||
import { Buffer } from 'node:buffer';
|
||||
import type dgram from 'node:dgram';
|
||||
import type { IEndpoint } from '../sip/index.ts';
|
||||
import type { SipMessage } from '../sip/index.ts';
|
||||
import type { SipDialog } from '../sip/index.ts';
|
||||
import type { IRtpTranscoder } from '../codec.ts';
|
||||
import type { ILeg } from './leg.ts';
|
||||
import type { TLegState, TLegType, ILegStatus } from './types.ts';
|
||||
import { DtmfDetector } from './dtmf-detector.ts';
|
||||
import type { IDtmfDigit } from './dtmf-detector.ts';
|
||||
import { AudioRecorder } from './audio-recorder.ts';
|
||||
import type { IRecordingResult } from './audio-recorder.ts';
|
||||
import { PromptCache, playPromptG722, playPromptOpus } from './prompt-cache.ts';
|
||||
import type { ICachedPrompt } from './prompt-cache.ts';
|
||||
import { buildRtpHeader, rtpClockIncrement } from './leg.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export type TSystemLegMode = 'ivr' | 'voicemail-greeting' | 'voicemail-recording' | 'idle';
|
||||
|
||||
export interface ISystemLegConfig {
|
||||
/** Logging function. */
|
||||
log: (msg: string) => void;
|
||||
/** The prompt cache for TTS playback. */
|
||||
promptCache: PromptCache;
|
||||
/**
|
||||
* Codec payload type used by the caller's leg.
|
||||
* Determines whether G.722 (9) or Opus (111) frames are played.
|
||||
* Default: 9 (G.722, typical for SIP callers).
|
||||
*/
|
||||
callerCodecPt?: number;
|
||||
/** Called when a DTMF digit is detected. */
|
||||
onDtmfDigit?: (digit: IDtmfDigit) => void;
|
||||
/** Called when a voicemail recording is complete. */
|
||||
onRecordingComplete?: (result: IRecordingResult) => void;
|
||||
/** Called when the SystemLeg wants to signal an IVR action. */
|
||||
onAction?: (action: string, data?: any) => void;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SystemLeg
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export class SystemLeg implements ILeg {
|
||||
readonly id: string;
|
||||
readonly type: TLegType = 'system';
|
||||
state: TLegState = 'connected'; // Immediately "connected" — no setup phase.
|
||||
|
||||
/** Current operating mode. */
|
||||
mode: TSystemLegMode = 'idle';
|
||||
|
||||
// --- ILeg required fields (virtual — no real network resources) ---
|
||||
readonly sipCallId: string;
|
||||
readonly rtpPort: number | null = null;
|
||||
readonly rtpSock: dgram.Socket | null = null;
|
||||
remoteMedia: IEndpoint | null = null;
|
||||
codec: number | null = null;
|
||||
transcoder: IRtpTranscoder | null = null;
|
||||
pktSent = 0;
|
||||
pktReceived = 0;
|
||||
readonly dialog: SipDialog | null = null;
|
||||
|
||||
/**
|
||||
* Set by Call.addLeg() — firing this injects audio into the Call hub,
|
||||
* which forwards it to the caller's leg.
|
||||
*/
|
||||
onRtpReceived: ((data: Buffer) => void) | null = null;
|
||||
|
||||
// --- Internal components ---
|
||||
private dtmfDetector: DtmfDetector;
|
||||
private recorder: AudioRecorder | null = null;
|
||||
private promptCache: PromptCache;
|
||||
private promptCancel: (() => void) | null = null;
|
||||
private callerCodecPt: number;
|
||||
private log: (msg: string) => void;
|
||||
readonly config: ISystemLegConfig;
|
||||
|
||||
/** Stable SSRC for all prompt playback (random, stays constant for the leg's lifetime). */
|
||||
private ssrc: number;
|
||||
|
||||
/** Sequence/timestamp counters for Opus prompt playback (shared for seamless transitions). */
|
||||
private opusCounters = { seq: 0, ts: 0 };
|
||||
|
||||
constructor(id: string, config: ISystemLegConfig) {
|
||||
this.id = id;
|
||||
this.sipCallId = `system-${id}`; // Virtual Call-ID — not a real SIP dialog.
|
||||
this.config = config;
|
||||
this.log = config.log;
|
||||
this.promptCache = config.promptCache;
|
||||
this.callerCodecPt = config.callerCodecPt ?? 9; // Default G.722
|
||||
|
||||
this.ssrc = (Math.random() * 0xffffffff) >>> 0;
|
||||
this.opusCounters.seq = Math.floor(Math.random() * 0xffff);
|
||||
this.opusCounters.ts = Math.floor(Math.random() * 0xffffffff);
|
||||
|
||||
// Initialize DTMF detector.
|
||||
this.dtmfDetector = new DtmfDetector(this.log);
|
||||
this.dtmfDetector.onDigit = (digit) => {
|
||||
this.log(`[system-leg:${this.id}] DTMF '${digit.digit}' (${digit.source})`);
|
||||
this.config.onDtmfDigit?.(digit);
|
||||
};
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// ILeg: sendRtp — receives caller's audio from the Call hub
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Called by the Call hub (via forwardRtp) to deliver the caller's audio
|
||||
* to this leg. We use this for DTMF detection and recording.
|
||||
*/
|
||||
sendRtp(data: Buffer): void {
|
||||
this.pktReceived++;
|
||||
|
||||
// Feed DTMF detector (it checks PT internally, ignores non-101 packets).
|
||||
this.dtmfDetector.processRtp(data);
|
||||
|
||||
// Feed recorder if active.
|
||||
if (this.mode === 'voicemail-recording' && this.recorder) {
|
||||
this.recorder.processRtp(data);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// ILeg: handleSipMessage — handles SIP INFO for DTMF
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Handle a SIP message routed to this leg. Only SIP INFO (DTMF) is relevant.
|
||||
*/
|
||||
handleSipMessage(msg: SipMessage, _rinfo: IEndpoint): void {
|
||||
if (msg.method === 'INFO') {
|
||||
this.dtmfDetector.processSipInfo(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Prompt playback
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Play a cached prompt by ID.
|
||||
* The audio is injected into the Call hub via onRtpReceived.
|
||||
*
|
||||
* @param promptId - ID of the prompt in the PromptCache
|
||||
* @param onDone - called when playback completes (not on cancel)
|
||||
* @returns true if playback started, false if prompt not found
|
||||
*/
|
||||
playPrompt(promptId: string, onDone?: () => void): boolean {
|
||||
const prompt = this.promptCache.get(promptId);
|
||||
if (!prompt) {
|
||||
this.log(`[system-leg:${this.id}] prompt "${promptId}" not found`);
|
||||
onDone?.();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Cancel any in-progress playback.
|
||||
this.cancelPrompt();
|
||||
|
||||
this.log(`[system-leg:${this.id}] playing prompt "${promptId}" (${prompt.durationMs}ms)`);
|
||||
|
||||
// Select G.722 or Opus frames based on caller codec.
|
||||
if (this.callerCodecPt === 111) {
|
||||
// WebRTC caller: play Opus frames.
|
||||
this.promptCancel = playPromptOpus(
|
||||
prompt,
|
||||
(pkt) => this.injectPacket(pkt),
|
||||
this.ssrc,
|
||||
this.opusCounters,
|
||||
() => {
|
||||
this.promptCancel = null;
|
||||
onDone?.();
|
||||
},
|
||||
);
|
||||
} else {
|
||||
// SIP caller: play G.722 frames (works for all SIP codecs since the
|
||||
// SipLeg's RTP socket sends whatever we give it — the provider's
|
||||
// media endpoint accepts the codec negotiated in the SDP).
|
||||
this.promptCancel = playPromptG722(
|
||||
prompt,
|
||||
(pkt) => this.injectPacket(pkt),
|
||||
this.ssrc,
|
||||
() => {
|
||||
this.promptCancel = null;
|
||||
onDone?.();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return this.promptCancel !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Play a sequence of prompts, one after another.
|
||||
*/
|
||||
playPromptSequence(promptIds: string[], onDone?: () => void): void {
|
||||
let index = 0;
|
||||
const playNext = () => {
|
||||
if (index >= promptIds.length) {
|
||||
onDone?.();
|
||||
return;
|
||||
}
|
||||
const id = promptIds[index++];
|
||||
if (!this.playPrompt(id, playNext)) {
|
||||
// Prompt not found — skip and play next.
|
||||
playNext();
|
||||
}
|
||||
};
|
||||
playNext();
|
||||
}
|
||||
|
||||
/** Cancel any in-progress prompt playback. */
|
||||
cancelPrompt(): void {
|
||||
if (this.promptCancel) {
|
||||
this.promptCancel();
|
||||
this.promptCancel = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether a prompt is currently playing. */
|
||||
get isPlaying(): boolean {
|
||||
return this.promptCancel !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject an RTP packet into the Call hub.
|
||||
* This simulates "receiving" audio on this leg — the hub
|
||||
* will forward it to the caller's leg.
|
||||
*/
|
||||
private injectPacket(pkt: Buffer): void {
|
||||
this.pktSent++;
|
||||
this.onRtpReceived?.(pkt);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Recording
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Start recording the caller's audio.
|
||||
* @param outputDir - directory to write the WAV file
|
||||
* @param fileId - unique ID for the file name
|
||||
*/
|
||||
async startRecording(outputDir: string, fileId?: string): Promise<void> {
|
||||
if (this.recorder) {
|
||||
await this.recorder.stop();
|
||||
}
|
||||
|
||||
this.recorder = new AudioRecorder({
|
||||
outputDir,
|
||||
log: this.log,
|
||||
maxDurationSec: 120,
|
||||
silenceTimeoutSec: 5,
|
||||
});
|
||||
|
||||
this.recorder.onStopped = (result) => {
|
||||
this.log(`[system-leg:${this.id}] recording auto-stopped (${result.stopReason})`);
|
||||
this.config.onRecordingComplete?.(result);
|
||||
};
|
||||
|
||||
this.mode = 'voicemail-recording';
|
||||
await this.recorder.start(fileId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop recording and finalize the WAV file.
|
||||
*/
|
||||
async stopRecording(): Promise<IRecordingResult | null> {
|
||||
if (!this.recorder) return null;
|
||||
|
||||
const result = await this.recorder.stop();
|
||||
this.recorder = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Cancel recording — stops and deletes the file. */
|
||||
async cancelRecording(): Promise<void> {
|
||||
if (this.recorder) {
|
||||
await this.recorder.cancel();
|
||||
this.recorder = null;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Lifecycle
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Release all resources. */
|
||||
teardown(): void {
|
||||
this.cancelPrompt();
|
||||
|
||||
// Stop recording gracefully.
|
||||
if (this.recorder && this.recorder.state === 'recording') {
|
||||
this.recorder.stop().then((result) => {
|
||||
this.config.onRecordingComplete?.(result);
|
||||
});
|
||||
this.recorder = null;
|
||||
}
|
||||
|
||||
this.dtmfDetector.destroy();
|
||||
this.state = 'terminated';
|
||||
this.mode = 'idle';
|
||||
this.onRtpReceived = null;
|
||||
|
||||
this.log(`[system-leg:${this.id}] torn down`);
|
||||
}
|
||||
|
||||
/** Status snapshot for the dashboard. */
|
||||
getStatus(): ILegStatus {
|
||||
return {
|
||||
id: this.id,
|
||||
type: this.type,
|
||||
state: this.state,
|
||||
remoteMedia: null,
|
||||
rtpPort: null,
|
||||
pktSent: this.pktSent,
|
||||
pktReceived: this.pktReceived,
|
||||
codec: this.callerCodecPt === 111 ? 'Opus' : 'G.722',
|
||||
transcoding: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
/**
|
||||
* Hub model type definitions — Call, Leg, and status types.
|
||||
*/
|
||||
|
||||
import type { IEndpoint } from '../sip/index.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// State types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export type TCallState =
|
||||
| 'setting-up'
|
||||
| 'ringing'
|
||||
| 'connected'
|
||||
| 'on-hold'
|
||||
| 'voicemail'
|
||||
| 'ivr'
|
||||
| 'transferring'
|
||||
| 'terminating'
|
||||
| 'terminated';
|
||||
|
||||
export type TLegState =
|
||||
| 'inviting'
|
||||
| 'ringing'
|
||||
| 'connected'
|
||||
| 'on-hold'
|
||||
| 'terminating'
|
||||
| 'terminated';
|
||||
|
||||
export type TLegType = 'sip-device' | 'sip-provider' | 'webrtc' | 'system';
|
||||
|
||||
export type TCallDirection = 'inbound' | 'outbound' | 'internal';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Status interfaces (for frontend dashboard)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface ILegStatus {
|
||||
id: string;
|
||||
type: TLegType;
|
||||
state: TLegState;
|
||||
remoteMedia: IEndpoint | null;
|
||||
rtpPort: number | null;
|
||||
pktSent: number;
|
||||
pktReceived: number;
|
||||
codec: string | null;
|
||||
transcoding: boolean;
|
||||
}
|
||||
|
||||
export interface ICallStatus {
|
||||
id: string;
|
||||
state: TCallState;
|
||||
direction: TCallDirection;
|
||||
callerNumber: string | null;
|
||||
calleeNumber: string | null;
|
||||
providerUsed: string | null;
|
||||
createdAt: number;
|
||||
duration: number;
|
||||
legs: ILegStatus[];
|
||||
}
|
||||
|
||||
export interface ICallHistoryEntry {
|
||||
id: string;
|
||||
direction: TCallDirection;
|
||||
callerNumber: string | null;
|
||||
calleeNumber: string | null;
|
||||
providerUsed: string | null;
|
||||
startedAt: number;
|
||||
duration: number;
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
/**
|
||||
* Streaming WAV file writer — opens a file, writes a placeholder header,
|
||||
* appends raw PCM data in chunks, and finalizes (patches sizes) on close.
|
||||
*
|
||||
* Produces standard RIFF/WAVE format compatible with the WAV parser
|
||||
* in announcement.ts (extractPcmFromWav).
|
||||
*/
|
||||
|
||||
import fs from 'node:fs';
|
||||
import { Buffer } from 'node:buffer';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface IWavWriterOptions {
|
||||
/** Full path to the output WAV file. */
|
||||
filePath: string;
|
||||
/** Sample rate in Hz (e.g. 16000). */
|
||||
sampleRate: number;
|
||||
/** Number of channels (default 1 = mono). */
|
||||
channels?: number;
|
||||
/** Bits per sample (default 16). */
|
||||
bitsPerSample?: number;
|
||||
}
|
||||
|
||||
export interface IWavWriterResult {
|
||||
/** Full path to the WAV file. */
|
||||
filePath: string;
|
||||
/** Total duration in milliseconds. */
|
||||
durationMs: number;
|
||||
/** Sample rate of the output file. */
|
||||
sampleRate: number;
|
||||
/** Total number of audio samples written. */
|
||||
totalSamples: number;
|
||||
/** File size in bytes. */
|
||||
fileSize: number;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WAV header constants
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Standard WAV header size: RIFF(12) + fmt(24) + data-header(8) = 44 bytes. */
|
||||
const HEADER_SIZE = 44;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WavWriter
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export class WavWriter {
|
||||
private fd: number | null = null;
|
||||
private totalDataBytes = 0;
|
||||
private closed = false;
|
||||
|
||||
private filePath: string;
|
||||
private sampleRate: number;
|
||||
private channels: number;
|
||||
private bitsPerSample: number;
|
||||
|
||||
constructor(options: IWavWriterOptions) {
|
||||
this.filePath = options.filePath;
|
||||
this.sampleRate = options.sampleRate;
|
||||
this.channels = options.channels ?? 1;
|
||||
this.bitsPerSample = options.bitsPerSample ?? 16;
|
||||
}
|
||||
|
||||
/** Open the file and write a placeholder 44-byte WAV header. */
|
||||
open(): void {
|
||||
if (this.fd !== null) throw new Error('WavWriter already open');
|
||||
|
||||
this.fd = fs.openSync(this.filePath, 'w');
|
||||
this.totalDataBytes = 0;
|
||||
this.closed = false;
|
||||
|
||||
// Write 44 bytes of zeros as placeholder — patched in close().
|
||||
const placeholder = Buffer.alloc(HEADER_SIZE);
|
||||
fs.writeSync(this.fd, placeholder, 0, HEADER_SIZE, 0);
|
||||
}
|
||||
|
||||
/** Append raw 16-bit little-endian PCM samples. */
|
||||
write(pcm: Buffer): void {
|
||||
if (this.fd === null || this.closed) return;
|
||||
if (pcm.length === 0) return;
|
||||
|
||||
fs.writeSync(this.fd, pcm, 0, pcm.length);
|
||||
this.totalDataBytes += pcm.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finalize: rewrite the RIFF and data chunk sizes in the header, close the file.
|
||||
* Returns metadata about the written WAV.
|
||||
*/
|
||||
close(): IWavWriterResult {
|
||||
if (this.fd === null || this.closed) {
|
||||
return {
|
||||
filePath: this.filePath,
|
||||
durationMs: 0,
|
||||
sampleRate: this.sampleRate,
|
||||
totalSamples: 0,
|
||||
fileSize: HEADER_SIZE,
|
||||
};
|
||||
}
|
||||
|
||||
this.closed = true;
|
||||
|
||||
const blockAlign = this.channels * (this.bitsPerSample / 8);
|
||||
const byteRate = this.sampleRate * blockAlign;
|
||||
const fileSize = HEADER_SIZE + this.totalDataBytes;
|
||||
|
||||
// Build the complete 44-byte header.
|
||||
const hdr = Buffer.alloc(HEADER_SIZE);
|
||||
let offset = 0;
|
||||
|
||||
// RIFF chunk descriptor.
|
||||
hdr.write('RIFF', offset); offset += 4;
|
||||
hdr.writeUInt32LE(fileSize - 8, offset); offset += 4; // ChunkSize = fileSize - 8
|
||||
hdr.write('WAVE', offset); offset += 4;
|
||||
|
||||
// fmt sub-chunk.
|
||||
hdr.write('fmt ', offset); offset += 4;
|
||||
hdr.writeUInt32LE(16, offset); offset += 4; // Subchunk1Size (PCM = 16)
|
||||
hdr.writeUInt16LE(1, offset); offset += 2; // AudioFormat (1 = PCM)
|
||||
hdr.writeUInt16LE(this.channels, offset); offset += 2;
|
||||
hdr.writeUInt32LE(this.sampleRate, offset); offset += 4;
|
||||
hdr.writeUInt32LE(byteRate, offset); offset += 4;
|
||||
hdr.writeUInt16LE(blockAlign, offset); offset += 2;
|
||||
hdr.writeUInt16LE(this.bitsPerSample, offset); offset += 2;
|
||||
|
||||
// data sub-chunk.
|
||||
hdr.write('data', offset); offset += 4;
|
||||
hdr.writeUInt32LE(this.totalDataBytes, offset); offset += 4;
|
||||
|
||||
// Patch the header at the beginning of the file.
|
||||
fs.writeSync(this.fd, hdr, 0, HEADER_SIZE, 0);
|
||||
fs.closeSync(this.fd);
|
||||
this.fd = null;
|
||||
|
||||
const bytesPerSample = this.bitsPerSample / 8;
|
||||
const totalSamples = Math.floor(this.totalDataBytes / (bytesPerSample * this.channels));
|
||||
const durationMs = (totalSamples / this.sampleRate) * 1000;
|
||||
|
||||
return {
|
||||
filePath: this.filePath,
|
||||
durationMs: Math.round(durationMs),
|
||||
sampleRate: this.sampleRate,
|
||||
totalSamples,
|
||||
fileSize,
|
||||
};
|
||||
}
|
||||
|
||||
/** Current recording duration in milliseconds. */
|
||||
get durationMs(): number {
|
||||
const bytesPerSample = this.bitsPerSample / 8;
|
||||
const totalSamples = Math.floor(this.totalDataBytes / (bytesPerSample * this.channels));
|
||||
return (totalSamples / this.sampleRate) * 1000;
|
||||
}
|
||||
|
||||
/** Whether the writer is still open and accepting data. */
|
||||
get isOpen(): boolean {
|
||||
return this.fd !== null && !this.closed;
|
||||
}
|
||||
}
|
||||
@@ -1,417 +0,0 @@
|
||||
/**
|
||||
* WebRtcLeg — a WebRTC connection from the Call hub to a browser client.
|
||||
*
|
||||
* Wraps a werift RTCPeerConnection and handles:
|
||||
* - WebRTC offer/answer/ICE negotiation
|
||||
* - Opus <-> G.722/PCMU/PCMA transcoding via Rust IPC
|
||||
* - RTP header rebuilding with correct PT, timestamp, SSRC
|
||||
*/
|
||||
|
||||
import dgram from 'node:dgram';
|
||||
import { Buffer } from 'node:buffer';
|
||||
import { WebSocket } from 'ws';
|
||||
import type { IEndpoint } from '../sip/index.ts';
|
||||
import type { TLegState, ILegStatus } from './types.ts';
|
||||
import type { ILeg } from './leg.ts';
|
||||
import { rtpClockIncrement, buildRtpHeader, codecDisplayName } from './leg.ts';
|
||||
import { createTranscoder, OPUS_PT } from '../codec.ts';
|
||||
import type { IRtpTranscoder } from '../codec.ts';
|
||||
import { createSession, destroySession } from '../opusbridge.ts';
|
||||
import type { SipDialog } from '../sip/index.ts';
|
||||
import type { SipMessage } from '../sip/index.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebRtcLeg config
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface IWebRtcLegConfig {
|
||||
/** The browser's WebSocket connection. */
|
||||
ws: WebSocket;
|
||||
/** The browser's session ID. */
|
||||
sessionId: string;
|
||||
/** RTP port and socket (pre-allocated from the pool). */
|
||||
rtpPort: number;
|
||||
rtpSock: dgram.Socket;
|
||||
/** Logging function. */
|
||||
log: (msg: string) => void;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebRtcLeg
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export class WebRtcLeg implements ILeg {
|
||||
readonly id: string;
|
||||
readonly type = 'webrtc' as const;
|
||||
state: TLegState = 'inviting';
|
||||
readonly sessionId: string;
|
||||
|
||||
/** The werift RTCPeerConnection instance. */
|
||||
private pc: any = null;
|
||||
|
||||
/** RTP socket for bridging to SIP. */
|
||||
readonly rtpSock: dgram.Socket;
|
||||
readonly rtpPort: number;
|
||||
|
||||
/** Remote media endpoint (the other side of the bridge, set by Call). */
|
||||
remoteMedia: IEndpoint | null = null;
|
||||
|
||||
/** Negotiated WebRTC codec payload type. */
|
||||
codec: number | null = null;
|
||||
|
||||
/** Transcoders for WebRTC <-> SIP conversion. */
|
||||
transcoder: IRtpTranscoder | null = null; // used by Call for fan-out
|
||||
private toSipTranscoder: IRtpTranscoder | null = null;
|
||||
private fromSipTranscoder: IRtpTranscoder | null = null;
|
||||
|
||||
/** RTP counters for outgoing (to SIP) direction. */
|
||||
private toSipSeq = 0;
|
||||
private toSipTs = 0;
|
||||
private toSipSsrc = (Math.random() * 0xffffffff) >>> 0;
|
||||
|
||||
/** RTP counters for incoming (from SIP) direction.
|
||||
* Initialized to random values so announcements and provider audio share
|
||||
* a continuous sequence — prevents the browser jitter buffer from discarding
|
||||
* packets after the announcement→provider transition. */
|
||||
readonly fromSipCounters = {
|
||||
seq: Math.floor(Math.random() * 0xffff),
|
||||
ts: Math.floor(Math.random() * 0xffffffff),
|
||||
};
|
||||
fromSipSsrc = (Math.random() * 0xffffffff) >>> 0;
|
||||
|
||||
/** Packet counters. */
|
||||
pktSent = 0;
|
||||
pktReceived = 0;
|
||||
|
||||
/** Callback set by Call. */
|
||||
onRtpReceived: ((data: Buffer) => void) | null = null;
|
||||
|
||||
/** Callback to send transcoded RTP to the provider via the SipLeg's socket.
|
||||
* Set by CallManager when the bridge is established. If null, falls back to own rtpSock. */
|
||||
onSendToProvider: ((data: Buffer, dest: IEndpoint) => void) | null = null;
|
||||
|
||||
/** Lifecycle callbacks. */
|
||||
onConnected: ((leg: WebRtcLeg) => void) | null = null;
|
||||
onTerminated: ((leg: WebRtcLeg) => void) | null = null;
|
||||
|
||||
/** Cancel handle for an in-progress announcement. */
|
||||
announcementCancel: (() => void) | null = null;
|
||||
|
||||
private ws: WebSocket;
|
||||
private config: IWebRtcLegConfig;
|
||||
private pendingIceCandidates: any[] = [];
|
||||
|
||||
// SipDialog is not applicable for WebRTC legs.
|
||||
readonly dialog: SipDialog | null = null;
|
||||
readonly sipCallId: string;
|
||||
|
||||
constructor(id: string, config: IWebRtcLegConfig) {
|
||||
this.id = id;
|
||||
this.sessionId = config.sessionId;
|
||||
this.ws = config.ws;
|
||||
this.rtpSock = config.rtpSock;
|
||||
this.rtpPort = config.rtpPort;
|
||||
this.config = config;
|
||||
this.sipCallId = `webrtc-${id}`;
|
||||
|
||||
// Log RTP arriving on this socket (symmetric RTP from provider).
|
||||
// Audio forwarding is handled by the Call hub: SipLeg → forwardRtp → WebRtcLeg.sendRtp.
|
||||
// We do NOT transcode here to avoid double-processing (the SipLeg also receives these packets).
|
||||
let sipRxCount = 0;
|
||||
this.rtpSock.on('message', (data: Buffer) => {
|
||||
sipRxCount++;
|
||||
if (sipRxCount === 1 || sipRxCount === 50 || sipRxCount % 500 === 0) {
|
||||
this.config.log(`[webrtc-leg:${this.id}] SIP->browser rtp #${sipRxCount} (${data.length}b) [symmetric, ignored]`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// WebRTC offer/answer
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Handle a WebRTC offer from the browser. Creates the PeerConnection,
|
||||
* sets remote offer, creates answer, and sends it back.
|
||||
*/
|
||||
async handleOffer(offerSdp: string): Promise<void> {
|
||||
this.config.log(`[webrtc-leg:${this.id}] received offer`);
|
||||
|
||||
try {
|
||||
const werift = await import('werift');
|
||||
|
||||
this.pc = new werift.RTCPeerConnection({ iceServers: [] });
|
||||
|
||||
// Add sendrecv transceiver before setRemoteDescription.
|
||||
this.pc.addTransceiver('audio', { direction: 'sendrecv' });
|
||||
|
||||
// Handle incoming audio from browser.
|
||||
this.pc.ontrack = (event: any) => {
|
||||
const track = event.track;
|
||||
this.config.log(`[webrtc-leg:${this.id}] got track: ${track.kind}`);
|
||||
|
||||
let rxCount = 0;
|
||||
track.onReceiveRtp.subscribe((rtp: any) => {
|
||||
if (!this.remoteMedia) return;
|
||||
rxCount++;
|
||||
if (rxCount === 1 || rxCount === 50 || rxCount % 500 === 0) {
|
||||
this.config.log(`[webrtc-leg:${this.id}] browser->SIP rtp #${rxCount}`);
|
||||
}
|
||||
|
||||
this.forwardToSip(rtp, rxCount);
|
||||
});
|
||||
};
|
||||
|
||||
// ICE candidate handling.
|
||||
this.pc.onicecandidate = (candidate: any) => {
|
||||
if (candidate) {
|
||||
const json = candidate.toJSON?.() || candidate;
|
||||
this.wsSend({ type: 'webrtc-ice', sessionId: this.sessionId, candidate: json });
|
||||
}
|
||||
};
|
||||
|
||||
this.pc.onconnectionstatechange = () => {
|
||||
this.config.log(`[webrtc-leg:${this.id}] connection state: ${this.pc.connectionState}`);
|
||||
if (this.pc.connectionState === 'connected') {
|
||||
this.state = 'connected';
|
||||
this.onConnected?.(this);
|
||||
} else if (this.pc.connectionState === 'failed' || this.pc.connectionState === 'closed') {
|
||||
this.state = 'terminated';
|
||||
this.onTerminated?.(this);
|
||||
}
|
||||
};
|
||||
|
||||
if (this.pc.oniceconnectionstatechange !== undefined) {
|
||||
this.pc.oniceconnectionstatechange = () => {
|
||||
this.config.log(`[webrtc-leg:${this.id}] ICE state: ${this.pc.iceConnectionState}`);
|
||||
};
|
||||
}
|
||||
|
||||
// Set remote offer and create answer.
|
||||
await this.pc.setRemoteDescription({ type: 'offer', sdp: offerSdp });
|
||||
const answer = await this.pc.createAnswer();
|
||||
await this.pc.setLocalDescription(answer);
|
||||
|
||||
const sdp: string = this.pc.localDescription!.sdp;
|
||||
|
||||
// Detect negotiated codec.
|
||||
const mAudio = sdp.match(/m=audio\s+\d+\s+\S+\s+(\d+)/);
|
||||
if (mAudio) {
|
||||
this.codec = parseInt(mAudio[1], 10);
|
||||
this.config.log(`[webrtc-leg:${this.id}] negotiated audio PT=${this.codec}`);
|
||||
}
|
||||
|
||||
// Extract sender SSRC from SDP.
|
||||
const ssrcMatch = sdp.match(/a=ssrc:(\d+)\s/);
|
||||
if (ssrcMatch) {
|
||||
this.fromSipSsrc = parseInt(ssrcMatch[1], 10);
|
||||
}
|
||||
// Also try from sender object.
|
||||
const senders = this.pc.getSenders();
|
||||
if (senders[0]) {
|
||||
const senderSsrc = (senders[0] as any).ssrc ?? (senders[0] as any)._ssrc;
|
||||
if (senderSsrc) this.fromSipSsrc = senderSsrc;
|
||||
}
|
||||
|
||||
// Send answer to browser.
|
||||
this.wsSend({ type: 'webrtc-answer', sessionId: this.sessionId, sdp });
|
||||
this.config.log(`[webrtc-leg:${this.id}] sent answer, rtp port=${this.rtpPort}`);
|
||||
|
||||
// Process buffered ICE candidates.
|
||||
for (const c of this.pendingIceCandidates) {
|
||||
try { await this.pc.addIceCandidate(c); } catch { /* ignore */ }
|
||||
}
|
||||
this.pendingIceCandidates = [];
|
||||
|
||||
} catch (err: any) {
|
||||
this.config.log(`[webrtc-leg:${this.id}] offer error: ${err.message}`);
|
||||
this.wsSend({ type: 'webrtc-error', sessionId: this.sessionId, error: err.message });
|
||||
this.state = 'terminated';
|
||||
this.onTerminated?.(this);
|
||||
}
|
||||
}
|
||||
|
||||
/** Add an ICE candidate from the browser. */
|
||||
async addIceCandidate(candidate: any): Promise<void> {
|
||||
if (!this.pc) {
|
||||
this.pendingIceCandidates.push(candidate);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (candidate) await this.pc.addIceCandidate(candidate);
|
||||
} catch (err: any) {
|
||||
this.config.log(`[webrtc-leg:${this.id}] ICE error: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Transcoding setup
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Codec session ID for isolated Rust codec state (unique per leg). */
|
||||
private codecSessionId = `webrtc-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
|
||||
/**
|
||||
* Set up transcoders for bridging between WebRTC and SIP codecs.
|
||||
* Called by the Call when the remote media endpoint is known.
|
||||
* Creates an isolated Rust codec session so concurrent calls don't
|
||||
* corrupt each other's stateful codec state (Opus/G.722 ADPCM).
|
||||
*/
|
||||
async setupTranscoders(sipPT: number): Promise<void> {
|
||||
const webrtcPT = this.codec ?? OPUS_PT;
|
||||
// Create isolated codec session for this leg.
|
||||
await createSession(this.codecSessionId);
|
||||
this.toSipTranscoder = createTranscoder(webrtcPT, sipPT, this.codecSessionId, 'to_sip');
|
||||
this.fromSipTranscoder = createTranscoder(sipPT, webrtcPT, this.codecSessionId, 'to_browser');
|
||||
const mode = this.toSipTranscoder ? `transcoding PT ${webrtcPT}<->${sipPT}` : `pass-through PT ${webrtcPT}`;
|
||||
this.config.log(`[webrtc-leg:${this.id}] ${mode} (session: ${this.codecSessionId})`);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// RTP forwarding
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Forward RTP from SIP side to browser via WebRTC. */
|
||||
private forwardToBrowser(data: Buffer, count: number): void {
|
||||
const sender = this.pc?.getSenders()[0];
|
||||
if (!sender) return;
|
||||
|
||||
if (this.fromSipTranscoder && data.length > 12) {
|
||||
const payload = Buffer.from(data.subarray(12));
|
||||
// Stop announcement if still playing — provider audio takes over.
|
||||
if (this.announcementCancel) {
|
||||
this.announcementCancel();
|
||||
this.announcementCancel = null;
|
||||
}
|
||||
// Capture seq/ts BEFORE async transcode to preserve ordering.
|
||||
const toPT = this.fromSipTranscoder.toPT;
|
||||
const seq = this.fromSipCounters.seq++;
|
||||
const ts = this.fromSipCounters.ts;
|
||||
this.fromSipCounters.ts += rtpClockIncrement(toPT);
|
||||
const result = this.fromSipTranscoder.payload(payload);
|
||||
const sendTranscoded = (transcoded: Buffer) => {
|
||||
if (transcoded.length === 0) return; // transcoding failed
|
||||
try {
|
||||
const hdr = buildRtpHeader(toPT, seq, ts, this.fromSipSsrc, false);
|
||||
const out = Buffer.concat([hdr, transcoded]);
|
||||
const r = sender.sendRtp(out);
|
||||
if (r instanceof Promise) r.catch(() => {});
|
||||
} catch { /* ignore */ }
|
||||
};
|
||||
if (result instanceof Promise) result.then(sendTranscoded).catch(() => {});
|
||||
else sendTranscoded(result);
|
||||
} else if (!this.fromSipTranscoder) {
|
||||
// No transcoder — either same codec or not set up yet.
|
||||
// Only forward if we don't expect transcoding.
|
||||
if (this.codec === null) {
|
||||
try { sender.sendRtp(data); } catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Forward RTP from browser to SIP side. */
|
||||
private forwardToSip(rtp: any, count: number): void {
|
||||
if (!this.remoteMedia) return;
|
||||
|
||||
if (this.toSipTranscoder) {
|
||||
const payload: Buffer = rtp.payload;
|
||||
if (!payload || payload.length === 0) return;
|
||||
// Capture seq/ts BEFORE async transcode to preserve ordering.
|
||||
const toPT = this.toSipTranscoder.toPT;
|
||||
const seq = this.toSipSeq++;
|
||||
const ts = this.toSipTs;
|
||||
this.toSipTs += rtpClockIncrement(toPT);
|
||||
const result = this.toSipTranscoder.payload(payload);
|
||||
const sendTranscoded = (transcoded: Buffer) => {
|
||||
if (transcoded.length === 0) return; // transcoding failed
|
||||
const hdr = buildRtpHeader(toPT, seq, ts, this.toSipSsrc, false);
|
||||
const out = Buffer.concat([hdr, transcoded]);
|
||||
if (this.onSendToProvider) {
|
||||
this.onSendToProvider(out, this.remoteMedia!);
|
||||
} else {
|
||||
this.rtpSock.send(out, this.remoteMedia!.port, this.remoteMedia!.address);
|
||||
}
|
||||
this.pktSent++;
|
||||
};
|
||||
if (result instanceof Promise) result.then(sendTranscoded).catch(() => {});
|
||||
else sendTranscoded(result);
|
||||
} else if (this.codec === null) {
|
||||
// Same codec (no transcoding needed) — pass through.
|
||||
const raw = rtp.serialize();
|
||||
if (this.onSendToProvider) {
|
||||
this.onSendToProvider(raw, this.remoteMedia);
|
||||
} else {
|
||||
this.rtpSock.send(raw, this.remoteMedia.port, this.remoteMedia.address);
|
||||
}
|
||||
this.pktSent++;
|
||||
}
|
||||
// If codec is set but transcoder is null, drop the packet — transcoder not ready yet.
|
||||
// This prevents raw Opus from being sent to a G.722 endpoint.
|
||||
}
|
||||
|
||||
/**
|
||||
* Send RTP to the browser via WebRTC (used by Call hub for fan-out).
|
||||
* This transcodes and sends through the PeerConnection, NOT to a UDP address.
|
||||
*/
|
||||
sendRtp(data: Buffer): void {
|
||||
this.forwardToBrowser(data, this.pktSent);
|
||||
this.pktSent++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a pre-encoded RTP packet directly to the browser via PeerConnection.
|
||||
* Used for announcements — the packet must already be in the correct codec (Opus).
|
||||
*/
|
||||
sendDirectToBrowser(pkt: Buffer): void {
|
||||
const sender = this.pc?.getSenders()[0];
|
||||
if (!sender) return;
|
||||
try {
|
||||
const r = sender.sendRtp(pkt);
|
||||
if (r instanceof Promise) r.catch(() => {});
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
/** No-op: WebRTC legs don't process SIP messages. */
|
||||
handleSipMessage(_msg: SipMessage, _rinfo: IEndpoint): void {
|
||||
// WebRTC legs don't handle SIP messages.
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Lifecycle
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
teardown(): void {
|
||||
this.state = 'terminated';
|
||||
try { this.pc?.close(); } catch { /* ignore */ }
|
||||
this.pc = null;
|
||||
// Destroy the isolated Rust codec session for this leg.
|
||||
destroySession(this.codecSessionId).catch(() => {});
|
||||
// Note: RTP socket is NOT closed here — the RtpPortPool manages that.
|
||||
}
|
||||
|
||||
getStatus(): ILegStatus {
|
||||
return {
|
||||
id: this.id,
|
||||
type: this.type,
|
||||
state: this.state,
|
||||
remoteMedia: this.remoteMedia,
|
||||
rtpPort: this.rtpPort,
|
||||
pktSent: this.pktSent,
|
||||
pktReceived: this.pktReceived,
|
||||
codec: codecDisplayName(this.codec),
|
||||
transcoding: this.toSipTranscoder !== null || this.fromSipTranscoder !== null,
|
||||
};
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private wsSend(data: unknown): void {
|
||||
try {
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(JSON.stringify(data));
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
40
ts/codec.ts
40
ts/codec.ts
@@ -1,40 +0,0 @@
|
||||
/**
|
||||
* 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
|
||||
},
|
||||
};
|
||||
}
|
||||
184
ts/config.ts
184
ts/config.ts
@@ -8,7 +8,15 @@
|
||||
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import type { IEndpoint } from './sip/index.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Shared types (previously in ts/sip/types.ts, now inlined)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface IEndpoint {
|
||||
address: string;
|
||||
port: number;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Config interfaces
|
||||
@@ -319,175 +327,5 @@ export function loadConfig(): IAppConfig {
|
||||
return cfg;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Pattern matching
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Test a value against a pattern string.
|
||||
* - undefined/empty pattern: matches everything (wildcard)
|
||||
* - Prefix: "pattern*" matches values starting with "pattern"
|
||||
* - Regex: "/pattern/" or "/pattern/i" compiles as RegExp
|
||||
* - Otherwise: exact match
|
||||
*/
|
||||
export function matchesPattern(pattern: string | undefined, value: string): boolean {
|
||||
if (!pattern) return true;
|
||||
|
||||
// Prefix match: "+49*"
|
||||
if (pattern.endsWith('*')) {
|
||||
return value.startsWith(pattern.slice(0, -1));
|
||||
}
|
||||
|
||||
// Regex match: "/^\\+49/" or "/pattern/i"
|
||||
if (pattern.startsWith('/')) {
|
||||
const lastSlash = pattern.lastIndexOf('/');
|
||||
if (lastSlash > 0) {
|
||||
const re = new RegExp(pattern.slice(1, lastSlash), pattern.slice(lastSlash + 1));
|
||||
return re.test(value);
|
||||
}
|
||||
}
|
||||
|
||||
// Exact match.
|
||||
return value === pattern;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Route resolution
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Result of resolving an outbound route. */
|
||||
export interface IOutboundRouteResult {
|
||||
provider: IProviderConfig;
|
||||
transformedNumber: string;
|
||||
}
|
||||
|
||||
/** Result of resolving an inbound route. */
|
||||
export interface IInboundRouteResult {
|
||||
/** Device IDs to ring (empty = all devices). */
|
||||
deviceIds: string[];
|
||||
ringBrowsers: boolean;
|
||||
/** If set, route directly to this voicemail box (skip ringing). */
|
||||
voicemailBox?: string;
|
||||
/** If set, route to this IVR menu (skip ringing). */
|
||||
ivrMenuId?: string;
|
||||
/** Override for no-answer timeout in seconds. */
|
||||
noAnswerTimeout?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve which provider to use for an outbound call, and transform the number.
|
||||
*
|
||||
* @param cfg - app config
|
||||
* @param dialedNumber - the number being dialed
|
||||
* @param sourceDeviceId - optional device originating the call
|
||||
* @param isProviderRegistered - callback to check if a provider is currently registered
|
||||
*/
|
||||
export function resolveOutboundRoute(
|
||||
cfg: IAppConfig,
|
||||
dialedNumber: string,
|
||||
sourceDeviceId?: string,
|
||||
isProviderRegistered?: (providerId: string) => boolean,
|
||||
): IOutboundRouteResult | null {
|
||||
const routes = cfg.routing.routes
|
||||
.filter((r) => r.enabled && r.match.direction === 'outbound')
|
||||
.sort((a, b) => b.priority - a.priority);
|
||||
|
||||
for (const route of routes) {
|
||||
const m = route.match;
|
||||
|
||||
if (!matchesPattern(m.numberPattern, dialedNumber)) continue;
|
||||
if (m.sourceDevice && m.sourceDevice !== sourceDeviceId) continue;
|
||||
|
||||
// Find a registered provider (primary + failovers).
|
||||
const candidates = [route.action.provider, ...(route.action.failoverProviders || [])].filter(Boolean) as string[];
|
||||
for (const pid of candidates) {
|
||||
const provider = getProvider(cfg, pid);
|
||||
if (!provider) continue;
|
||||
if (isProviderRegistered && !isProviderRegistered(pid)) continue;
|
||||
|
||||
// Apply number transformation.
|
||||
let num = dialedNumber;
|
||||
if (route.action.stripPrefix && num.startsWith(route.action.stripPrefix)) {
|
||||
num = num.slice(route.action.stripPrefix.length);
|
||||
}
|
||||
if (route.action.prependPrefix) {
|
||||
num = route.action.prependPrefix + num;
|
||||
}
|
||||
|
||||
return { provider, transformedNumber: num };
|
||||
}
|
||||
|
||||
// Route matched but no provider is available — continue to next route.
|
||||
}
|
||||
|
||||
// Fallback: first available provider.
|
||||
const fallback = cfg.providers[0];
|
||||
return fallback ? { provider: fallback, transformedNumber: dialedNumber } : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve which devices/browsers to ring for an inbound call.
|
||||
*
|
||||
* @param cfg - app config
|
||||
* @param providerId - the provider the call is coming from
|
||||
* @param calledNumber - the DID / called number (from Request-URI)
|
||||
* @param callerNumber - the caller ID (from From header)
|
||||
*/
|
||||
export function resolveInboundRoute(
|
||||
cfg: IAppConfig,
|
||||
providerId: string,
|
||||
calledNumber: string,
|
||||
callerNumber: string,
|
||||
): IInboundRouteResult {
|
||||
const routes = cfg.routing.routes
|
||||
.filter((r) => r.enabled && r.match.direction === 'inbound')
|
||||
.sort((a, b) => b.priority - a.priority);
|
||||
|
||||
for (const route of routes) {
|
||||
const m = route.match;
|
||||
|
||||
if (m.sourceProvider && m.sourceProvider !== providerId) continue;
|
||||
if (!matchesPattern(m.numberPattern, calledNumber)) continue;
|
||||
if (!matchesPattern(m.callerPattern, callerNumber)) continue;
|
||||
|
||||
return {
|
||||
deviceIds: route.action.targets || [],
|
||||
ringBrowsers: route.action.ringBrowsers ?? false,
|
||||
voicemailBox: route.action.voicemailBox,
|
||||
ivrMenuId: route.action.ivrMenuId,
|
||||
noAnswerTimeout: route.action.noAnswerTimeout,
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: ring all devices + browsers.
|
||||
return { deviceIds: [], ringBrowsers: true };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Lookup helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function getProvider(cfg: IAppConfig, id: string): IProviderConfig | null {
|
||||
return cfg.providers.find((p) => p.id === id) ?? null;
|
||||
}
|
||||
|
||||
export function getDevice(cfg: IAppConfig, id: string): IDeviceConfig | null {
|
||||
return cfg.devices.find((d) => d.id === id) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use resolveOutboundRoute() instead. Kept for backward compat.
|
||||
*/
|
||||
export function getProviderForOutbound(cfg: IAppConfig): IProviderConfig | null {
|
||||
const result = resolveOutboundRoute(cfg, '');
|
||||
return result?.provider ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use resolveInboundRoute() instead. Kept for backward compat.
|
||||
*/
|
||||
export function getDevicesForInbound(cfg: IAppConfig, providerId: string): IDeviceConfig[] {
|
||||
const result = resolveInboundRoute(cfg, providerId, '', '');
|
||||
if (!result.deviceIds.length) return cfg.devices;
|
||||
return result.deviceIds.map((id) => getDevice(cfg, id)).filter(Boolean) as IDeviceConfig[];
|
||||
}
|
||||
// Route resolution, pattern matching, and provider/device lookup
|
||||
// are now handled entirely by the Rust proxy-engine.
|
||||
|
||||
@@ -11,10 +11,13 @@ import path from 'node:path';
|
||||
import http from 'node:http';
|
||||
import https from 'node:https';
|
||||
import { WebSocketServer, WebSocket } from 'ws';
|
||||
import type { CallManager } from './call/index.ts';
|
||||
import { handleWebRtcSignaling } from './webrtcbridge.ts';
|
||||
import type { VoiceboxManager } from './voicebox.ts';
|
||||
|
||||
// CallManager was previously used for WebRTC call handling. Now replaced by Rust proxy-engine.
|
||||
// Kept as `any` type for backward compat with the function signature until full WebRTC port.
|
||||
type CallManager = any;
|
||||
|
||||
const CONFIG_PATH = path.join(process.cwd(), '.nogit', 'config.json');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -125,14 +128,19 @@ async function handleRequest(
|
||||
}
|
||||
}
|
||||
|
||||
// API: add leg to call.
|
||||
// API: add a SIP device to a call (mid-call INVITE to desk phone).
|
||||
if (url.pathname.startsWith('/api/call/') && url.pathname.endsWith('/addleg') && method === 'POST') {
|
||||
try {
|
||||
const callId = url.pathname.split('/')[3];
|
||||
const body = await readJsonBody(req);
|
||||
if (!body?.deviceId) return sendJson(res, { ok: false, error: 'missing deviceId' }, 400);
|
||||
const ok = callManager?.addDeviceToCall(callId, body.deviceId) ?? false;
|
||||
return sendJson(res, { ok });
|
||||
const { addDeviceLeg } = await import('./proxybridge.ts');
|
||||
const legId = await addDeviceLeg(callId, body.deviceId);
|
||||
if (legId) {
|
||||
return sendJson(res, { ok: true, legId });
|
||||
} else {
|
||||
return sendJson(res, { ok: false, error: 'device not registered or call not found' }, 404);
|
||||
}
|
||||
} catch (e: any) {
|
||||
return sendJson(res, { ok: false, error: e.message }, 400);
|
||||
}
|
||||
@@ -144,8 +152,9 @@ async function handleRequest(
|
||||
const callId = url.pathname.split('/')[3];
|
||||
const body = await readJsonBody(req);
|
||||
if (!body?.number) return sendJson(res, { ok: false, error: 'missing number' }, 400);
|
||||
const ok = callManager?.addExternalToCall(callId, body.number, body.providerId) ?? false;
|
||||
return sendJson(res, { ok });
|
||||
const { addLeg: addLegFn } = await import('./proxybridge.ts');
|
||||
const legId = await addLegFn(callId, body.number, body.providerId);
|
||||
return sendJson(res, { ok: !!legId, legId });
|
||||
} catch (e: any) {
|
||||
return sendJson(res, { ok: false, error: e.message }, 400);
|
||||
}
|
||||
@@ -157,22 +166,22 @@ async function handleRequest(
|
||||
const callId = url.pathname.split('/')[3];
|
||||
const body = await readJsonBody(req);
|
||||
if (!body?.legId) return sendJson(res, { ok: false, error: 'missing legId' }, 400);
|
||||
const ok = callManager?.removeLegFromCall(callId, body.legId) ?? false;
|
||||
const { removeLeg: removeLegFn } = await import('./proxybridge.ts');
|
||||
const ok = await removeLegFn(callId, body.legId);
|
||||
return sendJson(res, { ok });
|
||||
} catch (e: any) {
|
||||
return sendJson(res, { ok: false, error: e.message }, 400);
|
||||
}
|
||||
}
|
||||
|
||||
// API: transfer leg.
|
||||
// API: transfer leg (not yet implemented).
|
||||
if (url.pathname === '/api/transfer' && method === 'POST') {
|
||||
try {
|
||||
const body = await readJsonBody(req);
|
||||
if (!body?.sourceCallId || !body?.legId || !body?.targetCallId) {
|
||||
return sendJson(res, { ok: false, error: 'missing sourceCallId, legId, or targetCallId' }, 400);
|
||||
}
|
||||
const ok = callManager?.transferLeg(body.sourceCallId, body.legId, body.targetCallId) ?? false;
|
||||
return sendJson(res, { ok });
|
||||
return sendJson(res, { ok: false, error: 'not yet implemented' }, 501);
|
||||
} catch (e: any) {
|
||||
return sendJson(res, { ok: false, error: e.message }, 400);
|
||||
}
|
||||
@@ -337,6 +346,12 @@ export function initWebUi(
|
||||
onConfigSaved?: () => void,
|
||||
callManager?: CallManager,
|
||||
voiceboxManager?: VoiceboxManager,
|
||||
/** WebRTC signaling handlers — forwarded to Rust proxy-engine. */
|
||||
onWebRtcOffer?: (sessionId: string, sdp: string, ws: WebSocket) => Promise<void>,
|
||||
onWebRtcIce?: (sessionId: string, candidate: any) => Promise<void>,
|
||||
onWebRtcClose?: (sessionId: string) => Promise<void>,
|
||||
/** Called when browser sends webrtc-accept (callId + sessionId linking). */
|
||||
onWebRtcAccept?: (callId: string, sessionId: string) => void,
|
||||
): void {
|
||||
const WEB_PORT = 3060;
|
||||
|
||||
@@ -372,17 +387,26 @@ export function initWebUi(
|
||||
socket.on('message', (raw) => {
|
||||
try {
|
||||
const msg = JSON.parse(raw.toString());
|
||||
if (msg.type === 'webrtc-accept' && msg.callId) {
|
||||
log(`[webrtc] browser accepted call ${msg.callId} session=${msg.sessionId || 'none'}`);
|
||||
const ok = callManager?.acceptBrowserCall(msg.callId, msg.sessionId) ?? false;
|
||||
log(`[webrtc] acceptBrowserCall result: ${ok}`);
|
||||
} else if (msg.type === 'webrtc-offer' && msg.sessionId) {
|
||||
callManager?.handleWebRtcOffer(msg.sessionId, msg.sdp, socket as any).catch((e: any) =>
|
||||
log(`[webrtc] offer error: ${e.message}`));
|
||||
if (msg.type === 'webrtc-offer' && msg.sessionId) {
|
||||
// Forward to Rust proxy-engine for WebRTC handling.
|
||||
if (onWebRtcOffer) {
|
||||
log(`[webrtc-ws] offer msg keys: ${Object.keys(msg).join(',')}, sdp type: ${typeof msg.sdp}, sdp len: ${msg.sdp?.length || 0}`);
|
||||
onWebRtcOffer(msg.sessionId, msg.sdp, socket as any).catch((e: any) =>
|
||||
log(`[webrtc] offer error: ${e.message}`));
|
||||
}
|
||||
} else if (msg.type === 'webrtc-ice' && msg.sessionId) {
|
||||
callManager?.handleWebRtcIce(msg.sessionId, msg.candidate).catch(() => {});
|
||||
if (onWebRtcIce) {
|
||||
onWebRtcIce(msg.sessionId, msg.candidate).catch(() => {});
|
||||
}
|
||||
} else if (msg.type === 'webrtc-hangup' && msg.sessionId) {
|
||||
callManager?.handleWebRtcHangup(msg.sessionId);
|
||||
if (onWebRtcClose) {
|
||||
onWebRtcClose(msg.sessionId).catch(() => {});
|
||||
}
|
||||
} else if (msg.type === 'webrtc-accept' && msg.callId) {
|
||||
log(`[webrtc] accept: call=${msg.callId} session=${msg.sessionId || 'none'}`);
|
||||
if (onWebRtcAccept && msg.sessionId) {
|
||||
onWebRtcAccept(msg.callId, msg.sessionId);
|
||||
}
|
||||
} else if (msg.type?.startsWith('webrtc-')) {
|
||||
msg._remoteIp = remoteIp;
|
||||
handleWebRtcSignaling(socket as any, msg);
|
||||
|
||||
209
ts/ivr.ts
209
ts/ivr.ts
@@ -1,209 +0,0 @@
|
||||
/**
|
||||
* IVR engine — state machine that navigates callers through menus
|
||||
* based on DTMF digit input.
|
||||
*
|
||||
* The IvrEngine is instantiated per-call and drives a SystemLeg:
|
||||
* - Plays menu prompts via the SystemLeg's prompt playback
|
||||
* - Receives DTMF digits and resolves them to actions
|
||||
* - Fires an onAction callback for the CallManager to execute
|
||||
* (route to extension, voicemail, transfer, etc.)
|
||||
*/
|
||||
|
||||
import type { IIvrConfig, IIvrMenu, TIvrAction } from './config.ts';
|
||||
import type { SystemLeg } from './call/system-leg.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// IVR Engine
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export class IvrEngine {
|
||||
private config: IIvrConfig;
|
||||
private systemLeg: SystemLeg;
|
||||
private onAction: (action: TIvrAction) => void;
|
||||
private log: (msg: string) => void;
|
||||
|
||||
/** The currently active menu. */
|
||||
private currentMenu: IIvrMenu | null = null;
|
||||
|
||||
/** How many times the current menu has been replayed (for retry limit). */
|
||||
private retryCount = 0;
|
||||
|
||||
/** Timer for digit input timeout. */
|
||||
private digitTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
/** Whether the engine is waiting for a digit (prompt finished playing). */
|
||||
private waitingForDigit = false;
|
||||
|
||||
/** Whether the engine has been destroyed. */
|
||||
private destroyed = false;
|
||||
|
||||
constructor(
|
||||
config: IIvrConfig,
|
||||
systemLeg: SystemLeg,
|
||||
onAction: (action: TIvrAction) => void,
|
||||
log: (msg: string) => void,
|
||||
) {
|
||||
this.config = config;
|
||||
this.systemLeg = systemLeg;
|
||||
this.onAction = onAction;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Public API
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Start the IVR — navigates to the entry menu and plays its prompt.
|
||||
*/
|
||||
start(): void {
|
||||
const entryMenu = this.getMenu(this.config.entryMenuId);
|
||||
if (!entryMenu) {
|
||||
this.log(`[ivr] entry menu "${this.config.entryMenuId}" not found — hanging up`);
|
||||
this.onAction({ type: 'hangup' });
|
||||
return;
|
||||
}
|
||||
|
||||
this.navigateToMenu(entryMenu);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a DTMF digit from the caller.
|
||||
*/
|
||||
handleDigit(digit: string): void {
|
||||
if (this.destroyed || !this.currentMenu) return;
|
||||
|
||||
// Clear the timeout — caller pressed something.
|
||||
this.clearDigitTimeout();
|
||||
|
||||
// Cancel any playing prompt (caller interrupted it).
|
||||
this.systemLeg.cancelPrompt();
|
||||
this.waitingForDigit = false;
|
||||
|
||||
this.log(`[ivr] digit '${digit}' in menu "${this.currentMenu.id}"`);
|
||||
|
||||
// Look up the digit in the current menu.
|
||||
const entry = this.currentMenu.entries.find((e) => e.digit === digit);
|
||||
if (entry) {
|
||||
this.executeAction(entry.action);
|
||||
} else {
|
||||
this.log(`[ivr] invalid digit '${digit}' in menu "${this.currentMenu.id}"`);
|
||||
this.executeAction(this.currentMenu.invalidAction);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up timers and state.
|
||||
*/
|
||||
destroy(): void {
|
||||
this.destroyed = true;
|
||||
this.clearDigitTimeout();
|
||||
this.currentMenu = null;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Internal
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Navigate to a menu: play its prompt, then wait for digit. */
|
||||
private navigateToMenu(menu: IIvrMenu): void {
|
||||
if (this.destroyed) return;
|
||||
|
||||
this.currentMenu = menu;
|
||||
this.waitingForDigit = false;
|
||||
this.clearDigitTimeout();
|
||||
|
||||
const promptId = `ivr-menu-${menu.id}`;
|
||||
this.log(`[ivr] playing menu "${menu.id}" prompt`);
|
||||
|
||||
this.systemLeg.playPrompt(promptId, () => {
|
||||
if (this.destroyed) return;
|
||||
// Prompt finished — start digit timeout.
|
||||
this.waitingForDigit = true;
|
||||
this.startDigitTimeout();
|
||||
});
|
||||
}
|
||||
|
||||
/** Start the timeout timer for digit input. */
|
||||
private startDigitTimeout(): void {
|
||||
const timeoutSec = this.currentMenu?.timeoutSec ?? 5;
|
||||
|
||||
this.digitTimeout = setTimeout(() => {
|
||||
if (this.destroyed || !this.currentMenu) return;
|
||||
this.log(`[ivr] digit timeout in menu "${this.currentMenu.id}"`);
|
||||
this.handleTimeout();
|
||||
}, timeoutSec * 1000);
|
||||
}
|
||||
|
||||
/** Handle timeout (no digit pressed). */
|
||||
private handleTimeout(): void {
|
||||
if (!this.currentMenu) return;
|
||||
|
||||
this.retryCount++;
|
||||
const maxRetries = this.currentMenu.maxRetries ?? 3;
|
||||
|
||||
if (this.retryCount >= maxRetries) {
|
||||
this.log(`[ivr] max retries (${maxRetries}) reached in menu "${this.currentMenu.id}"`);
|
||||
this.executeAction(this.currentMenu.timeoutAction);
|
||||
} else {
|
||||
this.log(`[ivr] retry ${this.retryCount}/${maxRetries} in menu "${this.currentMenu.id}"`);
|
||||
// Replay the current menu.
|
||||
this.navigateToMenu(this.currentMenu);
|
||||
}
|
||||
}
|
||||
|
||||
/** Execute an IVR action. */
|
||||
private executeAction(action: TIvrAction): void {
|
||||
if (this.destroyed) return;
|
||||
|
||||
switch (action.type) {
|
||||
case 'submenu': {
|
||||
const submenu = this.getMenu(action.menuId);
|
||||
if (submenu) {
|
||||
this.retryCount = 0;
|
||||
this.navigateToMenu(submenu);
|
||||
} else {
|
||||
this.log(`[ivr] submenu "${action.menuId}" not found — hanging up`);
|
||||
this.onAction({ type: 'hangup' });
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'repeat': {
|
||||
if (this.currentMenu) {
|
||||
this.navigateToMenu(this.currentMenu);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'play-message': {
|
||||
// Play a message prompt, then return to the current menu.
|
||||
this.systemLeg.playPrompt(action.promptId, () => {
|
||||
if (this.destroyed || !this.currentMenu) return;
|
||||
this.navigateToMenu(this.currentMenu);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// All other actions (route-extension, route-voicemail, transfer, hangup)
|
||||
// are handled by the CallManager via the onAction callback.
|
||||
this.log(`[ivr] action: ${action.type}`);
|
||||
this.onAction(action);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Look up a menu by ID. */
|
||||
private getMenu(menuId: string): IIvrMenu | null {
|
||||
return this.config.menus.find((m) => m.id === menuId) ?? null;
|
||||
}
|
||||
|
||||
/** Clear the digit timeout timer. */
|
||||
private clearDigitTimeout(): void {
|
||||
if (this.digitTimeout) {
|
||||
clearTimeout(this.digitTimeout);
|
||||
this.digitTimeout = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,306 +0,0 @@
|
||||
/**
|
||||
* Per-provider runtime state and upstream registration.
|
||||
*
|
||||
* Each configured provider gets its own ProviderState instance tracking
|
||||
* registration status, public IP, and the periodic REGISTER cycle.
|
||||
*/
|
||||
|
||||
import { Buffer } from 'node:buffer';
|
||||
import {
|
||||
SipMessage,
|
||||
generateCallId,
|
||||
generateTag,
|
||||
generateBranch,
|
||||
parseDigestChallenge,
|
||||
computeDigestAuth,
|
||||
} from './sip/index.ts';
|
||||
import type { IEndpoint } from './sip/index.ts';
|
||||
import type { IProviderConfig } from './config.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Provider state
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export class ProviderState {
|
||||
readonly config: IProviderConfig;
|
||||
publicIp: string | null;
|
||||
isRegistered = false;
|
||||
registeredAor: string;
|
||||
|
||||
// Registration transaction state.
|
||||
private regCallId: string;
|
||||
private regCSeq = 0;
|
||||
private regFromTag: string;
|
||||
private regTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private sendSip: ((buf: Buffer, dest: IEndpoint) => void) | null = null;
|
||||
private logFn: ((msg: string) => void) | null = null;
|
||||
private onRegistrationChange: ((provider: ProviderState) => void) | null = null;
|
||||
|
||||
constructor(config: IProviderConfig, publicIpSeed: string | null) {
|
||||
this.config = config;
|
||||
this.publicIp = publicIpSeed;
|
||||
this.registeredAor = `sip:${config.username}@${config.domain}`;
|
||||
this.regCallId = generateCallId();
|
||||
this.regFromTag = generateTag();
|
||||
}
|
||||
|
||||
private log(msg: string): void {
|
||||
this.logFn?.(`[provider:${this.config.id}] ${msg}`);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Upstream registration
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Start the periodic REGISTER cycle with this provider.
|
||||
*/
|
||||
startRegistration(
|
||||
lanIp: string,
|
||||
lanPort: number,
|
||||
sendSip: (buf: Buffer, dest: IEndpoint) => void,
|
||||
log: (msg: string) => void,
|
||||
onRegistrationChange: (provider: ProviderState) => void,
|
||||
): void {
|
||||
this.sendSip = sendSip;
|
||||
this.logFn = log;
|
||||
this.onRegistrationChange = onRegistrationChange;
|
||||
|
||||
// Initial registration.
|
||||
this.sendRegister(lanIp, lanPort);
|
||||
|
||||
// Re-register periodically.
|
||||
const intervalMs = (this.config.registerIntervalSec * 0.85) * 1000;
|
||||
this.regTimer = setInterval(() => this.sendRegister(lanIp, lanPort), intervalMs);
|
||||
}
|
||||
|
||||
stopRegistration(): void {
|
||||
if (this.regTimer) {
|
||||
clearInterval(this.regTimer);
|
||||
this.regTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private sendRegister(lanIp: string, lanPort: number): void {
|
||||
this.regCSeq++;
|
||||
const pub = this.publicIp || lanIp;
|
||||
const { config } = this;
|
||||
|
||||
const register = SipMessage.createRequest('REGISTER', `sip:${config.domain}`, {
|
||||
via: { host: pub, port: lanPort },
|
||||
from: { uri: this.registeredAor, tag: this.regFromTag },
|
||||
to: { uri: this.registeredAor },
|
||||
callId: this.regCallId,
|
||||
cseq: this.regCSeq,
|
||||
contact: `<sip:${config.username}@${pub}:${lanPort}>`,
|
||||
maxForwards: 70,
|
||||
extraHeaders: [
|
||||
['Expires', String(config.registerIntervalSec)],
|
||||
['User-Agent', 'SipRouter/1.0'],
|
||||
['Allow', 'INVITE, ACK, OPTIONS, CANCEL, BYE, SUBSCRIBE, NOTIFY, INFO, REFER, UPDATE'],
|
||||
],
|
||||
});
|
||||
|
||||
this.log(`REGISTER -> ${config.outboundProxy.address}:${config.outboundProxy.port} (CSeq ${this.regCSeq})`);
|
||||
this.sendSip!(register.serialize(), config.outboundProxy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming SIP response that belongs to this provider's registration.
|
||||
* Returns true if the message was consumed.
|
||||
*/
|
||||
handleRegistrationResponse(msg: SipMessage): boolean {
|
||||
if (!msg.isResponse) return false;
|
||||
if (msg.callId !== this.regCallId) return false;
|
||||
if (msg.cseqMethod?.toUpperCase() !== 'REGISTER') return false;
|
||||
|
||||
const code = msg.statusCode ?? 0;
|
||||
this.log(`REGISTER <- ${code}`);
|
||||
|
||||
if (code === 200) {
|
||||
const wasRegistered = this.isRegistered;
|
||||
this.isRegistered = true;
|
||||
if (!wasRegistered) {
|
||||
this.log('registered');
|
||||
this.onRegistrationChange?.(this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (code === 401 || code === 407) {
|
||||
const challengeHeader = code === 401
|
||||
? msg.getHeader('WWW-Authenticate')
|
||||
: msg.getHeader('Proxy-Authenticate');
|
||||
|
||||
if (!challengeHeader) {
|
||||
this.log(`${code} but no challenge header`);
|
||||
return true;
|
||||
}
|
||||
|
||||
const challenge = parseDigestChallenge(challengeHeader);
|
||||
if (!challenge) {
|
||||
this.log(`${code} could not parse digest challenge`);
|
||||
return true;
|
||||
}
|
||||
|
||||
const authValue = computeDigestAuth({
|
||||
username: this.config.username,
|
||||
password: this.config.password,
|
||||
realm: challenge.realm,
|
||||
nonce: challenge.nonce,
|
||||
method: 'REGISTER',
|
||||
uri: `sip:${this.config.domain}`,
|
||||
algorithm: challenge.algorithm,
|
||||
opaque: challenge.opaque,
|
||||
});
|
||||
|
||||
// Resend REGISTER with auth.
|
||||
this.regCSeq++;
|
||||
const pub = this.publicIp || 'unknown';
|
||||
// We need lanIp/lanPort but don't have them here — reconstruct from Via.
|
||||
const via = msg.getHeader('Via') || '';
|
||||
const viaHost = via.match(/SIP\/2\.0\/UDP\s+([^;:]+)/)?.[1] || pub;
|
||||
const viaPort = parseInt(via.match(/:(\d+)/)?.[1] || '5070', 10);
|
||||
|
||||
const register = SipMessage.createRequest('REGISTER', `sip:${this.config.domain}`, {
|
||||
via: { host: viaHost, port: viaPort },
|
||||
from: { uri: this.registeredAor, tag: this.regFromTag },
|
||||
to: { uri: this.registeredAor },
|
||||
callId: this.regCallId,
|
||||
cseq: this.regCSeq,
|
||||
contact: `<sip:${this.config.username}@${viaHost}:${viaPort}>`,
|
||||
maxForwards: 70,
|
||||
extraHeaders: [
|
||||
[code === 401 ? 'Authorization' : 'Proxy-Authorization', authValue],
|
||||
['Expires', String(this.config.registerIntervalSec)],
|
||||
['User-Agent', 'SipRouter/1.0'],
|
||||
['Allow', 'INVITE, ACK, OPTIONS, CANCEL, BYE, SUBSCRIBE, NOTIFY, INFO, REFER, UPDATE'],
|
||||
],
|
||||
});
|
||||
|
||||
this.log(`REGISTER -> (with auth, CSeq ${this.regCSeq})`);
|
||||
this.sendSip!(register.serialize(), this.config.outboundProxy);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (code >= 400) {
|
||||
const wasRegistered = this.isRegistered;
|
||||
this.isRegistered = false;
|
||||
if (wasRegistered) {
|
||||
this.log(`registration lost (${code})`);
|
||||
this.onRegistrationChange?.(this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return true; // consume 1xx etc.
|
||||
}
|
||||
|
||||
/**
|
||||
* Update public IP from Via received= parameter.
|
||||
*/
|
||||
detectPublicIp(via: string): void {
|
||||
const m = via.match(/received=([\d.]+)/);
|
||||
if (m && m[1] !== this.publicIp) {
|
||||
this.log(`publicIp = ${m[1]}`);
|
||||
this.publicIp = m[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Provider state management
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
let providerStates: Map<string, ProviderState>;
|
||||
|
||||
export function initProviderStates(
|
||||
providers: IProviderConfig[],
|
||||
publicIpSeed: string | null,
|
||||
): Map<string, ProviderState> {
|
||||
providerStates = new Map();
|
||||
for (const p of providers) {
|
||||
providerStates.set(p.id, new ProviderState(p, publicIpSeed));
|
||||
}
|
||||
return providerStates;
|
||||
}
|
||||
|
||||
export function getProviderState(id: string): ProviderState | null {
|
||||
return providerStates?.get(id) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync running provider states with updated config.
|
||||
* - New providers: create state + start registration.
|
||||
* - Removed providers: stop registration + delete state.
|
||||
* - Changed providers: stop old, create new, start registration (preserves detected publicIp).
|
||||
*/
|
||||
export function syncProviderStates(
|
||||
newProviders: IProviderConfig[],
|
||||
publicIpSeed: string | null,
|
||||
lanIp: string,
|
||||
lanPort: number,
|
||||
sendSip: (buf: Buffer, dest: IEndpoint) => void,
|
||||
log: (msg: string) => void,
|
||||
onRegistrationChange: (provider: ProviderState) => void,
|
||||
): void {
|
||||
if (!providerStates) return;
|
||||
|
||||
const newIds = new Set(newProviders.map(p => p.id));
|
||||
const oldIds = new Set(providerStates.keys());
|
||||
|
||||
// Remove providers no longer in config.
|
||||
for (const id of oldIds) {
|
||||
if (!newIds.has(id)) {
|
||||
const ps = providerStates.get(id)!;
|
||||
ps.stopRegistration();
|
||||
providerStates.delete(id);
|
||||
log(`[provider:${id}] removed`);
|
||||
}
|
||||
}
|
||||
|
||||
for (const p of newProviders) {
|
||||
if (!oldIds.has(p.id)) {
|
||||
// New provider.
|
||||
const ps = new ProviderState(p, publicIpSeed);
|
||||
providerStates.set(p.id, ps);
|
||||
ps.startRegistration(lanIp, lanPort, sendSip, log, onRegistrationChange);
|
||||
log(`[provider:${p.id}] added — registration started`);
|
||||
} else {
|
||||
// Existing provider — check if config changed.
|
||||
const existing = providerStates.get(p.id)!;
|
||||
if (JSON.stringify(existing.config) !== JSON.stringify(p)) {
|
||||
existing.stopRegistration();
|
||||
const ps = new ProviderState(p, existing.publicIp || publicIpSeed);
|
||||
providerStates.set(p.id, ps);
|
||||
ps.startRegistration(lanIp, lanPort, sendSip, log, onRegistrationChange);
|
||||
log(`[provider:${p.id}] config changed — re-registering`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find which provider sent a packet, by matching the source address
|
||||
* against all providers' outbound proxy addresses.
|
||||
*/
|
||||
export function getProviderByUpstreamAddress(address: string, port: number): ProviderState | null {
|
||||
if (!providerStates) return null;
|
||||
for (const ps of providerStates.values()) {
|
||||
if (ps.config.outboundProxy.address === address && ps.config.outboundProxy.port === port) {
|
||||
return ps;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a response belongs to any provider's registration transaction.
|
||||
*/
|
||||
export function handleProviderRegistrationResponse(msg: SipMessage): boolean {
|
||||
if (!providerStates || !msg.isResponse) return false;
|
||||
for (const ps of providerStates.values()) {
|
||||
if (ps.handleRegistrationResponse(msg)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -41,6 +41,44 @@ type TProxyCommands = {
|
||||
params: { call_id: string };
|
||||
result: { file_path: string; duration_ms: number };
|
||||
};
|
||||
add_device_leg: {
|
||||
params: { call_id: string; device_id: string };
|
||||
result: { leg_id: string };
|
||||
};
|
||||
transfer_leg: {
|
||||
params: { source_call_id: string; leg_id: string; target_call_id: string };
|
||||
result: Record<string, never>;
|
||||
};
|
||||
replace_leg: {
|
||||
params: { call_id: string; old_leg_id: string; number: string; provider_id?: string };
|
||||
result: { new_leg_id: string };
|
||||
};
|
||||
start_interaction: {
|
||||
params: {
|
||||
call_id: string;
|
||||
leg_id: string;
|
||||
prompt_wav: string;
|
||||
expected_digits: string;
|
||||
timeout_ms: number;
|
||||
};
|
||||
result: { result: 'digit' | 'timeout' | 'cancelled'; digit?: string };
|
||||
};
|
||||
add_tool_leg: {
|
||||
params: {
|
||||
call_id: string;
|
||||
tool_type: 'recording' | 'transcription';
|
||||
config?: Record<string, unknown>;
|
||||
};
|
||||
result: { tool_leg_id: string };
|
||||
};
|
||||
remove_tool_leg: {
|
||||
params: { call_id: string; tool_leg_id: string };
|
||||
result: Record<string, never>;
|
||||
};
|
||||
set_leg_metadata: {
|
||||
params: { call_id: string; leg_id: string; key: string; value: unknown };
|
||||
result: Record<string, never>;
|
||||
};
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -157,6 +195,24 @@ export async function configureProxyEngine(config: Record<string, unknown>): Pro
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate an outbound call via Rust. Returns the call ID or null on failure.
|
||||
*/
|
||||
export async function makeCall(number: string, deviceId?: string, providerId?: string): Promise<string | null> {
|
||||
if (!bridge || !initialized) return null;
|
||||
try {
|
||||
const result = await bridge.sendCommand('make_call', {
|
||||
number,
|
||||
device_id: deviceId,
|
||||
provider_id: providerId,
|
||||
} as any);
|
||||
return (result as any)?.call_id || null;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] make_call error: ${e?.message || e}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a hangup command.
|
||||
*/
|
||||
@@ -170,11 +226,262 @@ export async function hangupCall(callId: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a WebRTC offer to the proxy engine. Returns the SDP answer.
|
||||
*/
|
||||
export async function webrtcOffer(sessionId: string, sdp: string): Promise<{ sdp: string } | null> {
|
||||
if (!bridge || !initialized) return null;
|
||||
try {
|
||||
const result = await bridge.sendCommand('webrtc_offer', { session_id: sessionId, sdp } as any);
|
||||
return result as any;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] webrtc_offer error: ${e?.message || e}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward an ICE candidate to the proxy engine.
|
||||
*/
|
||||
export async function webrtcIce(sessionId: string, candidate: any): Promise<void> {
|
||||
if (!bridge || !initialized) return;
|
||||
try {
|
||||
await bridge.sendCommand('webrtc_ice', {
|
||||
session_id: sessionId,
|
||||
candidate: candidate?.candidate || candidate,
|
||||
sdp_mid: candidate?.sdpMid,
|
||||
sdp_mline_index: candidate?.sdpMLineIndex,
|
||||
} as any);
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
/**
|
||||
* Link a WebRTC session to a SIP call — enables audio bridging.
|
||||
* The browser's Opus audio will be transcoded and sent to the provider.
|
||||
*/
|
||||
export async function webrtcLink(sessionId: string, callId: string, providerMediaAddr: string, providerMediaPort: number, sipPt: number = 9): Promise<boolean> {
|
||||
if (!bridge || !initialized) return false;
|
||||
try {
|
||||
await bridge.sendCommand('webrtc_link', {
|
||||
session_id: sessionId,
|
||||
call_id: callId,
|
||||
provider_media_addr: providerMediaAddr,
|
||||
provider_media_port: providerMediaPort,
|
||||
sip_pt: sipPt,
|
||||
} as any);
|
||||
return true;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] webrtc_link error: ${e?.message || e}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an external SIP leg to an existing call (multiparty).
|
||||
*/
|
||||
export async function addLeg(callId: string, number: string, providerId?: string): Promise<string | null> {
|
||||
if (!bridge || !initialized) return null;
|
||||
try {
|
||||
const result = await bridge.sendCommand('add_leg', {
|
||||
call_id: callId,
|
||||
number,
|
||||
provider_id: providerId,
|
||||
} as any);
|
||||
return (result as any)?.leg_id || null;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] add_leg error: ${e?.message || e}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a leg from a call.
|
||||
*/
|
||||
export async function removeLeg(callId: string, legId: string): Promise<boolean> {
|
||||
if (!bridge || !initialized) return false;
|
||||
try {
|
||||
await bridge.sendCommand('remove_leg', { call_id: callId, leg_id: legId } as any);
|
||||
return true;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] remove_leg error: ${e?.message || e}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a WebRTC session.
|
||||
*/
|
||||
export async function webrtcClose(sessionId: string): Promise<void> {
|
||||
if (!bridge || !initialized) return;
|
||||
try {
|
||||
await bridge.sendCommand('webrtc_close', { session_id: sessionId } as any);
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Device leg & interaction commands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Add a local SIP device to an existing call (mid-call INVITE to desk phone).
|
||||
*/
|
||||
export async function addDeviceLeg(callId: string, deviceId: string): Promise<string | null> {
|
||||
if (!bridge || !initialized) return null;
|
||||
try {
|
||||
const result = await bridge.sendCommand('add_device_leg', {
|
||||
call_id: callId,
|
||||
device_id: deviceId,
|
||||
} as any);
|
||||
return (result as any)?.leg_id || null;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] add_device_leg error: ${e?.message || e}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer a leg from one call to another (leg stays connected, switches mixer).
|
||||
*/
|
||||
export async function transferLeg(
|
||||
sourceCallId: string,
|
||||
legId: string,
|
||||
targetCallId: string,
|
||||
): Promise<boolean> {
|
||||
if (!bridge || !initialized) return false;
|
||||
try {
|
||||
await bridge.sendCommand('transfer_leg', {
|
||||
source_call_id: sourceCallId,
|
||||
leg_id: legId,
|
||||
target_call_id: targetCallId,
|
||||
} as any);
|
||||
return true;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] transfer_leg error: ${e?.message || e}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a leg: terminate the old leg and dial a new number into the same call.
|
||||
*/
|
||||
export async function replaceLeg(
|
||||
callId: string,
|
||||
oldLegId: string,
|
||||
number: string,
|
||||
providerId?: string,
|
||||
): Promise<string | null> {
|
||||
if (!bridge || !initialized) return null;
|
||||
try {
|
||||
const result = await bridge.sendCommand('replace_leg', {
|
||||
call_id: callId,
|
||||
old_leg_id: oldLegId,
|
||||
number,
|
||||
provider_id: providerId,
|
||||
} as any);
|
||||
return (result as any)?.new_leg_id || null;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] replace_leg error: ${e?.message || e}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start an interaction on a specific leg — isolate it, play a prompt, collect DTMF.
|
||||
* Blocks until the interaction completes (digit pressed, timeout, or cancelled).
|
||||
*/
|
||||
export async function startInteraction(
|
||||
callId: string,
|
||||
legId: string,
|
||||
promptWav: string,
|
||||
expectedDigits: string,
|
||||
timeoutMs: number,
|
||||
): Promise<{ result: 'digit' | 'timeout' | 'cancelled'; digit?: string } | null> {
|
||||
if (!bridge || !initialized) return null;
|
||||
try {
|
||||
const result = await bridge.sendCommand('start_interaction', {
|
||||
call_id: callId,
|
||||
leg_id: legId,
|
||||
prompt_wav: promptWav,
|
||||
expected_digits: expectedDigits,
|
||||
timeout_ms: timeoutMs,
|
||||
} as any);
|
||||
return result as any;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] start_interaction error: ${e?.message || e}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a tool leg (recording or transcription) to a call.
|
||||
* Tool legs receive per-source unmerged audio from all participants.
|
||||
*/
|
||||
export async function addToolLeg(
|
||||
callId: string,
|
||||
toolType: 'recording' | 'transcription',
|
||||
config?: Record<string, unknown>,
|
||||
): Promise<string | null> {
|
||||
if (!bridge || !initialized) return null;
|
||||
try {
|
||||
const result = await bridge.sendCommand('add_tool_leg', {
|
||||
call_id: callId,
|
||||
tool_type: toolType,
|
||||
config,
|
||||
} as any);
|
||||
return (result as any)?.tool_leg_id || null;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] add_tool_leg error: ${e?.message || e}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a tool leg from a call. Triggers finalization (WAV files, metadata).
|
||||
*/
|
||||
export async function removeToolLeg(callId: string, toolLegId: string): Promise<boolean> {
|
||||
if (!bridge || !initialized) return false;
|
||||
try {
|
||||
await bridge.sendCommand('remove_tool_leg', {
|
||||
call_id: callId,
|
||||
tool_leg_id: toolLegId,
|
||||
} as any);
|
||||
return true;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] remove_tool_leg error: ${e?.message || e}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a metadata key-value pair on a leg.
|
||||
*/
|
||||
export async function setLegMetadata(
|
||||
callId: string,
|
||||
legId: string,
|
||||
key: string,
|
||||
value: unknown,
|
||||
): Promise<boolean> {
|
||||
if (!bridge || !initialized) return false;
|
||||
try {
|
||||
await bridge.sendCommand('set_leg_metadata', {
|
||||
call_id: callId,
|
||||
leg_id: legId,
|
||||
key,
|
||||
value,
|
||||
} as any);
|
||||
return true;
|
||||
} catch (e: any) {
|
||||
logFn?.(`[proxy-engine] set_leg_metadata error: ${e?.message || e}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to an event from the proxy engine.
|
||||
* Event names: incoming_call, outbound_device_call, call_ringing,
|
||||
* call_answered, call_ended, provider_registered, device_registered,
|
||||
* dtmf_digit, recording_done, sip_unhandled
|
||||
* dtmf_digit, recording_done, tool_recording_done, tool_transcription_done,
|
||||
* leg_added, leg_removed, sip_unhandled
|
||||
*/
|
||||
export function onProxyEvent(event: string, handler: (data: any) => void): void {
|
||||
if (!bridge) throw new Error('proxy engine not initialized');
|
||||
|
||||
214
ts/registrar.ts
214
ts/registrar.ts
@@ -1,118 +1,27 @@
|
||||
/**
|
||||
* Local SIP registrar — accepts REGISTER from devices and browser clients.
|
||||
* Browser device registration.
|
||||
*
|
||||
* Devices point their SIP registration at the proxy instead of the upstream
|
||||
* provider. The registrar responds with 200 OK and stores the device's
|
||||
* current contact (source IP:port). Browser softphones register via
|
||||
* WebSocket signaling.
|
||||
* SIP device registration is now handled entirely by the Rust proxy-engine.
|
||||
* This module only handles browser softphone registration via WebSocket.
|
||||
*/
|
||||
|
||||
import { createHash } from 'node:crypto';
|
||||
import {
|
||||
SipMessage,
|
||||
generateTag,
|
||||
} from './sip/index.ts';
|
||||
|
||||
/** Hash a string to a 6-char hex ID. */
|
||||
export function shortHash(input: string): string {
|
||||
return createHash('sha256').update(input).digest('hex').slice(0, 6);
|
||||
}
|
||||
import type { IEndpoint } from './sip/index.ts';
|
||||
import type { IDeviceConfig } from './config.ts';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// Browser device registration
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface IRegisteredDevice {
|
||||
deviceConfig: IDeviceConfig;
|
||||
contact: IEndpoint | null;
|
||||
registeredAt: number;
|
||||
expiresAt: number;
|
||||
aor: string;
|
||||
connected: boolean;
|
||||
isBrowser: boolean;
|
||||
}
|
||||
|
||||
export interface IDeviceStatusEntry {
|
||||
id: string;
|
||||
displayName: string;
|
||||
contact: IEndpoint | null;
|
||||
aor: string;
|
||||
connected: boolean;
|
||||
isBrowser: boolean;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// State
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const registeredDevices = new Map<string, IRegisteredDevice>();
|
||||
const browserDevices = new Map<string, IRegisteredDevice>();
|
||||
let knownDevices: IDeviceConfig[] = [];
|
||||
let logFn: (msg: string) => void = () => {};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public API
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function initRegistrar(
|
||||
devices: IDeviceConfig[],
|
||||
log: (msg: string) => void,
|
||||
): void {
|
||||
knownDevices = devices;
|
||||
logFn = log;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a REGISTER from a SIP device. Returns a 200 OK response to send back,
|
||||
* or null if this REGISTER should not be handled by the local registrar.
|
||||
*/
|
||||
export function handleDeviceRegister(
|
||||
msg: SipMessage,
|
||||
rinfo: IEndpoint,
|
||||
): SipMessage | null {
|
||||
if (msg.method !== 'REGISTER') return null;
|
||||
|
||||
const device = knownDevices.find((d) => d.expectedAddress === rinfo.address);
|
||||
if (!device) return null;
|
||||
|
||||
const from = msg.getHeader('From');
|
||||
const aor = from ? SipMessage.extractUri(from) || `sip:${device.extension}@${rinfo.address}` : `sip:${device.extension}@${rinfo.address}`;
|
||||
|
||||
const MAX_EXPIRES = 300;
|
||||
const expiresHeader = msg.getHeader('Expires');
|
||||
const requested = expiresHeader ? parseInt(expiresHeader, 10) : 3600;
|
||||
const expires = Math.min(requested, MAX_EXPIRES);
|
||||
|
||||
const entry: IRegisteredDevice = {
|
||||
deviceConfig: device,
|
||||
contact: { address: rinfo.address, port: rinfo.port },
|
||||
registeredAt: Date.now(),
|
||||
expiresAt: Date.now() + expires * 1000,
|
||||
aor,
|
||||
connected: true,
|
||||
isBrowser: false,
|
||||
};
|
||||
registeredDevices.set(device.id, entry);
|
||||
|
||||
logFn(`[registrar] ${device.displayName} (${device.id}) registered from ${rinfo.address}:${rinfo.port} expires=${expires}`);
|
||||
|
||||
const contact = msg.getHeader('Contact') || `<sip:${rinfo.address}:${rinfo.port}>`;
|
||||
const response = SipMessage.createResponse(200, 'OK', msg, {
|
||||
toTag: generateTag(),
|
||||
contact,
|
||||
extraHeaders: [['Expires', String(expires)]],
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
const browserDevices = new Map<string, { deviceId: string; displayName: string; remoteIp: string }>();
|
||||
|
||||
/**
|
||||
* Register a browser softphone as a device.
|
||||
*/
|
||||
export function registerBrowserDevice(sessionId: string, userAgent?: string, remoteIp?: string): void {
|
||||
// Extract a short browser name from the UA string.
|
||||
let browserName = 'Browser';
|
||||
if (userAgent) {
|
||||
if (userAgent.includes('Firefox/')) browserName = 'Firefox';
|
||||
@@ -121,21 +30,11 @@ export function registerBrowserDevice(sessionId: string, userAgent?: string, rem
|
||||
else if (userAgent.includes('Safari/') && !userAgent.includes('Chrome/')) browserName = 'Safari';
|
||||
}
|
||||
|
||||
const entry: IRegisteredDevice = {
|
||||
deviceConfig: {
|
||||
id: `browser-${shortHash(sessionId)}`,
|
||||
displayName: browserName,
|
||||
expectedAddress: remoteIp || '127.0.0.1',
|
||||
extension: 'webrtc',
|
||||
},
|
||||
contact: null,
|
||||
registeredAt: Date.now(),
|
||||
expiresAt: Date.now() + 60 * 1000, // 60s — browser must re-register to stay alive
|
||||
aor: `sip:webrtc@browser`,
|
||||
connected: true,
|
||||
isBrowser: true,
|
||||
};
|
||||
browserDevices.set(sessionId, entry);
|
||||
browserDevices.set(sessionId, {
|
||||
deviceId: `browser-${shortHash(sessionId)}`,
|
||||
displayName: browserName,
|
||||
remoteIp: remoteIp || '127.0.0.1',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,96 +43,3 @@ export function registerBrowserDevice(sessionId: string, userAgent?: string, rem
|
||||
export function unregisterBrowserDevice(sessionId: string): void {
|
||||
browserDevices.delete(sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a registered device by its config ID.
|
||||
*/
|
||||
export function getRegisteredDevice(deviceId: string): IRegisteredDevice | null {
|
||||
const entry = registeredDevices.get(deviceId);
|
||||
if (!entry) return null;
|
||||
if (Date.now() > entry.expiresAt) {
|
||||
registeredDevices.delete(deviceId);
|
||||
return null;
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a registered device by source IP address.
|
||||
*/
|
||||
export function getRegisteredDeviceByAddress(address: string): IRegisteredDevice | null {
|
||||
for (const entry of registeredDevices.values()) {
|
||||
if (entry.contact?.address === address && Date.now() <= entry.expiresAt) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether an address belongs to a known device (by config expectedAddress).
|
||||
*/
|
||||
export function isKnownDeviceAddress(address: string): boolean {
|
||||
return knownDevices.some((d) => d.expectedAddress === address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all devices for the dashboard.
|
||||
* - Configured devices always show (connected or not).
|
||||
* - Browser devices only show while connected.
|
||||
*/
|
||||
export function getAllDeviceStatuses(): IDeviceStatusEntry[] {
|
||||
const now = Date.now();
|
||||
const result: IDeviceStatusEntry[] = [];
|
||||
|
||||
// Configured devices — always show.
|
||||
for (const dc of knownDevices) {
|
||||
const reg = registeredDevices.get(dc.id);
|
||||
const connected = reg ? now <= reg.expiresAt : false;
|
||||
if (reg && now > reg.expiresAt) {
|
||||
registeredDevices.delete(dc.id);
|
||||
}
|
||||
result.push({
|
||||
id: dc.id,
|
||||
displayName: dc.displayName,
|
||||
contact: connected && reg ? reg.contact : null,
|
||||
aor: reg?.aor || `sip:${dc.extension}@${dc.expectedAddress}`,
|
||||
connected,
|
||||
isBrowser: false,
|
||||
});
|
||||
}
|
||||
|
||||
// Browser devices — only while connected.
|
||||
for (const [, entry] of browserDevices) {
|
||||
const ip = entry.deviceConfig.expectedAddress;
|
||||
result.push({
|
||||
id: entry.deviceConfig.id,
|
||||
displayName: entry.deviceConfig.displayName,
|
||||
contact: ip && ip !== '127.0.0.1' ? { address: ip, port: 0 } : null,
|
||||
aor: entry.aor,
|
||||
connected: true,
|
||||
isBrowser: true,
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all currently registered (connected) SIP devices.
|
||||
*/
|
||||
export function getAllRegisteredDevices(): IRegisteredDevice[] {
|
||||
const now = Date.now();
|
||||
const result: IRegisteredDevice[] = [];
|
||||
for (const [id, entry] of registeredDevices) {
|
||||
if (now > entry.expiresAt) {
|
||||
registeredDevices.delete(id);
|
||||
} else {
|
||||
result.push(entry);
|
||||
}
|
||||
}
|
||||
for (const [, entry] of browserDevices) {
|
||||
result.push(entry);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
280
ts/sip/dialog.ts
280
ts/sip/dialog.ts
@@ -1,280 +0,0 @@
|
||||
/**
|
||||
* SipDialog — tracks the state of a SIP dialog (RFC 3261 §12).
|
||||
*
|
||||
* A dialog is created by a dialog-establishing request (INVITE, SUBSCRIBE, …)
|
||||
* and its 1xx/2xx response. It manages local/remote tags, CSeq counters,
|
||||
* the route set, and provides helpers to build in-dialog requests (ACK, BYE,
|
||||
* re-INVITE, …).
|
||||
*
|
||||
* Usage:
|
||||
* ```ts
|
||||
* // Caller (UAC) side — create from the outgoing INVITE we just sent:
|
||||
* const dialog = SipDialog.fromUacInvite(invite);
|
||||
*
|
||||
* // When a 200 OK arrives:
|
||||
* dialog.processResponse(response200);
|
||||
*
|
||||
* // Build ACK for the 2xx:
|
||||
* const ack = dialog.createAck();
|
||||
*
|
||||
* // Later — hang up:
|
||||
* const bye = dialog.createRequest('BYE');
|
||||
* ```
|
||||
*/
|
||||
|
||||
import { SipMessage } from './message.ts';
|
||||
import { generateTag, generateBranch } from './helpers.ts';
|
||||
import { Buffer } from 'node:buffer';
|
||||
|
||||
export type TDialogState = 'early' | 'confirmed' | 'terminated';
|
||||
|
||||
export class SipDialog {
|
||||
callId: string;
|
||||
localTag: string;
|
||||
remoteTag: string | null = null;
|
||||
localUri: string;
|
||||
remoteUri: string;
|
||||
localCSeq: number;
|
||||
remoteCSeq: number = 0;
|
||||
routeSet: string[] = [];
|
||||
remoteTarget: string; // Contact URI of the remote party
|
||||
state: TDialogState = 'early';
|
||||
|
||||
// Transport info for sending in-dialog messages.
|
||||
localHost: string;
|
||||
localPort: number;
|
||||
|
||||
constructor(options: {
|
||||
callId: string;
|
||||
localTag: string;
|
||||
remoteTag?: string;
|
||||
localUri: string;
|
||||
remoteUri: string;
|
||||
localCSeq: number;
|
||||
remoteTarget: string;
|
||||
localHost: string;
|
||||
localPort: number;
|
||||
routeSet?: string[];
|
||||
}) {
|
||||
this.callId = options.callId;
|
||||
this.localTag = options.localTag;
|
||||
this.remoteTag = options.remoteTag ?? null;
|
||||
this.localUri = options.localUri;
|
||||
this.remoteUri = options.remoteUri;
|
||||
this.localCSeq = options.localCSeq;
|
||||
this.remoteTarget = options.remoteTarget;
|
||||
this.localHost = options.localHost;
|
||||
this.localPort = options.localPort;
|
||||
this.routeSet = options.routeSet ?? [];
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Factory: create dialog from an outgoing INVITE (UAC side)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Create a dialog from an INVITE we are sending.
|
||||
* The dialog enters "early" state; call `processResponse()` when
|
||||
* provisional or final responses arrive.
|
||||
*/
|
||||
static fromUacInvite(invite: SipMessage, localHost: string, localPort: number): SipDialog {
|
||||
const from = invite.getHeader('From') || '';
|
||||
const to = invite.getHeader('To') || '';
|
||||
return new SipDialog({
|
||||
callId: invite.callId,
|
||||
localTag: SipMessage.extractTag(from) || generateTag(),
|
||||
localUri: SipMessage.extractUri(from) || '',
|
||||
remoteUri: SipMessage.extractUri(to) || '',
|
||||
localCSeq: parseInt((invite.getHeader('CSeq') || '1').split(/\s+/)[0], 10),
|
||||
remoteTarget: invite.requestUri || SipMessage.extractUri(to) || '',
|
||||
localHost: localHost,
|
||||
localPort: localPort,
|
||||
});
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Factory: create dialog from an incoming INVITE (UAS side)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Create a dialog from an INVITE we received.
|
||||
* Typically used when acting as a UAS (e.g. for call-back scenarios).
|
||||
*/
|
||||
static fromUasInvite(invite: SipMessage, localTag: string, localHost: string, localPort: number): SipDialog {
|
||||
const from = invite.getHeader('From') || '';
|
||||
const to = invite.getHeader('To') || '';
|
||||
const contact = invite.getHeader('Contact');
|
||||
return new SipDialog({
|
||||
callId: invite.callId,
|
||||
localTag,
|
||||
remoteTag: SipMessage.extractTag(from) || undefined,
|
||||
localUri: SipMessage.extractUri(to) || '',
|
||||
remoteUri: SipMessage.extractUri(from) || '',
|
||||
localCSeq: 0,
|
||||
remoteTarget: (contact ? SipMessage.extractUri(contact) : null) || SipMessage.extractUri(from) || '',
|
||||
localHost,
|
||||
localPort,
|
||||
});
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Response processing
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Update dialog state from a received response.
|
||||
* - 1xx with To-tag → early dialog
|
||||
* - 2xx → confirmed dialog
|
||||
* - 3xx–6xx → terminated
|
||||
*/
|
||||
processResponse(response: SipMessage): void {
|
||||
const to = response.getHeader('To') || '';
|
||||
const tag = SipMessage.extractTag(to);
|
||||
const code = response.statusCode ?? 0;
|
||||
// Always update remoteTag from 2xx (RFC 3261 §12.1.2: tag in 2xx is definitive).
|
||||
if (tag && (code >= 200 && code < 300)) {
|
||||
this.remoteTag = tag;
|
||||
} else if (tag && !this.remoteTag) {
|
||||
this.remoteTag = tag;
|
||||
}
|
||||
|
||||
// Update remote target from Contact.
|
||||
const contact = response.getHeader('Contact');
|
||||
if (contact) {
|
||||
const uri = SipMessage.extractUri(contact);
|
||||
if (uri) this.remoteTarget = uri;
|
||||
}
|
||||
|
||||
// Record-Route → route set (in reverse for UAC).
|
||||
if (this.state === 'early') {
|
||||
const rr: string[] = [];
|
||||
for (const [n, v] of response.headers) {
|
||||
if (n.toLowerCase() === 'record-route') rr.push(v);
|
||||
}
|
||||
if (rr.length) this.routeSet = rr.reverse();
|
||||
}
|
||||
|
||||
if (code >= 200 && code < 300) {
|
||||
this.state = 'confirmed';
|
||||
} else if (code >= 300) {
|
||||
this.state = 'terminated';
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Request building
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Build an in-dialog request (BYE, re-INVITE, INFO, …).
|
||||
* Automatically increments the local CSeq.
|
||||
*/
|
||||
createRequest(method: string, options?: {
|
||||
body?: string;
|
||||
contentType?: string;
|
||||
extraHeaders?: [string, string][];
|
||||
}): SipMessage {
|
||||
this.localCSeq++;
|
||||
const branch = generateBranch();
|
||||
|
||||
const headers: [string, string][] = [
|
||||
['Via', `SIP/2.0/UDP ${this.localHost}:${this.localPort};branch=${branch};rport`],
|
||||
['From', `<${this.localUri}>;tag=${this.localTag}`],
|
||||
['To', `<${this.remoteUri}>${this.remoteTag ? `;tag=${this.remoteTag}` : ''}`],
|
||||
['Call-ID', this.callId],
|
||||
['CSeq', `${this.localCSeq} ${method}`],
|
||||
['Max-Forwards', '70'],
|
||||
];
|
||||
|
||||
// Route set → Route headers.
|
||||
for (const route of this.routeSet) {
|
||||
headers.push(['Route', route]);
|
||||
}
|
||||
|
||||
headers.push(['Contact', `<sip:${this.localHost}:${this.localPort}>`]);
|
||||
|
||||
if (options?.extraHeaders) headers.push(...options.extraHeaders);
|
||||
|
||||
const body = options?.body || '';
|
||||
if (body && options?.contentType) {
|
||||
headers.push(['Content-Type', options.contentType]);
|
||||
}
|
||||
headers.push(['Content-Length', String(Buffer.byteLength(body, 'utf8'))]);
|
||||
|
||||
// Determine Request-URI from route set or remote target.
|
||||
let ruri = this.remoteTarget;
|
||||
if (this.routeSet.length) {
|
||||
const topRoute = SipMessage.extractUri(this.routeSet[0]);
|
||||
if (topRoute && topRoute.includes(';lr')) {
|
||||
ruri = this.remoteTarget; // loose routing — RURI stays as remote target
|
||||
} else if (topRoute) {
|
||||
ruri = topRoute; // strict routing — top route becomes RURI
|
||||
}
|
||||
}
|
||||
|
||||
return new SipMessage(`${method} ${ruri} SIP/2.0`, headers, body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an ACK for a 2xx response to INVITE (RFC 3261 §13.2.2.4).
|
||||
* ACK for 2xx is a new transaction, so it gets its own Via/branch.
|
||||
*/
|
||||
createAck(): SipMessage {
|
||||
const branch = generateBranch();
|
||||
|
||||
const headers: [string, string][] = [
|
||||
['Via', `SIP/2.0/UDP ${this.localHost}:${this.localPort};branch=${branch};rport`],
|
||||
['From', `<${this.localUri}>;tag=${this.localTag}`],
|
||||
['To', `<${this.remoteUri}>${this.remoteTag ? `;tag=${this.remoteTag}` : ''}`],
|
||||
['Call-ID', this.callId],
|
||||
['CSeq', `${this.localCSeq} ACK`],
|
||||
['Max-Forwards', '70'],
|
||||
];
|
||||
|
||||
for (const route of this.routeSet) {
|
||||
headers.push(['Route', route]);
|
||||
}
|
||||
|
||||
headers.push(['Content-Length', '0']);
|
||||
|
||||
let ruri = this.remoteTarget;
|
||||
if (this.routeSet.length) {
|
||||
const topRoute = SipMessage.extractUri(this.routeSet[0]);
|
||||
if (topRoute && topRoute.includes(';lr')) {
|
||||
ruri = this.remoteTarget;
|
||||
} else if (topRoute) {
|
||||
ruri = topRoute;
|
||||
}
|
||||
}
|
||||
|
||||
return new SipMessage(`ACK ${ruri} SIP/2.0`, headers, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a CANCEL for the original INVITE (same branch, CSeq).
|
||||
* Used before the dialog is confirmed.
|
||||
*/
|
||||
createCancel(originalInvite: SipMessage): SipMessage {
|
||||
const via = originalInvite.getHeader('Via') || '';
|
||||
const from = originalInvite.getHeader('From') || '';
|
||||
const to = originalInvite.getHeader('To') || '';
|
||||
|
||||
const headers: [string, string][] = [
|
||||
['Via', via],
|
||||
['From', from],
|
||||
['To', to],
|
||||
['Call-ID', this.callId],
|
||||
['CSeq', `${this.localCSeq} CANCEL`],
|
||||
['Max-Forwards', '70'],
|
||||
['Content-Length', '0'],
|
||||
];
|
||||
|
||||
const ruri = originalInvite.requestUri || this.remoteTarget;
|
||||
return new SipMessage(`CANCEL ${ruri} SIP/2.0`, headers, '');
|
||||
}
|
||||
|
||||
/** Transition the dialog to terminated state. */
|
||||
terminate(): void {
|
||||
this.state = 'terminated';
|
||||
}
|
||||
}
|
||||
@@ -1,241 +0,0 @@
|
||||
/**
|
||||
* SIP helper utilities — ID generation and SDP construction.
|
||||
*/
|
||||
|
||||
import { randomBytes, createHash } from 'node:crypto';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ID generators
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Generate a random SIP Call-ID. */
|
||||
export function generateCallId(domain?: string): string {
|
||||
const id = randomBytes(16).toString('hex');
|
||||
return domain ? `${id}@${domain}` : id;
|
||||
}
|
||||
|
||||
/** Generate a random SIP From/To tag. */
|
||||
export function generateTag(): string {
|
||||
return randomBytes(8).toString('hex');
|
||||
}
|
||||
|
||||
/** Generate a RFC 3261 compliant Via branch (starts with z9hG4bK magic cookie). */
|
||||
export function generateBranch(): string {
|
||||
return `z9hG4bK-${randomBytes(8).toString('hex')}`;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Codec registry
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const CODEC_NAMES: Record<number, string> = {
|
||||
0: 'PCMU/8000',
|
||||
3: 'GSM/8000',
|
||||
4: 'G723/8000',
|
||||
8: 'PCMA/8000',
|
||||
9: 'G722/8000',
|
||||
18: 'G729/8000',
|
||||
101: 'telephone-event/8000',
|
||||
};
|
||||
|
||||
/** Look up the rtpmap name for a static payload type. */
|
||||
export function codecName(pt: number): string {
|
||||
return CODEC_NAMES[pt] || `unknown/${pt}`;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SDP builder
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface ISdpOptions {
|
||||
/** IP address for the c= and o= lines. */
|
||||
ip: string;
|
||||
/** Audio port for the m=audio line. */
|
||||
port: number;
|
||||
/** RTP payload type numbers (e.g. [9, 0, 8, 101]). */
|
||||
payloadTypes?: number[];
|
||||
/** SDP session ID (random if omitted). */
|
||||
sessionId?: string;
|
||||
/** Session name for the s= line (defaults to '-'). */
|
||||
sessionName?: string;
|
||||
/** Direction attribute (defaults to 'sendrecv'). */
|
||||
direction?: 'sendrecv' | 'recvonly' | 'sendonly' | 'inactive';
|
||||
/** Extra a= lines to append (without "a=" prefix). */
|
||||
attributes?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a minimal SDP body suitable for SIP INVITE offers/answers.
|
||||
*
|
||||
* ```ts
|
||||
* const sdp = buildSdp({
|
||||
* ip: '192.168.5.66',
|
||||
* port: 20000,
|
||||
* payloadTypes: [9, 0, 101],
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export function buildSdp(options: ISdpOptions): string {
|
||||
const {
|
||||
ip,
|
||||
port,
|
||||
payloadTypes = [9, 0, 8, 101],
|
||||
sessionId = String(Math.floor(Math.random() * 1e9)),
|
||||
sessionName = '-',
|
||||
direction = 'sendrecv',
|
||||
attributes = [],
|
||||
} = options;
|
||||
|
||||
const lines: string[] = [
|
||||
'v=0',
|
||||
`o=- ${sessionId} ${sessionId} IN IP4 ${ip}`,
|
||||
`s=${sessionName}`,
|
||||
`c=IN IP4 ${ip}`,
|
||||
't=0 0',
|
||||
`m=audio ${port} RTP/AVP ${payloadTypes.join(' ')}`,
|
||||
];
|
||||
|
||||
for (const pt of payloadTypes) {
|
||||
const name = CODEC_NAMES[pt];
|
||||
if (name) lines.push(`a=rtpmap:${pt} ${name}`);
|
||||
if (pt === 101) lines.push(`a=fmtp:101 0-16`);
|
||||
}
|
||||
|
||||
lines.push(`a=${direction}`);
|
||||
for (const attr of attributes) lines.push(`a=${attr}`);
|
||||
lines.push(''); // trailing CRLF
|
||||
|
||||
return lines.join('\r\n');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SIP Digest authentication (RFC 2617)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface IDigestChallenge {
|
||||
realm: string;
|
||||
nonce: string;
|
||||
algorithm?: string;
|
||||
opaque?: string;
|
||||
qop?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a `Proxy-Authenticate` or `WWW-Authenticate` header value
|
||||
* into its constituent fields.
|
||||
*/
|
||||
export function parseDigestChallenge(header: string): IDigestChallenge | null {
|
||||
if (!header.toLowerCase().startsWith('digest ')) return null;
|
||||
const params = header.slice(7);
|
||||
const get = (key: string): string | undefined => {
|
||||
const re = new RegExp(`${key}\\s*=\\s*"([^"]*)"`, 'i');
|
||||
const m = params.match(re);
|
||||
if (m) return m[1];
|
||||
// unquoted value
|
||||
const re2 = new RegExp(`${key}\\s*=\\s*([^,\\s]+)`, 'i');
|
||||
const m2 = params.match(re2);
|
||||
return m2 ? m2[1] : undefined;
|
||||
};
|
||||
const realm = get('realm');
|
||||
const nonce = get('nonce');
|
||||
if (!realm || !nonce) return null;
|
||||
return { realm, nonce, algorithm: get('algorithm'), opaque: get('opaque'), qop: get('qop') };
|
||||
}
|
||||
|
||||
function md5(s: string): string {
|
||||
return createHash('md5').update(s).digest('hex');
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute a SIP Digest `Proxy-Authorization` or `Authorization` header value.
|
||||
*/
|
||||
export function computeDigestAuth(options: {
|
||||
username: string;
|
||||
password: string;
|
||||
realm: string;
|
||||
nonce: string;
|
||||
method: string;
|
||||
uri: string;
|
||||
algorithm?: string;
|
||||
opaque?: string;
|
||||
}): string {
|
||||
const ha1 = md5(`${options.username}:${options.realm}:${options.password}`);
|
||||
const ha2 = md5(`${options.method}:${options.uri}`);
|
||||
const response = md5(`${ha1}:${options.nonce}:${ha2}`);
|
||||
|
||||
let header = `Digest username="${options.username}", realm="${options.realm}", ` +
|
||||
`nonce="${options.nonce}", uri="${options.uri}", response="${response}", ` +
|
||||
`algorithm=${options.algorithm || 'MD5'}`;
|
||||
if (options.opaque) header += `, opaque="${options.opaque}"`;
|
||||
return header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the audio media port and connection address from an SDP body.
|
||||
* Returns null when no c= + m=audio pair is found.
|
||||
*/
|
||||
export function parseSdpEndpoint(sdp: string): { address: string; port: number } | null {
|
||||
let addr: string | null = null;
|
||||
let port: number | null = null;
|
||||
for (const raw of sdp.replace(/\r\n/g, '\n').split('\n')) {
|
||||
const line = raw.trim();
|
||||
if (line.startsWith('c=IN IP4 ')) {
|
||||
addr = line.slice('c=IN IP4 '.length).trim();
|
||||
} else if (line.startsWith('m=audio ')) {
|
||||
const parts = line.split(' ');
|
||||
if (parts.length >= 2) port = parseInt(parts[1], 10);
|
||||
}
|
||||
}
|
||||
return addr && port ? { address: addr, port } : null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// MWI (Message Waiting Indicator) — RFC 3842
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Build a SIP NOTIFY request for Message Waiting Indicator.
|
||||
*
|
||||
* Sent out-of-dialog to notify a device about voicemail message counts.
|
||||
* Uses the message-summary event package per RFC 3842.
|
||||
*/
|
||||
export interface IMwiOptions {
|
||||
/** Proxy LAN IP and port (Via / From / Contact). */
|
||||
proxyHost: string;
|
||||
proxyPort: number;
|
||||
/** Target device URI (e.g. "sip:user@192.168.5.100:5060"). */
|
||||
targetUri: string;
|
||||
/** Account URI for the voicebox (used in the From header). */
|
||||
accountUri: string;
|
||||
/** Number of new (unheard) voice messages. */
|
||||
newMessages: number;
|
||||
/** Number of old (heard) voice messages. */
|
||||
oldMessages: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the body and headers for an MWI NOTIFY (RFC 3842 message-summary).
|
||||
*
|
||||
* Returns the body string and extra headers needed. The caller builds
|
||||
* the SipMessage via SipMessage.createRequest('NOTIFY', ...).
|
||||
*/
|
||||
export function buildMwiBody(newMessages: number, oldMessages: number, accountUri: string): {
|
||||
body: string;
|
||||
contentType: string;
|
||||
extraHeaders: [string, string][];
|
||||
} {
|
||||
const hasNew = newMessages > 0;
|
||||
const body =
|
||||
`Messages-Waiting: ${hasNew ? 'yes' : 'no'}\r\n` +
|
||||
`Message-Account: ${accountUri}\r\n` +
|
||||
`Voice-Message: ${newMessages}/${oldMessages}\r\n`;
|
||||
|
||||
return {
|
||||
body,
|
||||
contentType: 'application/simple-message-summary',
|
||||
extraHeaders: [
|
||||
['Event', 'message-summary'],
|
||||
['Subscription-State', 'terminated;reason=noresource'],
|
||||
],
|
||||
};
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
export { SipMessage } from './message.ts';
|
||||
export { SipDialog } from './dialog.ts';
|
||||
export type { TDialogState } from './dialog.ts';
|
||||
export { rewriteSipUri, rewriteSdp } from './rewrite.ts';
|
||||
export {
|
||||
generateCallId,
|
||||
generateTag,
|
||||
generateBranch,
|
||||
codecName,
|
||||
buildSdp,
|
||||
parseSdpEndpoint,
|
||||
parseDigestChallenge,
|
||||
computeDigestAuth,
|
||||
buildMwiBody,
|
||||
} from './helpers.ts';
|
||||
export type { ISdpOptions, IDigestChallenge, IMwiOptions } from './helpers.ts';
|
||||
export type { IEndpoint } from './types.ts';
|
||||
@@ -1,316 +0,0 @@
|
||||
/**
|
||||
* SipMessage — parse, inspect, mutate, and serialize SIP messages.
|
||||
*
|
||||
* Provides a fluent (builder-style) API so callers can chain header
|
||||
* manipulations before serializing:
|
||||
*
|
||||
* const buf = SipMessage.parse(raw)!
|
||||
* .setHeader('Contact', newContact)
|
||||
* .prependHeader('Record-Route', rr)
|
||||
* .updateContentLength()
|
||||
* .serialize();
|
||||
*/
|
||||
|
||||
import { Buffer } from 'node:buffer';
|
||||
import { generateCallId, generateTag, generateBranch } from './helpers.ts';
|
||||
|
||||
const SIP_FIRST_LINE_RE = /^(?:[A-Z]+\s+\S+\s+SIP\/\d\.\d|SIP\/\d\.\d\s+\d+\s+)/;
|
||||
|
||||
export class SipMessage {
|
||||
startLine: string;
|
||||
headers: [string, string][];
|
||||
body: string;
|
||||
|
||||
constructor(startLine: string, headers: [string, string][], body: string) {
|
||||
this.startLine = startLine;
|
||||
this.headers = headers;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Parsing
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
static parse(buf: Buffer): SipMessage | null {
|
||||
if (!buf.length) return null;
|
||||
if (buf[0] < 0x41 || buf[0] > 0x7a) return null;
|
||||
|
||||
let text: string;
|
||||
try { text = buf.toString('utf8'); } catch { return null; }
|
||||
|
||||
let head: string;
|
||||
let body: string;
|
||||
let sep = text.indexOf('\r\n\r\n');
|
||||
if (sep !== -1) {
|
||||
head = text.slice(0, sep);
|
||||
body = text.slice(sep + 4);
|
||||
} else {
|
||||
sep = text.indexOf('\n\n');
|
||||
if (sep !== -1) {
|
||||
head = text.slice(0, sep);
|
||||
body = text.slice(sep + 2);
|
||||
} else {
|
||||
head = text;
|
||||
body = '';
|
||||
}
|
||||
}
|
||||
|
||||
const lines = head.replace(/\r\n/g, '\n').split('\n');
|
||||
if (!lines.length || !lines[0]) return null;
|
||||
const startLine = lines[0];
|
||||
if (!SIP_FIRST_LINE_RE.test(startLine)) return null;
|
||||
|
||||
const headers: [string, string][] = [];
|
||||
for (const line of lines.slice(1)) {
|
||||
if (!line.trim()) continue;
|
||||
const colon = line.indexOf(':');
|
||||
if (colon === -1) continue;
|
||||
headers.push([line.slice(0, colon).trim(), line.slice(colon + 1).trim()]);
|
||||
}
|
||||
return new SipMessage(startLine, headers, body);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Serialization
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
serialize(): Buffer {
|
||||
const head = [this.startLine, ...this.headers.map(([n, v]) => `${n}: ${v}`)].join('\r\n') + '\r\n\r\n';
|
||||
return Buffer.concat([Buffer.from(head, 'utf8'), Buffer.from(this.body || '', 'utf8')]);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Inspectors
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
get isRequest(): boolean {
|
||||
return !this.startLine.startsWith('SIP/');
|
||||
}
|
||||
|
||||
get isResponse(): boolean {
|
||||
return this.startLine.startsWith('SIP/');
|
||||
}
|
||||
|
||||
/** Request method (INVITE, REGISTER, ...) or null for responses. */
|
||||
get method(): string | null {
|
||||
if (!this.isRequest) return null;
|
||||
return this.startLine.split(' ')[0];
|
||||
}
|
||||
|
||||
/** Response status code or null for requests. */
|
||||
get statusCode(): number | null {
|
||||
if (!this.isResponse) return null;
|
||||
return parseInt(this.startLine.split(' ')[1], 10);
|
||||
}
|
||||
|
||||
get callId(): string {
|
||||
return this.getHeader('Call-ID') || 'noid';
|
||||
}
|
||||
|
||||
/** Method from the CSeq header (e.g. "INVITE"). */
|
||||
get cseqMethod(): string | null {
|
||||
const cseq = this.getHeader('CSeq');
|
||||
if (!cseq) return null;
|
||||
const parts = cseq.trim().split(/\s+/);
|
||||
return parts.length >= 2 ? parts[1] : null;
|
||||
}
|
||||
|
||||
/** True for INVITE, SUBSCRIBE, REFER, NOTIFY, UPDATE. */
|
||||
get isDialogEstablishing(): boolean {
|
||||
return /^(INVITE|SUBSCRIBE|REFER|NOTIFY|UPDATE)\s/.test(this.startLine);
|
||||
}
|
||||
|
||||
/** True when the body carries an SDP payload. */
|
||||
get hasSdpBody(): boolean {
|
||||
const ct = (this.getHeader('Content-Type') || '').toLowerCase();
|
||||
return !!this.body && ct.startsWith('application/sdp');
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Header accessors (fluent)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
getHeader(name: string): string | null {
|
||||
const nl = name.toLowerCase();
|
||||
for (const [n, v] of this.headers) if (n.toLowerCase() === nl) return v;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Overwrites the first header with the given name, or appends it. */
|
||||
setHeader(name: string, value: string): this {
|
||||
const nl = name.toLowerCase();
|
||||
for (const h of this.headers) {
|
||||
if (h[0].toLowerCase() === nl) { h[1] = value; return this; }
|
||||
}
|
||||
this.headers.push([name, value]);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Inserts a header at the top of the header list. */
|
||||
prependHeader(name: string, value: string): this {
|
||||
this.headers.unshift([name, value]);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Removes all headers with the given name. */
|
||||
removeHeader(name: string): this {
|
||||
const nl = name.toLowerCase();
|
||||
this.headers = this.headers.filter(([n]) => n.toLowerCase() !== nl);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Recalculates Content-Length to match the current body. */
|
||||
updateContentLength(): this {
|
||||
const len = Buffer.byteLength(this.body || '', 'utf8');
|
||||
return this.setHeader('Content-Length', String(len));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Start-line mutation
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Replaces the Request-URI (second token) of a request start line. */
|
||||
setRequestUri(uri: string): this {
|
||||
if (!this.isRequest) return this;
|
||||
const parts = this.startLine.split(' ');
|
||||
if (parts.length >= 2) {
|
||||
parts[1] = uri;
|
||||
this.startLine = parts.join(' ');
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Returns the Request-URI (second token) of a request start line. */
|
||||
get requestUri(): string | null {
|
||||
if (!this.isRequest) return null;
|
||||
return this.startLine.split(' ')[1] || null;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Factory methods — build new SIP messages from scratch
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Build a new SIP request.
|
||||
*
|
||||
* ```ts
|
||||
* const invite = SipMessage.createRequest('INVITE', 'sip:user@host', {
|
||||
* from: { uri: 'sip:me@proxy', tag: 'abc' },
|
||||
* to: { uri: 'sip:user@host' },
|
||||
* via: { host: '192.168.5.66', port: 5070 },
|
||||
* contact: '<sip:me@192.168.5.66:5070>',
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
static createRequest(method: string, requestUri: string, options: {
|
||||
via: { host: string; port: number; transport?: string; branch?: string };
|
||||
from: { uri: string; displayName?: string; tag?: string };
|
||||
to: { uri: string; displayName?: string; tag?: string };
|
||||
callId?: string;
|
||||
cseq?: number;
|
||||
contact?: string;
|
||||
maxForwards?: number;
|
||||
body?: string;
|
||||
contentType?: string;
|
||||
extraHeaders?: [string, string][];
|
||||
}): SipMessage {
|
||||
const branch = options.via.branch || generateBranch();
|
||||
const transport = options.via.transport || 'UDP';
|
||||
const fromTag = options.from.tag || generateTag();
|
||||
const callId = options.callId || generateCallId();
|
||||
const cseq = options.cseq ?? 1;
|
||||
|
||||
const fromDisplay = options.from.displayName ? `"${options.from.displayName}" ` : '';
|
||||
const toDisplay = options.to.displayName ? `"${options.to.displayName}" ` : '';
|
||||
const toTag = options.to.tag ? `;tag=${options.to.tag}` : '';
|
||||
|
||||
const headers: [string, string][] = [
|
||||
['Via', `SIP/2.0/${transport} ${options.via.host}:${options.via.port};branch=${branch};rport`],
|
||||
['From', `${fromDisplay}<${options.from.uri}>;tag=${fromTag}`],
|
||||
['To', `${toDisplay}<${options.to.uri}>${toTag}`],
|
||||
['Call-ID', callId],
|
||||
['CSeq', `${cseq} ${method}`],
|
||||
['Max-Forwards', String(options.maxForwards ?? 70)],
|
||||
];
|
||||
|
||||
if (options.contact) {
|
||||
headers.push(['Contact', options.contact]);
|
||||
}
|
||||
|
||||
if (options.extraHeaders) {
|
||||
headers.push(...options.extraHeaders);
|
||||
}
|
||||
|
||||
const body = options.body || '';
|
||||
if (body && options.contentType) {
|
||||
headers.push(['Content-Type', options.contentType]);
|
||||
}
|
||||
headers.push(['Content-Length', String(Buffer.byteLength(body, 'utf8'))]);
|
||||
|
||||
return new SipMessage(`${method} ${requestUri} SIP/2.0`, headers, body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a SIP response to an incoming request.
|
||||
*
|
||||
* Copies Via, From, To, Call-ID, and CSeq from the original request.
|
||||
*/
|
||||
static createResponse(
|
||||
statusCode: number,
|
||||
reasonPhrase: string,
|
||||
request: SipMessage,
|
||||
options?: {
|
||||
toTag?: string;
|
||||
contact?: string;
|
||||
body?: string;
|
||||
contentType?: string;
|
||||
extraHeaders?: [string, string][];
|
||||
},
|
||||
): SipMessage {
|
||||
const headers: [string, string][] = [];
|
||||
|
||||
// Copy all Via headers (order matters).
|
||||
for (const [n, v] of request.headers) {
|
||||
if (n.toLowerCase() === 'via') headers.push(['Via', v]);
|
||||
}
|
||||
|
||||
// From — copied verbatim.
|
||||
const from = request.getHeader('From');
|
||||
if (from) headers.push(['From', from]);
|
||||
|
||||
// To — add tag if provided and not already present.
|
||||
let to = request.getHeader('To') || '';
|
||||
if (options?.toTag && !to.includes('tag=')) {
|
||||
to += `;tag=${options.toTag}`;
|
||||
}
|
||||
headers.push(['To', to]);
|
||||
|
||||
headers.push(['Call-ID', request.callId]);
|
||||
|
||||
const cseq = request.getHeader('CSeq');
|
||||
if (cseq) headers.push(['CSeq', cseq]);
|
||||
|
||||
if (options?.contact) headers.push(['Contact', options.contact]);
|
||||
if (options?.extraHeaders) headers.push(...options.extraHeaders);
|
||||
|
||||
const body = options?.body || '';
|
||||
if (body && options?.contentType) {
|
||||
headers.push(['Content-Type', options.contentType]);
|
||||
}
|
||||
headers.push(['Content-Length', String(Buffer.byteLength(body, 'utf8'))]);
|
||||
|
||||
return new SipMessage(`SIP/2.0 ${statusCode} ${reasonPhrase}`, headers, body);
|
||||
}
|
||||
|
||||
/** Extract the tag from a From or To header value. */
|
||||
static extractTag(headerValue: string): string | null {
|
||||
const m = headerValue.match(/;tag=([^\s;>]+)/);
|
||||
return m ? m[1] : null;
|
||||
}
|
||||
|
||||
/** Extract the URI from an addr-spec or name-addr (From/To/Contact). */
|
||||
static extractUri(headerValue: string): string | null {
|
||||
const m = headerValue.match(/<([^>]+)>/);
|
||||
return m ? m[1] : headerValue.trim().split(/[;>]/)[0] || null;
|
||||
}
|
||||
}
|
||||
228
ts/sip/readme.md
228
ts/sip/readme.md
@@ -1,228 +0,0 @@
|
||||
# ts/sip — SIP Protocol Library
|
||||
|
||||
A zero-dependency SIP (Session Initiation Protocol) library for Deno / Node.
|
||||
Provides parsing, construction, mutation, and dialog management for SIP
|
||||
messages, plus helpers for SDP bodies and URI rewriting.
|
||||
|
||||
## Modules
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `message.ts` | `SipMessage` class — parse, inspect, mutate, serialize |
|
||||
| `dialog.ts` | `SipDialog` class — track dialog state, build in-dialog requests |
|
||||
| `helpers.ts` | ID generators, codec registry, SDP builder/parser |
|
||||
| `rewrite.ts` | SIP URI and SDP body rewriting |
|
||||
| `types.ts` | Shared types (`IEndpoint`) |
|
||||
| `index.ts` | Barrel re-export |
|
||||
|
||||
## Quick Start
|
||||
|
||||
```ts
|
||||
import {
|
||||
SipMessage,
|
||||
SipDialog,
|
||||
buildSdp,
|
||||
parseSdpEndpoint,
|
||||
rewriteSipUri,
|
||||
rewriteSdp,
|
||||
generateCallId,
|
||||
generateTag,
|
||||
generateBranch,
|
||||
} from './sip/index.ts';
|
||||
```
|
||||
|
||||
## SipMessage
|
||||
|
||||
### Parsing
|
||||
|
||||
```ts
|
||||
import { Buffer } from 'node:buffer';
|
||||
|
||||
const raw = Buffer.from(
|
||||
'INVITE sip:user@example.com SIP/2.0\r\n' +
|
||||
'Via: SIP/2.0/UDP 10.0.0.1:5060;branch=z9hG4bK776\r\n' +
|
||||
'From: <sip:alice@example.com>;tag=abc\r\n' +
|
||||
'To: <sip:bob@example.com>\r\n' +
|
||||
'Call-ID: a84b4c76e66710@10.0.0.1\r\n' +
|
||||
'CSeq: 1 INVITE\r\n' +
|
||||
'Content-Length: 0\r\n\r\n'
|
||||
);
|
||||
|
||||
const msg = SipMessage.parse(raw);
|
||||
// msg.method → "INVITE"
|
||||
// msg.isRequest → true
|
||||
// msg.callId → "a84b4c76e66710@10.0.0.1"
|
||||
// msg.cseqMethod → "INVITE"
|
||||
// msg.isDialogEstablishing → true
|
||||
```
|
||||
|
||||
### Fluent mutation
|
||||
|
||||
All setter methods return `this` for chaining:
|
||||
|
||||
```ts
|
||||
const buf = SipMessage.parse(raw)!
|
||||
.setHeader('Contact', '<sip:proxy@192.168.1.1:5070>')
|
||||
.prependHeader('Record-Route', '<sip:192.168.1.1:5070;lr>')
|
||||
.updateContentLength()
|
||||
.serialize();
|
||||
```
|
||||
|
||||
### Building requests from scratch
|
||||
|
||||
```ts
|
||||
const invite = SipMessage.createRequest('INVITE', 'sip:+4930123@voip.example.com', {
|
||||
via: { host: '192.168.5.66', port: 5070 },
|
||||
from: { uri: 'sip:alice@example.com', displayName: 'Alice' },
|
||||
to: { uri: 'sip:+4930123@voip.example.com' },
|
||||
contact: '<sip:192.168.5.66:5070>',
|
||||
body: sdpBody,
|
||||
contentType: 'application/sdp',
|
||||
});
|
||||
// Call-ID, From tag, Via branch are auto-generated if not provided.
|
||||
```
|
||||
|
||||
### Building responses
|
||||
|
||||
```ts
|
||||
const ok = SipMessage.createResponse(200, 'OK', incomingInvite, {
|
||||
toTag: generateTag(),
|
||||
contact: '<sip:192.168.5.66:5070>',
|
||||
body: answerSdp,
|
||||
contentType: 'application/sdp',
|
||||
});
|
||||
```
|
||||
|
||||
### Inspectors
|
||||
|
||||
| Property | Type | Description |
|
||||
|----------|------|-------------|
|
||||
| `isRequest` | `boolean` | True for requests (INVITE, BYE, ...) |
|
||||
| `isResponse` | `boolean` | True for responses (SIP/2.0 200 OK, ...) |
|
||||
| `method` | `string \| null` | Request method or null |
|
||||
| `statusCode` | `number \| null` | Response status code or null |
|
||||
| `callId` | `string` | Call-ID header value |
|
||||
| `cseqMethod` | `string \| null` | Method from CSeq header |
|
||||
| `requestUri` | `string \| null` | Request-URI (second token of start line) |
|
||||
| `isDialogEstablishing` | `boolean` | INVITE, SUBSCRIBE, REFER, NOTIFY, UPDATE |
|
||||
| `hasSdpBody` | `boolean` | Body present with Content-Type: application/sdp |
|
||||
|
||||
### Static helpers
|
||||
|
||||
```ts
|
||||
SipMessage.extractTag('<sip:alice@x.com>;tag=abc') // → "abc"
|
||||
SipMessage.extractUri('"Alice" <sip:alice@x.com>') // → "sip:alice@x.com"
|
||||
```
|
||||
|
||||
## SipDialog
|
||||
|
||||
Tracks dialog state per RFC 3261 §12. A dialog is created from a
|
||||
dialog-establishing request and updated as responses arrive.
|
||||
|
||||
### UAC (caller) side
|
||||
|
||||
```ts
|
||||
// 1. Build and send INVITE
|
||||
const invite = SipMessage.createRequest('INVITE', destUri, { ... });
|
||||
const dialog = SipDialog.fromUacInvite(invite, '192.168.5.66', 5070);
|
||||
|
||||
// 2. Process responses as they arrive
|
||||
dialog.processResponse(trying100); // state stays 'early'
|
||||
dialog.processResponse(ringing180); // state stays 'early', remoteTag learned
|
||||
dialog.processResponse(ok200); // state → 'confirmed'
|
||||
|
||||
// 3. ACK the 200
|
||||
const ack = dialog.createAck();
|
||||
|
||||
// 4. In-dialog requests
|
||||
const bye = dialog.createRequest('BYE');
|
||||
dialog.terminate();
|
||||
```
|
||||
|
||||
### UAS (callee) side
|
||||
|
||||
```ts
|
||||
const dialog = SipDialog.fromUasInvite(incomingInvite, generateTag(), localHost, localPort);
|
||||
```
|
||||
|
||||
### CANCEL (before answer)
|
||||
|
||||
```ts
|
||||
const cancel = dialog.createCancel(originalInvite);
|
||||
```
|
||||
|
||||
### Dialog states
|
||||
|
||||
`'early'` → `'confirmed'` → `'terminated'`
|
||||
|
||||
## Helpers
|
||||
|
||||
### ID generation
|
||||
|
||||
```ts
|
||||
generateCallId() // → "a3f8b2c1d4e5f6a7b8c9d0e1f2a3b4c5"
|
||||
generateCallId('example.com') // → "a3f8b2c1...@example.com"
|
||||
generateTag() // → "1a2b3c4d5e6f7a8b"
|
||||
generateBranch() // → "z9hG4bK-1a2b3c4d5e6f7a8b"
|
||||
```
|
||||
|
||||
### SDP builder
|
||||
|
||||
```ts
|
||||
const sdp = buildSdp({
|
||||
ip: '192.168.5.66',
|
||||
port: 20000,
|
||||
payloadTypes: [9, 0, 8, 101], // G.722, PCMU, PCMA, telephone-event
|
||||
direction: 'sendrecv',
|
||||
});
|
||||
```
|
||||
|
||||
### SDP parser
|
||||
|
||||
```ts
|
||||
const ep = parseSdpEndpoint(sdpBody);
|
||||
// → { address: '10.0.0.1', port: 20000 } or null
|
||||
```
|
||||
|
||||
### Codec names
|
||||
|
||||
```ts
|
||||
codecName(9) // → "G722/8000"
|
||||
codecName(0) // → "PCMU/8000"
|
||||
codecName(101) // → "telephone-event/8000"
|
||||
```
|
||||
|
||||
## Rewriting
|
||||
|
||||
### SIP URI
|
||||
|
||||
Replaces the host:port in all `sip:` / `sips:` URIs found in a header value:
|
||||
|
||||
```ts
|
||||
rewriteSipUri('<sip:user@10.0.0.1:5060>', '203.0.113.1', 5070)
|
||||
// → '<sip:user@203.0.113.1:5070>'
|
||||
```
|
||||
|
||||
### SDP body
|
||||
|
||||
Rewrites the connection address and audio media port, returning the original
|
||||
endpoint that was replaced:
|
||||
|
||||
```ts
|
||||
const { body, original } = rewriteSdp(sdpBody, '203.0.113.1', 20000);
|
||||
// original → { address: '10.0.0.1', port: 8000 }
|
||||
```
|
||||
|
||||
## Architecture Notes
|
||||
|
||||
This library is intentionally low-level — it operates on individual messages
|
||||
and dialogs rather than providing a full SIP stack with transport and
|
||||
transaction layers. This makes it suitable for building:
|
||||
|
||||
- **SIP proxies** — parse, rewrite headers/SDP, serialize, forward
|
||||
- **B2BUA (back-to-back user agents)** — manage two dialogs, bridge media
|
||||
- **SIP testing tools** — craft and send arbitrary messages
|
||||
- **Protocol analyzers** — parse and inspect SIP traffic
|
||||
|
||||
The library does not manage sockets, timers, or retransmissions — those
|
||||
concerns belong to the application layer.
|
||||
@@ -1,54 +0,0 @@
|
||||
/**
|
||||
* SIP URI and SDP body rewriting helpers.
|
||||
*/
|
||||
|
||||
import type { IEndpoint } from './types.ts';
|
||||
|
||||
const SIP_URI_RE = /(sips?:)([^@>;,\s]+@)?([^>;,\s:]+)(:\d+)?/g;
|
||||
|
||||
/**
|
||||
* Replaces the host:port in every `sip:` / `sips:` URI found in `value`.
|
||||
*/
|
||||
export function rewriteSipUri(value: string, host: string, port: number): string {
|
||||
return value.replace(SIP_URI_RE, (_m, scheme: string, userpart?: string) =>
|
||||
`${scheme}${userpart || ''}${host}:${port}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrites the connection address (`c=`) and audio media port (`m=audio`)
|
||||
* in an SDP body. Returns the rewritten body together with the original
|
||||
* endpoint that was replaced (if any).
|
||||
*/
|
||||
export function rewriteSdp(
|
||||
body: string,
|
||||
ip: string,
|
||||
port: number,
|
||||
): { body: string; original: IEndpoint | null } {
|
||||
let origAddr: string | null = null;
|
||||
let origPort: number | null = null;
|
||||
|
||||
const out = body
|
||||
.replace(/\r\n/g, '\n')
|
||||
.split('\n')
|
||||
.map((line) => {
|
||||
if (line.startsWith('c=IN IP4 ')) {
|
||||
origAddr = line.slice('c=IN IP4 '.length).trim();
|
||||
return `c=IN IP4 ${ip}`;
|
||||
}
|
||||
if (line.startsWith('m=audio ')) {
|
||||
const parts = line.split(' ');
|
||||
if (parts.length >= 2) {
|
||||
origPort = parseInt(parts[1], 10);
|
||||
parts[1] = String(port);
|
||||
}
|
||||
return parts.join(' ');
|
||||
}
|
||||
return line;
|
||||
})
|
||||
.join('\r\n');
|
||||
|
||||
return {
|
||||
body: out,
|
||||
original: origAddr && origPort ? { address: origAddr, port: origPort } : null,
|
||||
};
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
/**
|
||||
* Shared SIP types.
|
||||
*/
|
||||
|
||||
export interface IEndpoint {
|
||||
address: string;
|
||||
port: number;
|
||||
}
|
||||
308
ts/sipproxy.ts
308
ts/sipproxy.ts
@@ -33,7 +33,14 @@ import {
|
||||
configureProxyEngine,
|
||||
onProxyEvent,
|
||||
hangupCall,
|
||||
makeCall,
|
||||
shutdownProxyEngine,
|
||||
webrtcOffer,
|
||||
webrtcIce,
|
||||
webrtcLink,
|
||||
webrtcClose,
|
||||
addLeg,
|
||||
removeLeg,
|
||||
} from './proxybridge.ts';
|
||||
import type {
|
||||
IIncomingCallEvent,
|
||||
@@ -89,6 +96,16 @@ interface IDeviceStatus {
|
||||
isBrowser: boolean;
|
||||
}
|
||||
|
||||
interface IActiveLeg {
|
||||
id: string;
|
||||
type: 'sip-device' | 'sip-provider' | 'webrtc' | 'tool';
|
||||
state: string;
|
||||
codec: string | null;
|
||||
rtpPort: number | null;
|
||||
remoteMedia: string | null;
|
||||
metadata: Record<string, unknown>;
|
||||
}
|
||||
|
||||
interface IActiveCall {
|
||||
id: string;
|
||||
direction: string;
|
||||
@@ -97,6 +114,13 @@ interface IActiveCall {
|
||||
providerUsed: string | null;
|
||||
state: string;
|
||||
startedAt: number;
|
||||
legs: Map<string, IActiveLeg>;
|
||||
}
|
||||
|
||||
interface IHistoryLeg {
|
||||
id: string;
|
||||
type: string;
|
||||
metadata: Record<string, unknown>;
|
||||
}
|
||||
|
||||
interface ICallHistoryEntry {
|
||||
@@ -106,6 +130,7 @@ interface ICallHistoryEntry {
|
||||
calleeNumber: string | null;
|
||||
startedAt: number;
|
||||
duration: number;
|
||||
legs: IHistoryLeg[];
|
||||
}
|
||||
|
||||
const providerStatuses = new Map<string, IProviderStatus>();
|
||||
@@ -114,6 +139,12 @@ const activeCalls = new Map<string, IActiveCall>();
|
||||
const callHistory: ICallHistoryEntry[] = [];
|
||||
const MAX_HISTORY = 100;
|
||||
|
||||
// WebRTC session ↔ call linking state.
|
||||
// Both pieces (session accept + call media info) can arrive in any order.
|
||||
const webrtcSessionToCall = new Map<string, string>(); // sessionId → callId
|
||||
const webrtcCallToSession = new Map<string, string>(); // callId → sessionId
|
||||
const pendingCallMedia = new Map<string, { addr: string; port: number; sipPt: number }>(); // callId → provider media info
|
||||
|
||||
// Initialize provider statuses from config (all start as unregistered).
|
||||
for (const p of appConfig.providers) {
|
||||
providerStatuses.set(p.id, {
|
||||
@@ -152,16 +183,40 @@ initWebRtcSignaling({ log });
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function getStatus() {
|
||||
// Merge SIP devices (from Rust) + browser devices (from TS WebSocket).
|
||||
const devices = [...deviceStatuses.values()];
|
||||
for (const bid of getAllBrowserDeviceIds()) {
|
||||
devices.push({
|
||||
id: bid,
|
||||
displayName: 'Browser',
|
||||
address: null,
|
||||
port: 0,
|
||||
connected: true,
|
||||
isBrowser: true,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
instanceId,
|
||||
uptime: Math.floor((Date.now() - startTime) / 1000),
|
||||
lanIp: appConfig.proxy.lanIp,
|
||||
providers: [...providerStatuses.values()],
|
||||
devices: [...deviceStatuses.values()],
|
||||
devices,
|
||||
calls: [...activeCalls.values()].map((c) => ({
|
||||
...c,
|
||||
duration: Math.floor((Date.now() - c.startedAt) / 1000),
|
||||
legs: [],
|
||||
legs: [...c.legs.values()].map((l) => ({
|
||||
id: l.id,
|
||||
type: l.type,
|
||||
state: l.state,
|
||||
codec: l.codec,
|
||||
rtpPort: l.rtpPort,
|
||||
remoteMedia: l.remoteMedia,
|
||||
metadata: l.metadata || {},
|
||||
pktSent: 0,
|
||||
pktReceived: 0,
|
||||
transcoding: false,
|
||||
})),
|
||||
})),
|
||||
callHistory,
|
||||
contacts: appConfig.contacts || [],
|
||||
@@ -216,6 +271,7 @@ async function startProxyEngine(): Promise<void> {
|
||||
providerUsed: data.provider_id,
|
||||
state: 'ringing',
|
||||
startedAt: Date.now(),
|
||||
legs: new Map(),
|
||||
});
|
||||
|
||||
// Notify browsers of incoming call.
|
||||
@@ -240,19 +296,80 @@ async function startProxyEngine(): Promise<void> {
|
||||
providerUsed: null,
|
||||
state: 'setting-up',
|
||||
startedAt: Date.now(),
|
||||
legs: new Map(),
|
||||
});
|
||||
});
|
||||
|
||||
onProxyEvent('outbound_call_started', (data: any) => {
|
||||
log(`[call] outbound started: ${data.call_id} → ${data.number} via ${data.provider_id}`);
|
||||
activeCalls.set(data.call_id, {
|
||||
id: data.call_id,
|
||||
direction: 'outbound',
|
||||
callerNumber: null,
|
||||
calleeNumber: data.number,
|
||||
providerUsed: data.provider_id,
|
||||
state: 'setting-up',
|
||||
startedAt: Date.now(),
|
||||
legs: new Map(),
|
||||
});
|
||||
|
||||
// Notify all browser devices — they can connect via WebRTC to listen/talk.
|
||||
const browserIds = getAllBrowserDeviceIds();
|
||||
for (const bid of browserIds) {
|
||||
sendToBrowserDevice(bid, {
|
||||
type: 'webrtc-incoming',
|
||||
callId: data.call_id,
|
||||
from: data.number,
|
||||
deviceId: bid,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onProxyEvent('call_ringing', (data: { call_id: string }) => {
|
||||
const call = activeCalls.get(data.call_id);
|
||||
if (call) call.state = 'ringing';
|
||||
});
|
||||
|
||||
onProxyEvent('call_answered', (data: { call_id: string }) => {
|
||||
onProxyEvent('call_answered', (data: { call_id: string; provider_media_addr?: string; provider_media_port?: number; sip_pt?: number }) => {
|
||||
const call = activeCalls.get(data.call_id);
|
||||
if (call) {
|
||||
call.state = 'connected';
|
||||
log(`[call] ${data.call_id} connected`);
|
||||
|
||||
// Enrich provider leg with media info from the answered event.
|
||||
if (data.provider_media_addr && data.provider_media_port) {
|
||||
for (const leg of call.legs.values()) {
|
||||
if (leg.type === 'sip-provider') {
|
||||
leg.remoteMedia = `${data.provider_media_addr}:${data.provider_media_port}`;
|
||||
if (data.sip_pt !== undefined) {
|
||||
const codecNames: Record<number, string> = { 0: 'PCMU', 8: 'PCMA', 9: 'G.722', 111: 'Opus' };
|
||||
leg.codec = codecNames[data.sip_pt] || `PT${data.sip_pt}`;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try to link WebRTC session to this call for audio bridging.
|
||||
if (data.provider_media_addr && data.provider_media_port) {
|
||||
const sessionId = webrtcCallToSession.get(data.call_id);
|
||||
if (sessionId) {
|
||||
// Both session and media info available — link now.
|
||||
const sipPt = data.sip_pt ?? 9;
|
||||
log(`[webrtc] linking session=${sessionId.slice(0, 8)} to call=${data.call_id} media=${data.provider_media_addr}:${data.provider_media_port} pt=${sipPt}`);
|
||||
webrtcLink(sessionId, data.call_id, data.provider_media_addr, data.provider_media_port, sipPt).then((ok) => {
|
||||
log(`[webrtc] link result: ${ok}`);
|
||||
});
|
||||
} else {
|
||||
// Session not yet accepted — store media info for when it arrives.
|
||||
pendingCallMedia.set(data.call_id, {
|
||||
addr: data.provider_media_addr,
|
||||
port: data.provider_media_port,
|
||||
sipPt: data.sip_pt ?? 9,
|
||||
});
|
||||
log(`[webrtc] media info cached for call=${data.call_id}, waiting for session accept`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -260,6 +377,15 @@ async function startProxyEngine(): Promise<void> {
|
||||
const call = activeCalls.get(data.call_id);
|
||||
if (call) {
|
||||
log(`[call] ${data.call_id} ended: ${data.reason} (${data.duration}s)`);
|
||||
// Snapshot legs with metadata for history.
|
||||
const historyLegs: IHistoryLeg[] = [];
|
||||
for (const [, leg] of call.legs) {
|
||||
historyLegs.push({
|
||||
id: leg.id,
|
||||
type: leg.type,
|
||||
metadata: leg.metadata || {},
|
||||
});
|
||||
}
|
||||
// Move to history.
|
||||
callHistory.unshift({
|
||||
id: call.id,
|
||||
@@ -268,9 +394,22 @@ async function startProxyEngine(): Promise<void> {
|
||||
calleeNumber: call.calleeNumber,
|
||||
startedAt: call.startedAt,
|
||||
duration: data.duration,
|
||||
legs: historyLegs,
|
||||
});
|
||||
if (callHistory.length > MAX_HISTORY) callHistory.pop();
|
||||
activeCalls.delete(data.call_id);
|
||||
|
||||
// Notify browser(s) that the call ended.
|
||||
broadcastWs('webrtc-call-ended', { callId: data.call_id });
|
||||
|
||||
// Clean up WebRTC session mappings.
|
||||
const sessionId = webrtcCallToSession.get(data.call_id);
|
||||
if (sessionId) {
|
||||
webrtcCallToSession.delete(data.call_id);
|
||||
webrtcSessionToCall.delete(sessionId);
|
||||
webrtcClose(sessionId).catch(() => {});
|
||||
}
|
||||
pendingCallMedia.delete(data.call_id);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -278,6 +417,95 @@ async function startProxyEngine(): Promise<void> {
|
||||
log(`[sip] unhandled ${data.method_or_status} Call-ID=${data.call_id?.slice(0, 20)} from=${data.from_addr}:${data.from_port}`);
|
||||
});
|
||||
|
||||
// Leg events (multiparty) — update shadow state so the dashboard shows legs.
|
||||
onProxyEvent('leg_added', (data: any) => {
|
||||
log(`[leg] added: call=${data.call_id} leg=${data.leg_id} kind=${data.kind} state=${data.state}`);
|
||||
const call = activeCalls.get(data.call_id);
|
||||
if (call) {
|
||||
call.legs.set(data.leg_id, {
|
||||
id: data.leg_id,
|
||||
type: data.kind,
|
||||
state: data.state,
|
||||
codec: null,
|
||||
rtpPort: null,
|
||||
remoteMedia: null,
|
||||
metadata: data.metadata || {},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onProxyEvent('leg_removed', (data: any) => {
|
||||
log(`[leg] removed: call=${data.call_id} leg=${data.leg_id}`);
|
||||
activeCalls.get(data.call_id)?.legs.delete(data.leg_id);
|
||||
});
|
||||
|
||||
onProxyEvent('leg_state_changed', (data: any) => {
|
||||
log(`[leg] state: call=${data.call_id} leg=${data.leg_id} → ${data.state}`);
|
||||
const call = activeCalls.get(data.call_id);
|
||||
if (!call) return;
|
||||
const leg = call.legs.get(data.leg_id);
|
||||
if (leg) {
|
||||
leg.state = data.state;
|
||||
if (data.metadata) leg.metadata = data.metadata;
|
||||
} else {
|
||||
// Initial legs (provider/device) don't emit leg_added — create on first state change.
|
||||
const legId: string = data.leg_id;
|
||||
const type = legId.includes('-prov') ? 'sip-provider' : legId.includes('-dev') ? 'sip-device' : 'webrtc';
|
||||
call.legs.set(data.leg_id, {
|
||||
id: data.leg_id,
|
||||
type,
|
||||
state: data.state,
|
||||
codec: null,
|
||||
rtpPort: null,
|
||||
remoteMedia: null,
|
||||
metadata: data.metadata || {},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// WebRTC events from Rust — forward ICE candidates to browser via WebSocket.
|
||||
onProxyEvent('webrtc_ice_candidate', (data: any) => {
|
||||
// Find the browser's WebSocket by session ID and send the ICE candidate.
|
||||
broadcastWs('webrtc-ice', {
|
||||
sessionId: data.session_id,
|
||||
candidate: { candidate: data.candidate, sdpMid: data.sdp_mid, sdpMLineIndex: data.sdp_mline_index },
|
||||
});
|
||||
});
|
||||
|
||||
onProxyEvent('webrtc_state', (data: any) => {
|
||||
log(`[webrtc] session=${data.session_id?.slice(0, 8)} state=${data.state}`);
|
||||
});
|
||||
|
||||
onProxyEvent('webrtc_track', (data: any) => {
|
||||
log(`[webrtc] session=${data.session_id?.slice(0, 8)} track=${data.kind} codec=${data.codec}`);
|
||||
});
|
||||
|
||||
onProxyEvent('webrtc_audio_rx', (data: any) => {
|
||||
if (data.packet_count === 1 || data.packet_count === 50) {
|
||||
log(`[webrtc] session=${data.session_id?.slice(0, 8)} browser audio rx #${data.packet_count}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Voicemail events.
|
||||
onProxyEvent('voicemail_started', (data: any) => {
|
||||
log(`[voicemail] started for call ${data.call_id} caller=${data.caller_number}`);
|
||||
});
|
||||
|
||||
onProxyEvent('recording_done', (data: any) => {
|
||||
log(`[voicemail] recording done: ${data.file_path} (${data.duration_ms}ms) caller=${data.caller_number}`);
|
||||
// Save voicemail metadata via VoiceboxManager.
|
||||
voiceboxManager.addMessage?.('default', {
|
||||
callerNumber: data.caller_number || 'Unknown',
|
||||
callerName: null,
|
||||
fileName: data.file_path,
|
||||
durationMs: data.duration_ms,
|
||||
});
|
||||
});
|
||||
|
||||
onProxyEvent('voicemail_error', (data: any) => {
|
||||
log(`[voicemail] error: ${data.error} call=${data.call_id}`);
|
||||
});
|
||||
|
||||
// Send full config to Rust — this binds the SIP socket and starts registrations.
|
||||
const configured = await configureProxyEngine({
|
||||
proxy: appConfig.proxy,
|
||||
@@ -330,12 +558,29 @@ async function startProxyEngine(): Promise<void> {
|
||||
initWebUi(
|
||||
getStatus,
|
||||
log,
|
||||
(number, _deviceId, _providerId) => {
|
||||
(number, deviceId, providerId) => {
|
||||
// Outbound calls from dashboard — send make_call command to Rust.
|
||||
// For now, log only. Full implementation needs make_call in Rust.
|
||||
log(`[dashboard] start call requested: ${number}`);
|
||||
// TODO: send make_call command when implemented in Rust
|
||||
return null;
|
||||
log(`[dashboard] start call: ${number} device=${deviceId || 'any'} provider=${providerId || 'auto'}`);
|
||||
// Fire-and-forget — the async result comes via events.
|
||||
makeCall(number, deviceId, providerId).then((callId) => {
|
||||
if (callId) {
|
||||
log(`[dashboard] call started: ${callId}`);
|
||||
activeCalls.set(callId, {
|
||||
id: callId,
|
||||
direction: 'outbound',
|
||||
callerNumber: null,
|
||||
calleeNumber: number,
|
||||
providerUsed: providerId || null,
|
||||
state: 'setting-up',
|
||||
startedAt: Date.now(),
|
||||
legs: new Map(),
|
||||
});
|
||||
} else {
|
||||
log(`[dashboard] call failed for ${number}`);
|
||||
}
|
||||
});
|
||||
// Return a temporary ID so the frontend doesn't show "failed" immediately.
|
||||
return { id: `pending-${Date.now()}` };
|
||||
},
|
||||
(callId) => {
|
||||
hangupCall(callId);
|
||||
@@ -377,8 +622,51 @@ initWebUi(
|
||||
log(`[config] reload failed: ${e.message}`);
|
||||
}
|
||||
},
|
||||
undefined, // callManager — WebRTC calls handled separately in Phase 2
|
||||
voiceboxManager,
|
||||
undefined, // callManager — legacy, replaced by Rust proxy-engine
|
||||
voiceboxManager, // voiceboxManager
|
||||
// WebRTC signaling → forwarded to Rust proxy-engine.
|
||||
async (sessionId, sdp, ws) => {
|
||||
log(`[webrtc] offer from browser session=${sessionId.slice(0, 8)} sdp_type=${typeof sdp} sdp_len=${sdp?.length || 0}`);
|
||||
if (!sdp || typeof sdp !== 'string' || sdp.length < 10) {
|
||||
log(`[webrtc] WARNING: invalid SDP (type=${typeof sdp}), skipping offer`);
|
||||
return;
|
||||
}
|
||||
log(`[webrtc] sending offer to Rust (${sdp.length}b)...`);
|
||||
const result = await webrtcOffer(sessionId, sdp);
|
||||
log(`[webrtc] Rust result: ${JSON.stringify(result)?.slice(0, 200)}`);
|
||||
if (result?.sdp) {
|
||||
ws.send(JSON.stringify({ type: 'webrtc-answer', sessionId, sdp: result.sdp }));
|
||||
log(`[webrtc] answer sent to browser session=${sessionId.slice(0, 8)}`);
|
||||
} else {
|
||||
log(`[webrtc] ERROR: no answer SDP from Rust`);
|
||||
}
|
||||
},
|
||||
async (sessionId, candidate) => {
|
||||
await webrtcIce(sessionId, candidate);
|
||||
},
|
||||
async (sessionId) => {
|
||||
await webrtcClose(sessionId);
|
||||
},
|
||||
// onWebRtcAccept — browser has accepted a call, linking session to call.
|
||||
(callId: string, sessionId: string) => {
|
||||
log(`[webrtc] accept: callId=${callId} sessionId=${sessionId.slice(0, 8)}`);
|
||||
|
||||
// Store bidirectional mapping.
|
||||
webrtcSessionToCall.set(sessionId, callId);
|
||||
webrtcCallToSession.set(callId, sessionId);
|
||||
|
||||
// Check if we already have media info for this call (provider answered first).
|
||||
const media = pendingCallMedia.get(callId);
|
||||
if (media) {
|
||||
pendingCallMedia.delete(callId);
|
||||
log(`[webrtc] linking session=${sessionId.slice(0, 8)} to call=${callId} media=${media.addr}:${media.port} pt=${media.sipPt}`);
|
||||
webrtcLink(sessionId, callId, media.addr, media.port, media.sipPt).then((ok) => {
|
||||
log(`[webrtc] link result: ${ok}`);
|
||||
});
|
||||
} else {
|
||||
log(`[webrtc] session ${sessionId.slice(0, 8)} accepted, waiting for call_answered media info`);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: 'siprouter',
|
||||
version: '1.11.0',
|
||||
version: '1.15.0',
|
||||
description: 'undefined'
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ export interface IDeviceStatus {
|
||||
|
||||
export interface ILegStatus {
|
||||
id: string;
|
||||
type: 'sip-device' | 'sip-provider' | 'webrtc';
|
||||
type: 'sip-device' | 'sip-provider' | 'webrtc' | 'tool';
|
||||
state: string;
|
||||
remoteMedia: { address: string; port: number } | null;
|
||||
rtpPort: number | null;
|
||||
@@ -28,6 +28,7 @@ export interface ILegStatus {
|
||||
pktReceived: number;
|
||||
codec: string | null;
|
||||
transcoding: boolean;
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface ICallStatus {
|
||||
@@ -42,6 +43,12 @@ export interface ICallStatus {
|
||||
legs: ILegStatus[];
|
||||
}
|
||||
|
||||
export interface IHistoryLeg {
|
||||
id: string;
|
||||
type: string;
|
||||
metadata: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface ICallHistoryEntry {
|
||||
id: string;
|
||||
direction: 'inbound' | 'outbound' | 'internal';
|
||||
@@ -50,6 +57,7 @@ export interface ICallHistoryEntry {
|
||||
providerUsed: string | null;
|
||||
startedAt: number;
|
||||
duration: number;
|
||||
legs?: IHistoryLeg[];
|
||||
}
|
||||
|
||||
export interface IContact {
|
||||
|
||||
Reference in New Issue
Block a user