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