From 08fdd73272537e7e81ee6232a6da5544f6da2ffb Mon Sep 17 00:00:00 2001 From: Simon Morlat Date: Tue, 18 Jun 2013 12:05:31 +0200 Subject: [PATCH] LinphoneEvent improvements --- coreapi/callbacks.c | 9 ++------ coreapi/event.c | 51 ++++++++++++++++++++++++++++++++++----------- coreapi/event.h | 25 ++++++++++++++++++++-- coreapi/private.h | 5 ++--- mediastreamer2 | 2 +- 5 files changed, 67 insertions(+), 25 deletions(-) diff --git a/coreapi/callbacks.c b/coreapi/callbacks.c index 1bd9bbc59..f8ef9dfba 100644 --- a/coreapi/callbacks.c +++ b/coreapi/callbacks.c @@ -1017,7 +1017,6 @@ static void subscribe_response(SalOp *op, SalSubscribeStatus status, SalError er }else{ linphone_event_set_reason(lev, linphone_reason_from_sal(reason)); linphone_event_set_state(lev,LinphoneSubscriptionError); - linphone_event_destroy(lev); } } @@ -1028,17 +1027,14 @@ static void notify(SalOp *op, SalSubscribeStatus st, const char *eventname, cons if (lev==NULL) { /*out of subscribe notify */ - lev=linphone_event_new_with_op(lc,op,LinphoneSubscriptionOutgoing); + lev=linphone_event_new_with_op(lc,op,LinphoneSubscriptionOutgoing,eventname); } if (lc->vtable.notify_received){ lc->vtable.notify_received(lc,lev,eventname,linphone_content_from_sal_body(&content,body)); } if (st!=SalSubscribeNone){ linphone_event_set_state(lev,linphone_subscription_state_from_sal(st)); - if (st==SalSubscribeTerminated) - linphone_event_destroy(lev); } - } static void subscribe_received(SalOp *op, const char *eventname, const SalBody *body){ @@ -1046,7 +1042,7 @@ static void subscribe_received(SalOp *op, const char *eventname, const SalBody * LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op)); if (lev==NULL) { - lev=linphone_event_new_with_op(lc,op,LinphoneSubscriptionIncoming); + lev=linphone_event_new_with_op(lc,op,LinphoneSubscriptionIncoming,eventname); linphone_event_set_state(lev,LinphoneSubscriptionIncomingReceived); }else{ /*subscribe refresh, unhandled*/ @@ -1058,7 +1054,6 @@ static void subscribe_closed(SalOp *op){ LinphoneEvent *lev=(LinphoneEvent*)sal_op_get_user_pointer(op); linphone_event_set_state(lev,LinphoneSubscriptionTerminated); - linphone_event_destroy(lev); } SalCallbacks linphone_sal_callbacks={ diff --git a/coreapi/event.c b/coreapi/event.c index 3d350b578..12bccdf29 100644 --- a/coreapi/event.c +++ b/coreapi/event.c @@ -27,6 +27,8 @@ struct _LinphoneEvent{ LinphoneSubscriptionState state; LinphoneReason reason; void *userdata; + int refcnt; + char *name; }; LinphoneSubscriptionState linphone_subscription_state_from_sal(SalSubscribeStatus ss){ @@ -39,21 +41,23 @@ LinphoneSubscriptionState linphone_subscription_state_from_sal(SalSubscribeStatu return LinphoneSubscriptionNone; } -LinphoneEvent *linphone_event_new(LinphoneCore *lc, LinphoneSubscriptionDir dir){ +static LinphoneEvent * linphone_event_new_base(LinphoneCore *lc, LinphoneSubscriptionDir dir, const char *name, SalOp *op){ LinphoneEvent *lev=ms_new0(LinphoneEvent,1); lev->lc=lc; lev->dir=dir; - lev->op=sal_op_new(lc->sal); + lev->op=op; + lev->refcnt=1; sal_op_set_user_pointer(lev->op,lev); return lev; } -LinphoneEvent *linphone_event_new_with_op(LinphoneCore *lc, SalOp *op, LinphoneSubscriptionDir dir){ - LinphoneEvent *lev=ms_new0(LinphoneEvent,1); - lev->lc=lc; - lev->dir=LinphoneSubscriptionIncoming; - lev->op=op; - sal_op_set_user_pointer(lev->op,lev); +LinphoneEvent *linphone_event_new(LinphoneCore *lc, LinphoneSubscriptionDir dir, const char *name){ + LinphoneEvent *lev=linphone_event_new_base(lc, dir, name, sal_op_new(lc->sal)); + return lev; +} + +LinphoneEvent *linphone_event_new_with_op(LinphoneCore *lc, SalOp *op, LinphoneSubscriptionDir dir, const char *name){ + LinphoneEvent *lev=linphone_event_new_base(lc, dir, name, op); return lev; } @@ -64,6 +68,9 @@ void linphone_event_set_state(LinphoneEvent *lev, LinphoneSubscriptionState stat if (lc->vtable.subscription_state_changed){ lc->vtable.subscription_state_changed(lev->lc,lev,state); } + if (state==LinphoneSubscriptionError || state==LinphoneSubscriptionTerminated){ + linphone_event_unref(lev); + } } } @@ -76,7 +83,7 @@ LinphoneReason linphone_event_get_reason(const LinphoneEvent *lev){ } LinphoneEvent *linphone_core_subscribe(LinphoneCore *lc, const LinphoneAddress *resource, const char *event, int expires, const LinphoneContent *body){ - LinphoneEvent *lev=linphone_event_new(lc, LinphoneSubscriptionOutgoing); + LinphoneEvent *lev=linphone_event_new(lc, LinphoneSubscriptionOutgoing, event); SalBody salbody; linphone_configure_op(lc,lev->op,resource,NULL,TRUE); sal_subscribe(lev->op,NULL,NULL,event,expires,sal_body_from_content(&salbody,body)); @@ -137,7 +144,7 @@ int linphone_event_notify(LinphoneEvent *lev, const LinphoneContent *body){ LinphoneEvent *linphone_core_publish(LinphoneCore *lc, const LinphoneAddress *resource, const char *event, int expires, const LinphoneContent *body){ SalBody salbody; - LinphoneEvent *lev=linphone_event_new(lc,LinphoneSubscriptionInvalidDir); + LinphoneEvent *lev=linphone_event_new(lc,LinphoneSubscriptionInvalidDir, event); linphone_configure_op(lc,lev->op,resource,NULL,FALSE); sal_publish(lev->op,NULL,NULL,event,expires,sal_body_from_content(&salbody,body)); return lev; @@ -167,15 +174,35 @@ void linphone_event_terminate(LinphoneEvent *lev){ if (lev->state!=LinphoneSubscriptionNone){ linphone_event_set_state(lev,LinphoneSubscriptionTerminated); } - linphone_event_destroy(lev); } -void linphone_event_destroy(LinphoneEvent *lev){ + +LinphoneEvent *linphone_event_ref(LinphoneEvent *lev){ + lev->refcnt++; + return lev; +} + +static void linphone_event_destroy(LinphoneEvent *lev){ if (lev->op) sal_op_release(lev->op); + ms_free(lev->name); ms_free(lev); } +void linphone_event_unref(LinphoneEvent *lev){ + lev->refcnt--; + if (lev->refcnt==0) linphone_event_destroy(lev); +} + LinphoneSubscriptionDir linphone_event_get_dir(LinphoneEvent *lev){ return lev->dir; } + +LinphoneSubscriptionState linphone_event_get_subscription_state(const LinphoneEvent *lev){ + return lev->state; +} + +const char *linphone_event_get_name(const LinphoneEvent *lev){ + return lev->name; +} + diff --git a/coreapi/event.h b/coreapi/event.h index aa87a42cc..d4a2c9de7 100644 --- a/coreapi/event.h +++ b/coreapi/event.h @@ -59,9 +59,8 @@ typedef enum _LinphoneSubscriptionState LinphoneSubscriptionState; /** * Callback prototype for notifying the application about notification received from the network. - * If the notification is not associated with any outgoing subscription, then the LinphoneEvent argument is NULL. **/ -typedef void (*LinphoneEventIncomingNotifyCb)(LinphoneCore *lc, LinphoneEvent *lev, const char *event_name, const LinphoneContent *body); +typedef void (*LinphoneEventIncomingNotifyCb)(LinphoneCore *lc, LinphoneEvent *lev, const char *notified_event, const LinphoneContent *body); /** * Callback prototype for notifying the application about changes of subscription states, including arrival of new subscriptions. @@ -130,8 +129,14 @@ int linphone_event_update_publish(LinphoneEvent *lev, const LinphoneContent *bod **/ LinphoneReason linphone_event_get_reason(const LinphoneEvent *lev); +/** + * Get subscription state. If the event object was not created by a subscription mechanism, #LinphoneSubscriptionNone is returned. +**/ +LinphoneSubscriptionState linphone_event_get_subscription_state(const LinphoneEvent *lev); + /** * Get subscription direction. + * If the object wasn't created by a subscription mechanism, #LinphoneSubscriptionInvalidDir is returned. **/ LinphoneSubscriptionDir linphone_event_get_dir(LinphoneEvent *lev); @@ -150,6 +155,22 @@ void *linphone_event_get_user_data(const LinphoneEvent *ev); **/ void linphone_event_terminate(LinphoneEvent *lev); + +/** + * Increase reference count. +**/ +LinphoneEvent *linphone_event_ref(LinphoneEvent *lev); + +/** + * Decrease reference count. +**/ +void linphone_event_unref(LinphoneEvent *lev); + +/** + * Get the name of the event as specified in the event package RFC. +**/ +const char *linphone_event_get_name(const LinphoneEvent *lev); + /** * @} **/ diff --git a/coreapi/private.h b/coreapi/private.h index 383faa2f0..f64a59c86 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -748,9 +748,8 @@ LinphoneContent *linphone_content_copy_from_sal_body(LinphoneContent *obj, const SalBody *sal_body_from_content(SalBody *body, const LinphoneContent *lc); SalReason linphone_reason_to_sal(LinphoneReason reason); LinphoneReason linphone_reason_from_sal(SalReason reason); -LinphoneEvent *linphone_event_new(LinphoneCore *lc, LinphoneSubscriptionDir dir); -LinphoneEvent *linphone_event_new_with_op(LinphoneCore *lc, SalOp *op, LinphoneSubscriptionDir dir); -void linphone_event_destroy(LinphoneEvent *lev); +LinphoneEvent *linphone_event_new(LinphoneCore *lc, LinphoneSubscriptionDir dir, const char *name); +LinphoneEvent *linphone_event_new_with_op(LinphoneCore *lc, SalOp *op, LinphoneSubscriptionDir dir, const char *name); void linphone_event_set_state(LinphoneEvent *lev, LinphoneSubscriptionState state); void linphone_event_set_reason(LinphoneEvent *lev, LinphoneReason reason); LinphoneSubscriptionState linphone_subscription_state_from_sal(SalSubscribeStatus ss); diff --git a/mediastreamer2 b/mediastreamer2 index 7564a5073..318a21921 160000 --- a/mediastreamer2 +++ b/mediastreamer2 @@ -1 +1 @@ -Subproject commit 7564a50732292a017363bc39bba63e225c8b93c9 +Subproject commit 318a2192108cdfb4946fb9eb73c35f8a055d121f