comparison rtp.c @ 1445:db97355877b1 libavformat

add valid statistics for the RTCP receiver report. Basically taken verbatim from RFC 1889. Patch by Ryan Martell % rdm4 A martellventures P com % Original thread: Date: Oct 31, 2006 12:43 AM Subject: [Ffmpeg-devel] [PATCH] RTCP valid receiver statistics....
author gpoirier
date Fri, 03 Nov 2006 07:55:57 +0000
parents 404048ea11bc
children 234a04b906f9
comparison
equal deleted inserted replaced
1444:74cb68ad9dce 1445:db97355877b1
256 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time; 256 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
257 s->last_rtcp_timestamp = decode_be32(buf + 16); 257 s->last_rtcp_timestamp = decode_be32(buf + 16);
258 return 0; 258 return 0;
259 } 259 }
260 260
261 #define RTP_SEQ_MOD (1<<16)
262
263 /**
264 * called on parse open packet
265 */
266 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence) // called on parse open packet.
267 {
268 memset(s, 0, sizeof(RTPStatistics));
269 s->max_seq= base_sequence;
270 s->probation= 1;
271 }
272
273 /**
274 * called whenever there is a large jump in sequence numbers, or when they get out of probation...
275 */
276 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
277 {
278 s->max_seq= seq;
279 s->cycles= 0;
280 s->base_seq= seq -1;
281 s->bad_seq= RTP_SEQ_MOD + 1;
282 s->received= 0;
283 s->expected_prior= 0;
284 s->received_prior= 0;
285 s->jitter= 0;
286 s->transit= 0;
287 }
288
289 /**
290 * returns 1 if we should handle this packet.
291 */
292 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
293 {
294 uint16_t udelta= seq - s->max_seq;
295 const int MAX_DROPOUT= 3000;
296 const int MAX_MISORDER = 100;
297 const int MIN_SEQUENTIAL = 2;
298
299 /* source not valid until MIN_SEQUENTIAL packets with sequence seq. numbers have been received */
300 if(s->probation)
301 {
302 if(seq==s->max_seq + 1) {
303 s->probation--;
304 s->max_seq= seq;
305 if(s->probation==0) {
306 rtp_init_sequence(s, seq);
307 s->received++;
308 return 1;
309 }
310 } else {
311 s->probation= MIN_SEQUENTIAL - 1;
312 s->max_seq = seq;
313 }
314 } else if (udelta < MAX_DROPOUT) {
315 // in order, with permissible gap
316 if(seq < s->max_seq) {
317 //sequence number wrapped; count antother 64k cycles
318 s->cycles += RTP_SEQ_MOD;
319 }
320 s->max_seq= seq;
321 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
322 // sequence made a large jump...
323 if(seq==s->bad_seq) {
324 // two sequential packets-- assume that the other side restarted without telling us; just resync.
325 rtp_init_sequence(s, seq);
326 } else {
327 s->bad_seq= (seq + 1) & (RTP_SEQ_MOD-1);
328 return 0;
329 }
330 } else {
331 // duplicate or reordered packet...
332 }
333 s->received++;
334 return 1;
335 }
336
337 #if 0
338 /**
339 * This function is currently unused; without a valid local ntp time, I don't see how we could calculate the
340 * difference between the arrival and sent timestamp. As a result, the jitter and transit statistics values
341 * never change. I left this in in case someone else can see a way. (rdm)
342 */
343 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp, uint32_t arrival_timestamp)
344 {
345 uint32_t transit= arrival_timestamp - sent_timestamp;
346 int d;
347 s->transit= transit;
348 d= FFABS(transit - s->transit);
349 s->jitter += d - ((s->jitter + 8)>>4);
350 }
351 #endif
352
261 /** 353 /**
262 * some rtp servers assume client is dead if they don't hear from them... 354 * some rtp servers assume client is dead if they don't hear from them...
263 * so we send a Receiver Report to the provided ByteIO context 355 * so we send a Receiver Report to the provided ByteIO context
264 * (we don't have access to the rtcp handle from here) 356 * (we don't have access to the rtcp handle from here)
265 */ 357 */
267 { 359 {
268 ByteIOContext pb; 360 ByteIOContext pb;
269 uint8_t *buf; 361 uint8_t *buf;
270 int len; 362 int len;
271 int rtcp_bytes; 363 int rtcp_bytes;
364 RTPStatistics *stats= &s->statistics;
365 uint32_t lost;
366 uint32_t extended_max;
367 uint32_t expected_interval;
368 uint32_t received_interval;
369 uint32_t lost_interval;
370 uint32_t expected;
371 uint32_t fraction;
372 uint64_t ntp_time= s->last_rtcp_ntp_time; // TODO: Get local ntp time?
272 373
273 if (!s->rtp_ctx || (count < 1)) 374 if (!s->rtp_ctx || (count < 1))
274 return -1; 375 return -1;
275 376
377 /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
276 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */ 378 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
277 s->octet_count += count; 379 s->octet_count += count;
278 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) / 380 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
279 RTCP_TX_RATIO_DEN; 381 RTCP_TX_RATIO_DEN;
280 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !? 382 rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
290 put_byte(&pb, 201); 392 put_byte(&pb, 201);
291 put_be16(&pb, 7); /* length in words - 1 */ 393 put_be16(&pb, 7); /* length in words - 1 */
292 put_be32(&pb, s->ssrc); // our own SSRC 394 put_be32(&pb, s->ssrc); // our own SSRC
293 put_be32(&pb, s->ssrc); // XXX: should be the server's here! 395 put_be32(&pb, s->ssrc); // XXX: should be the server's here!
294 // some placeholders we should really fill... 396 // some placeholders we should really fill...
295 put_be32(&pb, ((0 << 24) | (0 & 0x0ffffff))); /* 0% lost, total 0 lost */ 397 // RFC 1889/p64
296 put_be32(&pb, (0 << 16) | s->seq); 398 extended_max= stats->cycles + stats->max_seq;
297 put_be32(&pb, 0x68); /* jitter */ 399 expected= extended_max - stats->base_seq + 1;
298 put_be32(&pb, -1); /* last SR timestamp */ 400 lost= expected - stats->received;
299 put_be32(&pb, 1); /* delay since last SR */ 401 lost= FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
402 expected_interval= expected - stats->expected_prior;
403 stats->expected_prior= expected;
404 received_interval= stats->received - stats->received_prior;
405 stats->received_prior= stats->received;
406 lost_interval= expected_interval - received_interval;
407 if (expected_interval==0 || lost_interval<=0) fraction= 0;
408 else fraction = (lost_interval<<8)/expected_interval;
409
410 fraction= (fraction<<24) | lost;
411
412 put_be32(&pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
413 put_be32(&pb, extended_max); /* max sequence received */
414 put_be32(&pb, stats->jitter>>4); /* jitter */
415
416 if(s->last_rtcp_ntp_time==AV_NOPTS_VALUE)
417 {
418 put_be32(&pb, 0); /* last SR timestamp */
419 put_be32(&pb, 0); /* delay since last SR */
420 } else {
421 uint32_t middle_32_bits= s->last_rtcp_ntp_time>>16; // this is valid, right? do we need to handle 64 bit values special?
422 uint32_t delay_since_last= ntp_time - s->last_rtcp_ntp_time;
423
424 put_be32(&pb, middle_32_bits); /* last SR timestamp */
425 put_be32(&pb, delay_since_last); /* delay since last SR */
426 }
300 427
301 // CNAME 428 // CNAME
302 put_byte(&pb, (RTP_VERSION << 6) + 1); /* 1 report block */ 429 put_byte(&pb, (RTP_VERSION << 6) + 1); /* 1 report block */
303 put_byte(&pb, 202); 430 put_byte(&pb, 202);
304 len = strlen(s->hostname); 431 len = strlen(s->hostname);
313 } 440 }
314 441
315 put_flush_packet(&pb); 442 put_flush_packet(&pb);
316 len = url_close_dyn_buf(&pb, &buf); 443 len = url_close_dyn_buf(&pb, &buf);
317 if ((len > 0) && buf) { 444 if ((len > 0) && buf) {
445 int result;
318 #if defined(DEBUG) 446 #if defined(DEBUG)
319 printf("sending %d bytes of RR\n", len); 447 printf("sending %d bytes of RR\n", len);
320 #endif 448 #endif
321 url_write(s->rtp_ctx, buf, len); 449 result= url_write(s->rtp_ctx, buf, len);
450 #if defined(DEBUG)
451 printf("result from url_write: %d\n", result);
452 #endif
322 av_free(buf); 453 av_free(buf);
323 } 454 }
324 return 0; 455 return 0;
325 } 456 }
326 457
341 s->last_rtcp_ntp_time = AV_NOPTS_VALUE; 472 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
342 s->first_rtcp_ntp_time = AV_NOPTS_VALUE; 473 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
343 s->ic = s1; 474 s->ic = s1;
344 s->st = st; 475 s->st = st;
345 s->rtp_payload_data = rtp_payload_data; 476 s->rtp_payload_data = rtp_payload_data;
477 rtp_init_statistics(&s->statistics, 0); // do we know the initial sequence from sdp?
346 if (!strcmp(AVRtpPayloadTypes[payload_type].enc_name, "MP2T")) { 478 if (!strcmp(AVRtpPayloadTypes[payload_type].enc_name, "MP2T")) {
347 s->ts = mpegts_parse_open(s->ic); 479 s->ts = mpegts_parse_open(s->ic);
348 if (s->ts == NULL) { 480 if (s->ts == NULL) {
349 av_free(s); 481 av_free(s);
350 return NULL; 482 return NULL;
512 /* NOTE: we can handle only one payload type */ 644 /* NOTE: we can handle only one payload type */
513 if (s->payload_type != payload_type) 645 if (s->payload_type != payload_type)
514 return -1; 646 return -1;
515 647
516 st = s->st; 648 st = s->st;
517 #if defined(DEBUG) || 1 649 // only do something with this if all the rtp checks pass...
518 if (seq != ((s->seq + 1) & 0xffff)) { 650 if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
651 {
519 av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n", 652 av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
520 payload_type, seq, ((s->seq + 1) & 0xffff)); 653 payload_type, seq, ((s->seq + 1) & 0xffff));
521 } 654 return -1;
522 #endif 655 }
656
523 s->seq = seq; 657 s->seq = seq;
524 len -= 12; 658 len -= 12;
525 buf += 12; 659 buf += 12;
526 660
527 if (!st) { 661 if (!st) {