From 8eaf19fd172374faa67bca21770ea69bd597ed6e Mon Sep 17 00:00:00 2001 From: Jehan Monnier Date: Tue, 15 Jun 2010 14:14:19 +0200 Subject: [PATCH] fix ring back tone implement multitasking on udp socket --- Classes/linphoneAppDelegate.h | 1 + Classes/linphoneAppDelegate.m | 81 ++++++++++++++++++++++++++++++++--- linphonerc | 3 +- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/Classes/linphoneAppDelegate.h b/Classes/linphoneAppDelegate.h index ae97b51e9..bdcbfb8e0 100644 --- a/Classes/linphoneAppDelegate.h +++ b/Classes/linphoneAppDelegate.h @@ -52,6 +52,7 @@ LinphoneCore* myLinphoneCore; SCNetworkReachabilityContext proxyReachabilityContext; SCNetworkReachabilityRef proxyReachability; + CFReadStreamRef mReadStream; } diff --git a/Classes/linphoneAppDelegate.m b/Classes/linphoneAppDelegate.m index 7afe8b047..a0c0fd8a9 100644 --- a/Classes/linphoneAppDelegate.m +++ b/Classes/linphoneAppDelegate.m @@ -26,6 +26,11 @@ #import "ConsoleViewController.h" #import "MoreViewController.h" +#include +#include +#include + + extern void ms_au_register_card(); void linphone_iphone_keepAliveHandler () { ms_message("keepalive handler invoked"); @@ -98,11 +103,29 @@ LinphoneCoreVTable linphonec_vtable = { - (void)applicationDidEnterBackground:(UIApplication *)application { #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 40000 - + + struct addrinfo hints; + struct addrinfo *res=NULL; + int err; + LinphoneProxyConfig* proxyCfg; + LinphoneAddress *addr; + int sipsock = linphone_core_get_sip_socket(myLinphoneCore); linphone_core_get_default_proxy(myLinphoneCore, &proxyCfg); + if (backgroundSupported && proxyCfg) { - if ([[UIApplication sharedApplication] setKeepAliveTimeout:(NSTimeInterval)linphone_proxy_config_get_expires(proxyCfg) + //first register + + linphone_core_set_network_reachable(myLinphoneCore,false); + linphone_core_iterate(myLinphoneCore); + linphone_core_set_network_reachable(myLinphoneCore,true); + + int i=0; + while (!linphone_proxy_config_is_registered(proxyCfg) && i++<200 ) { + linphone_core_iterate(myLinphoneCore); + usleep(100000); + } + if ([[UIApplication sharedApplication] setKeepAliveTimeout:600/*(NSTimeInterval)linphone_proxy_config_get_expires(proxyCfg)*/ handler:^{ ms_warning("keepalive handler"); linphone_core_set_network_reachable(myLinphoneCore,false); @@ -117,10 +140,38 @@ LinphoneCoreVTable linphonec_vtable = { } else { ms_warning("keepalive handler cannot be registered"); } - - - + if (mReadStream == nil) { + const char *port; + addr=linphone_address_new(linphone_proxy_config_get_addr(proxyCfg)); + memset(&hints,0,sizeof(hints)); + hints.ai_family=linphone_core_ipv6_enabled(myLinphoneCore) ? AF_INET6 : AF_INET; + port=linphone_address_get_port(addr); + if (port==NULL) port="5060"; + err=getaddrinfo(linphone_address_get_domain(addr),port,&hints,&res); + if (err!=0){ + ms_error("getaddrinfo() failed for %s: %s",linphone_address_get_domain(addr),gai_strerror(err)); + linphone_address_destroy(addr); + return; + } + err=connect(sipsock,res->ai_addr,res->ai_addrlen); + if (err==-1){ + ms_error("Connect failed: %s",strerror(errno)); + } + freeaddrinfo(res); + + CFStreamCreatePairWithSocket(NULL, (CFSocketNativeHandle)sipsock, &mReadStream,nil); + + if (!CFReadStreamSetProperty(mReadStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP)) { + ms_error("cannot set service type to voip for read stream"); + } + + + if (!CFReadStreamOpen(mReadStream)) { + ms_error("cannot open read stream"); + } + } } + #endif } @@ -208,6 +259,18 @@ LinphoneCoreVTable linphonec_vtable = { } else { isStarted=true; } + if (mReadStream !=nil) { + + //unconnect + int socket = linphone_core_get_sip_socket(myLinphoneCore); + struct addrinfo hints; + memset(&hints,0,sizeof(hints)); + hints.ai_family=AF_UNSPEC; + connect(socket,&hints,sizeof(hints)); + CFReadStreamClose(mReadStream); + CFRelease(mReadStream); + mReadStream=nil; + } } -(void)selectDialerTab { @@ -326,6 +389,13 @@ extern void libmsilbc_init(); //initial state is network off linphone_core_set_network_reachable(myLinphoneCore,false); + // Set audio assets + NSBundle* myBundle = [NSBundle mainBundle]; + const char* lRing = [[myBundle pathForResource:@"oldphone-mono"ofType:@"wav"] cStringUsingEncoding:[NSString defaultCStringEncoding]]; + linphone_core_set_ring(myLinphoneCore, lRing ); + const char* lRingBack = [[myBundle pathForResource:@"ringback"ofType:@"wav"] cStringUsingEncoding:[NSString defaultCStringEncoding]]; + linphone_core_set_ringback(myLinphoneCore, lRingBack); + @@ -409,7 +479,6 @@ extern void libmsilbc_init(); proxyReachability=SCNetworkReachabilityCreateWithName(nil, "linphone.org"); } - proxyReachabilityContext.info=self; SCNetworkReachabilitySetCallback(proxyReachability, (SCNetworkReachabilityCallBack)networkReachabilityCallBack,&proxyReachabilityContext); SCNetworkReachabilityScheduleWithRunLoop(proxyReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); diff --git a/linphonerc b/linphonerc index c2451501e..7bfbc4584 100644 --- a/linphonerc +++ b/linphonerc @@ -5,7 +5,7 @@ firewall_policy=0 mtu=0 [sip] -sip_port=5060 +sip_random_port=1 guess_hostname=1 contact=sip:toto@unknown-host inc_timeout=15 @@ -14,7 +14,6 @@ use_ipv6=0 register_only_when_network_is_up=0 default_proxy=-1 auto_net_state_mon=0 -transport=udp keepalive_period=3600000 [rtp]