Asterisk + pjsua
A SIP lab on your laptop: Asterisk in Docker and pjsua
Two softphone accounts, an echo test and a small dialplan, then sngrep and Wireshark to see what actually went over the wire.
7 min readPart 1 of 2
Most SIP problems get easier once you've watched a working call go over the wire. You don't need a carrier or a desk phone for that. Asterisk in a container and pjsua on the command line are enough to register two accounts, call between them and capture every packet, and the result is a safe place to try things before they go anywhere near a real system.
By the end of the post you'll have:
- Asterisk in Docker with two SIP accounts,
aliceandbob - an echo test, a playback test, and alice calling bob
- sngrep and Wireshark showing exactly what happened
This assumes a Linux host. Docker Desktop on macOS or Windows can be made to work, but RTP through its VM adds a pile of NAT problems that aren't what this post is about.
Asterisk in a container
There's no official Asterisk image, so build a small one from Ubuntu's package:
FROM ubuntu:24.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y asterisk asterisk-core-sounds-en-gsm \
&& rm -rf /var/lib/apt/lists/*
COPY pjsip.conf extensions.conf rtp.conf /etc/asterisk/
CMD ["asterisk", "-f", "-vvv"]Ubuntu 24.04 packages Asterisk 20, and -f keeps it in the foreground so Docker can supervise it. Configure it with the PJSIP channel driver, which is what you want anyway: the old chan_sip was removed entirely in Asterisk 21, so there's no point learning it now.
services:
asterisk:
build: .
container_name: asterisk
network_mode: host
restart: unless-stoppedHost networking is a deliberate shortcut. Asterisk sees the host's real interfaces, so the addresses it writes into SIP headers and SDP are ones your softphones can actually reach. On a bridge network you'd have to publish the whole RTP port range and tell Asterisk its external address, and one-way audio is the usual reward for getting that slightly wrong.
RTP ports
Each call's audio uses UDP ports from a configurable range. The default is 10000 to 20000, far more than a lab needs, and a smaller range makes captures and firewall rules simpler:
[general]
rtpstart=10000
rtpend=10100Two accounts
PJSIP splits configuration into object types. A transport says where to listen. Each user then gets an endpoint (how their calls behave), an auth (their credentials) and an aor (where they can be reached once registered). Templates keep the repetition down:
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0:5060
[endpoint-tpl](!)
type=endpoint
context=lab
disallow=all
allow=ulaw,alaw
direct_media=no
[auth-tpl](!)
type=auth
auth_type=userpass
[aor-tpl](!)
type=aor
max_contacts=1
remove_existing=yes
[alice](endpoint-tpl)
auth=alice
aors=alice
[alice](auth-tpl)
username=alice
password=alice-lab-secret
[alice](aor-tpl)
[bob](endpoint-tpl)
auth=bob
aors=bob
[bob](auth-tpl)
username=bob
password=bob-lab-secret
[bob](aor-tpl)Yes, alice appears three times. Section names can repeat as long as the type differs, and that's the normal way to write it.
Two settings are there on purpose. direct_media=no keeps Asterisk in the audio path, so its RTP debug output shows every packet. And remove_existing=yes lets a fresh registration replace the old one. Without it, max_contacts=1 rejects pjsua's new registration every time you restart it, until the old one expires, and you'll spend ten minutes wondering why your password suddenly stopped working.
A dialplan worth calling
[lab]
exten => 100,1,Answer()
same => n,Playback(hello-world)
same => n,Hangup()
exten => 600,1,Answer()
same => n,Echo()
same => n,Hangup()
exten => alice,1,Dial(PJSIP/alice,20)
same => n,Hangup()
exten => bob,1,Dial(PJSIP/bob,20)
same => n,Hangup()100 plays a recording. 600 sends your own audio back to you. Dialing a user's name rings them for 20 seconds.
Bring it up and check Asterisk loaded both endpoints:
docker compose up -d --build
docker exec -it asterisk asterisk -rx "pjsip show endpoints"Both should be listed as Unavailable for now. That just means nobody has registered yet.
pjsua as the phone
pjsua is the command-line softphone that ships with pjproject, the same SIP stack Asterisk's PJSIP driver is built on. Distros don't package it, so build it yourself, pinned to a release:
git clone --depth 1 --branch 2.17 https://github.com/pjsip/pjproject.git
cd pjproject
./configure
make dep && make -j"$(nproc)"
ls pjsip-apps/bin/The binary is named after your target triple, something like pjsua-x86_64-unknown-linux-gnu. Symlink it to pjsua somewhere on your PATH and forget about it.
Register alice in one terminal:
pjsua --id sip:alice@127.0.0.1 --registrar sip:127.0.0.1 \
--realm '*' --username alice --password alice-lab-secret \
--local-port 5070 --null-audioAnd bob in a second one, set to answer every call:
pjsua --id sip:bob@127.0.0.1 --registrar sip:127.0.0.1 \
--realm '*' --username bob --password bob-lab-secret \
--local-port 5080 --null-audio --auto-answer 200Each instance needs its own --local-port, since Asterisk already owns 5060 on the host. --null-audio means no sound card is required, so this also works over SSH. Back in Asterisk, pjsip show contacts should now list both users.
From alice's prompt, press m and enter sip:600@127.0.0.1 for the echo test, or sip:bob@127.0.0.1 to ring bob. h hangs up and q quits. With --null-audio you won't hear anything, of course. Drop the flag on a machine with speakers, or add --play-file hello.wav --auto-play to send a WAV file into the call.
Watching the call
This is the part that makes the lab worth building.
sngrep shows SIP dialogs as a live ladder diagram in your terminal:
sudo sngrep port 5060Pick alice's call to bob and you'll see the whole conversation. The REGISTER gets challenged with a 401, then succeeds once pjsua answers the challenge. The INVITE gets challenged too (Asterisk wants credentials for calls as well as registrations), so it goes INVITE, 401, ACK, then the INVITE again with credentials, followed by 100 Trying, 180 Ringing, 200 OK and ACK. Hanging up is a BYE and a 200 OK.
Press Enter on any message to read it in full, SDP body included. These are the SDP lines that matter:
c=IN IP4 192.168.1.20
m=audio 10014 RTP/AVP 0 8 101
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000c= and m= say where to send the audio: that address, UDP port 10014. The numbers after RTP/AVP are payload types: 0 is G.711 µ-law, 8 is A-law, and 101 carries DTMF digits as events. The offer lists everything the caller can do. The answer's m= line is what the call actually uses.
Wireshark is better for the audio itself. Capture on the lab host and open the file on your desktop:
sudo tcpdump -ni any -w call.pcap 'udp port 5060 or udp portrange 10000-10100'In Wireshark, Telephony → VoIP Calls lists every call in the capture. Flow Sequence draws the same ladder sngrep does, and Telephony → RTP → RTP Streams shows loss, jitter and sequence errors per stream. If the RTP shows up as plain UDP, enable the rtp_udp heuristic under Analyze → Enabled Protocols and it'll be decoded properly.
When it doesn't work
The most common failures in a setup like this, and what they usually turn out to be:
| What you see | Usual cause |
|---|---|
401 Unauthorized on every attempt, then 403 Forbidden | Wrong username or password in pjsua |
404 Not Found on the INVITE | The number isn't in the endpoint's context |
488 Not Acceptable Here | No codec in common; compare allow= with the offer |
| Call connects but audio only goes one way | An address in the SDP that the other side can't reach (NAT, Docker bridge) |
| Call drops after about 32 seconds | The ACK never arrived, usually NAT or a firewall again |
The 32-second one deserves a word. When the 200 OK for an INVITE isn't acknowledged, the sender keeps retransmitting it and gives up after 64 times the T1 timer, which is 32 seconds with the default 500 ms. So a call that dies at exactly that mark is almost never about audio. Go and look for the missing ACK.
Asterisk will tell you a lot if you ask it. Attach with docker exec -it asterisk asterisk -rvvv and run:
pjsip set logger on
rtp set debug on
core set verbose 5The PJSIP logger prints every SIP message Asterisk sends and receives. Most of the time that alone is enough.
In the next post, the two terminals get replaced by a Python script that places calls, plays audio, records what comes back and fails a CI build when the result is wrong.
References
- Asterisk documentation: PJSIP configuration sections and relationships and the res_pjsip configuration reference, which documents every option used here, including
direct_media,max_contactsandremove_existing. - Asterisk 21 Module Removal: chan_sip, asterisk.org.
- asterisk in Ubuntu 24.04, Launchpad: the packaged version.
- pjproject on GitHub, the 2.17 release, and pjsua's option list in its source.
- sngrep.
- Wireshark: VoIP Calls in the User's Guide and the RTP page on the wiki, which covers the
rtp_udpheuristic. - RFC 3261, SIP: digest challenges, and the 64×T1 limit on retransmitting a
200 OKthat never gets its ACK. - RFC 8866, SDP, RFC 3551 (static payload types 0 and 8) and RFC 4733 (
telephone-eventfor DTMF).
#sip #asterisk #pjsip