Skip to content

Commit

Permalink
Fixed deadlock between stream and ICE (pjsip#3801)
Browse files Browse the repository at this point in the history
  • Loading branch information
sauwming authored and xhit committed Dec 14, 2023
1 parent 3f7cb0e commit 455c73a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 7 additions & 5 deletions pjmedia/src/pjmedia/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,10 +1087,11 @@ static pj_status_t send_rtcp(pjmedia_stream *stream,
pj_status_t status;

/* We need to prevent data race since there is only a single instance
* of rtcp packet buffer. Let's just use the JB mutex for this instead
* of creating a separate lock.
* of rtcp packet buffer. And to avoid deadlock with media transport,
* we use the transport's group lock.
*/
pj_mutex_lock(stream->jb_mutex);
if (stream->transport->grp_lock)
pj_grp_lock_acquire(stream->transport->grp_lock);

/* Build RTCP RR/SR packet */
pjmedia_rtcp_build_rtcp(&stream->rtcp, &sr_rr_pkt, &len);
Expand Down Expand Up @@ -1211,7 +1212,8 @@ static pj_status_t send_rtcp(pjmedia_stream *stream,
}
}

pj_mutex_unlock(stream->jb_mutex);
if (stream->transport->grp_lock)
pj_grp_lock_release(stream->transport->grp_lock);

return status;
}
Expand Down Expand Up @@ -3068,7 +3070,7 @@ PJ_DEF(pj_status_t) pjmedia_stream_destroy( pjmedia_stream *stream )
PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL);

/* Send RTCP BYE (also SDES & XR) */
if (stream->transport && stream->jb_mutex && !stream->rtcp_sdes_bye_disabled) {
if (stream->transport && !stream->rtcp_sdes_bye_disabled) {
#if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0)
send_rtcp(stream, PJ_TRUE, PJ_TRUE, stream->rtcp.xr_enabled, PJ_FALSE);
#else
Expand Down
12 changes: 9 additions & 3 deletions pjmedia/src/pjmedia/vid_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,12 @@ static pj_status_t send_rtcp(pjmedia_vid_stream *stream,
int len, max_len;
pj_status_t status;

pj_grp_lock_acquire( stream->grp_lock );

/* To avoid deadlock with media transport, we use the transport's
* group lock.
*/
if (stream->transport->grp_lock)
pj_grp_lock_acquire( stream->transport->grp_lock );

/* Build RTCP RR/SR packet */
pjmedia_rtcp_build_rtcp(&stream->rtcp, &sr_rr_pkt, &len);
Expand Down Expand Up @@ -667,7 +672,8 @@ static pj_status_t send_rtcp(pjmedia_vid_stream *stream,
}
}

pj_grp_lock_release( stream->grp_lock );
if (stream->transport->grp_lock)
pj_grp_lock_release( stream->transport->grp_lock );

return status;
}
Expand Down Expand Up @@ -2191,7 +2197,7 @@ PJ_DEF(pj_status_t) pjmedia_vid_stream_destroy( pjmedia_vid_stream *stream )
pjmedia_event_unsubscribe(NULL, &stream_event_cb, stream, &stream->rtcp);

/* Send RTCP BYE (also SDES) */
if (stream->transport && stream->grp_lock && !stream->rtcp_sdes_bye_disabled) {
if (stream->transport && !stream->rtcp_sdes_bye_disabled) {
send_rtcp(stream, PJ_TRUE, PJ_TRUE, PJ_FALSE, PJ_FALSE);
}

Expand Down

0 comments on commit 455c73a

Please sign in to comment.