feat(sip): add PJSUA2 engine — audio finally reaches the classifier
The gateway can now hear. Verified end to end against the Asterisk lab: speech classifies as live_human, hold music as music, the speech→music transition tracks the dialplan, and DTMF reaches a real IVR (Asterisk logged "caller pressed 1 -> accounts" and branched). RTP stats show 0% packet loss. PJSUAEngine implements the existing SIPEngine interface, so the gateway, call manager and hold-slayer service are unchanged. It is selected with SIP_ENGINE=pjsua2; the default stays "sippy" while this is proven, and the mock remains opt-in as before. Why a new engine rather than fixing the old path: PJSUA2 exposes no standalone RTP media object, so it will not surface media for a dialog it does not own. Owning the dialog is the price of owning the media. Sippy keeps the SBC roles it is good at — device registration, routing, leg bridging — and SippyEngine remains fully functional for signalling; it simply cannot carry media, which its media branch now says plainly instead of calling a method that could never work. The safety invariants are untouched. This engine is reachable only through gateway.make_call, which refuses emergency numbers and enforces the concurrency cap before any SIP action. No new dial path was introduced. MediaPipeline.add_remote_stream(host, port) is replaced by attach_call_media(stream_id, audio_media), called from onCallMediaState — the one place PJSUA2 hands out RTP-backed media. Taps requested before media comes up are attached when it does, so the classifier never misses the start of a call. Three crash/lifetime bugs found by running against the real bindings, none of which any unit test would have caught: - pj.Call and pj.Account objects finalised after libDestroy() abort the process on a native assertion, exactly as media ports do. Both are now dropped and collected before the pipeline destroys the endpoint. - PJSUA2 keeps delivering callbacks during interpreter teardown, when module globals may already be cleared. The callbacks alias what they need locally and swallow everything: a raise there escapes into C++ and takes the worker thread with it. - hangup() only queues the BYE, so shutdown deleted the account with a call still active and left the far end on an unclosed dialog. stop() now waits briefly for the teardown to complete. Threading follows the established rule: PJSUA2 worker threads reach the loop only through _post_from_pj → run_coroutine_threadsafe, and any thread PJSUA2 did not create registers itself before touching a PJSUA2 object. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -276,17 +276,22 @@ class SippyEngine(SIPEngine):
|
||||
if state == "connected":
|
||||
sdp = data.get("sdp")
|
||||
if sdp and self.media_pipeline:
|
||||
# Signalling only: PJSUA2 surfaces RTP media exclusively
|
||||
# through a call it owns, so a Sippy-owned dialog can
|
||||
# never be given media — the classifier stays deaf on this
|
||||
# engine. PJSUAEngine is the media-capable path; see
|
||||
# docs/architecture.md. Log the negotiated endpoint so the
|
||||
# SIP exchange is still debuggable.
|
||||
try:
|
||||
remote_rtp = self._parse_sdp_rtp_endpoint(sdp)
|
||||
if remote_rtp:
|
||||
leg.media_port = self.media_pipeline.add_remote_stream(
|
||||
leg.leg_id,
|
||||
remote_rtp["host"],
|
||||
remote_rtp["port"],
|
||||
remote_rtp["codec"],
|
||||
logger.info(
|
||||
f" {leg.leg_id}: remote RTP "
|
||||
f"{remote_rtp['host']}:{remote_rtp['port']} "
|
||||
f"({remote_rtp['codec']}) — no media on this engine"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f" Failed to set up media for {leg.leg_id}: {e}")
|
||||
logger.error(f" Failed to parse SDP for {leg.leg_id}: {e}")
|
||||
elif state == "terminated":
|
||||
if self.media_pipeline and leg.media_port is not None:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user