• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/rtpdec.c

Go to the documentation of this file.
00001 /*
00002  * RTP input format
00003  * Copyright (c) 2002 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavcodec/get_bits.h"
00023 #include "avformat.h"
00024 #include "mpegts.h"
00025 #include "url.h"
00026 
00027 #include <unistd.h>
00028 #include <strings.h>
00029 #include "network.h"
00030 
00031 #include "rtpdec.h"
00032 #include "rtpdec_formats.h"
00033 
00034 //#define DEBUG
00035 
00036 /* TODO: - add RTCP statistics reporting (should be optional).
00037 
00038          - add support for h263/mpeg4 packetized output : IDEA: send a
00039          buffer to 'rtp_write_packet' contains all the packets for ONE
00040          frame. Each packet should have a four byte header containing
00041          the length in big endian format (same trick as
00042          'ffio_open_dyn_packet_buf')
00043 */
00044 
00045 static RTPDynamicProtocolHandler ff_realmedia_mp3_dynamic_handler = {
00046     .enc_name           = "X-MP3-draft-00",
00047     .codec_type         = AVMEDIA_TYPE_AUDIO,
00048     .codec_id           = CODEC_ID_MP3ADU,
00049 };
00050 
00051 /* statistics functions */
00052 static RTPDynamicProtocolHandler *RTPFirstDynamicPayloadHandler= NULL;
00053 
00054 void ff_register_dynamic_payload_handler(RTPDynamicProtocolHandler *handler)
00055 {
00056     handler->next= RTPFirstDynamicPayloadHandler;
00057     RTPFirstDynamicPayloadHandler= handler;
00058 }
00059 
00060 void av_register_rtp_dynamic_payload_handlers(void)
00061 {
00062     ff_register_dynamic_payload_handler(&ff_mp4v_es_dynamic_handler);
00063     ff_register_dynamic_payload_handler(&ff_mpeg4_generic_dynamic_handler);
00064     ff_register_dynamic_payload_handler(&ff_amr_nb_dynamic_handler);
00065     ff_register_dynamic_payload_handler(&ff_amr_wb_dynamic_handler);
00066     ff_register_dynamic_payload_handler(&ff_h263_1998_dynamic_handler);
00067     ff_register_dynamic_payload_handler(&ff_h263_2000_dynamic_handler);
00068     ff_register_dynamic_payload_handler(&ff_h264_dynamic_handler);
00069     ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
00070     ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
00071     ff_register_dynamic_payload_handler(&ff_qdm2_dynamic_handler);
00072     ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
00073     ff_register_dynamic_payload_handler(&ff_mp4a_latm_dynamic_handler);
00074     ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
00075     ff_register_dynamic_payload_handler(&ff_qcelp_dynamic_handler);
00076     ff_register_dynamic_payload_handler(&ff_realmedia_mp3_dynamic_handler);
00077 
00078     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfv_handler);
00079     ff_register_dynamic_payload_handler(&ff_ms_rtp_asf_pfa_handler);
00080 
00081     ff_register_dynamic_payload_handler(&ff_qt_rtp_aud_handler);
00082     ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler);
00083     ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler);
00084     ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
00085 
00086     ff_register_dynamic_payload_handler(&ff_g726_16_dynamic_handler);
00087     ff_register_dynamic_payload_handler(&ff_g726_24_dynamic_handler);
00088     ff_register_dynamic_payload_handler(&ff_g726_32_dynamic_handler);
00089     ff_register_dynamic_payload_handler(&ff_g726_40_dynamic_handler);
00090 }
00091 
00092 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
00093                                                   enum AVMediaType codec_type)
00094 {
00095     RTPDynamicProtocolHandler *handler;
00096     for (handler = RTPFirstDynamicPayloadHandler;
00097          handler; handler = handler->next)
00098         if (!strcasecmp(name, handler->enc_name) &&
00099             codec_type == handler->codec_type)
00100             return handler;
00101     return NULL;
00102 }
00103 
00104 RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
00105                                                 enum AVMediaType codec_type)
00106 {
00107     RTPDynamicProtocolHandler *handler;
00108     for (handler = RTPFirstDynamicPayloadHandler;
00109          handler; handler = handler->next)
00110         if (handler->static_payload_id && handler->static_payload_id == id &&
00111             codec_type == handler->codec_type)
00112             return handler;
00113     return NULL;
00114 }
00115 
00116 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
00117 {
00118     int payload_len;
00119     while (len >= 4) {
00120         payload_len = FFMIN(len, (AV_RB16(buf + 2) + 1) * 4);
00121 
00122         switch (buf[1]) {
00123         case RTCP_SR:
00124             if (payload_len < 20) {
00125                 av_log(NULL, AV_LOG_ERROR, "Invalid length for RTCP SR packet\n");
00126                 return AVERROR_INVALIDDATA;
00127             }
00128 
00129             s->last_rtcp_ntp_time = AV_RB64(buf + 8);
00130             s->last_rtcp_timestamp = AV_RB32(buf + 16);
00131             if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
00132                 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
00133                 if (!s->base_timestamp)
00134                     s->base_timestamp = s->last_rtcp_timestamp;
00135                 s->rtcp_ts_offset = s->last_rtcp_timestamp - s->base_timestamp;
00136             }
00137 
00138             break;
00139         case RTCP_BYE:
00140             return -RTCP_BYE;
00141         }
00142 
00143         buf += payload_len;
00144         len -= payload_len;
00145     }
00146     return -1;
00147 }
00148 
00149 #define RTP_SEQ_MOD (1<<16)
00150 
00154 static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence) // called on parse open packet.
00155 {
00156     memset(s, 0, sizeof(RTPStatistics));
00157     s->max_seq= base_sequence;
00158     s->probation= 1;
00159 }
00160 
00164 static void rtp_init_sequence(RTPStatistics *s, uint16_t seq)
00165 {
00166     s->max_seq= seq;
00167     s->cycles= 0;
00168     s->base_seq= seq -1;
00169     s->bad_seq= RTP_SEQ_MOD + 1;
00170     s->received= 0;
00171     s->expected_prior= 0;
00172     s->received_prior= 0;
00173     s->jitter= 0;
00174     s->transit= 0;
00175 }
00176 
00180 static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq)
00181 {
00182     uint16_t udelta= seq - s->max_seq;
00183     const int MAX_DROPOUT= 3000;
00184     const int MAX_MISORDER = 100;
00185     const int MIN_SEQUENTIAL = 2;
00186 
00187     /* source not valid until MIN_SEQUENTIAL packets with sequence seq. numbers have been received */
00188     if(s->probation)
00189     {
00190         if(seq==s->max_seq + 1) {
00191             s->probation--;
00192             s->max_seq= seq;
00193             if(s->probation==0) {
00194                 rtp_init_sequence(s, seq);
00195                 s->received++;
00196                 return 1;
00197             }
00198         } else {
00199             s->probation= MIN_SEQUENTIAL - 1;
00200             s->max_seq = seq;
00201         }
00202     } else if (udelta < MAX_DROPOUT) {
00203         // in order, with permissible gap
00204         if(seq < s->max_seq) {
00205             //sequence number wrapped; count antother 64k cycles
00206             s->cycles += RTP_SEQ_MOD;
00207         }
00208         s->max_seq= seq;
00209     } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
00210         // sequence made a large jump...
00211         if(seq==s->bad_seq) {
00212             // two sequential packets-- assume that the other side restarted without telling us; just resync.
00213             rtp_init_sequence(s, seq);
00214         } else {
00215             s->bad_seq= (seq + 1) & (RTP_SEQ_MOD-1);
00216             return 0;
00217         }
00218     } else {
00219         // duplicate or reordered packet...
00220     }
00221     s->received++;
00222     return 1;
00223 }
00224 
00225 #if 0
00226 
00231 static void rtcp_update_jitter(RTPStatistics *s, uint32_t sent_timestamp, uint32_t arrival_timestamp)
00232 {
00233     uint32_t transit= arrival_timestamp - sent_timestamp;
00234     int d;
00235     s->transit= transit;
00236     d= FFABS(transit - s->transit);
00237     s->jitter += d - ((s->jitter + 8)>>4);
00238 }
00239 #endif
00240 
00241 int rtp_check_and_send_back_rr(RTPDemuxContext *s, int count)
00242 {
00243     AVIOContext *pb;
00244     uint8_t *buf;
00245     int len;
00246     int rtcp_bytes;
00247     RTPStatistics *stats= &s->statistics;
00248     uint32_t lost;
00249     uint32_t extended_max;
00250     uint32_t expected_interval;
00251     uint32_t received_interval;
00252     uint32_t lost_interval;
00253     uint32_t expected;
00254     uint32_t fraction;
00255     uint64_t ntp_time= s->last_rtcp_ntp_time; // TODO: Get local ntp time?
00256 
00257     if (!s->rtp_ctx || (count < 1))
00258         return -1;
00259 
00260     /* TODO: I think this is way too often; RFC 1889 has algorithm for this */
00261     /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
00262     s->octet_count += count;
00263     rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
00264         RTCP_TX_RATIO_DEN;
00265     rtcp_bytes /= 50; // mmu_man: that's enough for me... VLC sends much less btw !?
00266     if (rtcp_bytes < 28)
00267         return -1;
00268     s->last_octet_count = s->octet_count;
00269 
00270     if (avio_open_dyn_buf(&pb) < 0)
00271         return -1;
00272 
00273     // Receiver Report
00274     avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
00275     avio_w8(pb, RTCP_RR);
00276     avio_wb16(pb, 7); /* length in words - 1 */
00277     // our own SSRC: we use the server's SSRC + 1 to avoid conflicts
00278     avio_wb32(pb, s->ssrc + 1);
00279     avio_wb32(pb, s->ssrc); // server SSRC
00280     // some placeholders we should really fill...
00281     // RFC 1889/p64
00282     extended_max= stats->cycles + stats->max_seq;
00283     expected= extended_max - stats->base_seq + 1;
00284     lost= expected - stats->received;
00285     lost= FFMIN(lost, 0xffffff); // clamp it since it's only 24 bits...
00286     expected_interval= expected - stats->expected_prior;
00287     stats->expected_prior= expected;
00288     received_interval= stats->received - stats->received_prior;
00289     stats->received_prior= stats->received;
00290     lost_interval= expected_interval - received_interval;
00291     if (expected_interval==0 || lost_interval<=0) fraction= 0;
00292     else fraction = (lost_interval<<8)/expected_interval;
00293 
00294     fraction= (fraction<<24) | lost;
00295 
00296     avio_wb32(pb, fraction); /* 8 bits of fraction, 24 bits of total packets lost */
00297     avio_wb32(pb, extended_max); /* max sequence received */
00298     avio_wb32(pb, stats->jitter>>4); /* jitter */
00299 
00300     if(s->last_rtcp_ntp_time==AV_NOPTS_VALUE)
00301     {
00302         avio_wb32(pb, 0); /* last SR timestamp */
00303         avio_wb32(pb, 0); /* delay since last SR */
00304     } else {
00305         uint32_t middle_32_bits= s->last_rtcp_ntp_time>>16; // this is valid, right? do we need to handle 64 bit values special?
00306         uint32_t delay_since_last= ntp_time - s->last_rtcp_ntp_time;
00307 
00308         avio_wb32(pb, middle_32_bits); /* last SR timestamp */
00309         avio_wb32(pb, delay_since_last); /* delay since last SR */
00310     }
00311 
00312     // CNAME
00313     avio_w8(pb, (RTP_VERSION << 6) + 1); /* 1 report block */
00314     avio_w8(pb, RTCP_SDES);
00315     len = strlen(s->hostname);
00316     avio_wb16(pb, (6 + len + 3) / 4); /* length in words - 1 */
00317     avio_wb32(pb, s->ssrc);
00318     avio_w8(pb, 0x01);
00319     avio_w8(pb, len);
00320     avio_write(pb, s->hostname, len);
00321     // padding
00322     for (len = (6 + len) % 4; len % 4; len++) {
00323         avio_w8(pb, 0);
00324     }
00325 
00326     avio_flush(pb);
00327     len = avio_close_dyn_buf(pb, &buf);
00328     if ((len > 0) && buf) {
00329         int av_unused result;
00330         av_dlog(s->ic, "sending %d bytes of RR\n", len);
00331         result= ffurl_write(s->rtp_ctx, buf, len);
00332         av_dlog(s->ic, "result from ffurl_write: %d\n", result);
00333         av_free(buf);
00334     }
00335     return 0;
00336 }
00337 
00338 void rtp_send_punch_packets(URLContext* rtp_handle)
00339 {
00340     AVIOContext *pb;
00341     uint8_t *buf;
00342     int len;
00343 
00344     /* Send a small RTP packet */
00345     if (avio_open_dyn_buf(&pb) < 0)
00346         return;
00347 
00348     avio_w8(pb, (RTP_VERSION << 6));
00349     avio_w8(pb, 0); /* Payload type */
00350     avio_wb16(pb, 0); /* Seq */
00351     avio_wb32(pb, 0); /* Timestamp */
00352     avio_wb32(pb, 0); /* SSRC */
00353 
00354     avio_flush(pb);
00355     len = avio_close_dyn_buf(pb, &buf);
00356     if ((len > 0) && buf)
00357         ffurl_write(rtp_handle, buf, len);
00358     av_free(buf);
00359 
00360     /* Send a minimal RTCP RR */
00361     if (avio_open_dyn_buf(&pb) < 0)
00362         return;
00363 
00364     avio_w8(pb, (RTP_VERSION << 6));
00365     avio_w8(pb, RTCP_RR); /* receiver report */
00366     avio_wb16(pb, 1); /* length in words - 1 */
00367     avio_wb32(pb, 0); /* our own SSRC */
00368 
00369     avio_flush(pb);
00370     len = avio_close_dyn_buf(pb, &buf);
00371     if ((len > 0) && buf)
00372         ffurl_write(rtp_handle, buf, len);
00373     av_free(buf);
00374 }
00375 
00376 
00382 RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, int queue_size)
00383 {
00384     RTPDemuxContext *s;
00385 
00386     s = av_mallocz(sizeof(RTPDemuxContext));
00387     if (!s)
00388         return NULL;
00389     s->payload_type = payload_type;
00390     s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
00391     s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
00392     s->ic = s1;
00393     s->st = st;
00394     s->queue_size = queue_size;
00395     rtp_init_statistics(&s->statistics, 0); // do we know the initial sequence from sdp?
00396     if (!strcmp(ff_rtp_enc_name(payload_type), "MP2T")) {
00397         s->ts = ff_mpegts_parse_open(s->ic);
00398         if (s->ts == NULL) {
00399             av_free(s);
00400             return NULL;
00401         }
00402     } else {
00403         switch(st->codec->codec_id) {
00404         case CODEC_ID_MPEG1VIDEO:
00405         case CODEC_ID_MPEG2VIDEO:
00406         case CODEC_ID_MP2:
00407         case CODEC_ID_MP3:
00408         case CODEC_ID_MPEG4:
00409         case CODEC_ID_H263:
00410         case CODEC_ID_H264:
00411             st->need_parsing = AVSTREAM_PARSE_FULL;
00412             break;
00413         case CODEC_ID_ADPCM_G722:
00414             /* According to RFC 3551, the stream clock rate is 8000
00415              * even if the sample rate is 16000. */
00416             if (st->codec->sample_rate == 8000)
00417                 st->codec->sample_rate = 16000;
00418             break;
00419         default:
00420             break;
00421         }
00422     }
00423     // needed to send back RTCP RR in RTSP sessions
00424     s->rtp_ctx = rtpc;
00425     gethostname(s->hostname, sizeof(s->hostname));
00426     return s;
00427 }
00428 
00429 void
00430 rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
00431                                RTPDynamicProtocolHandler *handler)
00432 {
00433     s->dynamic_protocol_context = ctx;
00434     s->parse_packet = handler->parse_packet;
00435 }
00436 
00440 static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
00441 {
00442     if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE)
00443         return; /* Timestamp already set by depacketizer */
00444     if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE && timestamp != RTP_NOTS_VALUE) {
00445         int64_t addend;
00446         int delta_timestamp;
00447 
00448         /* compute pts from timestamp with received ntp_time */
00449         delta_timestamp = timestamp - s->last_rtcp_timestamp;
00450         /* convert to the PTS timebase */
00451         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);
00452         pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend +
00453                    delta_timestamp;
00454         return;
00455     }
00456     if (timestamp == RTP_NOTS_VALUE)
00457         return;
00458     if (!s->base_timestamp)
00459         s->base_timestamp = timestamp;
00460     pkt->pts = s->range_start_offset + timestamp - s->base_timestamp;
00461 }
00462 
00463 static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt,
00464                                      const uint8_t *buf, int len)
00465 {
00466     unsigned int ssrc, h;
00467     int payload_type, seq, ret, flags = 0;
00468     int ext;
00469     AVStream *st;
00470     uint32_t timestamp;
00471     int rv= 0;
00472 
00473     ext = buf[0] & 0x10;
00474     payload_type = buf[1] & 0x7f;
00475     if (buf[1] & 0x80)
00476         flags |= RTP_FLAG_MARKER;
00477     seq  = AV_RB16(buf + 2);
00478     timestamp = AV_RB32(buf + 4);
00479     ssrc = AV_RB32(buf + 8);
00480     /* store the ssrc in the RTPDemuxContext */
00481     s->ssrc = ssrc;
00482 
00483     /* NOTE: we can handle only one payload type */
00484     if (s->payload_type != payload_type)
00485         return -1;
00486 
00487     st = s->st;
00488     // only do something with this if all the rtp checks pass...
00489     if(!rtp_valid_packet_in_sequence(&s->statistics, seq))
00490     {
00491         av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
00492                payload_type, seq, ((s->seq + 1) & 0xffff));
00493         return -1;
00494     }
00495 
00496     if (buf[0] & 0x20) {
00497         int padding = buf[len - 1];
00498         if (len >= 12 + padding)
00499             len -= padding;
00500     }
00501 
00502     s->seq = seq;
00503     len -= 12;
00504     buf += 12;
00505 
00506     /* RFC 3550 Section 5.3.1 RTP Header Extension handling */
00507     if (ext) {
00508         if (len < 4)
00509             return -1;
00510         /* calculate the header extension length (stored as number
00511          * of 32-bit words) */
00512         ext = (AV_RB16(buf + 2) + 1) << 2;
00513 
00514         if (len < ext)
00515             return -1;
00516         // skip past RTP header extension
00517         len -= ext;
00518         buf += ext;
00519     }
00520 
00521     if (!st) {
00522         /* specific MPEG2TS demux support */
00523         ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len);
00524         /* The only error that can be returned from ff_mpegts_parse_packet
00525          * is "no more data to return from the provided buffer", so return
00526          * AVERROR(EAGAIN) for all errors */
00527         if (ret < 0)
00528             return AVERROR(EAGAIN);
00529         if (ret < len) {
00530             s->read_buf_size = len - ret;
00531             memcpy(s->buf, buf + ret, s->read_buf_size);
00532             s->read_buf_index = 0;
00533             return 1;
00534         }
00535         return 0;
00536     } else if (s->parse_packet) {
00537         rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
00538                              s->st, pkt, &timestamp, buf, len, flags);
00539     } else {
00540         // at this point, the RTP header has been stripped;  This is ASSUMING that there is only 1 CSRC, which in't wise.
00541         switch(st->codec->codec_id) {
00542         case CODEC_ID_MP2:
00543         case CODEC_ID_MP3:
00544             /* better than nothing: skip mpeg audio RTP header */
00545             if (len <= 4)
00546                 return -1;
00547             h = AV_RB32(buf);
00548             len -= 4;
00549             buf += 4;
00550             av_new_packet(pkt, len);
00551             memcpy(pkt->data, buf, len);
00552             break;
00553         case CODEC_ID_MPEG1VIDEO:
00554         case CODEC_ID_MPEG2VIDEO:
00555             /* better than nothing: skip mpeg video RTP header */
00556             if (len <= 4)
00557                 return -1;
00558             h = AV_RB32(buf);
00559             buf += 4;
00560             len -= 4;
00561             if (h & (1 << 26)) {
00562                 /* mpeg2 */
00563                 if (len <= 4)
00564                     return -1;
00565                 buf += 4;
00566                 len -= 4;
00567             }
00568             av_new_packet(pkt, len);
00569             memcpy(pkt->data, buf, len);
00570             break;
00571         default:
00572             av_new_packet(pkt, len);
00573             memcpy(pkt->data, buf, len);
00574             break;
00575         }
00576 
00577         pkt->stream_index = st->index;
00578     }
00579 
00580     // now perform timestamp things....
00581     finalize_packet(s, pkt, timestamp);
00582 
00583     return rv;
00584 }
00585 
00586 void ff_rtp_reset_packet_queue(RTPDemuxContext *s)
00587 {
00588     while (s->queue) {
00589         RTPPacket *next = s->queue->next;
00590         av_free(s->queue->buf);
00591         av_free(s->queue);
00592         s->queue = next;
00593     }
00594     s->seq       = 0;
00595     s->queue_len = 0;
00596     s->prev_ret  = 0;
00597 }
00598 
00599 static void enqueue_packet(RTPDemuxContext *s, uint8_t *buf, int len)
00600 {
00601     uint16_t seq = AV_RB16(buf + 2);
00602     RTPPacket *cur = s->queue, *prev = NULL, *packet;
00603 
00604     /* Find the correct place in the queue to insert the packet */
00605     while (cur) {
00606         int16_t diff = seq - cur->seq;
00607         if (diff < 0)
00608             break;
00609         prev = cur;
00610         cur = cur->next;
00611     }
00612 
00613     packet = av_mallocz(sizeof(*packet));
00614     if (!packet)
00615         return;
00616     packet->recvtime = av_gettime();
00617     packet->seq = seq;
00618     packet->len = len;
00619     packet->buf = buf;
00620     packet->next = cur;
00621     if (prev)
00622         prev->next = packet;
00623     else
00624         s->queue = packet;
00625     s->queue_len++;
00626 }
00627 
00628 static int has_next_packet(RTPDemuxContext *s)
00629 {
00630     return s->queue && s->queue->seq == (uint16_t) (s->seq + 1);
00631 }
00632 
00633 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s)
00634 {
00635     return s->queue ? s->queue->recvtime : 0;
00636 }
00637 
00638 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt)
00639 {
00640     int rv;
00641     RTPPacket *next;
00642 
00643     if (s->queue_len <= 0)
00644         return -1;
00645 
00646     if (!has_next_packet(s))
00647         av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
00648                "RTP: missed %d packets\n", s->queue->seq - s->seq - 1);
00649 
00650     /* Parse the first packet in the queue, and dequeue it */
00651     rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len);
00652     next = s->queue->next;
00653     av_free(s->queue->buf);
00654     av_free(s->queue);
00655     s->queue = next;
00656     s->queue_len--;
00657     return rv;
00658 }
00659 
00660 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt,
00661                      uint8_t **bufptr, int len)
00662 {
00663     uint8_t* buf = bufptr ? *bufptr : NULL;
00664     int ret, flags = 0;
00665     uint32_t timestamp;
00666     int rv= 0;
00667 
00668     if (!buf) {
00669         /* If parsing of the previous packet actually returned 0 or an error,
00670          * there's nothing more to be parsed from that packet, but we may have
00671          * indicated that we can return the next enqueued packet. */
00672         if (s->prev_ret <= 0)
00673             return rtp_parse_queued_packet(s, pkt);
00674         /* return the next packets, if any */
00675         if(s->st && s->parse_packet) {
00676             /* timestamp should be overwritten by parse_packet, if not,
00677              * the packet is left with pts == AV_NOPTS_VALUE */
00678             timestamp = RTP_NOTS_VALUE;
00679             rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
00680                                 s->st, pkt, &timestamp, NULL, 0, flags);
00681             finalize_packet(s, pkt, timestamp);
00682             return rv;
00683         } else {
00684             // TODO: Move to a dynamic packet handler (like above)
00685             if (s->read_buf_index >= s->read_buf_size)
00686                 return AVERROR(EAGAIN);
00687             ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
00688                                       s->read_buf_size - s->read_buf_index);
00689             if (ret < 0)
00690                 return AVERROR(EAGAIN);
00691             s->read_buf_index += ret;
00692             if (s->read_buf_index < s->read_buf_size)
00693                 return 1;
00694             else
00695                 return 0;
00696         }
00697     }
00698 
00699     if (len < 12)
00700         return -1;
00701 
00702     if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
00703         return -1;
00704     if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
00705         return rtcp_parse_packet(s, buf, len);
00706     }
00707 
00708     if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) {
00709         /* First packet, or no reordering */
00710         return rtp_parse_packet_internal(s, pkt, buf, len);
00711     } else {
00712         uint16_t seq = AV_RB16(buf + 2);
00713         int16_t diff = seq - s->seq;
00714         if (diff < 0) {
00715             /* Packet older than the previously emitted one, drop */
00716             av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING,
00717                    "RTP: dropping old packet received too late\n");
00718             return -1;
00719         } else if (diff <= 1) {
00720             /* Correct packet */
00721             rv = rtp_parse_packet_internal(s, pkt, buf, len);
00722             return rv;
00723         } else {
00724             /* Still missing some packet, enqueue this one. */
00725             enqueue_packet(s, buf, len);
00726             *bufptr = NULL;
00727             /* Return the first enqueued packet if the queue is full,
00728              * even if we're missing something */
00729             if (s->queue_len >= s->queue_size)
00730                 return rtp_parse_queued_packet(s, pkt);
00731             return -1;
00732         }
00733     }
00734 }
00735 
00745 int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
00746                      uint8_t **bufptr, int len)
00747 {
00748     int rv = rtp_parse_one_packet(s, pkt, bufptr, len);
00749     s->prev_ret = rv;
00750     while (rv == AVERROR(EAGAIN) && has_next_packet(s))
00751         rv = rtp_parse_queued_packet(s, pkt);
00752     return rv ? rv : has_next_packet(s);
00753 }
00754 
00755 void rtp_parse_close(RTPDemuxContext *s)
00756 {
00757     ff_rtp_reset_packet_queue(s);
00758     if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) {
00759         ff_mpegts_parse_close(s->ts);
00760     }
00761     av_free(s);
00762 }
00763 
00764 int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p,
00765                   int (*parse_fmtp)(AVStream *stream,
00766                                     PayloadContext *data,
00767                                     char *attr, char *value))
00768 {
00769     char attr[256];
00770     char *value;
00771     int res;
00772     int value_size = strlen(p) + 1;
00773 
00774     if (!(value = av_malloc(value_size))) {
00775         av_log(stream, AV_LOG_ERROR, "Failed to allocate data for FMTP.");
00776         return AVERROR(ENOMEM);
00777     }
00778 
00779     // remove protocol identifier
00780     while (*p && *p == ' ') p++; // strip spaces
00781     while (*p && *p != ' ') p++; // eat protocol identifier
00782     while (*p && *p == ' ') p++; // strip trailing spaces
00783 
00784     while (ff_rtsp_next_attr_and_value(&p,
00785                                        attr, sizeof(attr),
00786                                        value, value_size)) {
00787 
00788         res = parse_fmtp(stream, data, attr, value);
00789         if (res < 0 && res != AVERROR_PATCHWELCOME) {
00790             av_free(value);
00791             return res;
00792         }
00793     }
00794     av_free(value);
00795     return 0;
00796 }

Generated on Wed Apr 11 2012 07:31:36 for FFmpeg by  doxygen 1.7.1