00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #define _XOPEN_SOURCE 600
00024
00025 #include "libavcodec/get_bits.h"
00026 #include "avformat.h"
00027 #include "mpegts.h"
00028
00029 #include <unistd.h>
00030 #include "network.h"
00031
00032 #include "rtpdec.h"
00033 #include "rtpdec_amr.h"
00034 #include "rtpdec_asf.h"
00035 #include "rtpdec_h263.h"
00036 #include "rtpdec_h264.h"
00037 #include "rtpdec_xiph.h"
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler= NULL;
00052
00053 static RTPDynamicProtocolHandler mp4v_es_handler= {"MP4V-ES", AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG4};
00054 static RTPDynamicProtocolHandler mpeg4_generic_handler= {"mpeg4-generic", AVMEDIA_TYPE_AUDIO, CODEC_ID_AAC};
00055
00056 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
00057 {
00058 handler->next= RTPFirstDynamicPayloadHandler;
00059 RTPFirstDynamicPayloadHandler= handler;
00060 }
00061
00062 void av_register_rtp_dynamic_payload_handlers(void)
00063 {
00064 ff_register_dynamic_payload_handler(&mp4v_es_handler);
00065 ff_register_dynamic_payload_handler(&mpeg4_generic_handler);
00066 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
00067 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
00068 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
00069 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
00070 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
00071 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
00072 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
00073
00074 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
00075 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
00076 }
00077
00078 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
00079 {
00080 if (buf[1] != 200)
00081 return -1;
00082 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
00083 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE)
00084 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
00085 s->last_rtcp_timestamp = AV_RB32(buf + 16);
00086 return 0;
00087 }
00088
00089 #define RTP_SEQ_MOD (1<<16)
00090
00094 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
00095 {
00096 memset(s, 0, sizeof(RTPStatistics));
00097 s->max_seq= base_sequence;
00098 s->probation= 1;
00099 }
00100
00104 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
00105 {
00106 s->max_seq= seq;
00107 s->cycles= 0;
00108 s->base_seq= seq -1;
00109 s->bad_seq= RTP_SEQ_MOD + 1;
00110 s->received= 0;
00111 s->expected_prior= 0;
00112 s->received_prior= 0;
00113 s->jitter= 0;
00114 s->transit= 0;
00115 }
00116
00120 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
00121 {
00122 uint16_t udelta= seq - s->max_seq;
00123 const int MAX_DROPOUT= 3000;
00124 const int MAX_MISORDER = 100;
00125 const int MIN_SEQUENTIAL = 2;
00126
00127
00128 if(s->probation)
00129 {
00130 if(seq==s->max_seq + 1) {
00131 s->probation--;
00132 s->max_seq= seq;
00133 if(s->probation==0) {
00134 rtp_init_sequence(s, seq);
00135 s->received++;
00136 return 1;
00137 }
00138 } else {
00139 s->probation= MIN_SEQUENTIAL - 1;
00140 s->max_seq = seq;
00141 }
00142 } else if (udelta < MAX_DROPOUT) {
00143
00144 if(seq < s->max_seq) {
00145
00146 s->cycles += RTP_SEQ_MOD;
00147 }
00148 s->max_seq= seq;
00149 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
00150
00151 if(seq==s->bad_seq) {
00152
00153 rtp_init_sequence(s, seq);
00154 } else {
00155 s->bad_seq= (seq + 1) & (RTP_SEQ_MOD-1);
00156 return 0;
00157 }
00158 } else {
00159
00160 }
00161 s->received++;
00162 return 1;
00163 }
00164
00165 #if 0
00166
00171 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp, uint32_t arrival_timestamp)
00172 {
00173 uint32_t transit= arrival_timestamp - sent_timestamp;
00174 int d;
00175 s->transit= transit;
00176 d= FFABS(transit - s->transit);
00177 s->jitter += d - ((s->jitter + 8)>>4);
00178 }
00179 #endif
00180
00181 int rtp_check_and_send_back_rr(RTPDemuxContext *s, int count)
00182 {
00183 ByteIOContext *pb;
00184 uint8_t *buf;
00185 int len;
00186 int rtcp_bytes;
00187 RTPStatistics *stats= &s->statistics;
00188 uint32_t lost;
00189 uint32_t extended_max;
00190 uint32_t expected_interval;
00191 uint32_t received_interval;
00192 uint32_t lost_interval;
00193 uint32_t expected;
00194 uint32_t fraction;
00195 uint64_t ntp_time= s->last_rtcp_ntp_time;
00196
00197 if (!s->rtp_ctx || (count < 1))
00198 return -1;
00199
00200
00201
00202 s->octet_count += count;
00203 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
00204 RTCP_TX_RATIO_DEN;
00205 rtcp_bytes /= 50;
00206 if (rtcp_bytes < 28)
00207 return -1;
00208 s->last_octet_count = s->octet_count;
00209
00210 if (url_open_dyn_buf(&pb) < 0)
00211 return -1;
00212
00213
00214 put_byte(pb, (RTP_VERSION << 6) + 1);
00215 put_byte(pb, 201);
00216 put_be16(pb, 7);
00217 put_be32(pb, s->ssrc);
00218 put_be32(pb, s->ssrc);
00219
00220
00221 extended_max= stats->cycles + stats->max_seq;
00222 expected= extended_max - stats->base_seq + 1;
00223 lost= expected - stats->received;
00224 lost= FFMIN(lost, 0xffffff);
00225 expected_interval= expected - stats->expected_prior;
00226 stats->expected_prior= expected;
00227 received_interval= stats->received - stats->received_prior;
00228 stats->received_prior= stats->received;
00229 lost_interval= expected_interval - received_interval;
00230 if (expected_interval==0 || lost_interval<=0) fraction= 0;
00231 else fraction = (lost_interval<<8)/expected_interval;
00232
00233 fraction= (fraction<<24) | lost;
00234
00235 put_be32(pb, fraction);
00236 put_be32(pb, extended_max);
00237 put_be32(pb, stats->jitter>>4);
00238
00239 if(s->last_rtcp_ntp_time==AV_NOPTS_VALUE)
00240 {
00241 put_be32(pb, 0);
00242 put_be32(pb, 0);
00243 } else {
00244 uint32_t middle_32_bits= s->last_rtcp_ntp_time>>16;
00245 uint32_t delay_since_last= ntp_time - s->last_rtcp_ntp_time;
00246
00247 put_be32(pb, middle_32_bits);
00248 put_be32(pb, delay_since_last);
00249 }
00250
00251
00252 put_byte(pb, (RTP_VERSION << 6) + 1);
00253 put_byte(pb, 202);
00254 len = strlen(s->hostname);
00255 put_be16(pb, (6 + len + 3) / 4);
00256 put_be32(pb, s->ssrc);
00257 put_byte(pb, 0x01);
00258 put_byte(pb, len);
00259 put_buffer(pb, s->hostname, len);
00260
00261 for (len = (6 + len) % 4; len % 4; len++) {
00262 put_byte(pb, 0);
00263 }
00264
00265 put_flush_packet(pb);
00266 len = url_close_dyn_buf(pb, &buf);
00267 if ((len > 0) && buf) {
00268 int result;
00269 dprintf(s->ic, "sending %d bytes of RR\n", len);
00270 result= url_write(s->rtp_ctx, buf, len);
00271 dprintf(s->ic, "result from url_write: %d\n", result);
00272 av_free(buf);
00273 }
00274 return 0;
00275 }
00276
00277 void rtp_send_punch_packets(URLContext* rtp_handle)
00278 {
00279 ByteIOContext *pb;
00280 uint8_t *buf;
00281 int len;
00282
00283
00284 if (url_open_dyn_buf(&pb) < 0)
00285 return;
00286
00287 put_byte(pb, (RTP_VERSION << 6));
00288 put_byte(pb, 0);
00289 put_be16(pb, 0);
00290 put_be32(pb, 0);
00291 put_be32(pb, 0);
00292
00293 put_flush_packet(pb);
00294 len = url_close_dyn_buf(pb, &buf);
00295 if ((len > 0) && buf)
00296 url_write(rtp_handle, buf, len);
00297 av_free(buf);
00298
00299
00300 if (url_open_dyn_buf(&pb) < 0)
00301 return;
00302
00303 put_byte(pb, (RTP_VERSION << 6));
00304 put_byte(pb, 201);
00305 put_be16(pb, 1);
00306 put_be32(pb, 0);
00307
00308 put_flush_packet(pb);
00309 len = url_close_dyn_buf(pb, &buf);
00310 if ((len > 0) && buf)
00311 url_write(rtp_handle, buf, len);
00312 av_free(buf);
00313 }
00314
00315
00322 RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, RTPPayloadData *rtp_payload_data)
00323 {
00324 RTPDemuxContext *s;
00325
00326 s = av_mallocz(sizeof(RTPDemuxContext));
00327 if (!s)
00328 return NULL;
00329 s->payload_type = payload_type;
00330 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
00331 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
00332 s->ic = s1;
00333 s->st = st;
00334 s->rtp_payload_data = rtp_payload_data;
00335 rtp_init_statistics(&s->statistics, 0);
00336 if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
00337 s->ts = ff_mpegts_parse_open(s->ic);
00338 if (s->ts == NULL) {
00339 av_free(s);
00340 return NULL;
00341 }
00342 } else {
00343 av_set_pts_info(st, 32, 1, 90000);
00344 switch(st->codec->codec_id) {
00345 case CODEC_ID_MPEG1VIDEO:
00346 case CODEC_ID_MPEG2VIDEO:
00347 case CODEC_ID_MP2:
00348 case CODEC_ID_MP3:
00349 case CODEC_ID_MPEG4:
00350 case CODEC_ID_H263:
00351 case CODEC_ID_H264:
00352 st->need_parsing = AVSTREAM_PARSE_FULL;
00353 break;
00354 default:
00355 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00356 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
00357 }
00358 break;
00359 }
00360 }
00361
00362 s->rtp_ctx = rtpc;
00363 gethostname(s->hostname, sizeof(s->hostname));
00364 return s;
00365 }
00366
00367 void
00368 rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
00369 RTPDynamicProtocolHandler *handler)
00370 {
00371 s->dynamic_protocol_context = ctx;
00372 s->parse_packet = handler->parse_packet;
00373 }
00374
00375 static int rtp_parse_mp4_au(RTPDemuxContext *s, const uint8_t *buf)
00376 {
00377 int au_headers_length, au_header_size, i;
00378 GetBitContext getbitcontext;
00379 RTPPayloadData *infos;
00380
00381 infos = s->rtp_payload_data;
00382
00383 if (infos == NULL)
00384 return -1;
00385
00386
00387
00388 au_headers_length = AV_RB16(buf);
00389
00390 if (au_headers_length > RTP_MAX_PACKET_LENGTH)
00391 return -1;
00392
00393 infos->au_headers_length_bytes = (au_headers_length + 7) / 8;
00394
00395
00396 buf += 2;
00397
00398 init_get_bits(&getbitcontext, buf, infos->au_headers_length_bytes * 8);
00399
00400
00401 au_header_size = infos->sizelength + infos->indexlength;
00402 if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))
00403 return -1;
00404
00405 infos->nb_au_headers = au_headers_length / au_header_size;
00406 if (!infos->au_headers || infos->au_headers_allocated < infos->nb_au_headers) {
00407 av_free(infos->au_headers);
00408 infos->au_headers = av_malloc(sizeof(struct AUHeaders) * infos->nb_au_headers);
00409 infos->au_headers_allocated = infos->nb_au_headers;
00410 }
00411
00412
00413
00414
00415 infos->au_headers[0].size = 0;
00416 infos->au_headers[0].index = 0;
00417 for (i = 0; i < infos->nb_au_headers; ++i) {
00418 infos->au_headers[0].size += get_bits_long(&getbitcontext, infos->sizelength);
00419 infos->au_headers[0].index = get_bits_long(&getbitcontext, infos->indexlength);
00420 }
00421
00422 infos->nb_au_headers = 1;
00423
00424 return 0;
00425 }
00426
00430 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
00431 {
00432 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) {
00433 int64_t addend;
00434 int delta_timestamp;
00435
00436
00437 delta_timestamp = timestamp - s->last_rtcp_timestamp;
00438
00439 addend = av_rescale(s->last_rtcp_ntp_time - s->first_rtcp_ntp_time, s->st->time_base.den, (uint64_t)s->st->time_base.num << 32);
00440 pkt->pts = s->range_start_offset + addend + delta_timestamp;
00441 }
00442 }
00443
00453 int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
00454 const uint8_t *buf, int len)
00455 {
00456 unsigned int ssrc, h;
00457 int payload_type, seq, ret, flags = 0;
00458 AVStream *st;
00459 uint32_t timestamp;
00460 int rv= 0;
00461
00462 if (!buf) {
00463
00464 if(s->st && s->parse_packet) {
00465 timestamp= 0;
00466 rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
00467 s->st, pkt, ×tamp, NULL, 0, flags);
00468 finalize_packet(s, pkt, timestamp);
00469 return rv;
00470 } else {
00471
00472 if (s->read_buf_index >= s->read_buf_size)
00473 return -1;
00474 ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
00475 s->read_buf_size - s->read_buf_index);
00476 if (ret < 0)
00477 return -1;
00478 s->read_buf_index += ret;
00479 if (s->read_buf_index < s->read_buf_size)
00480 return 1;
00481 else
00482 return 0;
00483 }
00484 }
00485
00486 if (len < 12)
00487 return -1;
00488
00489 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
00490 return -1;
00491 if (buf[1] >= 200 && buf[1] <= 204) {
00492 rtcp_parse_packet(s, buf, len);
00493 return -1;
00494 }
00495 payload_type = buf[1] & 0x7f;
00496 if (buf[1] & 0x80)
00497 flags |= RTP_FLAG_MARKER;
00498 seq = AV_RB16(buf + 2);
00499 timestamp = AV_RB32(buf + 4);
00500 ssrc = AV_RB32(buf + 8);
00501
00502 s->ssrc = ssrc;
00503
00504
00505 if (s->payload_type != payload_type)
00506 return -1;
00507
00508 st = s->st;
00509
00510 if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
00511 {
00512 av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
00513 payload_type, seq, ((s->seq + 1) & 0xffff));
00514 return -1;
00515 }
00516
00517 s->seq = seq;
00518 len -= 12;
00519 buf += 12;
00520
00521 if (!st) {
00522
00523 ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
00524 if (ret < 0)
00525 return -1;
00526 if (ret < len) {
00527 s->read_buf_size = len - ret;
00528 memcpy(s->buf, buf + ret, s->read_buf_size);
00529 s->read_buf_index = 0;
00530 return 1;
00531 }
00532 return 0;
00533 } else if (s->parse_packet) {
00534 rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
00535 s->st, pkt, ×tamp, buf, len, flags);
00536 } else {
00537
00538 switch(st->codec->codec_id) {
00539 case CODEC_ID_MP2:
00540 case CODEC_ID_MP3:
00541
00542 if (len <= 4)
00543 return -1;
00544 h = AV_RB32(buf);
00545 len -= 4;
00546 buf += 4;
00547 av_new_packet(pkt, len);
00548 memcpy(pkt->data, buf, len);
00549 break;
00550 case CODEC_ID_MPEG1VIDEO:
00551 case CODEC_ID_MPEG2VIDEO:
00552
00553 if (len <= 4)
00554 return -1;
00555 h = AV_RB32(buf);
00556 buf += 4;
00557 len -= 4;
00558 if (h & (1 << 26)) {
00559
00560 if (len <= 4)
00561 return -1;
00562 buf += 4;
00563 len -= 4;
00564 }
00565 av_new_packet(pkt, len);
00566 memcpy(pkt->data, buf, len);
00567 break;
00568
00569
00570
00571 case CODEC_ID_AAC:
00572 if (rtp_parse_mp4_au(s, buf))
00573 return -1;
00574 {
00575 RTPPayloadData *infos = s->rtp_payload_data;
00576 if (infos == NULL)
00577 return -1;
00578 buf += infos->au_headers_length_bytes + 2;
00579 len -= infos->au_headers_length_bytes + 2;
00580
00581
00582
00583 av_new_packet(pkt, infos->au_headers[0].size);
00584 memcpy(pkt->data, buf, infos->au_headers[0].size);
00585 buf += infos->au_headers[0].size;
00586 len -= infos->au_headers[0].size;
00587 }
00588 s->read_buf_size = len;
00589 rv= 0;
00590 break;
00591 default:
00592 av_new_packet(pkt, len);
00593 memcpy(pkt->data, buf, len);
00594 break;
00595 }
00596
00597 pkt->stream_index = st->index;
00598 }
00599
00600
00601 finalize_packet(s, pkt, timestamp);
00602
00603 return rv;
00604 }
00605
00606 void rtp_parse_close(RTPDemuxContext *s)
00607 {
00608
00609 av_free(s->rtp_payload_data->mode);
00610 av_free(s->rtp_payload_data->au_headers);
00611 if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
00612 ff_mpegts_parse_close(s->ts);
00613 }
00614 av_free(s);
00615 }