Edit Template

How to Set Up a Basic SIP Application Using PJSIP

5 min read

Index

 

Introduction

Session Initiation Protocol (SIP) is a fundamental signaling protocol used to initiate, manage, and terminate real-time sessions involving voice, video, messaging, and other multimedia communications over IP networks. SIP plays a critical role in Voice over IP (VoIP) technologies, enabling effective communication across diverse devices and platforms.

PJSIP is an open-source multimedia communication library implemented in C that provides a comprehensive SIP stack, media handling, and NAT traversal functionalities. It is widely used by developers to build high-performance SIP-based applications, whether desktop softphones, mobile VoIP clients, or embedded communication systems.

This guide offers a detailed walkthrough on setting up a basic SIP application using PJSIP, covering installation, configuration, development, and testing, with a focus on key SIP concepts and practical code examples.

PJSIP Overview

PJSIP is designed for efficiency and flexibility, packing multiple communication capabilities into a lightweight library:

  • SIP Stack: Manages SIP message parsing, transactions, dialogs, and state machines.
  • Media Stack: Implements RTP/RTCP for media streaming over IP.
  • NAT Traversal: Supports STUN, TURN, and ICE protocols to handle firewall and NAT issues.
  • PJSUA API: High-level API to simplify SIP account management, calling, and media handling.
  • Multi-platform Support: Works on Linux, Windows, macOS, Android, and iOS.

This makes PJSIP a versatile choice for SIP-based application development requiring signaling, media, and network traversal functionalities.

Installing and Setting Up PJSIP

Step 1: Preparing the Environment

Ensure a development environment on Linux or Windows with build tools like gcc, make, and Python. Also install audio system libraries if audio support is required.

Step 2: Downloading PJSIP

wget https://github.com/pjsip/pjproject/archive/refs/tags/2.15.tar.gz
tar -xvzf 2.15.tar.gz
cd pjproject-2.15

Step 3: Configuring and Building

./configure
make dep
make
sudo make install

This builds and installs the PJSIP libraries.

Step 4: Setting Environment Variables

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

This ensures the libraries are discoverable by your system.

Building a Basic SIP Application Using PJSIP

The following steps outline creating a simple SIP client to register with a SIP server and place calls using PJSIP’s C++ API (PJSUA2):

Step 1: Initialize the PJSIP Endpoint

#include <pjsua2.hpp>

using namespace pj;

int main() {
    Endpoint ep;
    ep.libCreate();

    EpConfig ep_cfg;
    ep.libInit(ep_cfg);

    TransportConfig tcfg;
    tcfg.port = 5060;  // Default SIP port
    ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg);

    ep.libStart();
    return 0;
}

Step 2: Create a SIP Account

AccountConfig acc_cfg;
acc_cfg.idUri = "sip:username@sipserver.com";
acc_cfg.regConfig.registrarUri = "sip:sipserver.com";

AuthCredInfo cred("digest", "*", "username", 0, "password");
acc_cfg.sipConfig.authCreds.push_back(cred);

Account *acc = new Account();
acc->create(acc_cfg);

Step 3: Make a Call

Call *call = new Call(*acc);

CallOpParam prm(true);
prm.statusCode = PJSIP_SC_OK;

call->makeCall("sip:destination@sipserver.com", prm);

Step 4: Handle Incoming Calls

Override the callback methods in your Account or Call subclass to manage incoming call events, such as ringing, answering, and call state changes.

Testing and Debugging

Run your SIP client application and monitor console output for registration and call statuses. PJSIP provides detailed logging options configurable via EpConfig and environment variables, which are invaluable for troubleshooting SIP signaling and media flow issues.

Security Best Practices for SIP Applications

  • Use strong, unique passwords for SIP accounts to prevent unauthorized access.
  • Implement TLS encryption for SIP signaling to protect against interception.
  • Use SRTP (Secure RTP) to encrypt media streams for privacy.
  • Apply firewall rules carefully to restrict SIP traffic and consider disabling SIP ALG on routers.
  • Keep your PJSIP and related libraries updated with security patches.

Conclusion

Setting up a basic SIP application using PJSIP provides developers a robust foundation for building feature-rich VoIP solutions with full control over signaling, media, and network traversal. This guide covered essential concepts, installation steps, development code examples, testing practices, and security guidelines to get started confidently with SIP app development.

For further exploration, consider advanced topics like video calls, instant messaging, presence, and real-time database integration using PJSIP.

Share:

Previous Post
Next Post

Related Posts

When Should You Use LiveKit for Your Business Decision-making guide for CTOs

When Should You Use LiveKit for Your Business Decision-making guide for CTOs

Every technology decision a CTO makes carries two kinds of risk: the risk of choosing the wrong tool, and the...
Read More
LiveKit Architecture Deep Dive: SFU, Media Routing, and Scaling

LiveKit Architecture Deep Dive: SFU, Media Routing, and Scaling

Real-time communication has become the invisible backbone of modern software. From telehealth appointments to collaborative coding environments and AI-powered voice...
Read More
Building Enterprise Call Centers with Asterisk

Building Enterprise Call Centers with Asterisk

Every second counts in customer service. A customer waiting on hold, an agent struggling with outdated tools, a call routed...
Read More
Asterisk Integration with CRM: Salesforce, HubSpot, and Zendesk

Asterisk Integration with CRM: Salesforce, HubSpot, and Zendesk

The modern business landscape demands seamless integration between communication platforms and customer relationship management systems. Organizations worldwide are recognizing that...
Read More
How MVNOs Make Money: Business Model Breakdown

How MVNOs Make Money: Business Model Breakdown

In the competitive landscape of telecommunications, Mobile Virtual Network Operators (MVNOs) have carved out a unique and profitable niche. Unlike traditional carriers...
Read More
How to Save Money with MVNO Plans Without Sacrificing Quality

How to Save Money with MVNO Plans Without Sacrificing Quality

In today’s increasingly connected world, mobile phone service has become an essential utility for millions of consumers worldwide. However, the...
Read More

    Have a VoIP or WebRTC project in mind?

    Get 1 hour of free expert consulting from Sheerbit Technologies — no commitment required.






    Real Clients. Real Results.

    Hear how businesses like yours scaled faster with us.

    Edit Template