[FFmpeg-devel] avformat/rtsp: Add -first_rtcp_ntp_time_path option to write the first NTP time to a file.

Submitted by Jonathan Viney on May 23, 2019, 4:51 a.m.

Details

Message ID CAK-U3HcqNHD3y6urymzuzLom7rNDzfXZbPJ0ySrd=A+1j-P31g@mail.gmail.com
State New
Headers show

Commit Message

Jonathan Viney May 23, 2019, 4:51 a.m.
Apologies, here is the patch in text/plain.

On Thu, May 23, 2019 at 4:44 PM Jonathan Viney <jonathan.viney@gmail.com>
wrote:

> Hi,
>
> The NTP time from the first RTCP packet is currently extracted in
> libavformat/rtsp.c and stored in AVFormatContext.start_time_realtime.
> However, there is no way to access this value when using ffmpeg from the
> commandline.
>
> This patch adds an option when using an RTSP input to write the value to a
> file when it is received.
>
> Eg:
> ffmpeg -first_rtcp_ntp_time_path out.mp4.ntp -i rtsp://10.0.0.1/ -c copy
> out.mp4
>
> Regards,
> -Jonathan.
>
From e16826736640f132f0d3a6f170337ab9696e0038 Mon Sep 17 00:00:00 2001
From: Jonathan Viney <jonathan.viney@gmail.com>
Date: Thu, 23 May 2019 14:24:16 +1200
Subject: [PATCH] avformat/rtsp: add -first_rtcp_ntp_time_path option to write
 the first NTP time to a file.

Signed-off-by: Jonathan Viney <jonathan.viney@gmail.com>
---
 libavformat/rtsp.c | 16 ++++++++++++++++
 libavformat/rtsp.h |  6 ++++++
 2 files changed, 22 insertions(+)

Comments

Moritz Barsnick May 23, 2019, 7:14 a.m.
On Thu, May 23, 2019 at 16:51:16 +1200, Jonathan Viney wrote:
> > The NTP time from the first RTCP packet is currently extracted in
> > libavformat/rtsp.c and stored in AVFormatContext.start_time_realtime.
> > However, there is no way to access this value when using ffmpeg from the
> > commandline.
> >
> > This patch adds an option when using an RTSP input to write the value to a
> > file when it is received.

Is this useful for anything? Would it be more useful in a metadata key?
(Just wondering, not critisizing.)

> +                            av_strlcatf(buf, sizeof(buf), "%lld", s->start_time_realtime);

start_time_realtime is int64_t, so the format identifier should be
'"%" PRIi64'.

Moritz
Jonathan Viney May 23, 2019, 8:10 a.m.
On Thu, May 23, 2019 at 7:14 PM Moritz Barsnick <barsnick@gmx.net> wrote:

> On Thu, May 23, 2019 at 16:51:16 +1200, Jonathan Viney wrote:
> > > The NTP time from the first RTCP packet is currently extracted in
> > > libavformat/rtsp.c and stored in AVFormatContext.start_time_realtime.
> > > However, there is no way to access this value when using ffmpeg from
> the
> > > commandline.
> > >
> > > This patch adds an option when using an RTSP input to write the value
> to a
> > > file when it is received.
>
> Is this useful for anything? Would it be more useful in a metadata key?
> (Just wondering, not critisizing.)
>

I agree having it transferred to a metadata key in the output would be
useful. That was my first thought, but seems like it would be a larger
patch and I wasn't sure if it would be able to write to the metadata for
streaming formats like fragmented MP4 (which is what I'm using) because the
RTCP packet arrives some time after the output metadata would have be
written (from what I understand). Please correct me if I'm wrong about that.


>
> > +                            av_strlcatf(buf, sizeof(buf), "%lld",
> s->start_time_realtime);
>
> start_time_realtime is int64_t, so the format identifier should be
> '"%" PRIi64'.
>

Thanks - will update the patch.

Regards,
-Jonathan.


>
> Moritz
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

Patch hide | download patch | download mbox

diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index c153cac88b..4b048701e2 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -96,6 +96,7 @@  const AVOption ff_rtsp_options[] = {
     { "min_port", "set minimum local UDP port", OFFSET(rtp_port_min), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MIN}, 0, 65535, DEC|ENC },
     { "max_port", "set maximum local UDP port", OFFSET(rtp_port_max), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MAX}, 0, 65535, DEC|ENC },
     { "listen_timeout", "set maximum timeout (in seconds) to wait for incoming connections (-1 is infinite, imply flag listen)", OFFSET(initial_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, DEC },
+    { "first_rtcp_ntp_time_path", "path to write first NTP time (in microseconds) received in RTCP packet", OFFSET(first_rtcp_ntp_time_path), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
 #if FF_API_OLD_RTSP_OPTIONS
     { "timeout", "set maximum timeout (in seconds) to wait for incoming connections (-1 is infinite, imply flag listen) (deprecated, use listen_timeout)", OFFSET(initial_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, DEC },
     { "stimeout", "set timeout (in microseconds) of socket TCP I/O operations", OFFSET(stimeout), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC },
@@ -2256,6 +2257,21 @@  redo:
                                         (uint64_t) rtpctx->st->time_base.num * 1000000,
                                                    rtpctx->st->time_base.den);
                     }
+
+                    // Write the NTP start time
+                    if (rt->first_rtcp_ntp_time_path) {
+                        AVIOContext *ioctx = NULL;
+                        int ret;
+                        ret = avio_open(&ioctx, rt->first_rtcp_ntp_time_path, AVIO_FLAG_WRITE);
+                        if (ret < 0) {
+                            av_log(s, AV_LOG_WARNING, "unable to open %s to write first rtcp ntp time\n", rt->first_rtcp_ntp_time_path);
+                        } else {
+                            char buf[21] = "";
+                            av_strlcatf(buf, sizeof(buf), "%lld", s->start_time_realtime);
+                            avio_write(ioctx, buf, strlen(buf));
+                            avio_closep(&ioctx);
+                        }
+                    }
                 }
             }
             if (ret == -RTCP_BYE) {
diff --git a/libavformat/rtsp.h b/libavformat/rtsp.h
index 54a9a30c16..4df24b743a 100644
--- a/libavformat/rtsp.h
+++ b/libavformat/rtsp.h
@@ -408,6 +408,12 @@  typedef struct RTSPState {
      */
     char *user_agent;
 
+    /**
+     * Path to write the first RTCP unix time in microseconds, if
+     * it is received as part of the stream.
+     */
+    char *first_rtcp_ntp_time_path;
+
     char default_lang[4];
     int buffer_size;
     int pkt_size;