Skip to content

Instantly share code, notes, and snippets.

@moonpfe
Created August 29, 2018 04:42
Show Gist options
  • Save moonpfe/87025e9f5d64303bdf6ad96dda39aa78 to your computer and use it in GitHub Desktop.
Save moonpfe/87025e9f5d64303bdf6ad96dda39aa78 to your computer and use it in GitHub Desktop.
수신한 시간이 아닌 RTSP/RTP 패킷의 시간 정보를 이용
diff --git a/edc-rtsp.c b/edc-rtsp.c
index 161ca75d..8438cfb8 100644
--- a/edc-rtsp.c
+++ b/edc-rtsp.c
@@ -20,6 +20,7 @@
#include <libswresample/swresample.h>
#include <libavutil/random_seed.h>
#include <libavutil/intreadwrite.h>
+#include <libavutil/timestamp.h>
#ifndef _
#if defined(linux) && defined(PACKAGE)
@@ -109,7 +110,6 @@ struct _EdcRtspAudioOut
uint32_t ssrc;
};
-
/**
* EdcRtsp object
*/
@@ -120,6 +120,8 @@ struct _EdcRtsp
EdcRtspFrameRecv *frame_recv;
EdcRtspErrorFunc err_func;
+ int64_t base_timestamp;
+
struct
{
EdcRtspCheckExitFunc func;
@@ -173,6 +175,8 @@ struct _EdcRtsp
int width;
int height;
+ int64_t start_pts;
+
EdcBuf *(*make_frame) (EdcRtsp *rtsp,
AVPacket *pkt);
} video;
@@ -182,6 +186,8 @@ struct _EdcRtsp
int has_stream;
EdcCodecID codec_id;
+ int64_t start_pts;
+
EdcBuf *(*make_frame) (EdcRtsp *rtsp,
AVPacket *pkt);
/**
@@ -567,6 +573,9 @@ edc_rtsp_read_rtp_real (EdcRtsp *rtsp,
if (!pkt || pkt->size <= 0 || !pkt->data)
return NULL;
+ if (rtsp->base_timestamp == 0)
+ rtsp->base_timestamp = edc_get_real_time ();
+
/* RTP extension header */
if (rtsp->fmt.avfctx->rtp_ext_header_len > 0)
{
@@ -584,19 +593,37 @@ edc_rtsp_read_rtp_real (EdcRtsp *rtsp,
buf = NULL;
if (pkt->stream_index == rtsp->fmt.video_stream)
{
+ AVRational tb = { 1, 90000 };
+
+ if (rtsp->video.start_pts < 0)
+ rtsp->video.start_pts = pkt->pts;
+
if (!rtsp->video.has_stream ||
!rtsp->use_stream[EDC_RTSP_VIDEO])
return NULL;
+ if (rtsp->video.start_pts > pkt->pts)
+ rtsp->video.start_pts = pkt->pts;
+
+ pkt->pts -= rtsp->video.start_pts;
+
+ if (0) edc_rtsp_log (rtsp, "video time:%s pts:%ld", av_ts2timestr (pkt->pts, &tb), pkt->pts);
+
buf = rtsp->video.make_frame (rtsp, pkt);
if (buf)
{
buf->width = rtsp->video.width;
buf->height = rtsp->video.height;
+ buf->timestamp = rtsp->base_timestamp + av_rescale_q (pkt->pts, tb, AV_TIME_BASE_Q);
}
}
else if (pkt->stream_index == rtsp->fmt.audio_stream)
{
+ AVRational tb = { 1, 44100 };
+
+ if (rtsp->audio.start_pts < 0)
+ rtsp->audio.start_pts = pkt->pts;
+
if (!rtsp->audio.has_stream ||
!rtsp->use_stream[EDC_RTSP_AUDIO])
return NULL;
@@ -604,7 +631,13 @@ edc_rtsp_read_rtp_real (EdcRtsp *rtsp,
if (rtsp->ignore_audio_frame)
return NULL;
+ pkt->pts -= rtsp->audio.start_pts;
+
+ if (0) edc_rtsp_log (rtsp, "audio time:%s pts:%ld", av_ts2timestr (pkt->pts, &tb), pkt->pts);
+
buf = rtsp->audio.make_frame (rtsp, pkt);
+ if (buf)
+ buf->timestamp = rtsp->base_timestamp + av_rescale_q (pkt->pts, tb, AV_TIME_BASE_Q);
}
else if (pkt->stream_index == rtsp->fmt.metadata_stream)
{
@@ -1891,6 +1924,9 @@ edc_rtsp_start (EdcRtsp *rtsp)
}
rtsp->started = TRUE;
rtsp->key_received = FALSE;
+ rtsp->base_timestamp = 0;
+ rtsp->video.start_pts = -1;
+ rtsp->audio.start_pts = -1;
edc_reset_rel_timestamp (&rtsp->rel_timestamp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment