From 69f56f4dab1c9b7b76faf0d41a065ba22d0f1577 Mon Sep 17 00:00:00 2001 From: nicolasduteil Date: Tue, 11 May 2021 14:36:22 +0200 Subject: [PATCH] feat: support for SUBSCRIBE expiry (Expires header) in sip plugin (#2661) --- plugins/janus_sip.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/plugins/janus_sip.c b/plugins/janus_sip.c index afd4664053..d3013ee627 100644 --- a/plugins/janus_sip.c +++ b/plugins/janus_sip.c @@ -122,8 +122,9 @@ "headers" : "", "contact_params" : "", "incoming_header_prefixes" : "", - "refresh" : ", - "master_id" : + "refresh" : "", + "master_id" : "", + "register_ttl" : "" } \endverbatim * @@ -475,7 +476,8 @@ "request" : "subscribe", "event" : "", "accept" : "", - "to" : "" + "to" : "", + "subscribe_ttl" : "" } \endverbatim * @@ -747,12 +749,14 @@ static struct janus_json_parameter register_parameters[] = { {"contact_params", JSON_OBJECT, 0}, {"master_id", JANUS_JSON_INTEGER, 0}, {"refresh", JANUS_JSON_BOOL, 0}, - {"incoming_header_prefixes", JSON_ARRAY, 0} + {"incoming_header_prefixes", JSON_ARRAY, 0}, + {"register_ttl", JANUS_JSON_INTEGER, 0} }; static struct janus_json_parameter subscribe_parameters[] = { {"to", JSON_STRING, 0}, {"event", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}, - {"accept", JSON_STRING, 0} + {"accept", JSON_STRING, 0}, + {"subscribe_ttl", JANUS_JSON_INTEGER, 0} }; static struct janus_json_parameter proxy_parameters[] = { {"proxy", JSON_STRING, 0}, @@ -824,6 +828,8 @@ static gboolean behind_nat = FALSE; static char *user_agent; #define JANUS_DEFAULT_REGISTER_TTL 3600 static int register_ttl = JANUS_DEFAULT_REGISTER_TTL; +#define JANUS_DEFAULT_SUBSCRIBE_TTL 3600 +static int subscribe_ttl = JANUS_DEFAULT_SUBSCRIBE_TTL; static uint16_t rtp_range_min = 10000; static uint16_t rtp_range_max = 60000; static int dscp_audio_rtp = 0; @@ -3162,6 +3168,17 @@ static void *janus_sip_handler(void *data) { to = session->account.identity; const char *event_type = json_string_value(json_object_get(root, "event")); const char *accept = json_string_value(json_object_get(root, "accept")); + + /* TTL */ + int ttl = subscribe_ttl; + json_t *sub_ttl = json_object_get(root, "subscribe_ttl"); + if(sub_ttl && json_is_integer(sub_ttl)) + ttl = json_integer_value(sub_ttl); + if(ttl <= 0) + ttl = JANUS_DEFAULT_SUBSCRIBE_TTL; + char ttl_text[20]; + g_snprintf(ttl_text, sizeof(ttl_text), "%d", ttl); + /* Do we have a handle for this subscription already? */ janus_mutex_lock(&session->stack->smutex); nua_handle_t *nh = NULL; @@ -3209,6 +3226,7 @@ static void *janus_sip_handler(void *data) { SIPTAG_TO_STR(to), SIPTAG_EVENT_STR(event_type), SIPTAG_ACCEPT_STR(accept), + SIPTAG_EXPIRES_STR(ttl_text), NUTAG_PROXY(session->helper && session->master ? session->master->account.outbound_proxy : session->account.outbound_proxy), TAG_END()); @@ -5778,6 +5796,8 @@ void janus_sip_sofia_callback(nua_event_t event, int status, char const *phrase, json_t *headers = janus_sip_get_incoming_headers(sip, session); json_object_set_new(result, "headers", headers); } + if (sip->sip_expires) + json_object_set_new(result, "expires", json_integer(sip->sip_expires->ex_delta)); json_object_set_new(result, "reason", json_string(phrase ? phrase : "")); json_object_set_new(event, "result", result); int ret = gateway->push_event(session->handle, &janus_sip_plugin, session->transaction, event, NULL);