From ddd82ee25358fe2bb1ba8757636a3c39dd96eefa Mon Sep 17 00:00:00 2001 From: Pierre-Eric Pelloux-Prayer Date: Thu, 8 Mar 2012 11:07:55 +0100 Subject: [PATCH] Better handling of GSM call (with or without bg mode enabled) --- Classes/LinphoneAppDelegate.h | 4 +- Classes/LinphoneAppDelegate.m | 71 ++++++++++++++++++---------- Classes/LinphoneUI/LinphoneManager.h | 2 +- Classes/LinphoneUI/LinphoneManager.m | 10 ++-- Classes/StatusSubViewController.m | 2 +- Classes/StatusSubViewController.xib | 6 +-- 6 files changed, 60 insertions(+), 35 deletions(-) diff --git a/Classes/LinphoneAppDelegate.h b/Classes/LinphoneAppDelegate.h index 0de437353..cf01f937c 100644 --- a/Classes/LinphoneAppDelegate.h +++ b/Classes/LinphoneAppDelegate.h @@ -20,6 +20,7 @@ #import #import +#import "CoreTelephony/CTCallCenter.h" #define DIALER_TAB_INDEX 1 #define CONTACTS_TAB_INDEX 2 @@ -39,7 +40,8 @@ IBOutlet PhoneViewController* myPhoneViewController; CallHistoryTableViewController* myCallHistoryTableViewController; ContactPickerDelegate* myContactPickerDelegate; - + + CTCallCenter* callCenter; } @property (nonatomic, retain) IBOutlet UIWindow *window; diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index bea3ed987..8cbd45e2c 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -46,6 +46,24 @@ int __aeabi_idiv(int a, int b) { @synthesize myPeoplePickerController; @synthesize myPhoneViewController; +-(void) handleGSMCallInteration: (id) cCenter { + CTCallCenter* ct = (CTCallCenter*) cCenter; + + int callCount = [ct.currentCalls count]; + if (!callCount) { + NSLog(@"No GSM call -> enabling SIP calls"); + linphone_core_set_max_calls([LinphoneManager getLc], 3); + } else { + NSLog(@"%d GSM call(s) -> disabling SIP calls", callCount); + /* pause current call, if any */ + LinphoneCall* call = linphone_core_get_current_call([LinphoneManager getLc]); + if (call) { + NSLog(@"Pausing SIP call"); + linphone_core_pause_call([LinphoneManager getLc], call); + } + linphone_core_set_max_calls([LinphoneManager getLc], 0); + } +} -(void)applicationWillResignActive:(UIApplication *)application { LinphoneCore* lc = [LinphoneManager getLc]; @@ -65,11 +83,31 @@ int __aeabi_idiv(int a, int b) { } - (void)applicationDidEnterBackground:(UIApplication *)application { - [[LinphoneManager instance] enterBackgroundMode]; + if (![[LinphoneManager instance] enterBackgroundMode]) { + // destroying eventHandler if app cannot go in background. + // Otherwise if a GSM call happen and Linphone is resumed, + // the handler will be called before LinphoneCore is built. + // Then handler will be restored in appDidBecomeActive cb + callCenter.callEventHandler = nil; + [callCenter release]; + callCenter = nil; + } } - (void)applicationDidBecomeActive:(UIApplication *)application { [[LinphoneManager instance] becomeActive]; + if (callCenter == nil) { + callCenter = [[CTCallCenter alloc] init]; + callCenter.callEventHandler = ^(CTCall* call) { + // post on main thread + [self performSelectorOnMainThread:@selector(handleGSMCallInteration:) + withObject:callCenter + waitUntilDone:YES]; + }; + } + // check call state at startup + [self handleGSMCallInteration:callCenter]; + LinphoneCore* lc = [LinphoneManager getLc]; LinphoneCall* call = linphone_core_get_current_call(lc); if (call == NULL) @@ -130,6 +168,7 @@ int __aeabi_idiv(int a, int b) { [[NSUserDefaults standardUserDefaults] synchronize]; } + - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ /* @@ -182,30 +221,12 @@ int __aeabi_idiv(int a, int b) { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound]; - CTCallCenter* ct = [[CTCallCenter alloc] init]; - ct.callEventHandler = ^(CTCall* call) { - @synchronized([LinphoneManager instance]) { - if (call.callState == CTCallStateDisconnected) { - NSLog(@"GSM call disconnected"); - if ([ct.currentCalls count] > 0) { - NSLog(@"There are still some ongoing GSM call: disable SIP calls"); - linphone_core_set_max_calls([LinphoneManager getLc], 0); - } else { - NSLog(@"Re-enabling SIP calls"); - linphone_core_set_max_calls([LinphoneManager getLc], 3); - } - } else { - NSLog(@"GSM call existing"); - /* pause current call, if any */ - LinphoneCall* call = linphone_core_get_current_call([LinphoneManager getLc]); - if (call) { - NSLog(@"Pausing SIP call"); - linphone_core_pause_call([LinphoneManager getLc], call); - } - NSLog(@"Disabling SIP calls"); - linphone_core_set_max_calls([LinphoneManager getLc], 0); - } - } + callCenter = [[CTCallCenter alloc] init]; + callCenter.callEventHandler = ^(CTCall* call) { + // post on main thread + [self performSelectorOnMainThread:@selector(handleGSMCallInteration:) + withObject:callCenter + waitUntilDone:YES]; }; return YES; } diff --git a/Classes/LinphoneUI/LinphoneManager.h b/Classes/LinphoneUI/LinphoneManager.h index 95aaa8d60..d31962ddb 100644 --- a/Classes/LinphoneUI/LinphoneManager.h +++ b/Classes/LinphoneUI/LinphoneManager.h @@ -69,7 +69,7 @@ typedef struct _CallContext { -(BOOL) isNotIphone3G; -(void) destroyLibLinphone; --(void) enterBackgroundMode; +-(BOOL) enterBackgroundMode; -(void) becomeActive; -(void) kickOffNetworkConnection; -(NSString*) getDisplayNameFromAddressBook:(NSString*) number andUpdateCallLog:(LinphoneCallLog*)log; diff --git a/Classes/LinphoneUI/LinphoneManager.m b/Classes/LinphoneUI/LinphoneManager.m index c4d073c6a..beef0ac13 100644 --- a/Classes/LinphoneUI/LinphoneManager.m +++ b/Classes/LinphoneUI/LinphoneManager.m @@ -693,6 +693,7 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setDelegate:nil]; if (theLinphoneCore != nil) { //just in case application terminate before linphone core initialization + NSLog(@"Destroy linphonecore"); linphone_core_destroy(theLinphoneCore); theLinphoneCore = nil; SCNetworkReachabilityUnscheduleFromRunLoop(proxyReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); @@ -704,7 +705,7 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach } //**********************BG mode management*************************/////////// --(void) enterBackgroundMode { +-(BOOL) enterBackgroundMode { LinphoneProxyConfig* proxyCfg; linphone_core_get_default_proxy(theLinphoneCore, &proxyCfg); linphone_core_stop_dtmf_stream(theLinphoneCore); @@ -744,11 +745,12 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach if (linphone_core_get_sip_transports(theLinphoneCore, &transportValue)) { ms_error("cannot get current transport"); } - + return YES; } else { ms_warning("Entering lite bg mode"); [self destroyLibLinphone]; + return NO; } } @@ -802,6 +804,7 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach #endif /* Initialize linphone core*/ + NSLog(@"Create linphonecore"); theLinphoneCore = linphone_core_new (&linphonec_vtable , [confiFileName cStringUsingEncoding:[NSString defaultCStringEncoding]] , [factoryConfig cStringUsingEncoding:[NSString defaultCStringEncoding]] @@ -891,13 +894,12 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach } -(void) becomeActive { - if (theLinphoneCore == nil) { //back from standby and background mode is disabled [self startLibLinphone]; } else { if (![self reconfigureLinphoneIfNeeded:currentSettings]) { - ms_message("becomming active with no config modification, make sure we are registered"); + ms_message("becoming active with no config modification, make sure we are registered"); linphone_core_refresh_registers(theLinphoneCore);//just to make sure REGISTRATION is up to date } diff --git a/Classes/StatusSubViewController.m b/Classes/StatusSubViewController.m index e0d1cadcb..f1eaa2423 100644 --- a/Classes/StatusSubViewController.m +++ b/Classes/StatusSubViewController.m @@ -60,7 +60,7 @@ } -(BOOL) updateWithRegistrationState:(LinphoneRegistrationState)state message:(NSString*) message { - + label.hidden = NO; switch(state) { case LinphoneRegistrationCleared: image.hidden = NO; diff --git a/Classes/StatusSubViewController.xib b/Classes/StatusSubViewController.xib index d93f8f7f6..9ce474c3b 100644 --- a/Classes/StatusSubViewController.xib +++ b/Classes/StatusSubViewController.xib @@ -39,7 +39,7 @@ - 292 + -2147483356 {{0, -1}, {25, 23}} @@ -66,7 +66,7 @@ - 292 + -2147483356 {{28, 0}, {280, 21}} @@ -76,7 +76,7 @@ 7 NO IBCocoaTouchFramework - No SIP account defined + CARAMBA 3 MC42NjY2NjY2NjY3AA