Skip to content

Commit

Permalink
core: refactor events
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed Aug 6, 2024
1 parent dd1ecaa commit 0c78103
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 107 deletions.
12 changes: 7 additions & 5 deletions modules/menu/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,17 +652,19 @@ static void process_module_event(struct call *call, const char *prm)
}


static void ua_event_handler(struct ua *ua, enum ua_event ev,
struct call *call, const char *prm, void *arg)
static void event_handler(enum ua_event ev, struct bevent *event, void *arg)
{
struct call *call2 = NULL;
struct account *acc = ua_account(ua);
int32_t adelay = -1;
bool incall;
enum sdp_dir ardir, vrdir;
uint32_t count;
struct pl val;
char * uri;
const char *prm = bevent_get_text(event);
struct call *call = bevent_get_call(event);
struct ua *ua = bevent_get_ua(event);
struct account *acc = ua_account(bevent_get_ua(event));
int err;
(void)arg;

Expand Down Expand Up @@ -1179,7 +1181,7 @@ static int module_init(void)
if (err)
return err;

err = uag_event_register(ua_event_handler, NULL);
err = bevent_register(event_handler, NULL);
if (err)
return err;

Expand All @@ -1199,7 +1201,7 @@ static int module_close(void)

message_unlisten(baresip_message(), message_handler);

uag_event_unregister(ua_event_handler);
bevent_unregister(event_handler);
static_menu_unregister();
dial_menu_unregister();
dynamic_menu_unregister();
Expand Down
60 changes: 48 additions & 12 deletions src/bevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ static int add_rtcp_stats(struct odict *od_parent, const struct rtcp_stats *rs)
* @param call Call object (optional)
* @param prm Event parameters
*
* @deprecated Use odict_encode_event() instead
*
* @return 0 if success, otherwise errorcode
*/
int event_encode_dict(struct odict *od, struct ua *ua, enum ua_event ev,
Expand Down Expand Up @@ -570,6 +572,8 @@ int event_add_au_jb_stat(struct odict *od_parent, const struct call *call)
* @param h Event handler
* @param arg Handler argument
*
* @deprecated Use bevent_register()
*
* @return 0 if success, otherwise errorcode
*/
int uag_event_register(ua_event_h *h, void *arg)
Expand All @@ -579,6 +583,8 @@ int uag_event_register(ua_event_h *h, void *arg)
if (!h)
return EINVAL;

warning("Used deprecated uag_event_register(). "
"Use bevent_register() instead!\n");
uag_event_unregister(h);

eh = mem_zalloc(sizeof(*eh), eh_destructor);
Expand All @@ -598,6 +604,8 @@ int uag_event_register(ua_event_h *h, void *arg)
* Unregister a User-Agent event handler
*
* @param h Event handler
*
* @deprecated Use bevent_unregister()
*/
void uag_event_unregister(ua_event_h *h)
{
Expand Down Expand Up @@ -666,6 +674,25 @@ void bevent_unregister(bevent_h *eh)
}


static void ua_event_private(struct ua *ua, enum ua_event ev,
struct call *call, const char *txt)
{
/* send event to all clients */
struct le *le = ehl.head;
while (le) {
struct ua_eh *eh = le->data;
le = le->next;

if (call_is_evstop(call)) {
call_set_evstop(call, false);
break;
}

eh->h(ua, ev, call, txt, eh->arg);
}
}


/**
* Send a User-Agent event to all UA event handlers
*
Expand All @@ -674,31 +701,32 @@ void bevent_unregister(bevent_h *eh)
* @param call Call object (optional)
* @param fmt Formatted arguments
* @param ... Variable arguments
*
* @deprecated Use one of the event_xxx_emit() functions
*/
void ua_event(struct ua *ua, enum ua_event ev, struct call *call,
const char *fmt, ...)
{
struct le *le;
char buf[256];
va_list ap;

va_start(ap, fmt);
(void)re_vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);

/* send event to all clients */
le = ehl.head;
while (le) {
struct ua_eh *eh = le->data;
le = le->next;
ua_event_private(ua, ev, call, buf);

if (call_is_evstop(call)) {
call_set_evstop(call, false);
break;
}
warning("Used deprecated ua_event() for %s. "
"Use one of event_xxx_emit() instead!\n",
uag_event_str(ev));
struct bevent event = {ev, buf, 0, false, { 0 } };

eh->h(ua, ev, call, buf, eh->arg);
}
if (bevent_class(ev) == BEVENT_CLASS_CALL)
event.u.call = call;
else if (bevent_class(ev) == BEVENT_CLASS_UA)
event.u.ua = ua;

(void)bevent_emit_base(&event);
}


Expand Down Expand Up @@ -747,6 +775,9 @@ void module_event(const char *module, const char *event, struct ua *ua,
eh->h(ua, UA_EVENT_MODULE, call, buf, eh->arg);
}

struct bevent bevent = {UA_EVENT_MODULE, buf, 0, false, { 0 } };
bevent_emit_base(&bevent);

out:
mem_deref(buf);
}
Expand All @@ -771,6 +802,8 @@ static void bevent_emit_base(struct bevent *event)
static int bevent_emit(struct bevent *event, const char *fmt, va_list ap)
{
char *buf;
struct call *call = bevent_get_call(event);
struct ua *ua = bevent_get_ua(event);
int err;

if (!fmt)
Expand All @@ -788,9 +821,12 @@ static int bevent_emit(struct bevent *event, const char *fmt, va_list ap)
goto out;
}

/* backwards compatibility */
if (event->stop)
goto out;

ua_event_private(ua, event->ev, call, event->txt);

out:
mem_deref(buf);
return err;
Expand Down
49 changes: 25 additions & 24 deletions src/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct call {
bool use_video;
bool use_rtp;
char *user_data; /**< User data related to the call */
bool evstop; /**< UA events stopped flag */
bool evstop; /**< UA events stopped flag, @deprecated */
};


Expand Down Expand Up @@ -313,8 +313,8 @@ static int update_media(struct call *call)
{
debug("call: update media\n");

ua_event(call->ua, UA_EVENT_CALL_REMOTE_SDP, call,
call->got_offer ? "offer" : "answer");
bevent_call_emit(UA_EVENT_CALL_REMOTE_SDP, call,
call->got_offer ? "offer" : "answer");

return call_update_media(call);
}
Expand Down Expand Up @@ -384,8 +384,8 @@ static void audio_level_handler(bool tx, double lvl, void *arg)
struct call *call = arg;
MAGIC_CHECK(call);

ua_event(call->ua, tx ? UA_EVENT_VU_TX : UA_EVENT_VU_RX,
call, "%.2f", lvl);
bevent_call_emit(tx ? UA_EVENT_VU_TX : UA_EVENT_VU_RX, call,
"%.2f", lvl);
}


Expand All @@ -397,14 +397,15 @@ static void audio_error_handler(int err, const char *str, void *arg)
if (err) {
warning("call: audio device error: %m (%s)\n", err, str);

ua_event(call->ua, UA_EVENT_AUDIO_ERROR, call, "%d,%s",
err, str);
bevent_call_emit(UA_EVENT_AUDIO_ERROR, call,
"%d,%s", err, str);

call_stream_stop(call);
call_event_handler(call, CALL_EVENT_CLOSED,
"%s", str);
}
else
ua_event(call->ua, UA_EVENT_END_OF_FILE, call, "");
bevent_call_emit(UA_EVENT_END_OF_FILE, call, "");
}


Expand Down Expand Up @@ -525,8 +526,8 @@ static void stream_rtpestab_handler(struct stream *strm, void *arg)
struct call *call = arg;
MAGIC_CHECK(call);

ua_event(call->ua, UA_EVENT_CALL_RTPESTAB, call,
"%s", sdp_media_name(stream_sdpmedia(strm)));
bevent_call_emit(UA_EVENT_CALL_RTPESTAB, call,
"%s", sdp_media_name(stream_sdpmedia(strm)));
}


Expand All @@ -543,13 +544,13 @@ static void stream_rtcp_handler(struct stream *strm,
if (call->config_avt.rtp_stats)
call_set_xrtpstat(call);

ua_event(call->ua, UA_EVENT_CALL_RTCP, call,
"%s", sdp_media_name(stream_sdpmedia(strm)));
bevent_call_emit(UA_EVENT_CALL_RTCP, call,
"%s", sdp_media_name(stream_sdpmedia(strm)));
break;

case RTCP_APP:
ua_event(call->ua, UA_EVENT_CALL_RTCP, call,
"%s", sdp_media_name(stream_sdpmedia(strm)));
bevent_call_emit(UA_EVENT_CALL_RTCP, call,
"%s", sdp_media_name(stream_sdpmedia(strm)));
break;
}
}
Expand Down Expand Up @@ -1092,8 +1093,8 @@ int call_modify(struct call *call)
if (call_refresh_allowed(call)) {
err = call_sdp_get(call, &desc, true);
if (!err) {
ua_event(call->ua, UA_EVENT_CALL_LOCAL_SDP, call,
"offer");
bevent_call_emit(UA_EVENT_CALL_LOCAL_SDP, call,
"offer");

err = sipsess_modify(call->sess, desc);
if (err)
Expand Down Expand Up @@ -1226,7 +1227,7 @@ int call_progress_dir(struct call *call, enum sdp_dir adir, enum sdp_dir vdir)
goto out;

if (call->got_offer) {
ua_event(call->ua, UA_EVENT_CALL_LOCAL_SDP, call, "answer");
bevent_call_emit(UA_EVENT_CALL_LOCAL_SDP, call, "answer");
err = call_update_media(call);
}

Expand Down Expand Up @@ -1294,8 +1295,8 @@ int call_answer(struct call *call, uint16_t scode, enum vidmode vmode)
if (call->got_offer)
err = call_apply_sdp(call);

ua_event(call->ua, UA_EVENT_CALL_LOCAL_SDP, call,
"%s", !call->got_offer ? "offer" : "answer");
bevent_call_emit(UA_EVENT_CALL_LOCAL_SDP, call,
"%s", !call->got_offer ? "offer" : "answer");

err = sdp_encode(&desc, call->sdp, !call->got_offer);
if (err)
Expand Down Expand Up @@ -1749,9 +1750,9 @@ static int sipsess_offer_handler(struct mbuf **descp,
}

if (aurx && !(sdp_media_dir(m) & SDP_SENDONLY))
ua_event(call->ua, UA_EVENT_CALL_HOLD, call, "");
bevent_call_emit(UA_EVENT_CALL_HOLD, call, "");
else if (!aurx && sdp_media_dir(m) & SDP_SENDONLY)
ua_event(call->ua, UA_EVENT_CALL_RESUME, call, "");
bevent_call_emit(UA_EVENT_CALL_RESUME, call, "");

err = update_media(call);
if (err) {
Expand Down Expand Up @@ -2337,8 +2338,8 @@ static void redirect_handler(const struct sip_msg *msg, const char *uri,
struct call *call = arg;

info("call: redirect to %s\n", uri);
ua_event(call->ua, UA_EVENT_CALL_REDIRECT, call,
"%d,%s", msg->scode, uri);
bevent_call_emit(UA_EVENT_CALL_REDIRECT, call,
"%d,%s", msg->scode, uri);
return;
}

Expand Down Expand Up @@ -2434,7 +2435,7 @@ static int send_invite(struct call *call)
/* save call setup timer */
call->time_conn = time(NULL);

ua_event(call->ua, UA_EVENT_CALL_LOCAL_SDP, call, "offer");
bevent_call_emit(UA_EVENT_CALL_LOCAL_SDP, call, "offer");

return 0;
}
Expand Down
10 changes: 5 additions & 5 deletions src/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static void register_handler(int err, const struct sip_msg *msg, void *arg)

reg->scode = 999;

ua_event(reg->ua, evfail, NULL, "%m", err);
bevent_ua_emit(evfail, reg->ua, "%m", err);
return;
}

Expand Down Expand Up @@ -162,8 +162,8 @@ static void register_handler(int err, const struct sip_msg *msg, void *arg)
}
}

ua_event(reg->ua, evok, NULL, "%u %r",
msg->scode, &msg->reason);
bevent_ua_emit(evok, reg->ua,
"%u %r", msg->scode, &msg->reason);
}
else if (msg->scode >= 300) {

Expand All @@ -172,8 +172,8 @@ static void register_handler(int err, const struct sip_msg *msg, void *arg)

reg->scode = msg->scode;

ua_event(reg->ua, evfail, NULL, "%u %r",
msg->scode, &msg->reason);
bevent_ua_emit(evfail, reg->ua,
"%u %r", msg->scode, &msg->reason);
}
}

Expand Down
Loading

0 comments on commit 0c78103

Please sign in to comment.