Experimentations for extracting the core from the main thread

This commit is contained in:
QuentinArguillere
2023-09-12 16:55:20 +02:00
parent 6c7585aac8
commit 59d7747e86
14 changed files with 754 additions and 88 deletions

View File

@@ -9,19 +9,22 @@
import linphonesw
import AVFoundation
class CallKitExampleContext : ObservableObject
{
private let queue = DispatchQueue(label:"core.queue")
var mCore: Core!
@Published var coreVersion: String = Core.getVersion
var mAccount: Account?
var mCoreDelegate : CoreDelegate!
@Published var username : String = "user"
@Published var passwd : String = "pwd"
@Published var domain : String = "sip.example.org"
var mIterateTimer : Timer!
@Published var coreVersion: String = Core.getVersion
@Published var username : String = "quentindev"
@Published var passwd : String = "dev"
@Published var domain : String = "sip.linphone.org"
@Published var loggedIn: Bool = false
@Published var transportType : String = "TLS"
@Published var callMsg : String = ""
@Published var isCallIncoming : Bool = false
@Published var isCallRunning : Bool = false
@@ -35,6 +38,18 @@ class CallKitExampleContext : ObservableObject
var mProviderDelegate : CallKitProviderDelegate!
var mCallAlreadyStopped : Bool = false;
func postOnCoreQueue(lambda : @escaping ()->()) {
queue.async {
lambda()
}
}
func postOnMainQueue(lambda : @escaping()->()) {
DispatchQueue.main.async {
lambda()
}
}
init()
{
LoggingService.Instance.logLevel = LogLevel.Debug
@@ -46,26 +61,28 @@ class CallKitExampleContext : ObservableObject
// We also need to enable "Push Notitifications" and "Background Mode - Voice Over IP"
let configDir = factory.getConfigDir(context: nil)
try? mCore = factory.createCore(configPath: "\(configDir)/MyConfig", factoryConfigPath: "", systemContext: nil)
mProviderDelegate = CallKitProviderDelegate(context: self)
// enabling push notifications management in the core
mCore.callkitEnabled = true
mCore.pushNotificationEnabled = true
try? mCore.start()
mCore.autoIterateEnabled = false
mCoreDelegate = CoreDelegateStub( onCallStateChanged: { (core: Core, call: Call, state: Call.State, message: String) in
self.callMsg = message
if (state == .PushIncomingReceived){
// We're being called by someone (and app is in background)
self.mCall = call
self.isCallIncoming = true
self.mProviderDelegate.incomingCall()
self.isCallIncoming = true
self.callMsg = message
} else if (state == .IncomingReceived) {
// If app is in foreground, it's likely that we will receive the SIP invite before the Push notification
if (!self.isCallIncoming) {
self.mCall = call
self.isCallIncoming = true
self.mProviderDelegate.incomingCall()
self.isCallIncoming = true
self.callMsg = message
}
self.remoteAddress = call.remoteAddress!.asStringUriOnly()
} else if (state == .Connected) {
@@ -91,51 +108,70 @@ class CallKitExampleContext : ObservableObject
self.loggedIn = false
}
})
mIterateTimer = Timer.scheduledTimer(withTimeInterval: 0.02, repeats: true) { [weak self] timer in
self?.postOnCoreQueue {
self?.mCore.iterate()
}
}
mProviderDelegate = CallKitProviderDelegate(context: self)
mCore.addDelegate(delegate: mCoreDelegate)
postOnCoreQueue {
try? self.mCore.start()
}
}
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
// Enable push notifications on this account
accountParams.pushNotificationAllowed = true
// We're in a sandbox application, so we must set the provider to "apns.dev" since it will be "apns" by default, which is used only for production apps
accountParams.pushNotificationConfig?.provider = "apns.dev"
mAccount = try mCore.createAccount(params: accountParams)
mCore.addAuthInfo(info: authInfo)
try mCore.addAccount(account: mAccount!)
mCore.defaultAccount = mAccount
} catch { NSLog(error.localizedDescription) }
postOnCoreQueue {
do {
var transport : TransportType
if (self.transportType == "TLS") { transport = TransportType.Tls }
else if (self.transportType == "TCP") { transport = TransportType.Tcp }
else { transport = TransportType.Udp }
let authInfo = try Factory.Instance.createAuthInfo(username: self.username, userid: "", passwd: self.passwd, ha1: "", realm: "", domain: self.domain)
let accountParams = try self.mCore.createAccountParams()
let identity = try Factory.Instance.createAddress(addr: String("sip:" + self.username + "@" + self.domain))
try! accountParams.setIdentityaddress(newValue: identity)
let address = try Factory.Instance.createAddress(addr: String("sip:" + self.domain))
try address.setTransport(newValue: transport)
try accountParams.setServeraddress(newValue: address)
accountParams.registerEnabled = true
// Enable push notifications on this account
accountParams.pushNotificationAllowed = true
// We're in a sandbox application, so we must set the provider to "apns.dev" since it will be "apns" by default, which is used only for production apps
accountParams.pushNotificationConfig?.provider = "apns.dev"
self.mAccount = try self.mCore.createAccount(params: accountParams)
self.mCore.addAuthInfo(info: authInfo)
try self.mCore.addAccount(account: self.mAccount!)
self.mCore.defaultAccount = self.mAccount
} catch { NSLog(error.localizedDescription) }
}
}
func unregister()
{
if let account = mCore.defaultAccount {
let params = account.params
let clonedParams = params?.clone()
clonedParams?.registerEnabled = false
account.params = clonedParams
postOnCoreQueue {
if let account = self.mCore.defaultAccount {
let params = account.params
let clonedParams = params?.clone()
clonedParams?.registerEnabled = false
account.params = clonedParams
}
}
}
func delete() {
if let account = mCore.defaultAccount {
mCore.removeAccount(account: account)
mCore.clearAccounts()
mCore.clearAllAuthInfo()
postOnCoreQueue {
if let account = self.mCore.defaultAccount {
self.mCore.removeAccount(account: account)
self.mCore.clearAccounts()
self.mCore.clearAllAuthInfo()
}
}
}
}