summaryrefslogtreecommitdiff
path: root/res/res_pjsip_session.c
diff options
context:
space:
mode:
Diffstat (limited to 'res/res_pjsip_session.c')
-rw-r--r--res/res_pjsip_session.c104
1 files changed, 88 insertions, 16 deletions
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index 84c343d3c..ce5237717 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -1161,7 +1161,7 @@ static void session_destructor(void *obj)
struct ast_sip_session_delayed_request *delay;
ast_debug(3, "Destroying SIP session with endpoint %s\n",
- ast_sorcery_object_get_id(session->endpoint));
+ session->endpoint ? ast_sorcery_object_get_id(session->endpoint) : "<none>");
while ((supplement = AST_LIST_REMOVE_HEAD(&session->supplements, next))) {
if (supplement->session_destroy) {
@@ -1212,8 +1212,9 @@ static int add_supplements(struct ast_sip_session *session)
static int add_session_media(void *obj, void *arg, int flags)
{
struct sdp_handler_list *handler_list = obj;
- struct ast_sip_session * session = arg;
+ struct ast_sip_session *session = arg;
RAII_VAR(struct ast_sip_session_media *, session_media, NULL, ao2_cleanup);
+
session_media = ao2_alloc(sizeof(*session_media) + strlen(handler_list->stream_type), session_media_dtor);
if (!session_media) {
return CMP_STOP;
@@ -1253,9 +1254,11 @@ struct ast_sip_channel_pvt *ast_sip_channel_pvt_alloc(void *pvt, struct ast_sip_
struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
struct ast_sip_contact *contact, pjsip_inv_session *inv_session)
{
- RAII_VAR(struct ast_sip_session *, session, ao2_alloc(sizeof(*session), session_destructor), ao2_cleanup);
+ RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
struct ast_sip_session_supplement *iter;
int dsp_features = 0;
+
+ session = ao2_alloc(sizeof(*session), session_destructor);
if (!session) {
return NULL;
}
@@ -1296,6 +1299,7 @@ struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
if (dsp_features) {
if (!(session->dsp = ast_dsp_new())) {
+ /* Release the ref held by session->inv_session */
ao2_ref(session, -1);
return NULL;
}
@@ -1304,6 +1308,7 @@ struct ast_sip_session *ast_sip_session_alloc(struct ast_sip_endpoint *endpoint,
}
if (add_supplements(session)) {
+ /* Release the ref held by session->inv_session */
ao2_ref(session, -1);
return NULL;
}
@@ -1688,6 +1693,23 @@ int ast_sip_session_defer_termination(struct ast_sip_session *session)
return res;
}
+/*!
+ * \internal
+ * \brief Stop the defer termination timer if it is still running.
+ * \since 13.5.0
+ *
+ * \param session Which session to stop the timer.
+ *
+ * \return Nothing
+ */
+static void sip_session_defer_termination_stop_timer(struct ast_sip_session *session)
+{
+ if (pj_timer_heap_cancel(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
+ &session->scheduled_termination)) {
+ ao2_ref(session, -1);
+ }
+}
+
void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session)
{
if (!session->defer_terminate) {
@@ -1702,10 +1724,7 @@ void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session)
}
/* Stop the termination timer if it is still running. */
- if (pj_timer_heap_cancel(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
- &session->scheduled_termination)) {
- ao2_ref(session, -1);
- }
+ sip_session_defer_termination_stop_timer(session);
}
struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg)
@@ -2247,25 +2266,39 @@ static void handle_outgoing(struct ast_sip_session *session, pjsip_tx_data *tdat
}
}
-static int session_end(struct ast_sip_session *session)
+static void session_end(struct ast_sip_session *session)
{
struct ast_sip_session_supplement *iter;
/* Stop the scheduled termination */
- if (pj_timer_heap_cancel(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()), &session->scheduled_termination)) {
- ao2_ref(session, -1);
- }
+ sip_session_defer_termination_stop_timer(session);
- /* Session is dead. Let's get rid of the reference to the session */
+ /* Session is dead. Notify the supplements. */
AST_LIST_TRAVERSE(&session->supplements, iter, next) {
if (iter->session_end) {
iter->session_end(session);
}
}
+}
+
+/*!
+ * \internal
+ * \brief Complete ending session activities.
+ * \since 13.5.0
+ *
+ * \param vsession Which session to complete stopping.
+ *
+ * \retval 0 on success.
+ * \retval -1 on error.
+ */
+static int session_end_completion(void *vsession)
+{
+ struct ast_sip_session *session = vsession;
- session->inv_session->mod_data[session_module.id] = NULL;
ast_sip_dialog_set_serializer(session->inv_session->dlg, NULL);
ast_sip_dialog_set_endpoint(session->inv_session->dlg, NULL);
+
+ /* Now we can release the ref that was held by session->inv_session */
ao2_cleanup(session);
return 0;
}
@@ -2372,9 +2405,7 @@ static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_trans
print_debug_details(inv, tsx, e);
if (!session) {
- /* Transaction likely timed out after the call was hung up. Just
- * ignore such transaction changes
- */
+ /* The session has ended. Ignore the transaction change. */
return;
}
switch (e->body.tsx_state.type) {
@@ -2455,7 +2486,48 @@ static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_trans
}
break;
case PJSIP_EVENT_TRANSPORT_ERROR:
+ /*
+ * Clear the module data now to block session_inv_on_state_changed()
+ * from calling session_end() if it hasn't already done so.
+ */
+ inv->mod_data[session_module.id] = NULL;
+
+ if (inv->state != PJSIP_INV_STATE_DISCONNECTED) {
+ session_end(session);
+ }
+
+ /*
+ * Pass the session ref held by session->inv_session to
+ * session_end_completion().
+ */
+ session_end_completion(session);
+ return;
case PJSIP_EVENT_TIMER:
+ /*
+ * The timer event is run by the pjsip monitor thread and not
+ * by the session serializer.
+ */
+ if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
+ /*
+ * We are locking because ast_sip_dialog_get_session() needs
+ * the dialog locked to get the session by other threads.
+ */
+ pjsip_dlg_inc_lock(inv->dlg);
+ session = inv->mod_data[session_module.id];
+ inv->mod_data[session_module.id] = NULL;
+ pjsip_dlg_dec_lock(inv->dlg);
+
+ /*
+ * Pass the session ref held by session->inv_session to
+ * session_end_completion().
+ */
+ if (ast_sip_push_task(session->serializer, session_end_completion, session)) {
+ /* Do it anyway even though this is not the right thread. */
+ session_end_completion(session);
+ }
+ return;
+ }
+ break;
case PJSIP_EVENT_USER:
case PJSIP_EVENT_UNKNOWN:
case PJSIP_EVENT_TSX_STATE: