From e0e8d35a19f11291fca2eb65b967b2a6395fbc56 Mon Sep 17 00:00:00 2001 From: Paul Cartier Date: Tue, 17 Mar 2020 10:26:05 +0100 Subject: [PATCH] Move iterate timer to swift file --- Classes/CallManager.swift | 34 +++++++++++++----- Classes/CallOutgoingView.m | 2 +- Classes/CoreManager.swift | 55 +++++++++++++++++++++++++++++ Classes/LinphoneManager.h | 1 - Classes/LinphoneManager.m | 18 +++------- Classes/LinphoneUI/UIHangUpButton.m | 6 ++-- Classes/PhoneMainView.m | 2 +- 7 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 Classes/CoreManager.swift diff --git a/Classes/CallManager.swift b/Classes/CallManager.swift index a69344666..727cbb26f 100644 --- a/Classes/CallManager.swift +++ b/Classes/CallManager.swift @@ -37,7 +37,7 @@ import AVFoundation static var theCallManager: CallManager? let providerDelegate: ProviderDelegate! // to support callkit let callController: CXCallController! // to support callkit - let manager: CoreManager! // callbacks of the linphonecore + let manager: CoreManagerDelegate! // callbacks of the linphonecore var lc: Core? @objc var speakerBeforePause : Bool = false @objc var speakerEnabled : Bool = false @@ -49,7 +49,7 @@ import AVFoundation fileprivate override init() { providerDelegate = ProviderDelegate() callController = CXCallController() - manager = CoreManager() + manager = CoreManagerDelegate() } @objc static func instance() -> CallManager { @@ -348,9 +348,27 @@ import AVFoundation Log.directLog(BCTBX_LOG_WARNING, text: "CallKit: Unable to config audio session because : \(error)") } } + + @objc func terminateCall(call: OpaquePointer?) { // TODO PAUL : needs to be tested with CallKit changes + if (call == nil) { + Log.directLog(BCTBX_LOG_ERROR, text: "Can not terminate null call!") + return + } + let call = Call.getSwiftObject(cObject: call!) + do { + try call.terminate() + Log.directLog(BCTBX_LOG_DEBUG, text: "Call terminated") + } catch { + Log.directLog(BCTBX_LOG_ERROR, text: "Failed to terminate call") + } + if (UIApplication.shared.applicationState == .background) { + CoreManager.instance().stopIterateTimer() + lc!.stop() + } + } } -class CoreManager: CoreDelegate { +class CoreManagerDelegate: CoreDelegate { static var speaker_already_enabled : Bool = false override func onCallStateChanged(lc: Core, call: Call, cstate: Call.State, message: String) { @@ -361,7 +379,7 @@ class CoreManager: CoreDelegate { let video = call.params?.videoEnabled ?? false // we keep the speaker auto-enabled state in this static so that we don't // force-enable it on ICE re-invite if the user disabled it. - CoreManager.speaker_already_enabled = false + CoreManagerDelegate.speaker_already_enabled = false if (call.userData == nil) { let appData = CallAppData() @@ -419,7 +437,7 @@ class CoreManager: CoreDelegate { if (CallManager.instance().speakerBeforePause) { CallManager.instance().speakerBeforePause = false CallManager.instance().setSpeakerEnabled(enable: true) - CoreManager.speaker_already_enabled = true + CoreManagerDelegate.speaker_already_enabled = true } break case .OutgoingRinging: @@ -440,7 +458,7 @@ class CoreManager: CoreDelegate { case .End, .Error: UIDevice.current.isProximityMonitoringEnabled = false - CoreManager.speaker_already_enabled = false + CoreManagerDelegate.speaker_already_enabled = false if (CallManager.instance().lc!.callsNb == 0) { CallManager.instance().setSpeakerEnabled(enable: false) // disable this because I don't find anygood reason for it: _bluetoothAvailable = FALSE; @@ -487,9 +505,9 @@ class CoreManager: CoreDelegate { } if (cstate == .IncomingReceived || cstate == .OutgoingInit || cstate == .Connected || cstate == .StreamsRunning) { - if (video && !CoreManager.speaker_already_enabled && !CallManager.instance().bluetoothEnabled) { + if (video && !CoreManagerDelegate.speaker_already_enabled && !CallManager.instance().bluetoothEnabled) { CallManager.instance().setSpeakerEnabled(enable: true) - CoreManager.speaker_already_enabled = true + CoreManagerDelegate.speaker_already_enabled = true } } diff --git a/Classes/CallOutgoingView.m b/Classes/CallOutgoingView.m index 83a64e8bf..c97043be3 100644 --- a/Classes/CallOutgoingView.m +++ b/Classes/CallOutgoingView.m @@ -120,7 +120,7 @@ static UICompositeViewDescription *compositeDescription = nil; - (IBAction)onDeclineClick:(id)sender { LinphoneCall *call = linphone_core_get_current_call(LC); if (call) { - [LinphoneManager.instance terminateCall:call]; + [CallManager.instance terminateCallWithCall:call]; } } diff --git a/Classes/CoreManager.swift b/Classes/CoreManager.swift new file mode 100644 index 000000000..624a46fa3 --- /dev/null +++ b/Classes/CoreManager.swift @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2010-2019 Belledonne Communications SARL. +* +* This file is part of linphone-iphone +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +import Foundation +import linphonesw + +@objc class CoreManager: NSObject { + static var theCoreManager: CoreManager? + var lc: Core? + private var mIterateTimer: Timer? + + @objc static func instance() -> CoreManager { + if (theCoreManager == nil) { + theCoreManager = CoreManager() + } + return theCoreManager! + } + + @objc func setCore(core: OpaquePointer) { + lc = Core.getSwiftObject(cObject: core) + } + + @objc private func iterate() { + lc?.iterate() + } + + @objc func startIterateTimer() { + mIterateTimer = Timer.scheduledTimer(timeInterval: 0.02, target: self, selector: #selector(self.iterate), userInfo: nil, repeats: true) + Log.directLog(BCTBX_LOG_DEBUG, text: "start iterate timer") + + } + + @objc func stopIterateTimer() { + if let timer = mIterateTimer { + timer.invalidate() + Log.directLog(BCTBX_LOG_DEBUG, text: "stop iterate timer") + } + } +} diff --git a/Classes/LinphoneManager.h b/Classes/LinphoneManager.h index faa90e845..fcaddd521 100644 --- a/Classes/LinphoneManager.h +++ b/Classes/LinphoneManager.h @@ -149,7 +149,6 @@ typedef struct _LinphoneManagerSounds { - (void)send:(NSString *)replyText toChatRoom:(LinphoneChatRoom *)room; - (void)call:(const LinphoneAddress *)address; -- (void)terminateCall:(LinphoneCall *)call; +(id)getMessageAppDataForKey:(NSString*)key inMessage:(LinphoneChatMessage*)msg; +(void)setValueInMessageAppData:(id)value forKey:(NSString*)key inMessage:(LinphoneChatMessage*)msg; diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index 420c29795..e5a82f276 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -1408,12 +1408,11 @@ void popup_link_account_cb(LinphoneAccountCreator *creator, LinphoneAccountCreat - (void)startLinphoneCore { linphone_core_start([LinphoneManager getLc]); - mIterateTimer = - [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(iterate) userInfo:nil repeats:YES]; + [CoreManager.instance startIterateTimer]; } - (void)stopLinphoneCore { - [mIterateTimer invalidate]; + [CoreManager.instance stopIterateTimer]; linphone_core_stop([LinphoneManager getLc]); } @@ -1464,6 +1463,7 @@ void popup_link_account_cb(LinphoneAccountCreator *creator, LinphoneAccountCreat linphone_core_add_callbacks(theLinphoneCore, cbs); [CallManager.instance setCoreWithCore:theLinphoneCore]; + [CoreManager.instance setCoreWithCore:theLinphoneCore]; [ConfigManager.instance setDbWithDb:_configDb]; linphone_core_start(theLinphoneCore); @@ -1507,12 +1507,11 @@ void popup_link_account_cb(LinphoneAccountCreator *creator, LinphoneAccountCreat * grab, if any */ [self iterate]; // start scheduler - mIterateTimer = - [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(iterate) userInfo:nil repeats:YES]; + [CoreManager.instance startIterateTimer]; } - (void)destroyLinphoneCore { - [mIterateTimer invalidate]; + [CoreManager.instance stopIterateTimer]; // just in case [self removeCTCallCenterCb]; @@ -2003,13 +2002,6 @@ static int comp_call_state_paused(const LinphoneCall *call, const void *param) { [CallManager.instance startCallWithAddr:iaddr isSas:FALSE]; } -- (void)terminateCall:(LinphoneCall *)call {// TODO PAUL : needs to be tested with CallKit changes - linphone_call_terminate(call); - if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { - [LinphoneManager.instance stopLinphoneCore]; - } -} - #pragma mark - Property Functions - (void)setPushKitToken:(NSData *)pushKitToken { diff --git a/Classes/LinphoneUI/UIHangUpButton.m b/Classes/LinphoneUI/UIHangUpButton.m index 6368fcb6d..129a768a6 100644 --- a/Classes/LinphoneUI/UIHangUpButton.m +++ b/Classes/LinphoneUI/UIHangUpButton.m @@ -20,6 +20,8 @@ #import "UIHangUpButton.h" #import "LinphoneManager.h" +#import "linphoneapp-Swift.h" + @implementation UIHangUpButton #pragma mark - Static Functions @@ -97,11 +99,11 @@ LinphoneManager.instance.conf = TRUE; linphone_core_terminate_conference(LC); } else if (currentcall != NULL) { - [LinphoneManager.instance terminateCall:currentcall]; + [CallManager.instance terminateCallWithCall:currentcall]; } else { const MSList *calls = linphone_core_get_calls(LC); if (bctbx_list_size(calls) == 1) { // Only one call - [LinphoneManager.instance terminateCall:(LinphoneCall *)(calls->data)]; + [CallManager.instance terminateCallWithCall:(calls->data)]; } } } diff --git a/Classes/PhoneMainView.m b/Classes/PhoneMainView.m index b2a23f6e7..88e35d6de 100644 --- a/Classes/PhoneMainView.m +++ b/Classes/PhoneMainView.m @@ -821,7 +821,7 @@ static RootViewManager *rootViewManagerInstance = nil; } - (void)incomingCallDeclined:(LinphoneCall *)call { - [LinphoneManager.instance terminateCall:call]; + [CallManager.instance terminateCallWithCall:call]; } #pragma mark - Chat room Functions