|
|
|
@ -25,22 +25,19 @@ |
|
|
|
|
#include "libavcodec/get_bits.h" |
|
|
|
|
#include "avformat.h" |
|
|
|
|
#include "mpegts.h" |
|
|
|
|
#include "url.h" |
|
|
|
|
|
|
|
|
|
#include "network.h" |
|
|
|
|
|
|
|
|
|
#include "url.h" |
|
|
|
|
#include "rtpdec.h" |
|
|
|
|
#include "rtpdec_formats.h" |
|
|
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
|
|
|
|
|
/* TODO: - add RTCP statistics reporting (should be optional).
|
|
|
|
|
|
|
|
|
|
- add support for h263/mpeg4 packetized output : IDEA: send a |
|
|
|
|
buffer to 'rtp_write_packet' contains all the packets for ONE |
|
|
|
|
frame. Each packet should have a four byte header containing |
|
|
|
|
the length in big endian format (same trick as |
|
|
|
|
'ffio_open_dyn_packet_buf') |
|
|
|
|
/* TODO:
|
|
|
|
|
* - add RTCP statistics reporting (should be optional). |
|
|
|
|
* |
|
|
|
|
* - add support for H.263/MPEG-4 packetized output: IDEA: send a |
|
|
|
|
* buffer to 'rtp_write_packet' contains all the packets for ONE |
|
|
|
|
* frame. Each packet should have a four byte header containing |
|
|
|
|
* the length in big-endian format (same trick as |
|
|
|
|
* 'ffio_open_dyn_packet_buf'). |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
static RTPDynamicProtocolHandler realmedia_mp3_dynamic_handler = { |
|
|
|
@ -131,7 +128,8 @@ RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id, |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len) |
|
|
|
|
static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, |
|
|
|
|
int len) |
|
|
|
|
{ |
|
|
|
|
int payload_len; |
|
|
|
|
while (len >= 4) { |
|
|
|
@ -140,7 +138,8 @@ static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int l |
|
|
|
|
switch (buf[1]) { |
|
|
|
|
case RTCP_SR: |
|
|
|
|
if (payload_len < 20) { |
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Invalid length for RTCP SR packet\n"); |
|
|
|
|
av_log(NULL, AV_LOG_ERROR, |
|
|
|
|
"Invalid length for RTCP SR packet\n"); |
|
|
|
|
return AVERROR_INVALIDDATA; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -174,7 +173,8 @@ static void rtp_init_statistics(RTPStatistics *s, uint16_t base_sequence) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* called whenever there is a large jump in sequence numbers, or when they get out of probation... |
|
|
|
|
* Called whenever there is a large jump in sequence numbers, |
|
|
|
|
* or when they get out of probation... |
|
|
|
|
*/ |
|
|
|
|
static void rtp_init_sequence(RTPStatistics *s, uint16_t seq) |
|
|
|
|
{ |
|
|
|
@ -189,9 +189,7 @@ static void rtp_init_sequence(RTPStatistics *s, uint16_t seq) |
|
|
|
|
s->transit = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* returns 1 if we should handle this packet. |
|
|
|
|
*/ |
|
|
|
|
/* Returns 1 if we should handle this packet. */ |
|
|
|
|
static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq) |
|
|
|
|
{ |
|
|
|
|
uint16_t udelta = seq - s->max_seq; |
|
|
|
@ -199,7 +197,8 @@ static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq) |
|
|
|
|
const int MAX_MISORDER = 100; |
|
|
|
|
const int MIN_SEQUENTIAL = 2; |
|
|
|
|
|
|
|
|
|
/* source not valid until MIN_SEQUENTIAL packets with sequence seq. numbers have been received */ |
|
|
|
|
/* source not valid until MIN_SEQUENTIAL packets with sequence
|
|
|
|
|
* seq. numbers have been received */ |
|
|
|
|
if (s->probation) { |
|
|
|
|
if (seq == s->max_seq + 1) { |
|
|
|
|
s->probation--; |
|
|
|
@ -223,7 +222,8 @@ static int rtp_valid_packet_in_sequence(RTPStatistics *s, uint16_t seq) |
|
|
|
|
} else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) { |
|
|
|
|
// sequence made a large jump...
|
|
|
|
|
if (seq == s->bad_seq) { |
|
|
|
|
// two sequential packets-- assume that the other side restarted without telling us; just resync.
|
|
|
|
|
/* two sequential packets -- assume that the other side
|
|
|
|
|
* restarted without telling us; just resync. */ |
|
|
|
|
rtp_init_sequence(s, seq); |
|
|
|
|
} else { |
|
|
|
|
s->bad_seq = (seq + 1) & (RTP_SEQ_MOD - 1); |
|
|
|
@ -256,7 +256,7 @@ int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, int count) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
/* TODO: I think this is way too often; RFC 1889 has algorithm for this */ |
|
|
|
|
/* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */ |
|
|
|
|
/* XXX: MPEG pts hardcoded. RTCP send every 0.5 seconds */ |
|
|
|
|
s->octet_count += count; |
|
|
|
|
rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) / |
|
|
|
|
RTCP_TX_RATIO_DEN; |
|
|
|
@ -318,9 +318,8 @@ int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, int count) |
|
|
|
|
avio_w8(pb, len); |
|
|
|
|
avio_write(pb, s->hostname, len); |
|
|
|
|
// padding
|
|
|
|
|
for (len = (6 + len) % 4; len % 4; len++) { |
|
|
|
|
for (len = (6 + len) % 4; len % 4; len++) |
|
|
|
|
avio_w8(pb, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
avio_flush(pb); |
|
|
|
|
len = avio_close_dyn_buf(pb, &buf); |
|
|
|
@ -372,13 +371,14 @@ void ff_rtp_send_punch_packets(URLContext* rtp_handle) |
|
|
|
|
av_free(buf); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* open a new RTP parse context for stream 'st'. 'st' can be NULL for |
|
|
|
|
* MPEG2TS streams to indicate that they should be demuxed inside the |
|
|
|
|
* MPEG2-TS streams to indicate that they should be demuxed inside the |
|
|
|
|
* rtp demux (otherwise AV_CODEC_ID_MPEG2TS packets are returned) |
|
|
|
|
*/ |
|
|
|
|
RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st, URLContext *rtpc, int payload_type, int queue_size) |
|
|
|
|
RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st, |
|
|
|
|
URLContext *rtpc, int payload_type, |
|
|
|
|
int queue_size) |
|
|
|
|
{ |
|
|
|
|
RTPDemuxContext *s; |
|
|
|
|
|
|
|
|
@ -436,7 +436,8 @@ void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This was the second switch in rtp_parse packet. Normalizes time, if required, sets stream_index, etc. |
|
|
|
|
* This was the second switch in rtp_parse packet. |
|
|
|
|
* Normalizes time, if required, sets stream_index, etc. |
|
|
|
|
*/ |
|
|
|
|
static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp) |
|
|
|
|
{ |
|
|
|
@ -452,7 +453,9 @@ static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestam |
|
|
|
|
/* compute pts from timestamp with received ntp_time */ |
|
|
|
|
delta_timestamp = timestamp - s->last_rtcp_timestamp; |
|
|
|
|
/* convert to the PTS timebase */ |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
pkt->pts = s->range_start_offset + s->rtcp_ts_offset + addend + |
|
|
|
|
delta_timestamp; |
|
|
|
|
return; |
|
|
|
@ -460,13 +463,15 @@ static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestam |
|
|
|
|
|
|
|
|
|
if (!s->base_timestamp) |
|
|
|
|
s->base_timestamp = timestamp; |
|
|
|
|
/* assume that the difference is INT32_MIN < x < INT32_MAX, but allow the first timestamp to exceed INT32_MAX */ |
|
|
|
|
/* assume that the difference is INT32_MIN < x < INT32_MAX,
|
|
|
|
|
* but allow the first timestamp to exceed INT32_MAX */ |
|
|
|
|
if (!s->timestamp) |
|
|
|
|
s->unwrapped_timestamp += timestamp; |
|
|
|
|
else |
|
|
|
|
s->unwrapped_timestamp += (int32_t)(timestamp - s->timestamp); |
|
|
|
|
s->timestamp = timestamp; |
|
|
|
|
pkt->pts = s->unwrapped_timestamp + s->range_start_offset - s->base_timestamp; |
|
|
|
|
pkt->pts = s->unwrapped_timestamp + s->range_start_offset - |
|
|
|
|
s->base_timestamp; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt, |
|
|
|
@ -495,9 +500,9 @@ static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt, |
|
|
|
|
|
|
|
|
|
st = s->st; |
|
|
|
|
// only do something with this if all the rtp checks pass...
|
|
|
|
|
if(!rtp_valid_packet_in_sequence(&s->statistics, seq)) |
|
|
|
|
{ |
|
|
|
|
av_log(st?st->codec:NULL, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n", |
|
|
|
|
if (!rtp_valid_packet_in_sequence(&s->statistics, seq)) { |
|
|
|
|
av_log(st ? st->codec : NULL, AV_LOG_ERROR, |
|
|
|
|
"RTP: PT=%02x: bad cseq %04x expected=%04x\n", |
|
|
|
|
payload_type, seq, ((s->seq + 1) & 0xffff)); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
@ -528,7 +533,7 @@ static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!st) { |
|
|
|
|
/* specific MPEG2TS demux support */ |
|
|
|
|
/* specific MPEG2-TS demux support */ |
|
|
|
|
ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len); |
|
|
|
|
/* The only error that can be returned from ff_mpegts_parse_packet
|
|
|
|
|
* is "no more data to return from the provided buffer", so return |
|
|
|
@ -546,11 +551,12 @@ static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt, |
|
|
|
|
rv = s->parse_packet(s->ic, s->dynamic_protocol_context, |
|
|
|
|
s->st, pkt, ×tamp, buf, len, flags); |
|
|
|
|
} else { |
|
|
|
|
// at this point, the RTP header has been stripped; This is ASSUMING that there is only 1 CSRC, which in't wise.
|
|
|
|
|
/* At this point, the RTP header has been stripped;
|
|
|
|
|
* This is ASSUMING that there is only 1 CSRC, which isn't wise. */ |
|
|
|
|
switch (st->codec->codec_id) { |
|
|
|
|
case AV_CODEC_ID_MP2: |
|
|
|
|
case AV_CODEC_ID_MP3: |
|
|
|
|
/* better than nothing: skip mpeg audio RTP header */ |
|
|
|
|
/* better than nothing: skip MPEG audio RTP header */ |
|
|
|
|
if (len <= 4) |
|
|
|
|
return -1; |
|
|
|
|
h = AV_RB32(buf); |
|
|
|
@ -562,14 +568,14 @@ static int rtp_parse_packet_internal(RTPDemuxContext *s, AVPacket *pkt, |
|
|
|
|
break; |
|
|
|
|
case AV_CODEC_ID_MPEG1VIDEO: |
|
|
|
|
case AV_CODEC_ID_MPEG2VIDEO: |
|
|
|
|
/* better than nothing: skip mpeg video RTP header */ |
|
|
|
|
/* better than nothing: skip MPEG video RTP header */ |
|
|
|
|
if (len <= 4) |
|
|
|
|
return -1; |
|
|
|
|
h = AV_RB32(buf); |
|
|
|
|
buf += 4; |
|
|
|
|
len -= 4; |
|
|
|
|
if (h & (1 << 26)) { |
|
|
|
|
/* mpeg2 */ |
|
|
|
|
/* MPEG-2 */ |
|
|
|
|
if (len <= 4) |
|
|
|
|
return -1; |
|
|
|
|
buf += 4; |
|
|
|
@ -789,14 +795,16 @@ int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// remove protocol identifier
|
|
|
|
|
while (*p && *p == ' ') p++; // strip spaces
|
|
|
|
|
while (*p && *p != ' ') p++; // eat protocol identifier
|
|
|
|
|
while (*p && *p == ' ') p++; // strip trailing spaces
|
|
|
|
|
while (*p && *p == ' ') |
|
|
|
|
p++; // strip spaces
|
|
|
|
|
while (*p && *p != ' ') |
|
|
|
|
p++; // eat protocol identifier
|
|
|
|
|
while (*p && *p == ' ') |
|
|
|
|
p++; // strip trailing spaces
|
|
|
|
|
|
|
|
|
|
while (ff_rtsp_next_attr_and_value(&p, |
|
|
|
|
attr, sizeof(attr), |
|
|
|
|
value, value_size)) { |
|
|
|
|
|
|
|
|
|
res = parse_fmtp(stream, data, attr, value); |
|
|
|
|
if (res < 0 && res != AVERROR_PATCHWELCOME) { |
|
|
|
|
av_free(value); |
|
|
|
|