人生似水岂无涯,浮云吹作雪,世味煮成茶...

PJSIP笔记之一初探

2022年11月04日 18:17    0 人评论    624 人阅读

前言

PJSIP 是一个用 C 语言编写的免费开源多媒体通信库,实现了基于标准的协议,比如 SIP、SDP、RTP、STUN、TURN 和 ICE 等。它将信令协议(SIP)、丰富的多媒体框架和NAT穿越功能结合到高级 API 中,该 API 是可移植的,适用于几乎任何类型的系统,从桌面、嵌入式系统到移动手机。

PJSIP由主要以下几个部分组成的:

1).PJSIP - Open Source SIP Stack[开源的SIP协议栈]

2).PJMEDIA - Open Source Media Stack[开源的媒体栈]

3).PJNATH - Open Source NAT Traversal Helper Library[开源的NAT-T辅助库]

4).PJLIB-UTIL - Auxiliary Library[辅助工具库]

5).PJLIB - Ultra Portable Base Framework Library[基础框架库]

编译PJSIP

编译环境:Ubuntu18.04

1、下载源码

进入 PJSIP 的官网下载页下载源码,目前最新的 release 版本是 2.12.1。

2、解压源码

$ tar zxvf pjproject-2.12.1.tar.gz

3、编译源码

在 pjproject-2.12.1/pjlib/include/pj 目录下创建 config_site.h,这个文件可以为空,为空是使用默认值。也可以把 config_site_simple.h 复制一份重命名为 config_site.h。

$ cd pjproject-2.12.1
$ touch ./pjlib/include/pj/config_site.h

执行配置后编译,具体命令如下:

$ ./configure
$ make dep
$ make 

测试PJSIP

1、运行测试

这里使用 pjsua 示例进行测试,执行编译生成的 pjsua-x86_64-unknown-linux-gnu 可执行文件:

$ ./pjsip-apps/bin/pjsua-x86_64-unknown-linux-gnu

可以看到增加了一个 sip 账号 sip:192.168.0.103:5060;transport=TCP

$ ./pjsip-apps/bin/pjsua-x86_64-unknown-linux-gnu 
11:15:43.872         sip_endpoint.c !.Module "mod-pjsua-log" registered
11:15:43.872         sip_endpoint.c  .Module "mod-tsx-layer" registered
11:15:43.872         sip_endpoint.c  .Module "mod-stateful-util" registered
11:15:43.872         sip_endpoint.c  .Module "mod-ua" registered
11:15:43.872         sip_endpoint.c  .Module "mod-100rel" registered
11:15:43.872         sip_endpoint.c  .Module "mod-pjsua" registered
11:15:43.872         sip_endpoint.c  .Module "mod-invite" registered
11:15:43.872                  pjlib  ..select() I/O Queue created (0x55ba948b9af8)
11:15:43.874         sip_endpoint.c  .Module "mod-evsub" registered
11:15:43.874         sip_endpoint.c  .Module "mod-presence" registered
11:15:43.874         sip_endpoint.c  .Module "mod-mwi" registered
11:15:43.874         sip_endpoint.c  .Module "mod-refer" registered
11:15:43.874         sip_endpoint.c  .Module "mod-pjsua-pres" registered
11:15:43.874         sip_endpoint.c  .Module "mod-pjsua-im" registered
11:15:43.874         sip_endpoint.c  .Module "mod-pjsua-options" registered
11:15:43.874           pjsua_core.c  .1 SIP worker threads created
11:15:43.875           pjsua_core.c  .pjsua version 2.12.1 for Linux-5.4.0.125/x86_64/glibc-2.27 initialized
11:15:43.875           pjsua_core.c  .PJSUA state changed: CREATED --> INIT
11:15:43.875         sip_endpoint.c  Module "mod-default-handler" registered
11:15:43.875           pjsua_core.c  SIP UDP socket reachable at 192.168.0.103:5060
11:15:43.875      udp0x55ba948d3dc0  SIP UDP transport started, published address is 192.168.0.103:5060
11:15:43.875            pjsua_acc.c  Adding account: id=<sip:192.168.0.103:5060>
11:15:43.875            pjsua_acc.c  .Account <sip:192.168.0.103:5060> added with id 0
11:15:43.875            pjsua_acc.c  Modifying account 0
11:15:43.875            pjsua_acc.c  Acc 0: setting online status to 1..
11:15:43.875             tcptp:5060  SIP TCP listener ready for incoming connections at 192.168.0.103:5060
11:15:43.875            pjsua_acc.c  Adding account: id=<sip:192.168.0.103:5060;transport=TCP>
11:15:43.875            pjsua_acc.c  .Account <sip:192.168.0.103:5060;transport=TCP> added with id 1
11:15:43.875            pjsua_acc.c  Modifying account 1
11:15:43.875            pjsua_acc.c  Acc 1: setting online status to 1..
11:15:43.875           pjsua_core.c  PJSUA state changed: INIT --> STARTING
11:15:43.876         sip_endpoint.c  .Module "mod-unsolicited-mwi" registered
11:15:43.876           pjsua_core.c  .PJSUA state changed: STARTING --> RUNNING
11:15:43.876                 main.c  Ready: Success
>>>>
Account list:
  [ 0] <sip:192.168.0.103:5060>: does not register
       Online status: Online
 *[ 1] <sip:192.168.0.103:5060;transport=TCP>: does not register
       Online status: Online
Buddy list:
 -none-

+=============================================================================+
|       Call Commands:         |   Buddy, IM & Presence:  |     Account:      |
|                              |                          |                   |
|  m  Make new call            | +b  Add new buddy       .| +a  Add new accnt |
|  M  Make multiple calls      | -b  Delete buddy         | -a  Delete accnt. |
|  a  Answer call              |  i  Send IM              | !a  Modify accnt. |
|  h  Hangup call  (ha=all)    |  s  Subscribe presence   | rr  (Re-)register |
|  H  Hold call                |  u  Unsubscribe presence | ru  Unregister    |
|  v  re-inVite (release hold) |  t  ToGgle Online status |  >  Cycle next ac.|
|  U  send UPDATE              |  T  Set online status    |  <  Cycle prev ac.|
| ],[ Select next/prev call    +--------------------------+-------------------+
|  x  Xfer call                |      Media Commands:     |  Status & Config: |
|  X  Xfer with Replaces       |                          |                   |
|  #  Send RFC 2833 DTMF       | cl  List ports           |  d  Dump status   |
|  *  Send DTMF with INFO      | cc  Connect port         | dd  Dump detailed |
| dq  Dump curr. call quality  | cd  Disconnect port      | dc  Dump config   |
|                              |  V  Adjust audio Volume  |  f  Save config   |
|  S  Send arbitrary REQUEST   | Cp  Codec priorities     |                   |
+-----------------------------------------------------------------------------+
|  q  QUIT   L  ReLoad   sleep MS   echo [0|1|txt]     n: detect NAT type     |
+=============================================================================+
You have 0 active call
>>> 

2、客户端打电话测试

这里使用 SIP 安卓客户端 Linphone 拨打电话测试。

首先添加 ubuntu@192.168.0.103 用户,192.168.0.103 是 Ubuntu PJSIP 服务端的IP地址:

avatar

然后使用 Linphone 拨打电话测试,Ubuntu PJSIP 服务端部分打印信息提示如下:

From: <sip:android@192.168.0.12>
To: <sip:ubuntu@192.168.0.103>
Press a to answer or h to reject call

输入 a 应答来电,提示 Answer with code (100-699) (empty to cancel): 时输入 200 应答码进行接听。

这样 Android Linphone 客户端和 Ubuntu PJSIP 服务端就建立了通讯,但是却没有声音。这是怎么回事呢?

3、解决办法

查看打印信息可以看到如下错误提示:

    11:16:27.673            pjsua_aud.c  ....Error retrieving default audio device parameters: Unable to find default audio device (PJMEDIA_EAUD_NODEFDEV) [status=420006]
    11:16:27.673            pjsua_aud.c  ...Error opening sound device: Unable to find default audio device (PJMEDIA_EAUD_NODEFDEV) [status=420006]

错误原因是找不到音频设备,解决办法是安装 libasound2-dev 库:

sudo apt-get install libasound2-dev

安装好 libasound2-dev 库后,需要重新配置编译:

$ make clean
$ ./configure
$ make dep
$ make 

然后重新运行生成的 pjsua-x86_64-unknown-linux-gnu 可执行文件,使用 Linphone 重新拔掉电话测试,这样双方就都可以正常听到声音了。


如果文章对您有所帮助, 请随意打赏! 您的支持将鼓励我写出更好的文章!

发表评论 已发布 0