From 91614236e8b4a863c3cd4394dfccb36fa0cc8385 Mon Sep 17 00:00:00 2001 From: Christophe Deschamps Date: Tue, 19 Sep 2023 13:02:05 +0200 Subject: [PATCH] Handle [misc]max_calls configuration flag inside the application --- Classes/LinphoneAppDelegate.m | 5 +++++ Classes/LinphoneManager.m | 5 ++++- Classes/Swift/ProviderDelegate.swift | 18 +++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index 973921b52..540c3b9a9 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -841,6 +841,11 @@ - (void)ConfigurationStateUpdateEvent:(NSNotification *)notif { LinphoneConfiguringState state = [[notif.userInfo objectForKey:@"state"] intValue]; if (state == LinphoneConfiguringSuccessful) { + if (linphone_config_has_entry(LinphoneManager.instance.configDb, "misc", "max_calls")) { // Not doable on core on iOS (requires CallKit) -> flag moved to app section, and have app handle it in ProviderDelegate + linphone_config_set_int(LinphoneManager.instance.configDb, "app", "max_calls", linphone_config_get_int(LinphoneManager.instance.configDb,"misc", "max_calls",10)); + linphone_config_clean_entry(LinphoneManager.instance.configDb, "misc", "max_calls"); + + } [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneConfiguringStateUpdate object:nil]; [_waitingIndicator dismissViewControllerAnimated:YES completion:nil]; UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Success", nil) diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index da964e5f6..aa2c12a7d 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -1839,7 +1839,10 @@ static int comp_call_state_paused(const LinphoneCall *call, const void *param) { factory = factoryIpad; } _configDb = linphone_config_new_for_shared_core(kLinphoneMsgNotificationAppGroupId.UTF8String, @"linphonerc".UTF8String, factory.UTF8String); - linphone_config_clean_entry(_configDb, "misc", "max_calls"); + if (linphone_config_has_entry(_configDb, "misc", "max_calls")) { // Not doable on core on iOS (requires CallKit) -> flag moved to app section, and have app handle it in ProviderDelegate + linphone_config_set_int(_configDb, "app", "max_calls", linphone_config_get_int(_configDb,"misc", "max_calls",10)); + linphone_config_clean_entry(_configDb, "misc", "max_calls"); + } } #pragma mark - Audio route Functions diff --git a/Classes/Swift/ProviderDelegate.swift b/Classes/Swift/ProviderDelegate.swift index 6998bfefd..42024ea71 100644 --- a/Classes/Swift/ProviderDelegate.swift +++ b/Classes/Swift/ProviderDelegate.swift @@ -95,9 +95,21 @@ class ProviderDelegate: NSObject { update.remoteHandle = CXHandle(type:.generic, value: handle) update.hasVideo = hasVideo update.localizedCallerName = displayName - + let callInfo = callInfos[uuid] let callId = callInfo?.callId + + if (ConfigManager.instance().config?.hasEntry(section: "app", key: "max_calls") == 1) { // moved from misc to app section intentionally upon app start or remote configuration + if let maxCalls = ConfigManager.instance().config?.getInt(section: "app",key: "max_calls",defaultValue: 10), Core.get().callsNb > maxCalls { + Log.directLog(BCTBX_LOG_MESSAGE, text: "CallKit: declining call, as max calls (\(maxCalls)) reached call-id: [\(String(describing: callId))] and UUID: [\(uuid.description)]") + decline(uuid: uuid) + DispatchQueue.main.async { + try?call?.decline(reason: .Busy) + } + return + } + } + Log.directLog(BCTBX_LOG_MESSAGE, text: "CallKit: report new incoming call with call-id: [\(String(describing: callId))] and UUID: [\(uuid.description)]") //CallManager.instance().setHeldOtherCalls(exceptCallid: callId ?? "") provider.reportNewIncomingCall(with: uuid, update: update) { error in @@ -144,6 +156,10 @@ class ProviderDelegate: NSObject { func endCall(uuid: UUID) { provider.reportCall(with: uuid, endedAt: .init(), reason: .failed) } + + func decline(uuid: UUID) { + provider.reportCall(with: uuid, endedAt: .init(), reason: .unanswered) + } func endCallNotExist(uuid: UUID, timeout: DispatchTime) { DispatchQueue.main.asyncAfter(deadline: timeout) {