diff --git a/swift/HelloLinphone/HelloLinphone/ContentView.swift b/swift/HelloLinphone/HelloLinphone/ContentView.swift index 5f1ae9c..03ed6fd 100644 --- a/swift/HelloLinphone/HelloLinphone/ContentView.swift +++ b/swift/HelloLinphone/HelloLinphone/ContentView.swift @@ -6,12 +6,129 @@ // Copyright © 2020 belledonne. All rights reserved. // +import linphonesw import SwiftUI + +class LinphoneCoreHolder +{ + var mCore: Core! + var proxy_cfg: ProxyConfig! + var call: Call! + let mRegistrationTracer = LinphoneRegistrationTracer() + + var log : LoggingService? + var logManager : LinphoneLoggingServiceManager? + var callRunning : Bool = false + + + init() + { + enableLogs() + let factory = Factory.Instance + factory.enableLogCollection(state: LogCollectionState.Enabled) + + // Initialize Linphone Core + try? mCore = factory.createCore(configPath: "", factoryConfigPath: "", systemContext: nil) + + // main loop for receiving notifications and doing background linphonecore work: + mCore.autoIterateEnabled = true; + try? mCore.start() + } + + // Enable log collection + func enableLogs() + { + log = LoggingService.Instance + logManager = LinphoneLoggingServiceManager() + log!.addDelegate(delegate: logManager!) + log!.logLevel = LogLevel.Debug + } + + //Disable log collection + func disableLogs() + { + log = nil + logManager = nil + } + + + func registrationExample(identity id : String, password passwd: String) + { + let factory = Factory.Instance + do { + mCore.addDelegate(delegate: mRegistrationTracer) // Add registration specific logs + + proxy_cfg = try mCore.createProxyConfig() // create proxy config + let from = try factory.createAddress(addr: id)// parse identity + // create authentication structure from identity + let info = try factory.createAuthInfo(username: from.username, userid: "", passwd: passwd, ha1: "", realm: "", domain: "") + mCore.addAuthInfo(info: info) // add authentication info to LinphoneCore + + // configure proxy entries + try proxy_cfg.setIdentityaddress(newValue: from) // set identity with user name and domain + let server_addr = from.domain // extract domain address from identity + try proxy_cfg.setServeraddr(newValue: server_addr) // we assume domain = proxy server address + + proxy_cfg.registerEnabled = true // activate registration for this proxy config + + try mCore.addProxyConfig(config: proxy_cfg!) // add proxy config to linphone core + mCore.defaultProxyConfig = proxy_cfg // set to default proxy + } catch { + print(error) + } + + } + +} + struct ContentView: View { - var coreVersion: String = "" + var coreVersion: String = "#coreversion" + var coreHolder = LinphoneCoreHolder() + + @State var id : String = "sip:peche5@sip.linphone.org" + @State var passwd : String = "peche5" + var body: some View { - Text("Hello, Linphone, Core Version is \(coreVersion)") + + VStack { + HStack { + Text("Identity :") + .font(.headline) + TextField("", text : $id) + .textFieldStyle(RoundedBorderTextFieldStyle()) + } + HStack { + Text("Password :") + .font(.headline) + TextField("", text : $passwd) + .textFieldStyle(RoundedBorderTextFieldStyle()) + } + Button(action: { self.coreHolder.registrationExample(identity : self.id, password : self.passwd) }) + { + Text("Login") + .font(.largeTitle) + .foregroundColor(Color.white) + .frame(width: 100.0, height: 50.0) + .background(Color.gray) + } + Spacer() + Text("Hello, Linphone, Core Version is") + Text("\(coreVersion)") + } + .padding() + } +} + +class LinphoneLoggingServiceManager: LoggingServiceDelegate { + override func onLogMessageWritten(logService: LoggingService, domain: String, lev: LogLevel, message: String) { + print("Logging service log: \(message)s\n") + } +} + +class LinphoneRegistrationTracer: CoreDelegate { + override func onRegistrationStateChanged(lc: Core, cfg: ProxyConfig, cstate: RegistrationState, message: String?) { + print("New registration state \(cstate) for user id \( String(describing: cfg.identityAddress?.asString()))\n") } } diff --git a/swift/HelloLinphone/HelloLinphone/SceneDelegate.swift b/swift/HelloLinphone/HelloLinphone/SceneDelegate.swift index 580288d..b5c0383 100644 --- a/swift/HelloLinphone/HelloLinphone/SceneDelegate.swift +++ b/swift/HelloLinphone/HelloLinphone/SceneDelegate.swift @@ -10,76 +10,14 @@ import UIKit import SwiftUI import linphonesw -let DEBUG_LOGS : Bool = false; + class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? - var mCore: Core! - var proxy_cfg: ProxyConfig! - let mRegistrationTrace = LinphoneRegistrationTracer() - - var log : LoggingService? - var logManager : LinphoneLoggingServiceManager? - - class LinphoneLoggingServiceManager: LoggingServiceDelegate { - override func onLogMessageWritten(logService: LoggingService, domain: String, lev: LogLevel, message: String) { - print("Logging service log: \(message)s\n") - } - } - - class LinphoneRegistrationTracer: CoreDelegate { - override func onRegistrationStateChanged(lc: Core, cfg: ProxyConfig, cstate: RegistrationState, message: String?) { - print("New registration state \(cstate) for user id \( String(describing: cfg.identityAddress?.asString()))\n") - } - } - - func registrationExample() - { - let factory = Factory.Instance - do { - // main loop for receiving notifications and doing background linphonecore work: - mCore.autoIterateEnabled = true; - - mCore.addDelegate(delegate: mRegistrationTrace) // Add registration specific logs - try mCore.start() - - proxy_cfg = try mCore.createProxyConfig() // create proxy config - let from = try factory.createAddress(addr: "sip:peche5@sip.linphone.org")// parse identity - // create authentication structure from identity - let info = try factory.createAuthInfo(username: from.username, userid: "", passwd: "peche5", ha1: "", realm: "", domain: "") - mCore.addAuthInfo(info: info) // add authentication info to LinphoneCore - - // configure proxy entries - try proxy_cfg.setIdentityaddress(newValue: from) // set identity with user name and domain - let server_addr = from.domain // extract domain address from identity - try proxy_cfg.setServeraddr(newValue: server_addr) // we assume domain = proxy server address - - proxy_cfg.registerEnabled = true // activate registration for this proxy config - - try mCore.addProxyConfig(config: proxy_cfg!) // add proxy config to linphone core - mCore.defaultProxyConfig = proxy_cfg // set to default proxy - } catch { - print(error) - } - - } func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { - let factory = Factory.Instance - try? mCore = factory.createCore(configPath: "", factoryConfigPath: "", systemContext: nil) - - if (DEBUG_LOGS) - { - // enable liblinphone logs. - log = LoggingService.Instance - logManager = LinphoneLoggingServiceManager() - log!.addDelegate(delegate: logManager!) - log!.logLevel = LogLevel.Debug - Factory.Instance.enableLogCollection(state: LogCollectionState.Enabled) - } - // Create the SwiftUI view that provides the window contents. let contentView = ContentView(coreVersion: Core.getVersion) @@ -90,8 +28,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { self.window = window window.makeKeyAndVisible() } - - registrationExample(); } func sceneDidDisconnect(_ scene: UIScene) {