From 7ebb7541efd467a8e3eb89a0a83003a5bba8cfb4 Mon Sep 17 00:00:00 2001 From: Simon Morlat Date: Fri, 18 Apr 2014 17:44:15 +0200 Subject: [PATCH] update belle-sip so that background task is started during network operations remove register-waiting background task at UI level stop the video preview before going to background remove NSUserDefault usage, everything goes to LpConfig LinphoneManager has a LpConfig (configDb property) that is used everywhere, regardless of whether the core is started. --- Classes/DialerViewController.m | 5 +- Classes/LinphoneAppDelegate.m | 16 ++---- Classes/LinphoneCoreSettingsStore.m | 28 +++------- Classes/LinphoneManager.h | 5 +- Classes/LinphoneManager.m | 84 ++++++++++++++++------------- Resources/linphonerc | 3 ++ Resources/linphonerc~ipad | 2 + submodules/belle-sip | 2 +- submodules/linphone | 2 +- 9 files changed, 69 insertions(+), 78 deletions(-) diff --git a/Classes/DialerViewController.m b/Classes/DialerViewController.m index 791696d75..3820b6639 100644 --- a/Classes/DialerViewController.m +++ b/Classes/DialerViewController.m @@ -137,18 +137,20 @@ static UICompositeViewDescription *compositeDescription = nil; object:nil]; // Update on show if([LinphoneManager isLcReady]) { + LinphoneManager *mgr=[LinphoneManager instance]; LinphoneCore* lc = [LinphoneManager getLc]; LinphoneCall* call = linphone_core_get_current_call(lc); LinphoneCallState state = (call != NULL)?linphone_call_get_state(call): 0; [self callUpdate:call state:state]; if([LinphoneManager runningOnIpad]) { - if(linphone_core_video_enabled(lc) && linphone_core_video_preview_enabled(lc)) { + if(linphone_core_video_enabled(lc) && [mgr lpConfigBoolForKey:@"preview_preference"]) { linphone_core_set_native_preview_window_id(lc, (unsigned long)videoPreview); [backgroundView setHidden:FALSE]; [videoCameraSwitch setHidden:FALSE]; } else { linphone_core_set_native_preview_window_id(lc, (unsigned long)NULL); + linphone_core_enable_video_preview(lc, FALSE); [backgroundView setHidden:TRUE]; [videoCameraSwitch setHidden:TRUE]; } @@ -180,7 +182,6 @@ static UICompositeViewDescription *compositeDescription = nil; [[NSNotificationCenter defaultCenter] removeObserver:self name:kLinphoneCoreUpdate object:nil]; - } - (void)viewDidLoad { diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index f434c4510..c7c8d37bc 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -123,20 +123,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeNewsstandContentAvailability]; - //work around until we can access lpconfig without linphonecore - NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys: - @"YES", @"start_at_boot_preference", - @"YES", @"backgroundmode_preference", -#ifdef DEBUG - @"YES",@"debugenable_preference", -#else - @"NO",@"debugenable_preference", -#endif - nil]; - [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; - BOOL background_mode = [[NSUserDefaults standardUserDefaults] boolForKey:@"backgroundmode_preference"]; - BOOL start_at_boot = [[NSUserDefaults standardUserDefaults] boolForKey:@"start_at_boot_preference"]; + LinphoneManager* instance = [LinphoneManager instance]; + BOOL background_mode = [instance lpConfigBoolForKey:@"backgroundmode_preference"]; + BOOL start_at_boot = [instance lpConfigBoolForKey:@"start_at_boot_preference"]; if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] && [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) diff --git a/Classes/LinphoneCoreSettingsStore.m b/Classes/LinphoneCoreSettingsStore.m index b4444efd6..6b940b635 100644 --- a/Classes/LinphoneCoreSettingsStore.m +++ b/Classes/LinphoneCoreSettingsStore.m @@ -214,15 +214,8 @@ extern void linphone_iphone_log_handler(int lev, const char *fmt, va_list args); [self setBool:lp_config_get_int(conf, "sip", "use_ipv6", 0) forKey:@"use_ipv6"]; - /*keep this one also in the standardUserDefaults so that it can be read before starting liblinphone*/ - BOOL start_at_boot = TRUE; - if ([[NSUserDefaults standardUserDefaults] objectForKey:@"start_at_boot_preference"] != Nil) - start_at_boot = [[NSUserDefaults standardUserDefaults] boolForKey:@"start_at_boot_preference"]; - [self setBool: start_at_boot forKey:@"start_at_boot_preference"]; - BOOL background_mode = TRUE; - if ([[NSUserDefaults standardUserDefaults] objectForKey:@"backgroundmode_preference"] != Nil) - background_mode =[[NSUserDefaults standardUserDefaults] boolForKey:@"backgroundmode_preference"]; - [self setBool: background_mode forKey:@"backgroundmode_preference"]; + [self setBool: lp_config_get_int(conf,LINPHONERC_APPLICATION_KEY,"start_at_boot_preference",1) forKey:@"start_at_boot_preference"]; + [self setBool: lp_config_get_int(conf,LINPHONERC_APPLICATION_KEY,"backgroundmode_preference",1) forKey:@"backgroundmode_preference"]; if (linphone_core_tunnel_available()){ /*FIXME: enhance linphonecore API to handle tunnel more easily in applications */ @@ -238,7 +231,8 @@ extern void linphone_iphone_log_handler(int lev, const char *fmt, va_list args); [self setBool:(pol->automatically_initiate) forKey:@"start_video_preference"]; [self setBool:(pol->automatically_accept) forKey:@"accept_video_preference"]; [self setBool:linphone_core_self_view_enabled(lc) forKey:@"self_video_preference"]; - [self setBool:linphone_core_video_preview_enabled(lc) forKey:@"preview_preference"]; + BOOL previewEnabled=lp_config_get_int(conf,LINPHONERC_APPLICATION_KEY,"preview_preference",1); + [self setBool:previewEnabled forKey:@"preview_preference"]; MSVideoSize vsize = linphone_core_get_preferred_video_size(lc); int index; if ((vsize.width == MS_VIDEO_SIZE_720P_W) && (vsize.height == MS_VIDEO_SIZE_720P_H)) { @@ -552,7 +546,8 @@ extern void linphone_iphone_log_handler(int lev, const char *fmt, va_list args); policy.automatically_initiate = [self boolForKey:@"start_video_preference"]; linphone_core_set_video_policy(lc, &policy); linphone_core_enable_self_view(lc, [self boolForKey:@"self_video_preference"]); - linphone_core_enable_video_preview(lc, [self boolForKey:@"preview_preference"]); + BOOL preview_preference=[self boolForKey:@"preview_preference"]; + lp_config_set_int(config, LINPHONERC_APPLICATION_KEY, "preview_preference", preview_preference); MSVideoSize vsize; int bw; switch ([self integerForKey:@"video_preferred_size_preference"]) { @@ -647,7 +642,6 @@ extern void linphone_iphone_log_handler(int lev, const char *fmt, va_list args); } else { linphone_core_disable_logs(); } - [[NSUserDefaults standardUserDefaults] setBool:debugmode forKey:@"debugenable_preference"]; //to be used at linphone core startup BOOL animations = [self boolForKey:@"animations_preference"]; lp_config_set_int(config, LINPHONERC_APPLICATION_KEY, "animations_preference", animations); @@ -662,16 +656,6 @@ extern void linphone_iphone_log_handler(int lev, const char *fmt, va_list args); [[LinphoneManager instance] lpConfigSetString:sharing_server forKey:@"sharing_server_preference"]; - - /*keep this one also in the standardUserDefaults so that it can be read before starting liblinphone*/ - BOOL start_at_boot = [self boolForKey:@"start_at_boot_preference"]; - [[NSUserDefaults standardUserDefaults] setBool: start_at_boot forKey:@"start_at_boot_preference"]; - BOOL background_mode = [self boolForKey:@"backgroundmode_preference"]; - [[NSUserDefaults standardUserDefaults] setBool: background_mode forKey:@"backgroundmode_preference"]; - - // Force synchronize - [[NSUserDefaults standardUserDefaults] synchronize]; - [changedDict release]; changedDict = [[NSMutableDictionary alloc] init]; diff --git a/Classes/LinphoneManager.h b/Classes/LinphoneManager.h index df20cbaf2..5f74caddd 100644 --- a/Classes/LinphoneManager.h +++ b/Classes/LinphoneManager.h @@ -98,11 +98,10 @@ typedef struct _LinphoneManagerSounds { NSTimer* mIterateTimer; NSMutableArray* pendindCallIdFromRemoteNotif; Connectivity connectivity; - BOOL stopWaitingRegisters; UIBackgroundTaskIdentifier pausedCallBgTask; UIBackgroundTaskIdentifier incallBgTask; CTCallCenter* mCallCenter; - + NSDate *mLastKeepAliveDate; @public CallContext currentCallContextBeforeGoingBackground; } @@ -130,7 +129,6 @@ typedef struct _LinphoneManagerSounds { - (void)addPushTokenToProxyConfig: (LinphoneProxyConfig*)cfg; - (BOOL)shouldAutoAcceptCallForCallId:(NSString*) callId; - (void)acceptCallForCallId:(NSString*)callid; -- (void)waitForRegisterToArrive; + (void)kickOffNetworkConnection; - (void)setupNetworkReachabilityCallback; @@ -178,6 +176,7 @@ typedef struct _LinphoneManagerSounds { @property (readonly,copy) NSString* contactFilter; @property (copy) void (^silentPushCompletion)(UIBackgroundFetchResult); @property (readonly) BOOL wasRemoteProvisioned; +@property (readonly) LpConfig *configDb; @end diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index fcb9b1c98..0263402bc 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -107,6 +107,7 @@ extern void libmsbcg729_init(void); @synthesize photoLibrary; @synthesize silentPushCompletion; @synthesize wasRemoteProvisioned; +@synthesize configDb; struct codec_name_pref_table{ const char *name; @@ -260,7 +261,19 @@ struct codec_name_pref_table codec_pref_table[]={ [self copyDefaultSettings]; pendindCallIdFromRemoteNotif = [[NSMutableArray alloc] init ]; photoLibrary = [[ALAssetsLibrary alloc] init]; - + + NSString* factoryConfig = [LinphoneManager bundleFile:[LinphoneManager runningOnIpad]?@"linphonerc-factory~ipad":@"linphonerc-factory"]; + NSString *confiFileName = [LinphoneManager documentFile:@".linphonerc"]; + configDb=lp_config_new_with_factory([confiFileName cStringUsingEncoding:[NSString defaultCStringEncoding]] , [factoryConfig cStringUsingEncoding:[NSString defaultCStringEncoding]]); + + //set default values for first boot + if (lp_config_get_string(configDb,LINPHONERC_APPLICATION_KEY,"debugenable_preference",NULL)==NULL){ +#ifdef DEBUG + [self lpConfigSetBool:TRUE forKey:@"debugenable_preference"]; +#else + [self lpConfigSetBool:FALSE forKey:@"debugenable_preference"]; +#endif + } } return self; } @@ -360,9 +373,9 @@ static void dump_section(const char* section, void* data){ } + (void)dumpLCConfig { - if(theLinphoneCore ){ - LpConfig* conf = linphone_core_get_config(theLinphoneCore); - lp_config_for_each_section(conf, dump_section, conf); + if (theLinphoneCore ){ + LpConfig *conf=[LinphoneManager instance].configDb; + lp_config_for_each_section(conf, dump_section, conf); } } @@ -514,6 +527,8 @@ static void linphone_iphone_display_status(struct _LinphoneCore * lc, const char if (!incallBgTask){ incallBgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{ [LinphoneLogger log:LinphoneLoggerWarning format:@"Call cannot ring any more, too late"]; + [[UIApplication sharedApplication] endBackgroundTask:incallBgTask]; + incallBgTask=0; }]; } @@ -657,8 +672,6 @@ static void linphone_iphone_configuring_status_changed(LinphoneCore *lc, Linphon - (void)onRegister:(LinphoneCore *)lc cfg:(LinphoneProxyConfig*) cfg state:(LinphoneRegistrationState) state message:(const char*) message { [LinphoneLogger logc:LinphoneLoggerLog format:"NEW REGISTRATION STATE: '%s' (message: '%s')", linphone_registration_state_to_string(state), message]; - if (state==LinphoneRegistrationOk) - [LinphoneManager instance]->stopWaitingRegisters=TRUE; // Post event NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: @@ -822,7 +835,7 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach [LinphoneManager kickOffNetworkConnection]; } else { Connectivity newConnectivity; - BOOL isWifiOnly = lp_config_get_int(linphone_core_get_config(theLinphoneCore), LINPHONERC_APPLICATION_KEY, "wifi_only_preference",FALSE); + BOOL isWifiOnly = lp_config_get_int(lLinphoneMgr.configDb, LINPHONERC_APPLICATION_KEY, "wifi_only_preference",FALSE); if (!ctx || ctx->testWWan) newConnectivity = flags & kSCNetworkReachabilityFlagsIsWWAN ? wwan:wifi; else @@ -847,8 +860,8 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach linphone_proxy_config_expires(proxy, 0); } linphone_core_set_network_reachable(theLinphoneCore,true); + linphone_core_iterate(theLinphoneCore); [LinphoneLogger logc:LinphoneLoggerLog format:"Network connectivity changed to type [%s]",(newConnectivity==wifi?"wifi":"wwan")]; - [lLinphoneMgr waitForRegisterToArrive]; } lLinphoneMgr.connectivity=newConnectivity; } @@ -1051,7 +1064,7 @@ static BOOL libStarted = FALSE; #endif /*to make sure we don't loose debug trace*/ - if ([[NSUserDefaults standardUserDefaults] boolForKey:@"debugenable_preference"]) { + if ([self lpConfigBoolForKey:@"debugenable_preference"]) { linphone_core_enable_logs_with_cb((OrtpLogFunc)linphone_iphone_log_handler); ortp_set_log_level_mask(ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL); } @@ -1095,11 +1108,9 @@ static BOOL libStarted = FALSE; connectivity=none; - NSString* factoryConfig = [LinphoneManager bundleFile:[LinphoneManager runningOnIpad]?@"linphonerc-factory~ipad":@"linphonerc-factory"]; - NSString *confiFileName = [LinphoneManager documentFile:@".linphonerc"]; - theLinphoneCore = linphone_core_new (&linphonec_vtable - , [confiFileName cStringUsingEncoding:[NSString defaultCStringEncoding]] - , [factoryConfig cStringUsingEncoding:[NSString defaultCStringEncoding]] + + theLinphoneCore = linphone_core_new_with_config (&linphonec_vtable + ,configDb ,self /* user_data */); /* set the CA file no matter what, since the remote provisioning could be hitting an HTTPS server */ @@ -1206,24 +1217,6 @@ static int comp_call_id(const LinphoneCall* call , const char *callid) { return YES; } -- (void)waitForRegisterToArrive{ - if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] - && [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { - stopWaitingRegisters = FALSE; - [LinphoneLogger logc:LinphoneLoggerLog format:"Starting long running task for registering"]; - UIBackgroundTaskIdentifier bgid = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{ - [LinphoneManager instance]->stopWaitingRegisters=TRUE; - [LinphoneLogger logc:LinphoneLoggerLog format:"Expiration handler called"]; - }]; - for(int i=0;i<100 && (!stopWaitingRegisters);i++){ - linphone_core_iterate(theLinphoneCore); - usleep(20000); - } - [LinphoneLogger logc:LinphoneLoggerLog format:"Ending long running task for registering"]; - [[UIApplication sharedApplication] endBackgroundTask:bgid]; - } -} - static int comp_call_state_paused (const LinphoneCall* call, const void* param) { return linphone_call_get_state(call) != LinphoneCallPaused; } @@ -1262,6 +1255,7 @@ static int comp_call_state_paused (const LinphoneCall* call, const void* param) if ([[UIApplication sharedApplication] setKeepAliveTimeout:600/*(NSTimeInterval)linphone_proxy_config_get_expires(proxyCfg)*/ handler:^{ [LinphoneLogger logc:LinphoneLoggerWarning format:"keepalive handler"]; + mLastKeepAliveDate=[NSDate date]; if (theLinphoneCore == nil) { [LinphoneLogger logc:LinphoneLoggerWarning format:"It seems that Linphone BG mode was deactivated, just skipping"]; return; @@ -1292,6 +1286,11 @@ static int comp_call_state_paused (const LinphoneCall* call, const void* param) /*if at least one call exist, enter normal bg mode */ shouldEnterBgMode=TRUE; } + /*stop the video preview*/ + if (theLinphoneCore){ + linphone_core_enable_video_preview(theLinphoneCore, FALSE); + linphone_core_iterate(theLinphoneCore); + } [LinphoneLogger logc:LinphoneLoggerLog format:"Entering [%s] bg mode",shouldEnterBgMode?"normal":"lite"]; @@ -1323,6 +1322,19 @@ static int comp_call_state_paused (const LinphoneCall* call, const void* param) /*IOS specific*/ linphone_core_start_dtmf_stream(theLinphoneCore); + + /*start the video preview in case we are in the main view*/ + if ([LinphoneManager runningOnIpad] && linphone_core_video_enabled(theLinphoneCore) && [self lpConfigBoolForKey:@"preview_preference"]){ + linphone_core_enable_video_preview(theLinphoneCore, TRUE); + } + /*check last keepalive handler date*/ + if (mLastKeepAliveDate!=Nil){ + NSDate *current=[NSDate date]; + if ([current timeIntervalSinceDate:mLastKeepAliveDate]>700){ + NSString *datestr=[mLastKeepAliveDate description]; + [LinphoneLogger logc:LinphoneLoggerWarning format:"keepalive handler was called for the last time at %@",datestr]; + } + } } @@ -1647,7 +1659,7 @@ static void audioRouteChangeListenerCallback ( - (void)lpConfigSetString:(NSString*)value forKey:(NSString*)key forSection:(NSString *)section { if (!key) return; - lp_config_set_string(linphone_core_get_config(theLinphoneCore), [section UTF8String], [key UTF8String], value?[value UTF8String]:NULL); + lp_config_set_string(configDb, [section UTF8String], [key UTF8String], value?[value UTF8String]:NULL); } - (NSString*)lpConfigStringForKey:(NSString*)key { @@ -1660,7 +1672,7 @@ static void audioRouteChangeListenerCallback ( - (NSString*)lpConfigStringForKey:(NSString*)key forSection:(NSString *)section { if (!key) return nil; - const char* value = lp_config_get_string(linphone_core_get_config(theLinphoneCore), [section UTF8String], [key UTF8String], NULL); + const char* value = lp_config_get_string(configDb, [section UTF8String], [key UTF8String], NULL); if (value) return [NSString stringWithUTF8String:value]; else @@ -1673,7 +1685,7 @@ static void audioRouteChangeListenerCallback ( - (void)lpConfigSetInt:(NSInteger)value forKey:(NSString*)key forSection:(NSString *)section { if (!key) return; - lp_config_set_int(linphone_core_get_config(theLinphoneCore), [section UTF8String], [key UTF8String], value ); + lp_config_set_int(configDb, [section UTF8String], [key UTF8String], value ); } - (NSInteger)lpConfigIntForKey:(NSString*)key { @@ -1682,7 +1694,7 @@ static void audioRouteChangeListenerCallback ( - (NSInteger)lpConfigIntForKey:(NSString*)key forSection:(NSString *)section { if (!key) return -1; - return lp_config_get_int(linphone_core_get_config(theLinphoneCore), [section UTF8String], [key UTF8String], -1); + return lp_config_get_int(configDb, [section UTF8String], [key UTF8String], -1); } - (void)lpConfigSetBool:(BOOL)value forKey:(NSString*)key { diff --git a/Resources/linphonerc b/Resources/linphonerc index be8cf3195..eb2ae79f8 100644 --- a/Resources/linphonerc +++ b/Resources/linphonerc @@ -26,6 +26,9 @@ rotation_preference=auto animations_preference=1 edge_opt_preference=0 use_system_contacts=0 +start_at_boot_preference=1 +backgroundmode_preference=1 + [default_values] reg_expires=600 \ No newline at end of file diff --git a/Resources/linphonerc~ipad b/Resources/linphonerc~ipad index 78405f5d4..1f424bab1 100644 --- a/Resources/linphonerc~ipad +++ b/Resources/linphonerc~ipad @@ -26,6 +26,8 @@ rotation_preference=auto animations_preference=1 edge_opt_preference=0 use_system_contacts=0 +start_at_boot_preference=1 +backgroundmode_preference=1 [default_values] reg_expires=600 diff --git a/submodules/belle-sip b/submodules/belle-sip index 1b86808a1..eb3467af3 160000 --- a/submodules/belle-sip +++ b/submodules/belle-sip @@ -1 +1 @@ -Subproject commit 1b86808a1b7c6803b71b8d08607b3a0d7a2e9ea9 +Subproject commit eb3467af3e8f42c5a9cea36d4253bd66641413f3 diff --git a/submodules/linphone b/submodules/linphone index 9f68674a4..c1ef06eb8 160000 --- a/submodules/linphone +++ b/submodules/linphone @@ -1 +1 @@ -Subproject commit 9f68674a4b10aa6a6907cebf65b941997719e9e7 +Subproject commit c1ef06eb8f2a7c88d6e54943930a838085d11e9e