Skip to content

Commit

Permalink
rtp: lock more rtcp_sess fields and code
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed Jan 4, 2024
1 parent e9bca3f commit e315ad9
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/rtp/sess.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ void rtcp_handler(struct rtcp_sess *sess, struct rtcp_msg *msg)
if (!sess || !msg)
return;

mtx_lock(sess->lock);
switch (msg->hdr.pt) {

case RTCP_SR:
Expand All @@ -246,6 +247,8 @@ void rtcp_handler(struct rtcp_sess *sess, struct rtcp_msg *msg)
default:
break;
}

mtx_unlock(sess->lock);
}


Expand Down Expand Up @@ -357,8 +360,10 @@ int rtcp_enable(struct rtcp_sess *sess, bool enabled, const char *cname)
if (!sess)
return EINVAL;

mtx_lock(sess->lock);
sess->cname = mem_deref(sess->cname);
err = str_dup(&sess->cname, cname);
mtx_unlock(sess->lock);
if (err)
return err;

Expand Down Expand Up @@ -583,12 +588,15 @@ void rtcp_sess_rx_rtp(struct rtcp_sess *sess, struct rtp_header *hdr,
if (!sess)
return;

mtx_lock(sess->lock);
mbr = get_member(sess, hdr->ssrc);
mtx_unlock(sess->lock);
if (!mbr) {
DEBUG_NOTICE("could not add member: 0x%08x\n", hdr->ssrc);
return;
}

mtx_lock(sess->lock);
if (!mbr->s) {
int err;

Expand All @@ -598,6 +606,7 @@ void rtcp_sess_rx_rtp(struct rtcp_sess *sess, struct rtp_header *hdr,
else
err = mutex_alloc(&mbr->s->lock);

mtx_unlock(sess->lock);
if (err) {
DEBUG_NOTICE("could not add sender: 0x%08x\n",
hdr->ssrc);
Expand All @@ -611,28 +620,42 @@ void rtcp_sess_rx_rtp(struct rtcp_sess *sess, struct rtp_header *hdr,
/* probation not used */
sa_cpy(&mbr->s->rtp_peer, peer);
source_unlock(mbr->s);
mtx_lock(sess->lock);
++sess->senderc;
mtx_unlock(sess->lock);
}
else {
mtx_unlock(sess->lock);
}

source_lock(mbr->s);
if (!source_update_seq(mbr->s, hdr->seq)) {
DEBUG_WARNING("rtp_update_seq() returned 0\n");
}
source_unlock(mbr->s);

mtx_lock(sess->lock);
if (sess->srate_rx) {
/* Convert from wall-clock time to timestamp units */
hdr->ts_arrive = tmr_jiffies() * sess->srate_rx / 1000;
mtx_unlock(sess->lock);

/*
* Calculate jitter only when the timestamp is different than
* last packet (see RTP FAQ
* https://www.cs.columbia.edu/~hgs/rtp/faq.html#jitter).
*/
source_lock(mbr->s);
if (hdr->ts != mbr->s->last_rtp_ts)
source_calc_jitter(mbr->s, hdr->ts,
(uint32_t)hdr->ts_arrive);
source_unlock(mbr->s);
}
else {
mtx_unlock(sess->lock);
}

source_lock(mbr->s);
mbr->s->last_rtp_ts = hdr->ts;
mbr->s->rtp_rx_bytes += payload_size;
source_unlock(mbr->s);
Expand Down Expand Up @@ -738,14 +761,14 @@ int rtcp_debug(struct re_printf *pf, const struct rtp_sock *rs)
return 0;

err |= re_hprintf(pf, "----- RTCP Session: -----\n");
mtx_lock(sess->lock);
err |= re_hprintf(pf, " cname=%s SSRC=0x%08x/%u rx=%uHz\n",
sess->cname,
rtp_sess_ssrc(sess->rs), rtp_sess_ssrc(sess->rs),
sess->srate_rx);

hash_apply(sess->members, debug_handler, pf);

mtx_lock(sess->lock);
err |= re_hprintf(pf, " TX: packets=%u, octets=%u\n",
sess->txstat.psent, sess->txstat.osent);
mtx_unlock(sess->lock);
Expand Down

0 comments on commit e315ad9

Please sign in to comment.