Add PJSUA2 AudioMediaAiPort wrapper for AI media port#4870
Open
Add PJSUA2 AudioMediaAiPort wrapper for AI media port#4870
Conversation
Contributor
There was a problem hiding this comment.
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) plusAiMediaEvent/AiMediaPortParamtypes 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
AudioMediaAiPortis director-enabled andpjmedia_ai_event_typeis 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
35747ae to
011997f
Compare
- 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
011997f to
3e90421
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Wraps the experimental
pjmedia_ai_port(from PR #4866) as a PJSUA2AudioMediasubclass, enabling C++/Java/Python/C# applications to use the AI media port through the high-level API.Ref: #4523
Usage
Events (connected, transcript, etc.) are delivered via the virtual
onEvent()callback, which can be overridden in C++ or target languages via SWIG directors.Design
pjmedia_ai_event_typeenum directly (no wrapper enum)user_databack-pointer under grp_lock to prevent use-after-free from in-flight callbacksLimitations
These are known limitations for reference in future improvements:
ssl_parambut it is not yet exposed in the PJSUA2 wrapper.isConnected()/getState()query method — connection state can only be inferred fromonEvent()callbacksreadObject/writeObject) forAiMediaPortParamChanges
pjsip/include/pjsua2/media.hppAiMediaEvent,AiMediaPortParam,AudioMediaAiPortclasspjsip/src/pjsua2/media.cpppjmedia/include/pjmedia/ai_port.hpjmedia_ai_port_set_user_data()pjmedia/src/pjmedia/ai_port.cset_user_data()pjmedia/include/pjmedia.hai_port.hto umbrella headerpjsip-apps/src/swig/pjsua2.iAudioMediaAiPortpjsip-apps/src/swig/symbols.lstpjmedia_ai_event_typepjsip-apps/src/swig/symbols.iimportsym2.pypjsip-apps/src/swig/python/test.pyTest plan
Co-Authored-By: Claude Code