从零开始搭建音视频通话服务
这是我参与2022首次更文挑战的第6天,活动详情查看:2022首次更文挑战
目录
- iOS 从零开始搭建音视频通话服务01-协议选择(Linphone)
- iOS 从零开始搭建音视频通话服务02-搭建一个sip服务器上
- iOS 从零开始搭建音视频通话服务03-搭建一个sip服务器下
- iOS 从零开始搭建音视频通话服务04-初始化和登录
- iOS 从零开始搭建音视频通话服务05-语音通话
前言
本地服务已经在ubuntu虚拟机中搭建完成了,但是媒体流的播放等还没弄,先不折腾了,先把初始化和登录弄一下。账号已经拿到了,服务也有了,可以试着登录。
集成linphone-sdk
使用pod集成
使用pod集成时,请大于5.0.48版本,最好使用最新的版本,在Podfile文件中加入下面这行。
pod 'linphone-sdk', '~> 5.0.48'
现在集成的是5.0.66版本
mac@macdeMacBook 1-LoginTutorial % pod install
Analyzing dependencies
Downloading dependencies
Installing linphone-sdk (5.0.66)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
mac@macdeMacBook 1-LoginTutorial %
文件导入
从linphone官网下载linphone-sdk,直接拖入到项目中即可。
如何使用(Swift)
直接import linphonesw就可以使用linphone-sdk。
设置日志打印级别(Swift)
先设置日志打印级别,开发的时候一般都是这只debug。
LoggingService.Instance.logLevel = LogLevel.Debug
初始化Core(Swift)
通过工厂生成一个Core实例,然后调用start()即可。
var mCore: Core!
try? mCore = Factory.Instance.createCore(configPath: "", factoryConfigPath: "", systemContext: nil)
try? mCore.start()
添加代理(Swift)
添加代理用来接收登录的回调,登录回调的是onAccountRegistrationStateChanged会返回有core,账号信息,注册状态和提示消息,根据状态可以进行下一步操作。
var mRegistrationDelegate : CoreDelegate!
mRegistrationDelegate = CoreDelegateStub(onAccountRegistrationStateChanged: { (core: Core, account: Account, state: RegistrationState, message: String) in
NSLog("New registration state is \(state) for user id \( String(describing: account.params?.identityAddress?.asString()))\n")
if (state == .Ok) {
self.loggedIn = true
} else if (state == .Cleared) {
self.loggedIn = false
}
})
mCore.addDelegate(delegate: mRegistrationDelegate)
发起登录(Swift)
设置好账号密码就可以发起登录了。在登录回调中可以收到登录的进度以及状态。
@Published var username : String = "user"
@Published var passwd : String = "pwd"
@Published var domain : String = "xxx.example.org"
@Published var loggedIn: Bool = false
@Published var transportType : String = "UDP"
do {
var transport : TransportType
if (transportType == "TLS") { transport = TransportType.Tls }
else if (transportType == "TCP") { transport = TransportType.Tcp }
else { transport = TransportType.Udp }
let authInfo = try Factory.Instance.createAuthInfo(username: username, userid: "", passwd: passwd, ha1: "", realm: "", domain: domain)
let accountParams = try mCore.createAccountParams()
let identity = try Factory.Instance.createAddress(addr: String("sip:" + username + "@" + domain))
try! accountParams.setIdentityaddress(newValue: identity)
let address = try Factory.Instance.createAddress(addr: String("sip:" + domain))
try address.setTransport(newValue: transport)
try accountParams.setServeraddress(newValue: address)
accountParams.registerEnabled = true
let account = try mCore.createAccount(params: accountParams)
mCore.addAuthInfo(info: authInfo)
try mCore.addAccount(account: account)
mCore.defaultAccount = account
} catch { NSLog(error.localizedDescription) }
完整代码如下(Swift)
var mCore: Core!
@Published var coreVersion: String = Core.getVersion
var mRegistrationDelegate : CoreDelegate!
@Published var username : String = "user"
@Published var passwd : String = "pwd"
@Published var domain : String = "xxx.example.org"
@Published var loggedIn: Bool = false
@Published var transportType : String = "UDP"
init()
{
LoggingService.Instance.logLevel = LogLevel.Debug
try? mCore = Factory.Instance.createCore(configPath: "", factoryConfigPath: "", systemContext: nil)
try? mCore.start()
mRegistrationDelegate = CoreDelegateStub(onAccountRegistrationStateChanged: { (core: Core, account: Account, state: RegistrationState, message: String) in
NSLog("New registration state is \(state) for user id \( String(describing: account.params?.identityAddress?.asString()))\n")
if (state == .Ok) {
self.loggedIn = true
} else if (state == .Cleared) {
self.loggedIn = false
}
})
mCore.addDelegate(delegate: mRegistrationDelegate)
}
func login() {
do {
var transport : TransportType
if (transportType == "TLS") { transport = TransportType.Tls }
else if (transportType == "TCP") { transport = TransportType.Tcp }
else { transport = TransportType.Udp }
let authInfo = try Factory.Instance.createAuthInfo(username: username, userid: "", passwd: passwd, ha1: "", realm: "", domain: domain)
let accountParams = try mCore.createAccountParams()
let identity = try Factory.Instance.createAddress(addr: String("sip:" + username + "@" + domain))
try! accountParams.setIdentityaddress(newValue: identity)
let address = try Factory.Instance.createAddress(addr: String("sip:" + domain))
try address.setTransport(newValue: transport)
try accountParams.setServeraddress(newValue: address)
accountParams.registerEnabled = true
let account = try mCore.createAccount(params: accountParams)
mCore.addAuthInfo(info: authInfo)
try mCore.addAccount(account: account)
mCore.defaultAccount = account
} catch { NSLog(error.localizedDescription) }
}