Skip to content

Commit

Permalink
Merging with master to pass CI test
Browse files Browse the repository at this point in the history
  • Loading branch information
sauwming committed Oct 13, 2023
2 parents e7069f7 + 5c5b328 commit fdccdfa
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 43 deletions.
2 changes: 2 additions & 0 deletions pjmedia/src/pjmedia/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ PJ_DEF(void) pjmedia_event_mgr_destroy(pjmedia_event_mgr *mgr)
mgr->is_quitting = PJ_TRUE;
pj_sem_post(mgr->sem);
pj_thread_join(mgr->thread);
pj_thread_destroy(mgr->thread);
mgr->thread = NULL;
}

if (mgr->sem) {
Expand Down
12 changes: 11 additions & 1 deletion pjmedia/src/pjmedia/sdp_neg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1573,18 +1573,28 @@ static pj_status_t create_answer( pj_pool_t *pool,
const pjmedia_sdp_media *om; /* offer */
const pjmedia_sdp_media *im; /* initial media */
pjmedia_sdp_media *am = NULL; /* answer/result */
pj_uint32_t om_tp;
unsigned j;

om = offer->media[i];

om_tp = pjmedia_sdp_transport_get_proto(&om->desc.transport);
PJMEDIA_TP_PROTO_TRIM_FLAG(om_tp, PJMEDIA_TP_PROFILE_RTCP_FB);

/* Find media description in our initial capability that matches
* the media type and transport type of offer's media, has
* matching codec, and has not been used to answer other offer.
*/
for (im=NULL, j=0; j<initial->media_count; ++j) {
pj_uint32_t im_tp;

im = initial->media[j];

im_tp = pjmedia_sdp_transport_get_proto(&im->desc.transport);
PJMEDIA_TP_PROTO_TRIM_FLAG(im_tp, PJMEDIA_TP_PROFILE_RTCP_FB);

if (pj_strcmp(&om->desc.media, &im->desc.media)==0 &&
pj_strcmp(&om->desc.transport, &im->desc.transport)==0 &&
om_tp == im_tp &&
media_used[j] == 0)
{
pj_status_t status2;
Expand Down
108 changes: 80 additions & 28 deletions pjmedia/src/pjmedia/transport_srtp_dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ static void DTLS_LOCK(dtls_srtp *ds) {
pj_lock_acquire(ds->ossl_lock);
}

static pj_status_t DTLS_TRY_LOCK(dtls_srtp *ds) {
if (ds->base.grp_lock)
return pj_grp_lock_tryacquire(ds->base.grp_lock);
else
return pj_lock_tryacquire(ds->ossl_lock);
}

static void DTLS_UNLOCK(dtls_srtp *ds) {
if (ds->base.grp_lock)
Expand Down Expand Up @@ -894,10 +900,27 @@ static void clock_cb(const pj_timestamp *ts, void *user_data)
dtls_srtp_channel *ds_ch = (dtls_srtp_channel*)user_data;
dtls_srtp *ds = ds_ch->dtls_srtp;
unsigned idx = ds_ch->channel;
pj_status_t status;

PJ_UNUSED_ARG(ts);

DTLS_LOCK(ds);
while (1) {
/* Check if we should quit before trying to acquire the lock. */
if (ds->nego_completed[idx])
return;

/* To avoid deadlock, we must use TRY_LOCK here. */
status = DTLS_TRY_LOCK(ds);
if (status == PJ_SUCCESS)
break;

/* Acquiring lock failed, check if we have been signaled to quit. */
if (ds->nego_completed[idx])
return;

pj_thread_sleep(20);
}


if (!ds->ossl_ssl[idx]) {
DTLS_UNLOCK(ds);
Expand Down Expand Up @@ -982,6 +1005,7 @@ static pj_status_t ssl_handshake_channel(dtls_srtp *ds, unsigned idx)

on_return:
if (status != PJ_SUCCESS) {
ds->nego_completed[idx] = PJ_TRUE;
if (ds->clock[idx])
pjmedia_clock_stop(ds->clock[idx]);
}
Expand Down Expand Up @@ -1265,32 +1289,50 @@ static pj_status_t dtls_on_recv(pjmedia_transport *tp, unsigned idx,
*/

/* Check remote address info, reattach member tp if changed */
if (idx == RTP_CHANNEL && !ds->use_ice && !ds->nego_completed[idx]) {
if (!ds->use_ice && !ds->nego_completed[idx]) {
pjmedia_transport_info info;
pj_bool_t reattach_tp = PJ_FALSE;

pjmedia_transport_get_info(ds->srtp->member_tp, &info);
if (pj_sockaddr_cmp(&ds->rem_addr, &info.src_rtp_name)) {

if (idx == RTP_CHANNEL &&
pj_sockaddr_cmp(&ds->rem_addr, &info.src_rtp_name))
{
pj_sockaddr_cp(&ds->rem_addr, &info.src_rtp_name);
reattach_tp = PJ_TRUE;
} else if (idx == RTCP_CHANNEL && !ds->srtp->use_rtcp_mux &&
pj_sockaddr_has_addr(&info.src_rtcp_name) &&
pj_sockaddr_cmp(&ds->rem_rtcp, &info.src_rtcp_name))
{
pj_sockaddr_cp(&ds->rem_rtcp, &info.src_rtcp_name);
reattach_tp = PJ_TRUE;
}

if (reattach_tp) {
pjmedia_transport_attach_param ap;
pj_status_t status;

/* Attach member transport */
pj_bzero(&ap, sizeof(ap));
ap.user_data = ds->srtp;
pj_sockaddr_cp(&ds->rem_addr, &info.src_rtp_name);
pj_sockaddr_cp(&ap.rem_addr, &ds->rem_addr);
ap.addr_len = pj_sockaddr_get_len(&ap.rem_addr);
if (pj_sockaddr_cmp(&info.sock_info.rtp_addr_name,
&info.sock_info.rtcp_addr_name) == 0)
{
if (pj_sockaddr_has_addr(&ds->rem_addr)) {
pj_sockaddr_cp(&ap.rem_addr, &ds->rem_addr);
} else {
pj_sockaddr_init(pj_AF_INET(), &ap.rem_addr, 0, 0);
}
if (ds->srtp->use_rtcp_mux) {
/* Using RTP & RTCP multiplexing */
pj_sockaddr_cp(&ds->rem_rtcp, &ds->rem_addr);
pj_sockaddr_cp(&ap.rem_rtcp, &ds->rem_rtcp);
pj_sockaddr_cp(&ap.rem_rtcp, &ap.rem_addr);
} else if (pj_sockaddr_has_addr(&ds->rem_rtcp)) {
pj_sockaddr_cp(&ap.rem_rtcp, &ds->rem_rtcp);
} else {
} else if (pj_sockaddr_has_addr(&ds->rem_addr)) {
pj_sockaddr_cp(&ap.rem_rtcp, &ds->rem_addr);
pj_sockaddr_set_port(&ap.rem_rtcp,
pj_sockaddr_get_port(&ds->rem_addr)+1);
pj_sockaddr_get_port(&ap.rem_rtcp) + 1);
} else {
pj_sockaddr_init(pj_AF_INET(), &ap.rem_rtcp, 0, 0);
}

ap.addr_len = pj_sockaddr_get_len(&ap.rem_addr);
status = pjmedia_transport_attach2(&ds->srtp->base, &ap);
if (status != PJ_SUCCESS) {
DTLS_UNLOCK(ds);
Expand All @@ -1300,11 +1342,13 @@ static pj_status_t dtls_on_recv(pjmedia_transport *tp, unsigned idx,
#if DTLS_DEBUG
{
char addr[PJ_INET6_ADDRSTRLEN];
char addr2[PJ_INET6_ADDRSTRLEN];
PJ_LOG(2,(ds->base.name, "Re-attached transport to update "
"remote addr=%s:%d",
"remote addr=%s remote rtcp=%s",
pj_sockaddr_print(&ap.rem_addr, addr,
sizeof(addr), 2),
pj_sockaddr_get_port(&ap.rem_addr)));
sizeof(addr), 3),
pj_sockaddr_print(&ap.rem_rtcp, addr2,
sizeof(addr2), 3)));
}
#endif
}
Expand All @@ -1325,11 +1369,11 @@ static pj_status_t dtls_on_recv(pjmedia_transport *tp, unsigned idx,
}
}

DTLS_UNLOCK(ds);

/* Send it to OpenSSL */
ssl_on_recv_packet(ds, idx, pkt, size);

DTLS_UNLOCK(ds);

return PJ_SUCCESS;
}

Expand Down Expand Up @@ -1428,6 +1472,7 @@ static pj_status_t dtls_media_create( pjmedia_transport *tp,

static void dtls_media_stop_channel(dtls_srtp *ds, unsigned idx)
{
ds->nego_completed[idx] = PJ_TRUE;
if (ds->clock[idx])
pjmedia_clock_stop(ds->clock[idx]);

Expand Down Expand Up @@ -1628,9 +1673,11 @@ static pj_status_t dtls_encode_sdp( pjmedia_transport *tp,
#if DTLS_DEBUG
{
char addr[PJ_INET6_ADDRSTRLEN];
PJ_LOG(2,(ds->base.name, "Attached transport, remote addr=%s:%d",
pj_sockaddr_print(&ap.rem_addr, addr, sizeof(addr), 2),
pj_sockaddr_get_port(&ap.rem_addr)));
char addr2[PJ_INET6_ADDRSTRLEN];
PJ_LOG(2,(ds->base.name, "Attached transport, remote addr=%s "
"remote rtcp=%s",
pj_sockaddr_print(&ap.rem_addr, addr2, sizeof(addr2), 3),
pj_sockaddr_print(&ap.rem_rtcp, addr, sizeof(addr), 3)));
}
#endif
}
Expand Down Expand Up @@ -1810,11 +1857,13 @@ static pj_status_t dtls_media_start( pjmedia_transport *tp,
#if DTLS_DEBUG
{
char addr[PJ_INET6_ADDRSTRLEN];
char addr2[PJ_INET6_ADDRSTRLEN];
PJ_LOG(2,(ds->base.name, "Attached transport, "
"remote addr=%s:%d",
"remote addr=%s remote rtcp=%s",
pj_sockaddr_print(&ap.rem_addr, addr,
sizeof(addr), 2),
pj_sockaddr_get_port(&ap.rem_addr)));
sizeof(addr), 3),
pj_sockaddr_print(&ap.rem_rtcp, addr2,
sizeof(addr2), 3)));
}
#endif

Expand Down Expand Up @@ -1856,6 +1905,7 @@ static pj_status_t dtls_media_stop(pjmedia_transport *tp)
static void dtls_destroy_channel(dtls_srtp *ds, unsigned idx)
{
if (ds->clock[idx]) {
ds->nego_completed[idx] = PJ_TRUE;
pjmedia_clock_destroy(ds->clock[idx]);
ds->clock[idx] = NULL;
}
Expand Down Expand Up @@ -1973,9 +2023,11 @@ PJ_DEF(pj_status_t) pjmedia_transport_srtp_dtls_start_nego(
#if DTLS_DEBUG
{
char addr[PJ_INET6_ADDRSTRLEN];
PJ_LOG(2,(ds->base.name, "Attached transport, remote addr=%s:%d",
pj_sockaddr_print(&ap.rem_addr, addr, sizeof(addr), 2),
pj_sockaddr_get_port(&ap.rem_addr)));
char addr2[PJ_INET6_ADDRSTRLEN];
PJ_LOG(2,(ds->base.name, "Attached transport, remote addr=%s "
"remote rtcp=%s",
pj_sockaddr_print(&ap.rem_addr, addr, sizeof(addr), 3),
pj_sockaddr_print(&ap.rem_addr, addr2, sizeof(addr2), 3)));
}
#endif

Expand Down
11 changes: 11 additions & 0 deletions pjnath/src/pjnath/turn_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,8 @@ static void turn_on_state(pj_turn_session *sess,
(*turn_sock->cb.on_state)(turn_sock, old_state, new_state);
}

pj_grp_lock_acquire(turn_sock->grp_lock);

/* Make sure user hasn't destroyed us in the callback */
if (turn_sock->sess && new_state == PJ_TURN_STATE_RESOLVED) {
pj_turn_session_info info;
Expand Down Expand Up @@ -1250,6 +1252,7 @@ static void turn_on_state(pj_turn_session *sess,
status = pj_sock_socket(turn_sock->af, sock_type, 0, &sock);
if (status != PJ_SUCCESS) {
turn_sock_destroy(turn_sock, status);
pj_grp_lock_release(turn_sock->grp_lock);
return;
}

Expand All @@ -1260,6 +1263,7 @@ static void turn_on_state(pj_turn_session *sess,
if (status != PJ_SUCCESS) {
pj_sock_close(sock);
turn_sock_destroy(turn_sock, status);
pj_grp_lock_release(turn_sock->grp_lock);
return;
}
/* Apply QoS, if specified */
Expand All @@ -1271,6 +1275,7 @@ static void turn_on_state(pj_turn_session *sess,
{
pj_sock_close(sock);
turn_sock_destroy(turn_sock, status);
pj_grp_lock_release(turn_sock->grp_lock);
return;
}

Expand Down Expand Up @@ -1387,6 +1392,7 @@ static void turn_on_state(pj_turn_session *sess,
}
if (status != PJ_SUCCESS) {
turn_sock_destroy(turn_sock, status);
pj_grp_lock_release(turn_sock->grp_lock);
return;
}
if (turn_sock->cert) {
Expand All @@ -1398,6 +1404,7 @@ static void turn_on_state(pj_turn_session *sess,

if (status != PJ_SUCCESS) {
turn_sock_destroy(turn_sock, status);
pj_grp_lock_release(turn_sock->grp_lock);
return;
}

Expand All @@ -1415,6 +1422,7 @@ static void turn_on_state(pj_turn_session *sess,

if (status != PJ_SUCCESS) {
turn_sock_destroy(turn_sock, status);
pj_grp_lock_release(turn_sock->grp_lock);
return;
}

Expand Down Expand Up @@ -1457,6 +1465,7 @@ static void turn_on_state(pj_turn_session *sess,
pj_sockaddr_print(&info.server, addrtxt,
sizeof(addrtxt), 3)));
turn_sock_destroy(turn_sock, status);
pj_grp_lock_release(turn_sock->grp_lock);
return;
}

Expand All @@ -1478,6 +1487,8 @@ static void turn_on_state(pj_turn_session *sess,
&delay, TIMER_DESTROY,
turn_sock->grp_lock);
}

pj_grp_lock_release(turn_sock->grp_lock);
}


Expand Down
42 changes: 28 additions & 14 deletions pjsip/src/pjsua-lib/pjsua_media.c
Original file line number Diff line number Diff line change
Expand Up @@ -3400,8 +3400,8 @@ static pj_bool_t is_ice_running(pjmedia_transport *tp)
static void check_srtp_roc(pjsua_call *call,
unsigned med_idx,
const pjsua_stream_info *new_si_,
const pjmedia_sdp_media *local_sdp,
const pjmedia_sdp_media *remote_sdp)
const pjmedia_sdp_session *local_sdp,
const pjmedia_sdp_session *rem_sdp)
{
pjsua_call_media *call_med = &call->media[med_idx];
pjmedia_transport_info tpinfo;
Expand Down Expand Up @@ -3528,11 +3528,19 @@ static void check_srtp_roc(pjsua_call *call,
pjmedia_sdp_attr *attr;

if (local_change) {
attr = pjmedia_sdp_attr_find(local_sdp->attr_count,
local_sdp->attr, &STR_ICE_UFRAG,
pjmedia_sdp_media *local_m = local_sdp->media[med_idx];

attr = pjmedia_sdp_attr_find(local_m->attr_count,
local_m->attr, &STR_ICE_UFRAG,
NULL);
if (!pj_strcmp(&call_med->prev_ice_info.loc_ufrag,
&attr->value))
if (attr == NULL) {
/* Find ice-ufrag attribute in session level */
attr = pjmedia_sdp_attr_find(local_sdp->attr_count,
local_sdp->attr, &STR_ICE_UFRAG,
NULL);
}
if (attr && !pj_strcmp(&call_med->prev_ice_info.loc_ufrag,
&attr->value))
{
PJ_LOG(4, (THIS_FILE, "ICE unchanged, SRTP TX ROC "
"maintained"));
Expand All @@ -3541,11 +3549,19 @@ static void check_srtp_roc(pjsua_call *call,
}

if (rem_change) {
attr = pjmedia_sdp_attr_find(remote_sdp->attr_count,
remote_sdp->attr, &STR_ICE_UFRAG,
pjmedia_sdp_media *rem_m = rem_sdp->media[med_idx];

attr = pjmedia_sdp_attr_find(rem_m->attr_count,
rem_m->attr, &STR_ICE_UFRAG,
NULL);
if (!pj_strcmp(&call_med->prev_ice_info.rem_ufrag,
&attr->value))
if (attr == NULL) {
/* Find ice-ufrag attribute in session level */
attr = pjmedia_sdp_attr_find(rem_sdp->attr_count,
rem_sdp->attr, &STR_ICE_UFRAG,
NULL);
}
if (attr && !pj_strcmp(&call_med->prev_ice_info.rem_ufrag,
&attr->value))
{
PJ_LOG(4, (THIS_FILE, "ICE unchanged, SRTP RX ROC "
"maintained"));
Expand Down Expand Up @@ -4003,8 +4019,7 @@ pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
#if PJSUA_MEDIA_HAS_PJMEDIA || PJSUA_THIRD_PARTY_STREAM_HAS_GET_INFO
#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
/* Check if we need to reset or maintain SRTP ROC */
check_srtp_roc(call, mi, &stream_info,
local_sdp->media[mi], remote_sdp->media[mi]);
check_srtp_roc(call, mi, &stream_info, local_sdp, remote_sdp);
#endif
#endif

Expand Down Expand Up @@ -4260,8 +4275,7 @@ pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
#if PJSUA_MEDIA_HAS_PJMEDIA || PJSUA_THIRD_PARTY_STREAM_HAS_GET_INFO
#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
/* Check if we need to reset or maintain SRTP ROC */
check_srtp_roc(call, mi, &stream_info,
local_sdp->media[mi], remote_sdp->media[mi]);
check_srtp_roc(call, mi, &stream_info, local_sdp, remote_sdp);
#endif
#endif

Expand Down

0 comments on commit fdccdfa

Please sign in to comment.