diff --git a/daemon/codec.c b/daemon/codec.c index a678f6e8b..2ad173789 100644 --- a/daemon/codec.c +++ b/daemon/codec.c @@ -2502,6 +2502,27 @@ static int packet_decode(struct codec_ssrc_handler *ch, struct transcode_packet return ret; } + +static void codec_calc_jitter(struct media_packet *mp, unsigned int clockrate) { + if (!mp->ssrc_in) + return; + struct ssrc_entry_call *sec = mp->ssrc_in->parent; + + // RFC 3550 A.8 + uint32_t transit = (((timeval_us(&mp->tv) / 1000) * clockrate) / 1000) + - ntohl(mp->rtp->timestamp); + mutex_lock(&sec->h.lock); + int32_t d = 0; + if (sec->transit) + d = transit - sec->transit; + sec->transit = transit; + if (d < 0) + d = -d; + sec->jitter += d - ((sec->jitter + 8) >> 4); + mutex_unlock(&sec->h.lock); +} + + static int handler_func_transcode(struct codec_handler *h, struct media_packet *mp) { if (G_UNLIKELY(!mp->rtp)) return handler_func_passthrough(h, mp); @@ -2514,6 +2535,8 @@ static int handler_func_transcode(struct codec_handler *h, struct media_packet * ntohl(mp->rtp->ssrc), mp->rtp->m_pt, ntohs(mp->rtp->seq_num), ntohl(mp->rtp->timestamp), mp->payload.len); + codec_calc_jitter(mp, h->source_pt.clock_rate); + if (h->stats_entry) { unsigned int idx = rtpe_now.tv_sec & 1; int last_tv_sec = g_atomic_int_get(&h->stats_entry->last_tv_sec[idx]); diff --git a/daemon/rtcp.c b/daemon/rtcp.c index ad93119f9..64909ddcd 100644 --- a/daemon/rtcp.c +++ b/daemon/rtcp.c @@ -1431,6 +1431,7 @@ GString *rtcp_sender_report(uint32_t ssrc, uint32_t ts, uint32_t packets, uint32 tv_diff = timeval_diff(&rtpe_now, &si->received); ntp_middle_bits = si->ntp_middle_bits; } + uint32_t jitter = se->jitter; mutex_unlock(&se->h.lock); uint64_t lost = atomic64_get(&s->packets_lost); @@ -1445,8 +1446,8 @@ GString *rtcp_sender_report(uint32_t ssrc, uint32_t ts, uint32_t packets, uint32 .high_seq_received = htonl(atomic64_get(&s->last_seq)), .lsr = htonl(ntp_middle_bits), .dlsr = htonl(tv_diff * 65536 / 1000000), + .jitter = htonl(jitter >> 4), }; - // XXX jitter n++; } ssrc_ctx_put(&s); diff --git a/include/ssrc.h b/include/ssrc.h index 6698d7c22..fd203bdc1 100644 --- a/include/ssrc.h +++ b/include/ssrc.h @@ -98,6 +98,7 @@ struct ssrc_entry_call { // for transcoding // input only packet_sequencer_t sequencer; + uint32_t jitter, transit; // output only uint16_t seq_diff; };