APP & programiOS

iOS 基于 PJSIP 开发 VOIP 应用 --- 编译集成

2023-01-27  本文已影响0人  试图与自己和解

前言

Telegram 的研究先放一放,在 Telegram 源码中看到了 VOIP 相关功能,不禁想到自己上家公司的业务就是做 VOIP 通讯,趁着现在自己这段时间勤奋劲还没过,特地记录下。

PJSIP 是什么?

PJSIP 是一个用 C 语言编写的开源嵌入式 SIP 协议库,实现了基于标准的协议(如SIP、SDP、RTP,STUN、TURN 和 ICE);把基于信令协议 SIP 的多媒体框架和 NAT 穿透功能整合成高层次、抽象的多媒体通信 API。

PJSIP 具有非常好的移植性,几乎支持现今所有系统:从桌面系统、嵌入式系统到智能手机。

PJSIP 同时支持语音、视频、状态呈现和即时通讯;PJSIP 具有非常完善的文档,对开发者非常友好。

PJSIP 基本框架

PJSIP 库主要包括:

SIP 协议部分
SIP 协议由下往上进行了多层封装:

对于 PJSIP 有以上基本了解就行,毕竟目前我们主要目的是编译出 iOS 多架构静态库,并导入到我们项目中进行功能开发~

PJSIP 自动化导入

pod 'pjsip'

懒人必备方式,已经有先驱们因为个人需要编译了这个版本

如果需要扩展某些功能,他人编译出来的版本未必会支持,到头来,还是得自己重新编译~

PJSIP 手动编译

官方编译文档点这里

截至目前(2023/01/28)为止,官方能下载的最新版本是 2.13,因此,我将在这个版本上编译。

image.png
一、下载并解压缩

首先,我们去官方下载源代码,推荐下载 .tar.gz 的压缩包。

下载完毕,我们可以将其放到一个合适的目录下(最好路径不包含中文),我这里就是将其放在桌面上;

若你电脑上已经安装了解压缩软件,就可以双击解压缩;没有的话也没关系,我们打开终端,用命令解压缩:

tar -jxvf pjproject-2.13.tar.gz

这样我们就能得到一个名为 pjproject-2.13 的文件夹。

二、编译

这里我已经写好了一个脚本:

#!/bin/sh
touch pjlib/include/pj/config_site.h
echo "#define PJ_CONFIG_IPHONE 1
#define PJ_HAS_IPV6 1
#include <pj/config_site_sample.h>
" > pjlib/include/pj/config_site.h

mkdir -p ./lib

export MIN_IOS="-miphoneos-version-min=8.0"
export DEVPATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer

#编译 i386、x86_64、arm64 3种CPU架构
export ARCH="-arch i386" CFLAGS="-O2 -m32 -mios-simulator-version-min=8.0" LDFLAGS="-O2 -m32 -mios-simulator-version-min=8.0"
./configure-iphone
make dep && make clean && make
export ARCH="-arch x86_64" CFLAGS="-O2 -m64 -mios-simulator-version-min=8.0" LDFLAGS="-O2 -m64 -mios-simulator-version-min=8.0"
./configure-iphone
make dep && make clean && make
export ARCH="-arch arm64"
./configure-iphone
make dep && make clean && make

#合并3种CPU架构的.a库
lipo -arch i386 third_party/lib/libg7221codec-i386-apple-darwin_ios.a -arch x86_64 third_party/lib/libg7221codec-x86_64-apple-darwin_ios.a -arch arm64 third_party/lib/libg7221codec-arm64-apple-darwin_ios.a -create -output lib/libg7221codec.a

lipo -arch i386 third_party/lib/libgsmcodec-i386-apple-darwin_ios.a -arch x86_64 third_party/lib/libgsmcodec-x86_64-apple-darwin_ios.a -arch arm64 third_party/lib/libgsmcodec-arm64-apple-darwin_ios.a -create -output lib/libgsmcodec.a

lipo -arch i386 third_party/lib/libilbccodec-i386-apple-darwin_ios.a -arch x86_64 third_party/lib/libilbccodec-x86_64-apple-darwin_ios.a -arch arm64 third_party/lib/libilbccodec-arm64-apple-darwin_ios.a -create -output lib/libilbccodec.a

lipo -arch i386 pjlib/lib/libpj-i386-apple-darwin_ios.a -arch x86_64 pjlib/lib/libpj-x86_64-apple-darwin_ios.a -arch arm64 pjlib/lib/libpj-arm64-apple-darwin_ios.a -create -output lib/libpj.a

lipo -arch i386 pjlib-util/lib/libpjlib-util-i386-apple-darwin_ios.a -arch x86_64 pjlib-util/lib/libpjlib-util-x86_64-apple-darwin_ios.a -arch arm64 pjlib-util/lib/libpjlib-util-arm64-apple-darwin_ios.a -create -output lib/libpjlib-util.a

lipo -arch i386 pjmedia/lib/libpjmedia-audiodev-i386-apple-darwin_ios.a -arch x86_64 pjmedia/lib/libpjmedia-audiodev-x86_64-apple-darwin_ios.a -arch arm64 pjmedia/lib/libpjmedia-audiodev-arm64-apple-darwin_ios.a -create -output lib/libpjmedia-audiodev.a

lipo -arch i386 pjmedia/lib/libpjmedia-codec-i386-apple-darwin_ios.a -arch x86_64 pjmedia/lib/libpjmedia-codec-x86_64-apple-darwin_ios.a -arch arm64 pjmedia/lib/libpjmedia-codec-arm64-apple-darwin_ios.a -create -output lib/libpjmedia-codec.a

lipo -arch i386 pjmedia/lib/libpjmedia-videodev-i386-apple-darwin_ios.a -arch x86_64 pjmedia/lib/libpjmedia-videodev-x86_64-apple-darwin_ios.a -arch arm64 pjmedia/lib/libpjmedia-videodev-arm64-apple-darwin_ios.a -create -output lib/libpjmedia-videodev.a

lipo -arch i386 pjmedia/lib/libpjmedia-i386-apple-darwin_ios.a -arch x86_64 pjmedia/lib/libpjmedia-x86_64-apple-darwin_ios.a -arch arm64 pjmedia/lib/libpjmedia-arm64-apple-darwin_ios.a -create -output lib/libpjmedia.a

lipo -arch i386 pjnath/lib/libpjnath-i386-apple-darwin_ios.a -arch x86_64 pjnath/lib/libpjnath-x86_64-apple-darwin_ios.a -arch arm64 pjnath/lib/libpjnath-arm64-apple-darwin_ios.a -create -output lib/libpjnath.a

lipo -arch i386 pjsip/lib/libpjsip-simple-i386-apple-darwin_ios.a -arch x86_64 pjsip/lib/libpjsip-simple-x86_64-apple-darwin_ios.a -arch arm64 pjsip/lib/libpjsip-simple-arm64-apple-darwin_ios.a -create -output lib/libpjsip-simple.a

lipo -arch i386 pjsip/lib/libpjsip-ua-i386-apple-darwin_ios.a -arch x86_64 pjsip/lib/libpjsip-ua-x86_64-apple-darwin_ios.a -arch arm64 pjsip/lib/libpjsip-ua-arm64-apple-darwin_ios.a -create -output lib/libpjsip-ua.a

lipo -arch i386 pjsip/lib/libpjsip-i386-apple-darwin_ios.a -arch x86_64 pjsip/lib/libpjsip-x86_64-apple-darwin_ios.a -arch arm64 pjsip/lib/libpjsip-arm64-apple-darwin_ios.a -create -output lib/libpjsip.a

lipo -arch i386 pjsip/lib/libpjsua-i386-apple-darwin_ios.a -arch x86_64 pjsip/lib/libpjsua-x86_64-apple-darwin_ios.a -arch arm64 pjsip/lib/libpjsua-arm64-apple-darwin_ios.a -create -output lib/libpjsua.a

lipo -arch i386 pjsip/lib/libpjsua2-i386-apple-darwin_ios.a -arch x86_64 pjsip/lib/libpjsua2-x86_64-apple-darwin_ios.a -arch arm64 pjsip/lib/libpjsua2-arm64-apple-darwin_ios.a -create -output lib/libpjsua2.a

lipo -arch i386 third_party/lib/libresample-i386-apple-darwin_ios.a -arch x86_64 third_party/lib/libresample-x86_64-apple-darwin_ios.a -arch arm64 third_party/lib/libresample-arm64-apple-darwin_ios.a -create -output lib/libresample.a

lipo -arch i386 third_party/lib/libspeex-i386-apple-darwin_ios.a -arch x86_64 third_party/lib/libspeex-x86_64-apple-darwin_ios.a -arch arm64 third_party/lib/libspeex-arm64-apple-darwin_ios.a -create -output lib/libspeex.a

lipo -arch i386 third_party/lib/libsrtp-i386-apple-darwin_ios.a -arch x86_64 third_party/lib/libsrtp-x86_64-apple-darwin_ios.a -arch arm64 third_party/lib/libsrtp-arm64-apple-darwin_ios.a -create -output lib/libsrtp.a

lipo -arch i386 third_party/lib/libwebrtc-i386-apple-darwin_ios.a -arch x86_64 third_party/lib/libwebrtc-x86_64-apple-darwin_ios.a -arch arm64 third_party/lib/libwebrtc-arm64-apple-darwin_ios.a -create -output lib/libwebrtc.a

lipo -arch i386 third_party/lib/libyuv-i386-apple-darwin_ios.a -arch x86_64 third_party/lib/libyuv-x86_64-apple-darwin_ios.a -arch arm64 third_party/lib/libyuv-arm64-apple-darwin_ios.a -create -output lib/libyuv.a

将该脚本保存为 build-iphone.sh 存放到刚解压完成的 pjproject-2.13 文件夹下,然后执行:

cd pjproject-2.13

进入到 pjproject-2.13 文件夹,接着执行

./build-iphone.sh

如果提示权限不够,那么使用:

chmod 777 build-iphone.sh

来赋予该文件最大的访问权限,接着再次执行:

./build-iphone.sh

执行完后,所有生成的不同架构的静态库都会被合并,且存放在 pjproject-2.13/lib 目录下:

image.png
三、集成

在桌面上新建一个文件夹,名为 PJSIP,将上一步 编译 完成的 lib 文件夹复制到 PJSIP 目录下,并在 PJSIP 文件夹目录下新建一个 includes 文件夹,存放对应的头文件。

image.png

includes 下新建的文件夹,分别存放对应 pjproject-2.13相同名字文件夹下的 include 文件。

然后,我们新建一个 iOS 的项目 testPJSIPDemo,将 PJSIP 拖入到项目根目录下,项目结构如图:

image.png

注意:includes 文件夹不要添加到项目中来,只需添加 lib 文件夹,但我们仍需保证上图中的目录结构

image.png

在项目工程配置中,在 targetstestPJSIPDemoBuild Settings 设置以下配置:

$(PROJECT_DIR)/testPJSIPDemo/PJSIP/includes/pjlib
$(PROJECT_DIR)/testPJSIPDemo/PJSIP/includes/pjlib-util
$(PROJECT_DIR)/testPJSIPDemo/PJSIP/includes/pjmedia
$(PROJECT_DIR)/testPJSIPDemo/PJSIP/includes/pjnath
$(PROJECT_DIR)/testPJSIPDemo/PJSIP/includes/pjsip

Build Phases 中的 Link Binary With Libraries 中添加下面几个 Framework

info.plist 添加相机(如果需要视频通话)和使用麦克风所需的权限:

走完上面的步骤,我们就可以正常的使用 pjsip 库了,Command + B 编译下你的工程,如果有警告或错误,那就说明你的配置可能出现了问题。

上一篇下一篇

猜你喜欢

热点阅读