Skip to content

Add PJSUA2 AudioMediaAiPort wrapper for AI media port#4870

Open
nanangizz wants to merge 3 commits intomasterfrom
feature/ai-port-pjsua2
Open

Add PJSUA2 AudioMediaAiPort wrapper for AI media port#4870
nanangizz wants to merge 3 commits intomasterfrom
feature/ai-port-pjsua2

Conversation

@nanangizz
Copy link
Member

@nanangizz nanangizz commented Mar 18, 2026

Summary

Wraps the experimental pjmedia_ai_port (from PR #4866) as a PJSUA2 AudioMedia subclass, enabling C++/Java/Python/C# applications to use the AI media port through the high-level API.

Ref: #4523

Usage

AudioMediaAiPort ai;
ai.createPort();
ai.connect("wss://api.openai.com/v1/realtime?model=...", apiKey);

// Route call audio to/from AI
callMedia.startTransmit(ai);
ai.startTransmit(callMedia);

// Later
ai.disconnect();

Events (connected, transcript, etc.) are delivered via the virtual onEvent() callback, which can be overridden in C++ or target languages via SWIG directors.

Design

  • Uses pjsua's SIP endpoint ioqueue and timer heap for WebSocket I/O — no separate worker thread, pjsua's existing worker threads handle polling
  • Uses pjmedia_ai_event_type enum directly (no wrapper enum)
  • Destructor nulls the user_data back-pointer under grp_lock to prevent use-after-free from in-flight callbacks
  • Wrapper pool lifetime tied to grp_lock destroy handler to prevent premature release

Limitations

These are known limitations for reference in future improvements:

  • No SSL/TLS parameter configuration (e.g. certificate verification, client certs, cipher suites). The C API supports ssl_param but it is not yet exposed in the PJSUA2 wrapper.
  • No way to customize the OpenAI session configuration (voice, instructions, modalities) — currently hardcoded in the backend
  • No isConnected() / getState() query method — connection state can only be inferred from onEvent() callbacks
  • No JSON persistence (readObject/writeObject) for AiMediaPortParam

Changes

File Description
pjsip/include/pjsua2/media.hpp AiMediaEvent, AiMediaPortParam, AudioMediaAiPort class
pjsip/src/pjsua2/media.cpp Implementation (~120 lines)
pjmedia/include/pjmedia/ai_port.h Add pjmedia_ai_port_set_user_data()
pjmedia/src/pjmedia/ai_port.c Implement set_user_data()
pjmedia/include/pjmedia.h Add ai_port.h to umbrella header
pjsip-apps/src/swig/pjsua2.i Add director feature for AudioMediaAiPort
pjsip-apps/src/swig/symbols.lst Add pjmedia_ai_event_type
pjsip-apps/src/swig/symbols.i Regenerated with importsym2.py
pjsip-apps/src/swig/python/test.py Python SWIG test for AI port lifecycle + optional OpenAI connection

Test plan

  • Build verified on Linux (zero warnings)
  • Build on Windows/MSVC (Release-Static, C++ lib clean)
  • Python SWIG test: create, conf bridge register, destroy (WSL)
  • Python SWIG test with OpenAI connection (requires API key + audio device)

Co-Authored-By: Claude Code

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR exposes the experimental pjmedia_ai_port (AI WebSocket media port) through PJSUA2 by adding an AudioMedia subclass, enabling higher-level language bindings (C++/Java/Python/C#) to connect conference-bridge audio to an AI backend (OpenAI Realtime) and receive events via callbacks.

Changes:

  • Add AudioMediaAiPort (PJSUA2) plus AiMediaEvent/AiMediaPortParam types and implement create/connect/disconnect with an OpenAI backend.
  • Add pjmedia_ai_port_set_user_data() to support safely clearing the back-pointer during teardown.
  • Update SWIG interface/symbol exports so AudioMediaAiPort is director-enabled and pjmedia_ai_event_type is available to bindings.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pjsip/include/pjsua2/media.hpp Adds the new PJSUA2 wrapper class and associated parameter/event structs.
pjsip/src/pjsua2/media.cpp Implements the wrapper, event callback bridging, and teardown logic.
pjmedia/include/pjmedia/ai_port.h Exposes a new public setter for AI port user data.
pjmedia/src/pjmedia/ai_port.c Implements the new user-data setter function.
pjmedia/include/pjmedia.h Pulls ai_port.h into the PJMEDIA umbrella header.
pjsip-apps/src/swig/pjsua2.i Enables SWIG directors for AudioMediaAiPort callbacks.
pjsip-apps/src/swig/symbols.lst Exports pjmedia_ai_event_type for SWIG symbol generation.
pjsip-apps/src/swig/symbols.i Regenerated SWIG symbols to include pjmedia_ai_event_type (and related enum updates).

You can also share your feedback on Copilot code review. Take the survey.

Wraps pjmedia_ai_port as a PJSUA2 AudioMedia subclass, enabling
C++/Java/Python/C# apps to connect to AI services (e.g. OpenAI
Realtime API) through the high-level API with startTransmit()/
stopTransmit().

Changes:
- Add AudioMediaAiPort class with createPort(), connect(),
  disconnect(), and virtual onEvent() callback
- Add AiMediaEvent struct and AiMediaPortParam for C++ API
- Use pjsua's SIP endpoint ioqueue/timer heap (no separate worker
  thread needed)
- Add pjmedia_ai_port_set_user_data() to C API for safe callback
  invalidation on destruction
- Add ai_port.h to pjmedia.h umbrella header
- Update SWIG: director feature for AudioMediaAiPort, symbols.lst
  entry for pjmedia_ai_event_type, regenerated symbols.i

Ref: #4523

Co-Authored-By: Claude Code
- Fix pool lifetime: register grp_lock destroy handler to release
  the wrapper pool (which holds the backend struct) only after the
  port's ref-count reaches zero, preventing use-after-free if
  pjmedia_port_destroy is deferred by the conference bridge
- Remove fixed 200ms sleep from destructor, rely on grp_lock
  ref-counting for proper synchronization
- Document thread-safety requirement on set_user_data (caller must
  hold grp_lock if callbacks may run concurrently)
- Fix createPort() doc: clarify it uses OpenAI backend, not a
  user-specified backend type

Co-Authored-By: Claude Code
@nanangizz nanangizz force-pushed the feature/ai-port-pjsua2 branch from 35747ae to 011997f Compare March 18, 2026 10:38
- Add ua_ai_port_test() to Python SWIG test: creates AI port,
  registers to conf bridge, optionally connects to OpenAI if
  OPENAI_API_KEY is set (interactive mic/speaker, ENTER to stop),
  clean destroy
- Fix disconnect() to silently ignore PJ_EINVALIDOP (not connected)
  instead of raising an error

Co-Authored-By: Claude Code
@nanangizz nanangizz force-pushed the feature/ai-port-pjsua2 branch from 011997f to 3e90421 Compare March 18, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants