00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/mathematics.h"
00023 #include "libavutil/avstring.h"
00024 #include "libavutil/time.h"
00025 #include "libavcodec/get_bits.h"
00026 #include "avformat.h"
00027 #include "mpegts.h"
00028 #include "url.h"
00029
00030 #include "network.h"
00031
00032 #include "rtpdec.h"
00033 #include "rtpdec_formats.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = {
00047 .enc_name = "X-MP3-draft-00",
00048 .codec_type = AVMEDIA_TYPE_AUDIO,
00049 .codec_id = AV_CODEC_ID_MP3ADU,
00050 };
00051
00052 static RTPDynamicProtocolHandler speex_dynamic_handler = {
00053 .enc_name = "speex",
00054 .codec_type = AVMEDIA_TYPE_AUDIO,
00055 .codec_id = AV_CODEC_ID_SPEEX,
00056 };
00057
00058
00059 static RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler= NULL;
00060
00061 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
00062 {
00063 handler->next= RTPFirstDynamicPayloadHandler;
00064 RTPFirstDynamicPayloadHandler= handler;
00065 }
00066
00067 void av_register_rtp_dynamic_payload_handlers(void)
00068 {
00069 ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
00070 ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
00071 ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
00072 ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
00073 ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
00074 ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
00075 ff_register_dynamic_payload_handler(&ff_h263_rfc2190_dynamic_handler);
00076 ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
00077 ff_register_dynamic_payload_handler(&ff_ilbc_dynamic_handler);
00078 ff_register_dynamic_payload_handler(&ff_jpeg_dynamic_handler);
00079 ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
00080 ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
00081 ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
00082 ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
00083 ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
00084 ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
00085 ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
00086 ff_register_dynamic_payload_handler(&realmedia_mp3_dynamic_handler);
00087 ff_register_dynamic_payload_handler(&speex_dynamic_handler);
00088
00089 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
00090 ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
00091
00092 ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
00093 ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
00094 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
00095 ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
00096
00097 ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
00098 ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
00099 ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
00100 ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
00101 }
00102
00103 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
00104 enum AVMediaType codec_type)
00105 {
00106 RTPDynamicProtocolHandler *handler;
00107 for (handler = RTPFirstDynamicPayloadHandler;
00108 handler; handler = handler->next)
00109 if (!av_strcasecmp(name, handler->enc_name) &&
00110 codec_type == handler->codec_type)
00111 return handler;
00112 return NULL;
00113 }
00114
00115 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
00116 enum AVMediaType codec_type)
00117 {
00118 RTPDynamicProtocolHandler *handler;
00119 for (handler = RTPFirstDynamicPayloadHandler;
00120 handler; handler = handler->next)
00121 if (handler->static_payload_id && handler->static_payload_id == id &&
00122 codec_type == handler->codec_type)
00123 return handler;
00124 return NULL;
00125 }
00126
00127 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
00128 {
00129 int payload_len;
00130 while (len >= 4) {
00131 payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
00132
00133 switch (buf[1]) {
00134 case RTCP_SR:
00135 if (payload_len < 20) {
00136 av_log(NULL, AV_LOG_ERROR, "Invalid length for RTCP SR packet\n");
00137 return AVERROR_INVALIDDATA;
00138 }
00139
00140 s->last_rtcp_ntp_time = AV_RB64(buf + 8);
00141 s->last_rtcp_timestamp = AV_RB32(buf + 16);
00142 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
00143 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
00144 if (!s->base_timestamp)
00145 s->base_timestamp = s->last_rtcp_timestamp;
00146 s->rtcp_ts_offset = s->last_rtcp_timestamp - s->base_timestamp;
00147 }
00148
00149 break;
00150 case RTCP_BYE:
00151 return -RTCP_BYE;
00152 }
00153
00154 buf += payload_len;
00155 len -= payload_len;
00156 }
00157 return -1;
00158 }
00159
00160 #define RTP_SEQ_MOD (1<<16)
00161
00165 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence)
00166 {
00167 memset(s, 0, sizeof(RTPStatistics));
00168 s->max_seq= base_sequence;
00169 s->probation= 1;
00170 }
00171
00175 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
00176 {
00177 s->max_seq= seq;
00178 s->cycles= 0;
00179 s->base_seq= seq -1;
00180 s->bad_seq= RTP_SEQ_MOD + 1;
00181 s->received= 0;
00182 s->expected_prior= 0;
00183 s->received_prior= 0;
00184 s->jitter= 0;
00185 s->transit= 0;
00186 }
00187
00191 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
00192 {
00193 uint16_t udelta= seq - s->max_seq;
00194 const int MAX_DROPOUT= 3000;
00195 const int MAX_MISORDER = 100;
00196 const int MIN_SEQUENTIAL = 2;
00197
00198
00199 if(s->probation)
00200 {
00201 if(seq==s->max_seq + 1) {
00202 s->probation--;
00203 s->max_seq= seq;
00204 if(s->probation==0) {
00205 rtp_init_sequence(s, seq);
00206 s->received++;
00207 return 1;
00208 }
00209 } else {
00210 s->probation= MIN_SEQUENTIAL - 1;
00211 s->max_seq = seq;
00212 }
00213 } else if (udelta < MAX_DROPOUT) {
00214
00215 if(seq < s->max_seq) {
00216
00217 s->cycles += RTP_SEQ_MOD;
00218 }
00219 s->max_seq= seq;
00220 } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
00221
00222 if(seq==s->bad_seq) {
00223
00224 rtp_init_sequence(s, seq);
00225 } else {
00226 s->bad_seq= (seq + 1) & (RTP_SEQ_MOD-1);
00227 return 0;
00228 }
00229 } else {
00230
00231 }
00232 s->received++;
00233 return 1;
00234 }
00235
00236 int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, int count)
00237 {
00238 AVIOContext *pb;
00239 uint8_t *buf;
00240 int len;
00241 int rtcp_bytes;
00242 RTPStatistics *stats= &s->statistics;
00243 uint32_t lost;
00244 uint32_t extended_max;
00245 uint32_t expected_interval;
00246 uint32_t received_interval;
00247 uint32_t lost_interval;
00248 uint32_t expected;
00249 uint32_t fraction;
00250 uint64_t ntp_time= s->last_rtcp_ntp_time;
00251
00252 if (!s->rtp_ctx || (count < 1))
00253 return -1;
00254
00255
00256
00257 s->octet_count += count;
00258 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
00259 RTCP_TX_RATIO_DEN;
00260 rtcp_bytes /= 50;
00261 if (rtcp_bytes < 28)
00262 return -1;
00263 s->last_octet_count = s->octet_count;
00264
00265 if (avio_open_dyn_buf(&pb) < 0)
00266 return -1;
00267
00268
00269 avio_w8(pb, (RTP_VERSION << 6) + 1);
00270 avio_w8(pb, RTCP_RR);
00271 avio_wb16(pb, 7);
00272
00273 avio_wb32(pb, s->ssrc + 1);
00274 avio_wb32(pb, s->ssrc);
00275
00276
00277 extended_max= stats->cycles + stats->max_seq;
00278 expected= extended_max - stats->base_seq + 1;
00279 lost= expected - stats->received;
00280 lost= FFMIN(lost, 0xffffff);
00281 expected_interval= expected - stats->expected_prior;
00282 stats->expected_prior= expected;
00283 received_interval= stats->received - stats->received_prior;
00284 stats->received_prior= stats->received;
00285 lost_interval= expected_interval - received_interval;
00286 if (expected_interval==0 || lost_interval<=0) fraction= 0;
00287 else fraction = (lost_interval<<8)/expected_interval;
00288
00289 fraction= (fraction<<24) | lost;
00290
00291 avio_wb32(pb, fraction);
00292 avio_wb32(pb, extended_max);
00293 avio_wb32(pb, stats->jitter>>4);
00294
00295 if(s->last_rtcp_ntp_time==AV_NOPTS_VALUE)
00296 {
00297 avio_wb32(pb, 0);
00298 avio_wb32(pb, 0);
00299 } else {
00300 uint32_t middle_32_bits= s->last_rtcp_ntp_time>>16;
00301 uint32_t delay_since_last= ntp_time - s->last_rtcp_ntp_time;
00302
00303 avio_wb32(pb, middle_32_bits);
00304 avio_wb32(pb, delay_since_last);
00305 }
00306
00307
00308 avio_w8(pb, (RTP_VERSION << 6) + 1);
00309 avio_w8(pb, RTCP_SDES);
00310 len = strlen(s->hostname);
00311 avio_wb16(pb, (6 + len + 3) / 4);
00312 avio_wb32(pb, s->ssrc + 1);
00313 avio_w8(pb, 0x01);
00314 avio_w8(pb, len);
00315 avio_write(pb, s->hostname, len);
00316
00317 for (len = (6 + len) % 4; len % 4; len++) {
00318 avio_w8(pb, 0);
00319 }
00320
00321 avio_flush(pb);
00322 len = avio_close_dyn_buf(pb, &buf);
00323 if ((len > 0) && buf) {
00324 int av_unused result;
00325 av_dlog(s->ic, "sending %d bytes of RR\n", len);
00326 result= ffurl_write(s->rtp_ctx, buf, len);
00327 av_dlog(s->ic, "result from ffurl_write: %d\n", result);
00328 av_free(buf);
00329 }
00330 return 0;
00331 }
00332
00333 void ff_rtp_send_punch_packets(URLContext* rtp_handle)
00334 {
00335 AVIOContext *pb;
00336 uint8_t *buf;
00337 int len;
00338
00339
00340 if (avio_open_dyn_buf(&pb) < 0)
00341 return;
00342
00343 avio_w8(pb, (RTP_VERSION << 6));
00344 avio_w8(pb, 0);
00345 avio_wb16(pb, 0);
00346 avio_wb32(pb, 0);
00347 avio_wb32(pb, 0);
00348
00349 avio_flush(pb);
00350 len = avio_close_dyn_buf(pb, &buf);
00351 if ((len > 0) && buf)
00352 ffurl_write(rtp_handle, buf, len);
00353 av_free(buf);
00354
00355
00356 if (avio_open_dyn_buf(&pb) < 0)
00357 return;
00358
00359 avio_w8(pb, (RTP_VERSION << 6));
00360 avio_w8(pb, RTCP_RR);
00361 avio_wb16(pb, 1);
00362 avio_wb32(pb, 0);
00363
00364 avio_flush(pb);
00365 len = avio_close_dyn_buf(pb, &buf);
00366 if ((len > 0) && buf)
00367 ffurl_write(rtp_handle, buf, len);
00368 av_free(buf);
00369 }
00370
00371
00377 RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, int queue_size)
00378 {
00379 RTPDemuxContext *s;
00380
00381 s = av_mallocz(sizeof(RTPDemuxContext));
00382 if (!s)
00383 return NULL;
00384 s->payload_type = payload_type;
00385 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
00386 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
00387 s->ic = s1;
00388 s->st = st;
00389 s->queue_size = queue_size;
00390 rtp_init_statistics(&s->statistics, 0);
00391 if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
00392 s->ts = ff_mpegts_parse_open(s->ic);
00393 if (s->ts == NULL) {
00394 av_free(s);
00395 return NULL;
00396 }
00397 } else if (st) {
00398 switch(st->codec->codec_id) {
00399 case AV_CODEC_ID_MPEG1VIDEO:
00400 case AV_CODEC_ID_MPEG2VIDEO:
00401 case AV_CODEC_ID_MP2:
00402 case AV_CODEC_ID_MP3:
00403 case AV_CODEC_ID_MPEG4:
00404 case AV_CODEC_ID_H263:
00405 case AV_CODEC_ID_H264:
00406 st->need_parsing = AVSTREAM_PARSE_FULL;
00407 break;
00408 case AV_CODEC_ID_VORBIS:
00409 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00410 break;
00411 case AV_CODEC_ID_ADPCM_G722:
00412
00413
00414 if (st->codec->sample_rate == 8000)
00415 st->codec->sample_rate = 16000;
00416 break;
00417 default:
00418 break;
00419 }
00420 }
00421
00422 s->rtp_ctx = rtpc;
00423 gethostname(s->hostname, sizeof(s->hostname));
00424 return s;
00425 }
00426
00427 void
00428 ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
00429 RTPDynamicProtocolHandler *handler)
00430 {
00431 s->dynamic_protocol_context = ctx;
00432 s->parse_packet = handler->parse_packet;
00433 }
00434
00438 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
00439 {
00440 if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
00441 return;
00442 if (timestamp == RTP_NOTS_VALUE)
00443 return;
00444
00445 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && s->ic->nb_streams > 1) {
00446 int64_t addend;
00447 int delta_timestamp;
00448
00449
00450 delta_timestamp = timestamp - s->last_rtcp_timestamp;
00451
00452 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);
00453 pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
00454 delta_timestamp;
00455 return;
00456 }
00457
00458 if (!s->base_timestamp)
00459 s->base_timestamp = timestamp;
00460
00461 if (!s->timestamp)
00462 s->unwrapped_timestamp += timestamp;
00463 else
00464 s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp);
00465 s->timestamp = timestamp;
00466 pkt->pts = s->unwrapped_timestamp + s->range_start_offset - s->base_timestamp;
00467 }
00468
00469 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
00470 const uint8_t *buf, int len)
00471 {
00472 unsigned int ssrc, h;
00473 int payload_type, seq, ret, flags = 0;
00474 int ext;
00475 AVStream *st;
00476 uint32_t timestamp;
00477 int rv= 0;
00478
00479 ext = buf[0] & 0x10;
00480 payload_type = buf[1] & 0x7f;
00481 if (buf[1] & 0x80)
00482 flags |= RTP_FLAG_MARKER;
00483 seq = AV_RB16(buf + 2);
00484 timestamp = AV_RB32(buf + 4);
00485 ssrc = AV_RB32(buf + 8);
00486
00487 s->ssrc = ssrc;
00488
00489
00490 if (s->payload_type != payload_type)
00491 return -1;
00492
00493 st = s->st;
00494
00495 if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
00496 {
00497 av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
00498 payload_type, seq, ((s->seq + 1) & 0xffff));
00499 return -1;
00500 }
00501
00502 if (buf[0] & 0x20) {
00503 int padding = buf[len - 1];
00504 if (len >= 12 + padding)
00505 len -= padding;
00506 }
00507
00508 s->seq = seq;
00509 len -= 12;
00510 buf += 12;
00511
00512
00513 if (ext) {
00514 if (len < 4)
00515 return -1;
00516
00517
00518 ext = (AV_RB16(buf + 2) + 1) << 2;
00519
00520 if (len < ext)
00521 return -1;
00522
00523 len -= ext;
00524 buf += ext;
00525 }
00526
00527 if (!st) {
00528
00529 ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
00530
00531
00532
00533 if (ret < 0)
00534 return AVERROR(EAGAIN);
00535 if (ret < len) {
00536 s->read_buf_size = len - ret;
00537 memcpy(s->buf, buf + ret, s->read_buf_size);
00538 s->read_buf_index = 0;
00539 return 1;
00540 }
00541 return 0;
00542 } else if (s->parse_packet) {
00543 rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
00544 s->st, pkt, ×tamp, buf, len, flags);
00545 } else {
00546
00547 switch(st->codec->codec_id) {
00548 case AV_CODEC_ID_MP2:
00549 case AV_CODEC_ID_MP3:
00550
00551 if (len <= 4)
00552 return -1;
00553 h = AV_RB32(buf);
00554 len -= 4;
00555 buf += 4;
00556 av_new_packet(pkt, len);
00557 memcpy(pkt->data, buf, len);
00558 break;
00559 case AV_CODEC_ID_MPEG1VIDEO:
00560 case AV_CODEC_ID_MPEG2VIDEO:
00561
00562 if (len <= 4)
00563 return -1;
00564 h = AV_RB32(buf);
00565 buf += 4;
00566 len -= 4;
00567 if (h & (1 << 26)) {
00568
00569 if (len <= 4)
00570 return -1;
00571 buf += 4;
00572 len -= 4;
00573 }
00574 av_new_packet(pkt, len);
00575 memcpy(pkt->data, buf, len);
00576 break;
00577 default:
00578 av_new_packet(pkt, len);
00579 memcpy(pkt->data, buf, len);
00580 break;
00581 }
00582
00583 pkt->stream_index = st->index;
00584 }
00585
00586
00587 finalize_packet(s, pkt, timestamp);
00588
00589 return rv;
00590 }
00591
00592 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
00593 {
00594 while (s->queue) {
00595 RTPPacket *next = s->queue->next;
00596 av_free(s->queue->buf);
00597 av_free(s->queue);
00598 s->queue = next;
00599 }
00600 s->seq = 0;
00601 s->queue_len = 0;
00602 s->prev_ret = 0;
00603 }
00604
00605 static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
00606 {
00607 uint16_t seq = AV_RB16(buf + 2);
00608 RTPPacket *cur = s->queue, *prev = NULL, *packet;
00609
00610
00611 while (cur) {
00612 int16_t diff = seq - cur->seq;
00613 if (diff < 0)
00614 break;
00615 prev = cur;
00616 cur = cur->next;
00617 }
00618
00619 packet = av_mallocz(sizeof(*packet));
00620 if (!packet)
00621 return;
00622 packet->recvtime = av_gettime();
00623 packet->seq = seq;
00624 packet->len = len;
00625 packet->buf = buf;
00626 packet->next = cur;
00627 if (prev)
00628 prev->next = packet;
00629 else
00630 s->queue = packet;
00631 s->queue_len++;
00632 }
00633
00634 static int has_next_packet(RTPDemuxContext *s)
00635 {
00636 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
00637 }
00638
00639 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
00640 {
00641 return s->queue ? s->queue->recvtime : 0;
00642 }
00643
00644 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
00645 {
00646 int rv;
00647 RTPPacket *next;
00648
00649 if (s->queue_len <= 0)
00650 return -1;
00651
00652 if (!has_next_packet(s))
00653 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
00654 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
00655
00656
00657 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
00658 next = s->queue->next;
00659 av_free(s->queue->buf);
00660 av_free(s->queue);
00661 s->queue = next;
00662 s->queue_len--;
00663 return rv;
00664 }
00665
00666 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
00667 uint8_t **bufptr, int len)
00668 {
00669 uint8_t* buf = bufptr ? *bufptr : NULL;
00670 int ret, flags = 0;
00671 uint32_t timestamp;
00672 int rv= 0;
00673
00674 if (!buf) {
00675
00676
00677
00678 if (s->prev_ret <= 0)
00679 return rtp_parse_queued_packet(s, pkt);
00680
00681 if(s->st && s->parse_packet) {
00682
00683
00684 timestamp = RTP_NOTS_VALUE;
00685 rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
00686 s->st, pkt, ×tamp, NULL, 0, flags);
00687 finalize_packet(s, pkt, timestamp);
00688 return rv;
00689 } else {
00690
00691 if (s->read_buf_index >= s->read_buf_size)
00692 return AVERROR(EAGAIN);
00693 ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
00694 s->read_buf_size - s->read_buf_index);
00695 if (ret < 0)
00696 return AVERROR(EAGAIN);
00697 s->read_buf_index += ret;
00698 if (s->read_buf_index < s->read_buf_size)
00699 return 1;
00700 else
00701 return 0;
00702 }
00703 }
00704
00705 if (len < 12)
00706 return -1;
00707
00708 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
00709 return -1;
00710 if (RTP_PT_IS_RTCP(buf[1])) {
00711 return rtcp_parse_packet(s, buf, len);
00712 }
00713
00714 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
00715
00716 return rtp_parse_packet_internal(s, pkt, buf, len);
00717 } else {
00718 uint16_t seq = AV_RB16(buf + 2);
00719 int16_t diff = seq - s->seq;
00720 if (diff < 0) {
00721
00722 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
00723 "RTP: dropping old packet received too late\n");
00724 return -1;
00725 } else if (diff <= 1) {
00726
00727 rv = rtp_parse_packet_internal(s, pkt, buf, len);
00728 return rv;
00729 } else {
00730
00731 enqueue_packet(s, buf, len);
00732 *bufptr = NULL;
00733
00734
00735 if (s->queue_len >= s->queue_size)
00736 return rtp_parse_queued_packet(s, pkt);
00737 return -1;
00738 }
00739 }
00740 }
00741
00751 int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
00752 uint8_t **bufptr, int len)
00753 {
00754 int rv = rtp_parse_one_packet(s, pkt, bufptr, len);
00755 s->prev_ret = rv;
00756 while (rv == AVERROR(EAGAIN) && has_next_packet(s))
00757 rv = rtp_parse_queued_packet(s, pkt);
00758 return rv ? rv : has_next_packet(s);
00759 }
00760
00761 void ff_rtp_parse_close(RTPDemuxContext *s)
00762 {
00763 ff_rtp_reset_packet_queue(s);
00764 if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
00765 ff_mpegts_parse_close(s->ts);
00766 }
00767 av_free(s);
00768 }
00769
00770 int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
00771 int (*parse_fmtp)(AVStream *stream,
00772 PayloadContext *data,
00773 char *attr, char *value))
00774 {
00775 char attr[256];
00776 char *value;
00777 int res;
00778 int value_size = strlen(p) + 1;
00779
00780 if (!(value = av_malloc(value_size))) {
00781 av_log(stream, AV_LOG_ERROR, "Failed to allocate data for FMTP.\n");
00782 return AVERROR(ENOMEM);
00783 }
00784
00785
00786 while (*p && *p == ' ') p++;
00787 while (*p && *p != ' ') p++;
00788 while (*p && *p == ' ') p++;
00789
00790 while (ff_rtsp_next_attr_and_value(&p,
00791 attr, sizeof(attr),
00792 value, value_size)) {
00793
00794 res = parse_fmtp(stream, data, attr, value);
00795 if (res < 0 && res != AVERROR_PATCHWELCOME) {
00796 av_free(value);
00797 return res;
00798 }
00799 }
00800 av_free(value);
00801 return 0;
00802 }