diff --git a/Classes/CallManager.swift b/Classes/CallManager.swift index 527a5a56e..159131468 100644 --- a/Classes/CallManager.swift +++ b/Classes/CallManager.swift @@ -39,7 +39,6 @@ import AVFoundation @objc var speakerEnabled : Bool = false @objc var bluetoothEnabled : Bool = false @objc var nextCallIsTransfer: Bool = false - var callAppDatas: [String : CallAppData] = [:] fileprivate override init() { @@ -60,12 +59,29 @@ import AVFoundation lc?.addDelegate(delegate: manager) } - @objc func getAppData (callId : String) -> CallAppData? { - return CallManager.instance().callAppDatas["\(callId)"] + @objc static func getAppData (call: OpaquePointer) -> CallAppData? { + let sCall = Call.getSwiftObject(cObject: call) + return getAppData(sCall: sCall) + } + + static func getAppData (sCall:Call) -> CallAppData? { + if (sCall.userData == nil) { + return nil + } + return Unmanaged.fromOpaque(sCall.userData!).takeUnretainedValue() } - @objc func setAppData (callId: String, appData: CallAppData) { - CallManager.instance().callAppDatas.updateValue(appData, forKey: callId) + @objc static func setAppData (call:OpaquePointer, appData: CallAppData) { + let sCall = Call.getSwiftObject(cObject: call) + setAppData(sCall: sCall, appData: appData) + } + + static func setAppData (sCall:Call, appData:CallAppData?) { + if (appData == nil) { + sCall.userData = nil + } else { + sCall.userData = UnsafeMutableRawPointer(Unmanaged.passUnretained(appData!).toOpaque()) + } } @objc func findCall(callId: String?) -> OpaquePointer? { @@ -239,17 +255,16 @@ import AVFoundation } let call = CallManager.instance().lc!.inviteAddressWithParams(addr: addr, params: lcallParams) if (call != nil) { - let callId = call!.callLog?.callId // The LinphoneCallAppData object should be set on call creation with callback // - (void)onCall:StateChanged:withMessage:. If not, we are in big trouble and expect it to crash // We are NOT responsible for creating the AppData. - let data = CallManager.instance().getAppData(callId: callId ?? "") + let data = CallManager.getAppData(sCall: call!) if (data == nil) { Log.directLog(BCTBX_LOG_ERROR, text: "New call instanciated but app data was not set. Expect it to crash.") /* will be used later to notify user if video was not activated because of the linphone core*/ } else { data!.videoRequested = lcallParams.videoEnabled - CallManager.instance().setAppData(callId: callId!, appData: data!) + CallManager.setAppData(sCall: call!, appData: data) } } } @@ -313,10 +328,12 @@ class CoreManager: CoreDelegate { // force-enable it on ICE re-invite if the user disabled it. CoreManager.speaker_already_enabled = false - if (callId != nil && CallManager.instance().callAppDatas["\(callId!)"] == nil) { - CallManager.instance().callAppDatas.updateValue(CallAppData(), forKey: callId!) + if (call.userData == nil) { + let appData = CallAppData() + call.userData = UnsafeMutableRawPointer(Unmanaged.passUnretained(appData).toOpaque()) } + switch cstate { case .IncomingReceived: if (CallManager.callKitEnabled()) { @@ -422,7 +439,7 @@ class CoreManager: CoreDelegate { } break case .Released: - CallManager.instance().callAppDatas.removeValue(forKey: callId ?? "") + CallManager.setAppData(sCall: call, appData: nil) break default: break diff --git a/Classes/CallView.m b/Classes/CallView.m index 5ae9f5749..d1cfd75f7 100644 --- a/Classes/CallView.m +++ b/Classes/CallView.m @@ -587,12 +587,7 @@ static void hideSpinner(LinphoneCall *call, void *user_data) { // check video, because video can be disabled because of the low bandwidth. if (!linphone_call_params_video_enabled(linphone_call_get_current_params(call))) { const LinphoneCallParams *param = linphone_call_get_current_params(call); - CallAppData *data = nil; - const char *callId = linphone_call_log_get_call_id(linphone_call_get_call_log(call)); - if (callId) { - data = [CallManager.instance getAppDataWithCallId:[NSString stringWithUTF8String:callId]]; - } - + CallAppData *data = [CallManager getAppDataWithCall:call]; if (state == LinphoneCallStreamsRunning && data && data.videoRequested && linphone_call_params_low_bandwidth_enabled(param)) { // too bad video was not enabled because low bandwidth UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Low bandwidth", nil) @@ -608,7 +603,7 @@ static void hideSpinner(LinphoneCall *call, void *user_data) { [errView addAction:defaultAction]; [self presentViewController:errView animated:YES completion:nil]; data.videoRequested = FALSE; - [CallManager.instance setAppDataWithCallId:[NSString stringWithUTF8String:callId] appData:data]; + [CallManager setAppDataWithCall:call appData:data]; } } break; diff --git a/Classes/LinphoneUI/UIVideoButton.m b/Classes/LinphoneUI/UIVideoButton.m index 69aa119fb..4872bb48c 100644 --- a/Classes/LinphoneUI/UIVideoButton.m +++ b/Classes/LinphoneUI/UIVideoButton.m @@ -42,10 +42,9 @@ INIT_WITH_COMMON_CF { LinphoneCall *call = linphone_core_get_current_call(LC); if (call) { - NSString *callId = [NSString stringWithUTF8String:linphone_call_log_get_call_id(linphone_call_get_call_log(call))]; - CallAppData *data = [CallManager.instance getAppDataWithCallId:callId]; + CallAppData *data = [CallManager getAppDataWithCall:call]; data.videoRequested = TRUE;/* will be used later to notify user if video was not activated because of the linphone core*/ - [CallManager.instance setAppDataWithCallId:callId appData:data]; + [CallManager setAppDataWithCall:call appData:data]; LinphoneCallParams *call_params = linphone_core_create_call_params(LC,call); linphone_call_params_enable_video(call_params, TRUE); linphone_call_update(call, call_params); diff --git a/Classes/PhoneMainView.m b/Classes/PhoneMainView.m index 0f4a3687d..e558042ab 100644 --- a/Classes/PhoneMainView.m +++ b/Classes/PhoneMainView.m @@ -776,8 +776,7 @@ static RootViewManager *rootViewManagerInstance = nil; LinphoneCall *call = linphone_core_get_current_call(LC); if (call && linphone_call_params_video_enabled(linphone_call_get_current_params(call))) { - NSString *callId = [NSString stringWithUTF8String:linphone_call_log_get_call_id(linphone_call_get_call_log(call))]; - CallAppData *data = [CallManager.instance getAppDataWithCallId:callId]; + CallAppData *data = [CallManager getAppDataWithCall:call]; if (data != nil) { if (state == UIDeviceBatteryStateUnplugged) { if (level <= 0.2f && !data.batteryWarningShown) { @@ -801,7 +800,7 @@ static RootViewManager *rootViewManagerInstance = nil; if (level > 0.2f) { data.batteryWarningShown = FALSE; } - [CallManager.instance setAppDataWithCallId:callId appData:data]; + [CallManager setAppDataWithCall:call appData:data]; } } }