Back to Insights
VoIP Engineering

PJSIP Explained: What It Is, How It Works and When to Use It

Telephony Engineering Team
6 min read
PJSIP Explained: What It Is, How It Works and When to Use It

PJSIP is a free, open-source communication library written in C. It implements SIP signaling, SDP, RTP media, and the NAT traversal protocols STUN, TURN, and ICE. It is built and maintained by Teluu, and it runs on Windows, Linux, macOS, iOS, Android, and many embedded systems.

If you have used Asterisk, you have already used PJSIP without necessarily knowing it. If you are building a softphone, an embedded device, or a test tool, it is one of the most widely used SIP stacks to build on.

What PJSIP is and what it is not

PJSIP is a toolkit for building SIP clients and endpoints. It is not a PBX and it is not a production SIP proxy. Where Kamailio routes signaling and FreeSWITCH handles media for calls on a server, PJSIP gives a developer the building blocks to make an application take part in SIP calls: register to a server, place and receive calls, send and receive audio, and punch through NAT.

That distinction matters when you choose tools:

You want to buildPJSIP is a good fit?
A SIP softphone for desktop or mobileYes, this is its main use
A SIP client inside a device or applianceYes
A load or functional test tool that makes SIP callsYes
A SIP proxy, registrar, or load balancerNo, use Kamailio or OpenSIPS
A full PBX or media serverNo, use FreeSWITCH or Asterisk
A browser-based softphoneNo, browsers use WebRTC (see SIP.js or JsSIP)

How PJSIP is organized

PJSIP is a set of layered libraries. You can use the high-level API for speed, or drop down to lower layers for control.

  • PJLIB and PJLIB-UTIL. Portability and utility layers: memory pools, threads, sockets, timers, and data structures.
  • PJSIP. The SIP protocol stack: transactions, dialogs, authentication, and transports (UDP, TCP, TLS).
  • PJMEDIA. The media stack: RTP/RTCP, codecs, conference bridge, echo cancellation, and SRTP.
  • PJNATH. NAT traversal: STUN, TURN, and ICE.
  • PJSUA-LIB. A high-level C API that combines the layers above into something closer to a ready-made client.
  • PJSUA2. An object-oriented API on top, with C++ classes and bindings for languages such as Python, Java, and C# generated through SWIG.

Most application developers start with PJSUA2 or PJSUA-LIB. The reference command-line client, pjsua, is built from the same code and is excellent for testing.

How Asterisk uses PJSIP

Asterisk's modern SIP channel driver, chan_pjsip (the res_pjsip family of modules), is built on the PJSIP library. It replaced the older chan_sip driver, which has been deprecated and removed in recent Asterisk releases. When people say "I configured PJSIP in Asterisk", they mean this driver, configured in pjsip.conf.

PJSIP configuration in Asterisk splits what chan_sip kept in one block into several objects. A minimal extension that can register looks like this:

[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0

[6001]
type=endpoint
context=default
disallow=all
allow=ulaw
auth=6001
aors=6001

[6001]
type=auth
auth_type=userpass
username=6001
password=use-a-strong-secret

[6001]
type=aor
max_contacts=1

The three sections share a name but have different type values:

  • endpoint defines call behavior: codecs, dialplan context, and which auth and AOR to use.
  • auth holds the credentials.
  • aor (address of record) stores where the device is currently registered.

If a phone cannot register, the usual causes are a mismatched name between the three objects, a wrong password, or a transport that does not match what the phone is sending.

Trying PJSIP yourself

You can build the library and the pjsua test client from source:

git clone https://github.com/pjsip/pjproject.git
cd pjproject
./configure
make dep && make

Then register to a SIP server from the command line:

./pjsip-apps/bin/pjsua-* \
  --id sip:6001@your-server.example \
  --registrar sip:your-server.example \
  --realm "*" --username 6001 --password your-secret

Replace the server name and credentials with your own. The pjsua console lets you call, answer, hold, and transfer from the keyboard, which makes it a fast way to reproduce a customer's SIP problem.

Features worth knowing

  • Transports: UDP, TCP, and TLS for signaling.
  • Media security: SRTP, including key exchange through SDES and DTLS-SRTP.
  • NAT traversal: ICE with STUN and TURN, which is the main reason PJSIP-based clients behave better behind home and mobile networks. See our NAT traversal guide for how these fit together.
  • Codecs: G.711, G.722, GSM, iLBC, Speex, and others built in, with Opus available through an integration. Codecs with patent licensing, such as G.729, are not bundled for free use.
  • Audio processing: echo cancellation, a conference bridge, and a flexible sound-device layer.
  • Video: supported through external codec libraries.
  • Multiple accounts, presence, and instant messaging.

Licensing

PJSIP is dual licensed. It is available under the GNU GPL version 2, and Teluu also offers commercial licenses. If you ship PJSIP inside a closed-source product, check which license applies to you before release. This is a legal question rather than a technical one, so involve whoever handles licensing in your company.

Common problems with PJSIP projects

  1. Calling PJSIP from threads it did not create. PJSIP expects threads that use its API to be registered with it. Forgetting this causes crashes that look random.
  2. Mobile background behavior. iOS and Android limit what an app can do in the background, so incoming calls usually need push notifications rather than a persistent registration.
  3. One-way audio. Almost always NAT. Enable ICE and TURN, and check that the SDP carries the addresses you expect. Our guide to what SIP is and how it works explains the signaling that carries them.
  4. Codec mismatch. A call that connects but has no audio, or fails at 488, often means the two sides share no codec.
  5. TLS build issues. TLS needs an SSL library such as OpenSSL available when you build.

PJSIP vs the alternatives

LibraryBest known for
PJSIPBroad platform support, a complete media and NAT stack, used inside Asterisk
Sofia-SIPThe SIP stack inside FreeSWITCH, a signaling library without a media layer
Linphone / liblinphoneA full open-source softphone and SDK
baresipA small, modular SIP user agent
SIP.js / JsSIPSIP over WebSocket and WebRTC in the browser

For a native mobile or desktop softphone, PJSIP is a common first choice because it covers signaling, media, and NAT traversal in one place.

Frequently asked questions

Is PJSIP free?

It is open source under GPL version 2, and a commercial license is available from Teluu for products that cannot use the GPL.

Is PJSIP the same thing as Asterisk?

No. PJSIP is a library. Asterisk is a PBX that uses the PJSIP library inside its chan_pjsip driver.

Can PJSIP act as a SIP server?

It is designed as a client and endpoint toolkit. For a production proxy, registrar, or load balancer, use a server such as Kamailio or OpenSIPS.

Does PJSIP support WebRTC?

PJSIP is a native library. Browser calling uses the browser's WebRTC stack, usually with a JavaScript SIP library and a gateway, such as in our WebRTC to SIP gateway guide.

Which languages can I use?

The core is C. PJSUA2 provides C++ classes and bindings for languages including Python, Java, and C#.

References


Need a softphone, SIP client, or test tooling built on PJSIP? Our engineers can help you hire VoIP developers for a defined project or ongoing support.

Discussion

0

Join the conversation

Sign in with your preferred account to comment, reply, and keep the discussion useful for other engineers.

Takes a few seconds. No separate password required.

C

Controva LLC

Engineering Team

We build digital empires that scale. Specializing in high-performance web architecture, AI automation, and headless commerce.

Work with us

Share this article