[FFmpeg-devel] [PATCH 1/1] Add "no packet" timeout option for mpegts

Wolfgang Haupt haupt.wolfgang at gmail.com
Mon Oct 31 12:56:45 EET 2022


Retrieving an mpegts stream with only stuffed PAT,
results in endless reading.
This change adds a new timeout that specifies a
timespan in AV_TIME_BASE units until when a full
packet must be read successfully.

Signed-off-by: Wolfgang Haupt <haupt.wolfgang at gmail.com>
---
 libavformat/mpegts.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index d97702fcd7..5b615cca63 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -32,6 +32,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
 #include "libavutil/dovi_meta.h"
+#include "libavutil/time.h"
 #include "libavcodec/avcodec.h"
 #include "libavcodec/bytestream.h"
 #include "libavcodec/get_bits.h"
@@ -180,6 +181,12 @@ struct MpegTSContext {
 
     AVStream *epg_stream;
     AVBufferPool* pools[32];
+
+    /**
+     * Timeout in AV_TIME_BASE units, until at least one packet is read
+     * from the stream.
+     */
+    int64_t packet_read_timeout;
 };
 
 #define MPEGTS_OPTIONS \
@@ -203,6 +210,8 @@ static const AVOption options[] = {
      {.i64 = 0}, 0, 1, 0 },
     {"max_packet_size", "maximum size of emitted packet", offsetof(MpegTSContext, max_packet_size), AV_OPT_TYPE_INT,
      {.i64 = 204800}, 1, INT_MAX/2, AV_OPT_FLAG_DECODING_PARAM },
+    {"packet_read_timeout", "Maximum time utnil at least one packet is successfully read from the stream", offsetof(MpegTSContext, packet_read_timeout), AV_OPT_TYPE_INT64,
+     {.i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_DECODING_PARAM },
     { NULL },
 };
 
@@ -2972,6 +2981,8 @@ static int handle_packets(MpegTSContext *ts, int64_t nb_packets)
     const uint8_t *data;
     int64_t packet_num;
     int ret = 0;
+    time_t start = 0;
+    time_t now = 0;
 
     if (avio_tell(s->pb) != ts->last_pos) {
         int i;
@@ -2996,8 +3007,15 @@ static int handle_packets(MpegTSContext *ts, int64_t nb_packets)
     ts->stop_parse = 0;
     packet_num = 0;
     memset(packet + TS_PACKET_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
+    start = av_gettime_relative();
     for (;;) {
         packet_num++;
+        now = av_gettime_relative();
+        if (now - start > ts->packet_read_timeout) {
+            av_log(ts->stream, AV_LOG_TRACE, "No packet after %"PRId64"ms\n", ts->packet_read_timeout/1000);
+            break;
+        }
+
         if (nb_packets != 0 && packet_num >= nb_packets ||
             ts->stop_parse > 1) {
             ret = AVERROR(EAGAIN);
-- 
2.25.1



More information about the ffmpeg-devel mailing list