Commit d79746e5 authored by Simon Morlat's avatar Simon Morlat

prevent appnaping on macos x during the time SIP messages are notified to application.

expose background_task api to application because it also needs it.
parent 6c5349a4
......@@ -47,17 +47,18 @@ LT_INIT(win32-dll)
case "$target" in
#macosx 64 bits
x86_64-apple-darwin*)
LIBS="$LIBS -mmacosx-version-min=10.6"
LIBS="$LIBS -mmacosx-version-min=10.6 -framework Foundation"
CXXFLAGS="$CXXFLAGS -mmacosx-version-min=10.6"
CFLAGS="$CFLAGS -mmacosx-version-min=10.6"
build_apple=yes
;;
i386-apple*|armv6-apple*|armv7-apple*|armv7s-apple*|arm64-apple*|aarch64-apple*)
LIBS="$LIBS -framework Foundation -framework CoreFoundation -framework CFNetwork -framework UIKit"
build_ios=yes
build_apple=yes
;;
esac
AM_CONDITIONAL([BUILD_IOS], [test "x$build_ios" = "xyes"])
AM_CONDITIONAL([BUILD_APPLE], [test "x$build_apple" = "xyes"])
dnl Workaround for mingw, whose compiler does not check in /usr/include ...
case "$target_os" in
......
......@@ -201,6 +201,13 @@ typedef int belle_sip_fd_t;
BELLESIP_EXPORT int belle_sip_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
BELLESIP_EXPORT void belle_sip_freeaddrinfo(struct addrinfo *res);
typedef void (*belle_sip_background_task_end_callback_t)(void *);
BELLESIP_EXPORT unsigned long belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data);
BELLESIP_EXPORT void belle_sip_end_background_task(unsigned long id);
BELLE_SIP_END_DECLS
#endif
......
......@@ -47,7 +47,7 @@ libbellesip_la_SOURCES= \
bodyhandler.c
if BUILD_IOS
if BUILD_APPLE
libbellesip_la_SOURCES+=backgroundtask.m
endif
......
......@@ -18,11 +18,30 @@
#include "belle_sip_internal.h"
#if TARGET_OS_IPHONE==1
#include <UIKit/UIApplication.h>
unsigned int belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
typedef struct fallback_callback_data{
unsigned long task_id;
}fallback_callback_data_t;
static void fallback_callback(void *data){
fallback_callback_data_t *fbd=(fallback_callback_data_t*)data;
belle_sip_end_background_task(fbd->task_id);
belle_sip_free(fbd);
}
unsigned long belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
UIApplication *app=[UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgid = UIBackgroundTaskInvalid;
fallback_callback_data_t *fbd=NULL;
if (cb==NULL){
cb=fallback_callback;
data=fbd=(fallback_callback_data_t*)belle_sip_malloc0(sizeof(fallback_callback_data_t));
}
void (^handler)() = ^{
cb(data);
};
......@@ -37,11 +56,50 @@ unsigned int belle_sip_begin_background_task(const char *name, belle_sip_backgro
belle_sip_error("Could not start background task %s.", name);
return 0;
}
return (unsigned int)bgid;
if (fbd) fbd->id=bgid;
return (unsigned long)bgid;
}
void belle_sip_end_background_task(unsigned int id){
void belle_sip_end_background_task(unsigned long id){
UIApplication *app=[UIApplication sharedApplication];
[app endBackgroundTask:(UIBackgroundTaskIdentifier)id];
}
#else
/*mac*/
#import <Foundation/NSProcessinfo.h>
static unsigned long dummy_id=0;
static id activity_id=0;
static int activity_refcnt=0;
unsigned long belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
activity_refcnt++;
if (activity_refcnt==1){
NSProcessInfo *pinfo=[NSProcessInfo processInfo];
if (pinfo && [pinfo respondsToSelector:@selector(beginActivityWithOptions:reason:)]){
activity_id=[pinfo beginActivityWithOptions:NSActivityUserInitiatedAllowingIdleSystemSleep reason:@"Processing SIP signaling"];
[activity_id retain];
belle_sip_message("Activity started");
}
}
return ++dummy_id;
}
void belle_sip_end_background_task(unsigned long id){
activity_refcnt--;
if (activity_refcnt==0){
if (activity_id!=0){
NSProcessInfo *pinfo=[NSProcessInfo processInfo];
[pinfo endActivity:activity_id];
[activity_id release];
belle_sip_message("Activity ended");
activity_id=0;
}
}
}
#endif
......@@ -543,7 +543,8 @@ static int belle_sip_channel_process_read_data(belle_sip_channel_t *obj){
int ret=BELLE_SIP_CONTINUE;
/*prevent system to suspend the process until we have finish reading everything from the socket and notified the upper layer*/
channel_begin_recv_background_task(obj);
if (obj->input_stream.state == WAITING_MESSAGE_START)
channel_begin_recv_background_task(obj);
if (obj->simulated_recv_return>0) {
num=belle_sip_channel_recv(obj,obj->input_stream.write_ptr,belle_sip_channel_input_stream_get_buff_length(&obj->input_stream)-1);
......@@ -580,7 +581,8 @@ static int belle_sip_channel_process_read_data(belle_sip_channel_t *obj){
channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
ret=BELLE_SIP_STOP;
}
channel_end_recv_background_task(obj);
if (obj->input_stream.state == WAITING_MESSAGE_START)
channel_end_recv_background_task(obj);
return ret;
}
......@@ -787,13 +789,13 @@ static void channel_on_send_background_task_ended(belle_sip_channel_t *obj){
static void channel_begin_send_background_task(belle_sip_channel_t *obj){
if (obj->bg_task_id==0){
obj->bg_task_id=belle_sip_begin_background_task("belle-sip send channel",(void (*)(void*))channel_on_send_background_task_ended, obj);
if (obj->bg_task_id) belle_sip_message("channel [%p]: starting send background task with id=[%x].",obj,obj->bg_task_id);
if (obj->bg_task_id) belle_sip_message("channel [%p]: starting send background task with id=[%lx].",obj,obj->bg_task_id);
}
}
static void channel_end_send_background_task(belle_sip_channel_t *obj){
if (obj->bg_task_id){
belle_sip_message("channel [%p]: ending send background task with id=[%x].",obj,obj->bg_task_id);
belle_sip_message("channel [%p]: ending send background task with id=[%lx].",obj,obj->bg_task_id);
belle_sip_end_background_task(obj->bg_task_id);
obj->bg_task_id=0;
}
......@@ -807,13 +809,13 @@ static void channel_on_recv_background_task_ended(belle_sip_channel_t *obj){
static void channel_begin_recv_background_task(belle_sip_channel_t *obj){
if (obj->recv_bg_task_id==0){
obj->recv_bg_task_id=belle_sip_begin_background_task("belle-sip recv channel",(void (*)(void*))channel_on_recv_background_task_ended, obj);
if (obj->recv_bg_task_id) belle_sip_message("channel [%p]: starting recv background task with id=[%x].",obj,obj->recv_bg_task_id);
if (obj->recv_bg_task_id) belle_sip_message("channel [%p]: starting recv background task with id=[%lx].",obj,obj->recv_bg_task_id);
}
}
static void channel_end_recv_background_task(belle_sip_channel_t *obj){
if (obj->recv_bg_task_id){
belle_sip_message("channel [%p]: ending recv background task with id=[%x].",obj,obj->recv_bg_task_id);
belle_sip_message("channel [%p]: ending recv background task with id=[%lx].",obj,obj->recv_bg_task_id);
belle_sip_end_background_task(obj->recv_bg_task_id);
obj->recv_bg_task_id=0;
}
......@@ -1330,22 +1332,22 @@ belle_sip_channel_t *belle_sip_channel_find_from_list(belle_sip_list_t *l, int a
#ifdef ANDROID
unsigned int belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
unsigned long belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
return wake_lock_acquire(name);
}
void belle_sip_end_background_task(unsigned int id){
void belle_sip_end_background_task(unsigned long id){
wake_lock_release(id);
}
#elif !TARGET_OS_IPHONE
#elif !TARGET_OS_IPHONE && !defined(__APPLE__)
/*defines stubs*/
unsigned int belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
return 0;
}
void belle_sip_end_background_task(unsigned int id){
void belle_sip_end_background_task(unsigned long id){
return;
}
......
......@@ -25,10 +25,6 @@
#endif
typedef void (*belle_sip_background_task_end_callback_t)(void *);
unsigned int belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data);
void belle_sip_end_background_task(unsigned int id);
#define belle_sip_network_buffer_size 65535
#define belle_sip_send_network_buffer_size 16384
......@@ -118,8 +114,8 @@ struct belle_sip_channel{
belle_sip_source_t *inactivity_timer;
uint64_t last_recv_time;
int simulated_recv_return; /* used to simulate network error. 0= no data (disconnected) >0= do nothing -1= network error*/
unsigned bg_task_id;
unsigned recv_bg_task_id;
unsigned long bg_task_id;
unsigned long recv_bg_task_id;
unsigned char force_close; /* when channel is intentionnaly disconnected, in order to prevent looping notifications*/
unsigned char learnt_ip_port;
unsigned char has_name; /*set when the name of the peer is known, which is not the case of inboud connections*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment