
【网络通信 -- SIP 电话】项目实战记录 -- PJSUA API 学习与客户端开发(实现简单的通话功能)
发布日期:2021-05-07 20:51:08
浏览次数:35
分类:原创文章
本文共 6662 字,大约阅读时间需要 22 分钟。
【网络通信 -- SIP 电话】项目实战记录 -- PJSUA API 学习与客户端开发(实现简单的通话功能)
【1】基于 PJSUA 库的 Linux C 开发环境搭建与 Makefile 编写
#Modify this to point to the PJSIP location.# PJBASE 指定 pjproject 的源代码路径PJBASE=/home/myself/pjproject-0.5.10.2include $(PJBASE)/build.makCC = $(PJ_CC)LDFLAGS = $(PJ_LDFLAGS)LDLIBS = $(PJ_LDLIBS)CFLAGS = $(PJ_CFLAGS)CPPFLAGS= ${CFLAGS}# If your application is in a file named myapp.cpp or myapp.c# this is the line you will need to build the binary.# 假定用于编写的 SIPSUA API 测试代码为 myapp.cppall: myappmyapp: myapp.cpp $(CC) -o $@ $< $(CPPFLAGS) $(LDFLAGS) $(LDLIBS)clean: rm -f myapp.o myapp
【2】基于 PJSUA API 实现的简单通话功能代码示例
PJSIP 官方提供的示例代码如下,该示例程序展示了使用 PJSUA API 编写客户端实现一次通话的全过程。
/* $Id: simple_pjsua.c 3553 2011-05-05 06:14:19Z nanang $ *//* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//** * simple_pjsua.c * * This is a very simple but fully featured SIP user agent, with the * following capabilities: * - SIP registration * - Making and receiving call * - Audio/media to sound device. * * Usage: * - To make outgoing call, start simple_pjsua with the URL of remote * destination to contact. * E.g.: * simpleua sip:user@remote * * - Incoming calls will automatically be answered with 200. * * This program will quit once it has completed a single call. */#include <pjsua-lib/pjsua.h>#define THIS_FILE "APP"#define SIP_DOMAIN "example.com"#define SIP_USER "alice"#define SIP_PASSWD "secret"/* Callback called by the library upon receiving incoming call */static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata){ pjsua_call_info ci; PJ_UNUSED_ARG(acc_id); PJ_UNUSED_ARG(rdata); pjsua_call_get_info(call_id, &ci); PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!", (int)ci.remote_info.slen, ci.remote_info.ptr)); /* Automatically answer incoming calls with 200/OK */ pjsua_call_answer(call_id, 200, NULL, NULL);}/* Callback called by the library when call's state has changed */static void on_call_state(pjsua_call_id call_id, pjsip_event *e){ pjsua_call_info ci; PJ_UNUSED_ARG(e); pjsua_call_get_info(call_id, &ci); PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id, (int)ci.state_text.slen, ci.state_text.ptr));}/* Callback called by the library when call's media state has changed */static void on_call_media_state(pjsua_call_id call_id){ pjsua_call_info ci; pjsua_call_get_info(call_id, &ci); if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) { // When media is active, connect call to sound device. pjsua_conf_connect(ci.conf_slot, 0); pjsua_conf_connect(0, ci.conf_slot); }}/* Display error and exit application */static void error_exit(const char *title, pj_status_t status){ pjsua_perror(THIS_FILE, title, status); pjsua_destroy(); exit(1);}/* * main() * * argv[1] may contain URL to call. */int main(int argc, char *argv[]){ pjsua_acc_id acc_id; pj_status_t status; /* Create pjsua first! */ status = pjsua_create(); if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status); /* If argument is specified, it's got to be a valid SIP URL */ if (argc > 1) { status = pjsua_verify_url(argv[1]); if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status); } /* Init pjsua */ { pjsua_config cfg; pjsua_logging_config log_cfg; pjsua_config_default(&cfg); cfg.cb.on_incoming_call = &on_incoming_call; cfg.cb.on_call_media_state = &on_call_media_state; cfg.cb.on_call_state = &on_call_state; pjsua_logging_config_default(&log_cfg); log_cfg.console_level = 4; status = pjsua_init(&cfg, &log_cfg, NULL); if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status); } /* Add UDP transport. */ { pjsua_transport_config cfg; pjsua_transport_config_default(&cfg); cfg.port = 5060; status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL); if (status != PJ_SUCCESS) error_exit("Error creating transport", status); } /* Initialization is done, now start pjsua */ status = pjsua_start(); if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status); /* Register to SIP server by creating SIP account. */ { pjsua_acc_config cfg; pjsua_acc_config_default(&cfg); cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN); cfg.reg_uri = pj_str("sip:" SIP_DOMAIN); cfg.cred_count = 1; cfg.cred_info[0].realm = pj_str(SIP_DOMAIN); cfg.cred_info[0].scheme = pj_str("digest"); cfg.cred_info[0].username = pj_str(SIP_USER); cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; cfg.cred_info[0].data = pj_str(SIP_PASSWD); status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id); if (status != PJ_SUCCESS) error_exit("Error adding account", status); } /* If URL is specified, make call to the URL. */ if (argc > 1) { pj_str_t uri = pj_str(argv[1]); status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL); if (status != PJ_SUCCESS) error_exit("Error making call", status); } /* Wait until user press "q" to quit. */ for (;;) { char option[10]; puts("Press 'h' to hangup all calls, 'q' to quit"); if (fgets(option, sizeof(option), stdin) == NULL) { puts("EOF while reading stdin, will quit now.."); break; } if (option[0] == 'q') break; if (option[0] == 'h') pjsua_call_hangup_all(); } /* Destroy pjsua */ pjsua_destroy(); return 0;}
【3】基于 PJSUA API 的通话 API 调用总结
上述示例代码的调用流程如下图所示,展示了整个通话的建立过程以及对应的 API。
【4】基于 PJSUA API 的通话流程分析
从通话日志中分析通话的流程,以及回调函数的调用时机
【5】基于 PJSUA 库的 SIP 电话注册状态监控
在 pjsua_config 结构体中添加监听 SIP 注册状态的回调函数pjsua_config cfg;cfg.cb.on_reg_state2 = &on_reg_state;实现 on_reg_state 以监听 SIP 电话注册状态void on_reg_state(pjsua_acc_id acc_id, pjsua_reg_info *info){ if(info->cbparam->code != 200) { error_exit("Register Error"); } if(info->cbparam->code == 200) { sip_phone_make_call(acc_id, target_sip_phone_address_val); }}
参考与致谢
本博客为博主的学习实践总结,并参考了众多博主的博文,在此表示感谢,博主若有不足之处,请批评指正。
文档资料与软件包
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月07日 19时00分06秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
GLFW 源码 下载-编译-使用/GLAD配置
2021-05-08
针对单个网站的渗透思路
2021-05-08
Typescript 学习笔记六:接口
2021-05-08
02、MySQL—数据库基本操作
2021-05-08
OpenJDK1.8.0 源码解析————HashMap的实现(一)
2021-05-08
MySQL-时区导致的时间前后端不一致
2021-05-08
2021-04-05阅读小笔记:局部性原理
2021-05-08
go语言简单介绍,增强了解
2021-05-08
架构师入门:搭建基本的Eureka架构(从项目里抽取)
2021-05-08
MongoDB 快速扫盲贴
2021-05-08
one + two = 3
2021-05-08
sctf_2019_easy_heap
2021-05-09
PyQt5之音乐播放器
2021-05-09
Redis进阶实践之十八 使用管道模式提高Redis查询的速度
2021-05-09
SQL注入
2021-05-09
MPI Maelstrom POJ - 1502 ⭐⭐ 【Dijkstra裸题】
2021-05-09
Problem 330A - Cakeminator (思维)
2021-05-09
LeetCode75 颜色分类 (三路快排C++实现与应用)
2021-05-09
C语言+easyX图形库的推箱子实现
2021-05-09
调试vs2019代码的流程
2021-05-09