From 3db5eb49125dfd7761a73fc597e42b352c3074da Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Wed, 7 Oct 2020 14:18:14 +0200 Subject: [PATCH] Update delegates, remove settings not really relevant to CallKit --- .../CallKitTutorial/CallExample.swift | 158 ++++++------------ .../CallKitProviderDelegate.swift | 3 +- .../CallKitTutorial/ContentView.swift | 28 ---- 3 files changed, 56 insertions(+), 133 deletions(-) diff --git a/swift/CallKitTutorial/CallKitTutorial/CallExample.swift b/swift/CallKitTutorial/CallKitTutorial/CallExample.swift index fe70995..9d83511 100644 --- a/swift/CallKitTutorial/CallKitTutorial/CallExample.swift +++ b/swift/CallKitTutorial/CallKitTutorial/CallExample.swift @@ -18,23 +18,20 @@ class CallKitExampleContext : ObservableObject var loggingUnit = LoggingUnit() /*------------ Call tutorial related variables ---------------*/ - let mCallStateTracer = CallStateDelegate() + let mCallKitTutorialDelegate = CallKitTutorialDelegate() var mCall: Call! var proxy_cfg : ProxyConfig! var mVideoDevices : [String] = [] var mUsedVideoDeviceId : Int = 0 var callAlreadyStopped = false; - @Published var audioEnabled : Bool = true - @Published var videoEnabled : Bool = false @Published var speakerEnabled : Bool = false @Published var callRunning : Bool = false @Published var isCallIncoming : Bool = false @Published var dest : String = "sip:arguillq@sip.linphone.org" - let mRegistrationDelegate = LinphoneRegistrationDelegate() - @Published var id : String = "sip:peche5@sip.linphone.org" - @Published var passwd : String = "peche5" + @Published var id : String = "sip:quentindev@sip.linphone.org" + @Published var passwd : String = "dev" @Published var loggedIn: Bool = false var mProviderDelegate : CallKitProviderDelegate! @@ -60,16 +57,14 @@ class CallKitExampleContext : ObservableObject mCore.pushNotificationEnabled = true // This is necessary to register to the server and handle push Notifications. Make sure you have a certificate to match your app's bundle ID. - let pushConfig = mCore.pushNotificationConfig! - pushConfig.provider = "apns.dev" + let pushConfig = mCore.pushNotificationConfig! + pushConfig.provider = "apns.dev" try? mCore.start() // Callbacks on registration and call events - mCallStateTracer.tutorialContext = self - mRegistrationDelegate.tutorialContext = self - mCore.addDelegate(delegate: mCallStateTracer) - mCore.addDelegate(delegate: mRegistrationDelegate) + mCallKitTutorialDelegate.tutorialContext = self + mCore.addDelegate(delegate: mCallKitTutorialDelegate) // Available video devices that can be selected to be used in video calls mVideoDevices = mCore.videoDevicesList @@ -102,26 +97,15 @@ class CallKitExampleContext : ObservableObject loggedIn = false } - func createCallParams() throws -> CallParams - { - let callParams = try mCore.createCallParams(call: nil) - callParams.videoEnabled = videoEnabled; - callParams.audioEnabled = audioEnabled; - - return callParams - } - // Initiate a call func outgoingCallExample() { do { - if (!callRunning) - { + if (!callRunning) { mProviderDelegate.outgoingCall() } - else - { - try mCall.update(params: createCallParams()) + else { + try mCall.update(params: mCore.createCallParams(call: nil)) } } catch { print(error) @@ -132,93 +116,61 @@ class CallKitExampleContext : ObservableObject // Terminate a call func stopCall() { - if ((callRunning || isCallIncoming) && mCall.state != Call.State.End) - { - callAlreadyStopped = true; + if ((callRunning || isCallIncoming) && mCall.state != Call.State.End) { + callAlreadyStopped = true; // terminate the call print("Terminating the call...\n") mProviderDelegate.stopCall() } } - - func speaker() - { - speakerEnabled = !speakerEnabled - do { - try AVAudioSession.sharedInstance().overrideOutputAudioPort(speakerEnabled ? AVAudioSession.PortOverride.speaker : AVAudioSession.PortOverride.none ) - } catch { - print(error) - } - } - - func changeVideoDevice() - { - mUsedVideoDeviceId = (mUsedVideoDeviceId + 1) % mVideoDevices.count - - do { - try mCore.setVideodevice(newValue: mVideoDevices[mUsedVideoDeviceId]) - } catch { - print(error) - } - } - } // Callback for actions when a change in the Registration State happens -class LinphoneRegistrationDelegate: CoreDelegate { +class CallKitTutorialDelegate: CoreDelegate { var tutorialContext : CallKitExampleContext! - - override func onRegistrationStateChanged(core lc: Core, proxyConfig cfg: ProxyConfig, state cstate: RegistrationState, message: String?) { - print("New registration state \(cstate) for user id \( String(describing: cfg.identityAddress?.asString()))\n") - if (cstate == .Ok) - { - tutorialContext.loggedIn = true - } - } -} + + func onRegistrationStateChanged(core: Core, proxyConfig: ProxyConfig, state: RegistrationState, message: String) { + print("New registration state \(state) for user id \( String(describing: proxyConfig.identityAddress?.asString()))\n") + if (state == .Ok) { + tutorialContext.loggedIn = true + } + } + + func onCallStateChanged(core lc: Core, call: Call, state cstate: Call.State, message: String) { + print("CallTrace - \(cstate)") + let initIncomingCall = { + self.tutorialContext.mCall = call + self.tutorialContext.isCallIncoming = true + self.tutorialContext.mProviderDelegate.incomingCall() + } -// Callback for actions when a change in the Call State happens -class CallStateDelegate: CoreDelegate { - - var tutorialContext : CallKitExampleContext! - - override func onCallStateChanged(core lc: Core, call: Call, state cstate: Call.State, message: String) { - print("CallTrace - \(cstate)") - - let initIncomingCall = { - self.tutorialContext.mCall = call - self.tutorialContext.isCallIncoming = true - self.tutorialContext.mProviderDelegate.incomingCall() - } - - if (cstate == .PushIncomingReceived) - { - // We're being called by someone (and app is in background) - initIncomingCall() - } - else if (cstate == .IncomingReceived && !tutorialContext.isCallIncoming) { - // We're being called by someone (and app is in foreground, so call hasn't been initialized yet) - initIncomingCall() - - } else if (cstate == .OutgoingRinging) { - // We're calling someone - tutorialContext.callRunning = true - } else if (cstate == .End) { - // Call has been terminated by any side - if (!tutorialContext.callAlreadyStopped) - { - // Report to CallKit that the call is over, if the terminate action was initiated by other end of the call - tutorialContext.mProviderDelegate.stopCall() - tutorialContext.callAlreadyStopped = false - } - tutorialContext.callRunning = false - tutorialContext.isCallIncoming = false - } else if (cstate == .StreamsRunning) - { - // Call has successfully began - tutorialContext.callRunning = true - } - } + if (cstate == .PushIncomingReceived){ + // We're being called by someone (and app is in background) + initIncomingCall() + } + else if (cstate == .IncomingReceived && !tutorialContext.isCallIncoming) { + // We're being called by someone (and app is in foreground, so call hasn't been initialized yet) + initIncomingCall() + } else if (cstate == .OutgoingRinging) { + // We're calling someone + tutorialContext.callRunning = true + } else if (cstate == .End || cstate == .Error) { + // Call has been terminated by any side + if (!tutorialContext.callAlreadyStopped) + { + // Report to CallKit that the call is over, if the terminate action was initiated by other end of the call + tutorialContext.mProviderDelegate.stopCall() + tutorialContext.callAlreadyStopped = false + } + tutorialContext.callRunning = false + tutorialContext.isCallIncoming = false + } else if (cstate == .StreamsRunning) + { + // Call has successfully began + tutorialContext.callRunning = true + } + } + } diff --git a/swift/CallKitTutorial/CallKitTutorial/CallKitProviderDelegate.swift b/swift/CallKitTutorial/CallKitTutorial/CallKitProviderDelegate.swift index a3731b8..08a1669 100644 --- a/swift/CallKitTutorial/CallKitTutorial/CallKitProviderDelegate.swift +++ b/swift/CallKitTutorial/CallKitTutorial/CallKitProviderDelegate.swift @@ -53,7 +53,6 @@ class CallKitProviderDelegate : NSObject incomingCallUUID = UUID() let update = CXCallUpdate() update.remoteHandle = CXHandle(type:.generic, value: tutorialContext.incomingCallName) - update.hasVideo = tutorialContext.videoEnabled provider.reportNewIncomingCall(with: incomingCallUUID, update: update, completion: { error in }) // Report to CallKit a call is incoming } @@ -107,7 +106,7 @@ extension CallKitProviderDelegate: CXProviderDelegate { do { let callDest = try Factory.Instance.createAddress(addr: tutorialContext.dest) // Place an outgoing call - tutorialContext.mCall = tutorialContext.mCore.inviteAddressWithParams(addr: callDest, params: try tutorialContext.createCallParams()) + tutorialContext.mCall = tutorialContext.mCore.inviteAddressWithParams(addr: callDest, params: try tutorialContext.mCore.createCallParams(call: nil)) } catch { print(error) } diff --git a/swift/CallKitTutorial/CallKitTutorial/ContentView.swift b/swift/CallKitTutorial/CallKitTutorial/ContentView.swift index 3ef70e7..0f55010 100644 --- a/swift/CallKitTutorial/CallKitTutorial/ContentView.swift +++ b/swift/CallKitTutorial/CallKitTutorial/ContentView.swift @@ -90,34 +90,6 @@ struct ContentView: View { } .padding(.top, 5) } - VStack(alignment: .leading) { - Toggle(isOn: $tutorialContext.audioEnabled) { - Text("Audio") - }.frame(width : 140.0) - Toggle(isOn: $tutorialContext.videoEnabled) { - Text("Video") - }.frame(width : 140.0) - Button(action: tutorialContext.changeVideoDevice) - { - Text(" Change camera ") - .font(.title) - .foregroundColor(Color.white) - .background(Color.gray) - } - .padding(.vertical) - HStack { - Text("Speaker :") - Button(action: tutorialContext.speaker) - { - Text(tutorialContext.speakerEnabled ? "ON" : "OFF") - .font(.title) - .foregroundColor(Color.white) - .frame(width: 60.0, height: 30.0) - .background(Color.gray) - } - } - } - .padding(.top, 5.0) Spacer() VStack { HStack {