From a63960ab0b837952d4efd884b47c1b901f49cfd9 Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Fri, 28 May 2021 16:00:43 +0200 Subject: [PATCH] Delegate refactoring, and enable audio route selection in CallTutorial --- .../CallKitTutorial/CallExample.swift | 4 +- .../CallTutorial/CallExample.swift | 115 ++++++++--------- .../CallTutorial/ContentView.swift | 4 +- .../ChatRoomTutorial/ChatRoomExample.swift | 116 +++++++++--------- .../LoginTutorial/LoginExample.swift | 24 ++-- .../VideoCallTutorial/VideoCallExample.swift | 4 +- 6 files changed, 136 insertions(+), 131 deletions(-) diff --git a/swift/CallKitTutorial/CallKitTutorial/CallExample.swift b/swift/CallKitTutorial/CallKitTutorial/CallExample.swift index 796a905..17bd842 100644 --- a/swift/CallKitTutorial/CallKitTutorial/CallExample.swift +++ b/swift/CallKitTutorial/CallKitTutorial/CallExample.swift @@ -125,8 +125,8 @@ class CallKitTutorialDelegate: CoreDelegate { var tutorialContext : CallKitExampleContext! - func onRegistrationStateChanged(core: Core, proxyConfig: ProxyConfig, state: RegistrationState, message: String) { - print("New registration state \(state) for user id \( String(describing: proxyConfig.identityAddress?.asString()))\n") + func onAccountRegistrationStateChanged(core: Core, account: Account, state: RegistrationState, message: String) { + print("New registration state \(state) for user id \( String(describing: account.params?.identityAddress?.asString()))\n") if (state == .Ok) { tutorialContext.loggedIn = true } diff --git a/swift/CallTutorial/CallTutorial/CallExample.swift b/swift/CallTutorial/CallTutorial/CallExample.swift index 8fe2b66..a2df046 100644 --- a/swift/CallTutorial/CallTutorial/CallExample.swift +++ b/swift/CallTutorial/CallTutorial/CallExample.swift @@ -25,7 +25,6 @@ class CallExampleContext : ObservableObject var loggingUnit = LoggingUnit() /*------------ Call tutorial related variables ---------------*/ - let mCallTutorialDelegate = CallTutorialDelegate() var mCall: Call! var account : Account! var callAlreadyStopped = false; @@ -34,17 +33,21 @@ class CallExampleContext : ObservableObject @Published var microphoneMuted : Bool = false @Published var callRunning : Bool = false @Published var isCallIncoming : Bool = false - @Published var dest : String = "sip:targetphone@sip.linphone.org" + @Published var dest : String = "sip:calldest@sip.linphone.org" - @Published var id : String = "sip:myphone@sip.linphone.org" - @Published var passwd : String = "mypassword" + @Published var id : String = "sip:youraccount@sip.linphone.org" + @Published var passwd : String = "yourpassword" @Published var loggedIn: Bool = false @Published var currentAudioDevice : AudioDevice! @Published var displayableDevices = [DisplayableDevice]() - + + var mRegistrationDelegate : CoreDelegate! + var mCallStateDelegate : CoreDelegate! + var mAudioDeviceChangedDelegate : CoreDelegate! + var mAudioDevicesListDelegate : CoreDelegate! + init() { - mCallTutorialDelegate.tutorialContext = self // Initialize Linphone Core try? mCore = Factory.Instance.createCore(configPath: "", factoryConfigPath: "", systemContext: nil) @@ -54,7 +57,52 @@ class CallExampleContext : ObservableObject try? mCore.start() currentAudioDevice = mCore.audioDevices[0] - mCore.addDelegate(delegate: mCallTutorialDelegate) + + // Callback for actions when a change in the RegistrationState of the Linphone Core happens + mRegistrationDelegate = CoreDelegateStub(onAccountRegistrationStateChanged: { (core: Core, account: Account, state: RegistrationState, message: String) in + print("New registration state \(state) for user id \( String(describing: account.params?.identityAddress?.asString()))\n") + if (state == .Ok) { + self.loggedIn = true + + } + }) + mCore.addDelegate(delegate: mRegistrationDelegate) + + // Callback for actions when a change in the CallState of the Linphone Core happens + mCallStateDelegate = CoreDelegateStub(onCallStateChanged: { (lc: Core, call: Call, cstate: Call.State, message: String) in + print("CallTrace - \(cstate)") + if (cstate == .IncomingReceived) { + // We're being called by someone + self.mCall = call + self.isCallIncoming = true + } else if (cstate == .OutgoingRinging) { + // We're calling someone + self.callRunning = true + } else if (cstate == .StreamsRunning) { + // Call has successfully began + self.updateAudioDevices() + self.callRunning = true + } else if (cstate == .End || cstate == .Error) { + // Call has been terminated by any side, or an error occured + self.callRunning = false + self.isCallIncoming = false + } + }) + mCore.addDelegate(delegate: mCallStateDelegate) + + // Callback for actions when a change in audio device in the Linphone Core happens + mAudioDeviceChangedDelegate = CoreDelegateStub(onAudioDeviceChanged: { (core: Core, audioDevice: AudioDevice) in + if (self.currentAudioDevice.deviceName != audioDevice.deviceName) { + self.currentAudioDevice = audioDevice + } + }) + mCore.addDelegate(delegate: mAudioDeviceChangedDelegate) + + // Callback when the audio devices list available to the Linphone Core is updated + mAudioDevicesListDelegate = CoreDelegateStub(onAudioDevicesListUpdated: { (core: Core) in + self.updateAudioDevices() + }) + mCore.addDelegate(delegate: mAudioDevicesListDelegate) } func createAccountAndRegister() { @@ -107,17 +155,15 @@ class CallExampleContext : ObservableObject } } } -/* + func updateAudioDevices() { var newDevices = [DisplayableDevice]() for device in mCore.audioDevices { newDevices.append(DisplayableDevice(name: device.deviceName)) } displayableDevices = newDevices - if let output = mCore.outputAudioDevice { - currentAudioDevice = output - } } + func switchAudioOutput(newDevice: String) { for device in mCore.audioDevices { if (newDevice == device.deviceName) { @@ -126,7 +172,7 @@ class CallExampleContext : ObservableObject break } } - }*/ + } func microphoneMuteToggle() { if (callRunning) { @@ -135,50 +181,9 @@ class CallExampleContext : ObservableObject } } - func acceptCall() - { + func acceptCall() { do { try mCall.accept() - } catch { - print(error) - } + } catch { print(error) } } } - -// Callback for actions when a change in the Registration State happens -class CallTutorialDelegate: CoreDelegate { - - var tutorialContext : CallExampleContext! - - 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)") - if (cstate == .IncomingReceived) { - // We're being called by someone - tutorialContext.mCall = call - tutorialContext.isCallIncoming = true - } else if (cstate == .OutgoingRinging) { - // We're calling someone - tutorialContext.callRunning = true - } else if (cstate == .StreamsRunning) { - // Call has successfully began - //tutorialContext.updateAudioDevices() - tutorialContext.callRunning = true - } else if (cstate == .End || cstate == .Error) { - // Call has been terminated by any side, or an error occured - tutorialContext.callRunning = false - tutorialContext.isCallIncoming = false - } - } - /* - func onAudioDevicesListUpdated(core: Core) { - tutorialContext.updateAudioDevices() - }*/ - -} diff --git a/swift/CallTutorial/CallTutorial/ContentView.swift b/swift/CallTutorial/CallTutorial/ContentView.swift index aa3b216..0ecc26e 100644 --- a/swift/CallTutorial/CallTutorial/ContentView.swift +++ b/swift/CallTutorial/CallTutorial/ContentView.swift @@ -85,7 +85,7 @@ struct ContentView: View { .background(Color.gray) } }.padding() - /*NavigationLink(destination : Group { + NavigationLink(destination : Group { if (tutorialContext.displayableDevices.count == 0) { Text("Please start a call\nbefore selecting the audio route") .multilineTextAlignment(.center) @@ -113,7 +113,7 @@ struct ContentView: View { .foregroundColor(Color.white) .multilineTextAlignment(.center) .background(Color.gray) - }.padding()*/ + }.padding() } Spacer() VStack { diff --git a/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift b/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift index 866d7c6..091222b 100644 --- a/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift +++ b/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift @@ -21,6 +21,65 @@ struct DisplayableUser : Identifiable { } + +class LinphoneCoreDelegate: CoreDelegate { + + var tutorialContext : ChatRoomExampleContext! + + // Called when the Linphone Core detects a change in the account registration state + func onAccountRegistrationStateChanged(core: Core, account: Account, state: RegistrationState, message: String) { + print("New registration state \(state) for user id \( String(describing: account.params?.identityAddress?.asString()))\n") + if (state == RegistrationState.Ok) { + tutorialContext.loggedIn = true + } + } + + // called when the Linphone Core receives a message + func onMessageReceived(core lc: Core, chatRoom room: ChatRoom, message: ChatMessage) { + if (tutorialContext.mChatRoom == nil) { + tutorialContext.mChatRoom = room + tutorialContext.chatroomState = ChatroomExampleState.Started + } + + if (message.hasTextContent()) { + tutorialContext.sReceivedMessages += "\n\(message.utf8Text)" + } + + for content in message.contents { + if (content.isFileTransfer) { + tutorialContext.mLastFileMessageReceived = message + tutorialContext.sReceivedMessages += "\n File(s) available(s) for download" + break; + } + } + } +} + +class LinphoneChatRoomStateTracker: ChatRoomDelegate { + var tutorialContext : ChatRoomExampleContext! + + func onConferenceJoined(chatRoom: ChatRoom, eventLog: EventLog) { + print("ChatRoomTrace - Chatroom ready to start") + tutorialContext.displayableUsers = [] + for part in chatRoom.participants { + tutorialContext.displayableUsers.append(DisplayableUser(name: part.address!.asString())) + } + tutorialContext.chatroomState = ChatroomExampleState.Started + } +} + +class LinphoneChatMessageTracker: ChatMessageDelegate { + var tutorialContext : ChatRoomExampleContext! + + func onMsgStateChanged(message msg: ChatMessage, state: ChatMessage.State) { + print("MessageTrace - msg state changed: \(state)\n") + if (state == ChatMessage.State.FileTransferDone && tutorialContext.isDownloading == true) { + tutorialContext.isDownloading = false + } + } +} + + class ChatRoomExampleContext : ObservableObject { var mCore: Core! // We need a Core for... anything, basically @Published var coreVersion: String = Core.getVersion @@ -222,7 +281,6 @@ class ChatRoomExampleContext : ObservableObject { } func removeParticipant(user : DisplayableUser) { - if let userAddr = try? Factory.Instance.createAddress(addr: user.name) { for part in mChatRoom!.participants { if (part.address!.equal(address2: userAddr)) @@ -238,62 +296,6 @@ class ChatRoomExampleContext : ObservableObject { break } } - } - -} - -class LinphoneCoreDelegate: CoreDelegate { - - var tutorialContext : ChatRoomExampleContext! - 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 == RegistrationState.Ok) { - tutorialContext.loggedIn = true - } - } - - func onMessageReceived(core lc: Core, chatRoom room: ChatRoom, message: ChatMessage) { - if (tutorialContext.mChatRoom == nil) { - tutorialContext.mChatRoom = room - tutorialContext.chatroomState = ChatroomExampleState.Started - } - - if (message.hasTextContent()) { - tutorialContext.sReceivedMessages += "\n\(message.utf8Text)" - } - - for content in message.contents { - if (content.isFileTransfer) { - tutorialContext.mLastFileMessageReceived = message - tutorialContext.sReceivedMessages += "\n File(s) available(s) for download" - break; - } - } - } -} - -class LinphoneChatRoomStateTracker: ChatRoomDelegate { - var tutorialContext : ChatRoomExampleContext! - - func onConferenceJoined(chatRoom: ChatRoom, eventLog: EventLog) { - print("ChatRoomTrace - Chatroom ready to start") - tutorialContext.displayableUsers = [] - for part in chatRoom.participants { - tutorialContext.displayableUsers.append(DisplayableUser(name: part.address!.asString())) - } - tutorialContext.chatroomState = ChatroomExampleState.Started - } -} - -class LinphoneChatMessageTracker: ChatMessageDelegate { - var tutorialContext : ChatRoomExampleContext! - - func onMsgStateChanged(message msg: ChatMessage, state: ChatMessage.State) { - print("MessageTrace - msg state changed: \(state)\n") - if (state == ChatMessage.State.FileTransferDone && tutorialContext.isDownloading == true) { - tutorialContext.isDownloading = false - } - } } diff --git a/swift/LoginTutorial/LoginTutorial/LoginExample.swift b/swift/LoginTutorial/LoginTutorial/LoginExample.swift index 169bb50..c7521b7 100644 --- a/swift/LoginTutorial/LoginTutorial/LoginExample.swift +++ b/swift/LoginTutorial/LoginTutorial/LoginExample.swift @@ -32,23 +32,21 @@ class LoginTutorialContext : ObservableObject // main loop for receiving notifications and doing background linphonecore work: mCore.autoIterateEnabled = true try? mCore.start() + // Add callbacks to the Linphone Core + mRegistrationDelegate = CoreDelegateStub(onAccountRegistrationStateChanged: { (core: Core, account: Account, state: RegistrationState, message: String) in + print("New registration state \(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 registrationExample() { - if (mRegistrationDelegate == nil) { - // Add callbacks to the Linphone Core - mRegistrationDelegate = CoreDelegateStub(onRegistrationStateChanged: { (core: Core, proxyConfig: ProxyConfig, state: RegistrationState, message: String) in - print("New registration state \(state) for user id \( String(describing: proxyConfig.identityAddress?.asString()))\n") - if (state == .Ok) { - self.loggedIn = true - } else if (state == .Cleared) { - self.loggedIn = false - } - }) - mCore.addDelegate(delegate: mRegistrationDelegate) - } - if (!loggedIn) { do { if (account == nil) { diff --git a/swift/VideoCallTutorial/VideoCallTutorial/VideoCallExample.swift b/swift/VideoCallTutorial/VideoCallTutorial/VideoCallExample.swift index 1469a3f..2e11ce1 100644 --- a/swift/VideoCallTutorial/VideoCallTutorial/VideoCallExample.swift +++ b/swift/VideoCallTutorial/VideoCallTutorial/VideoCallExample.swift @@ -46,8 +46,8 @@ class VideoCallExample : ObservableObject mVideoDevices = mCore.videoDevicesList // Callback for actions when a change in the RegistrationState of the Linphone Core happens - mRegistrationDelegate = CoreDelegateStub(onRegistrationStateChanged: { (core: Core, proxyConfig: ProxyConfig, state: RegistrationState, message: String) in - print("New registration state \(state) for user id \( String(describing: proxyConfig.identityAddress?.asString()))\n") + mRegistrationDelegate = CoreDelegateStub(onAccountRegistrationStateChanged: { (core: Core, account: Account, state: RegistrationState, message: String) in + print("New registration state \(state) for user id \( String(describing: account.params?.identityAddress?.asString()))\n") if (state == .Ok) { self.loggedIn = true