Skip to content

Instantly share code, notes, and snippets.

@j0sh
Created September 20, 2013 22:26
Show Gist options
  • Select an option

  • Save j0sh/6644754 to your computer and use it in GitHub Desktop.

Select an option

Save j0sh/6644754 to your computer and use it in GitHub Desktop.
commit b8429744c5963b2b475304abe4952fdf3c10589d
Author: Josh Allmann <joshua.allmann@gmail.com>
Date: Fri Sep 20 09:49:22 2013 -0700
rtpenc/wip: Retransmit H.264 extradata periodically.
diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c
index d330607..c9952c9 100644
--- a/libavformat/rtpenc.c
+++ b/libavformat/rtpenc.c
@@ -34,6 +34,7 @@ static const AVOption options[] = {
{ "ssrc", "Stream identifier", offsetof(RTPMuxContext, ssrc), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
{ "cname", "CNAME to include in RTCP SR packets", offsetof(RTPMuxContext, cname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
{ "seq", "Starting sequence number", offsetof(RTPMuxContext, seq), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, AV_OPT_FLAG_ENCODING_PARAM },
+ {"retransmit", "H.264 extradata retransmit time, in ms", offsetof(RTPMuxContext, retransmit_timer), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
{ NULL },
};
@@ -539,6 +540,17 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
rtp_send_mpegts_raw(s1, pkt->data, size);
break;
case AV_CODEC_ID_H264:
+ if (s->retransmit_timer) {
+ if (pkt->pts >= s->next_retransmit) {
+ int64_t next = av_rescale_q(s->retransmit_timer,
+ (AVRational){1, 1000},
+ st->time_base);
+ s->next_retransmit = pkt->pts + next;
+ av_log(s, AV_LOG_INFO, "Sending extradata in sequence %d sz %d first2bytes %x %x \n", s->seq, st->codec->extradata_size, st->codec->extradata[0], st->codec->extradata[1]);
+ ff_rtp_send_h264(s1, st->codec->extradata,
+ st->codec->extradata_size);
+ }
+ }
ff_rtp_send_h264(s1, pkt->data, size);
break;
case AV_CODEC_ID_H263:
diff --git a/libavformat/rtpenc.h b/libavformat/rtpenc.h
index 9d7340d..f4d336f 100644
--- a/libavformat/rtpenc.h
+++ b/libavformat/rtpenc.h
@@ -60,6 +60,8 @@ struct RTPMuxContext {
int flags;
unsigned int frame_count;
+ int retransmit_timer;
+ int64_t next_retransmit;
};
typedef struct RTPMuxContext RTPMuxContext;
diff --git a/libavformat/rtpenc_h264.c b/libavformat/rtpenc_h264.c
index 206d9ba..c4fb8c5 100644
--- a/libavformat/rtpenc_h264.c
+++ b/libavformat/rtpenc_h264.c
@@ -55,6 +55,10 @@ static void nal_send(AVFormatContext *s1, const uint8_t *buf, int size, int last
uint8_t type = buf[0] & 0x1F;
uint8_t nri = buf[0] & 0x60;
+ if (5 == type || 7 == type || 8 == type) {
+ //av_log(s1, AV_LOG_INFO, "Got NAL type %d of length %d at seq %d \n", type, size, s->seq);
+ }
+
if (s->flags & FF_RTP_FLAG_H264_MODE0) {
av_log(s1, AV_LOG_ERROR,
"NAL size %d > %d, try -slice-max-size %d\n", size,
@@ -91,13 +95,16 @@ void ff_rtp_send_h264(AVFormatContext *s1, const uint8_t *buf1, int size)
r = avc_mp4_find_startcode(buf1, end, s->nal_length_size) ? buf1 : end;
else
r = ff_avc_find_startcode(buf1, end);
+if (r >= end) av_log(s, AV_LOG_FATAL, "could not send h264 at %d, r > end!\n", s->seq);
while (r < end) {
const uint8_t *r1;
if (s->nal_length_size) {
r1 = avc_mp4_find_startcode(r, end, s->nal_length_size);
- if (!r1)
+ if (!r1) {
+ av_log(s, AV_LOG_INFO, "startcode not fonud for seq %d! \n", s->seq);
r1 = end;
+ }
r += s->nal_length_size;
} else {
while (!*(r++));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment