diff mbox series

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

Message ID 20221031105645.75816-2-haupt.wolfgang@gmail.com
State New
Headers show
Series Add "no packet timeout" to mpegts | expand

Checks

Context Check Description
yinshiyou/commit_msg_loongarch64 warning The first line of the commit message must start with a context terminated by a colon and a space, for example "lavu/opt: " or "doc: ".
andriy/commit_msg_x86 warning The first line of the commit message must start with a context terminated by a colon and a space, for example "lavu/opt: " or "doc: ".
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 fail Make fate failed
andriy/make_x86 success Make finished
andriy/make_fate_x86 fail Make fate failed

Commit Message

Wolfgang Haupt Oct. 31, 2022, 10:56 a.m. UTC
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@gmail.com>
---
 libavformat/mpegts.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Wolfgang Haupt Oct. 31, 2022, 12:52 p.m. UTC | #1
This fixes mpegts infinitely parsing streams that contain
only stuffed PATs, which apparently happens on some dead
livetv sources.

Wolfgang Haupt (1):
  Add "no packet" timeout option for mpegts

 libavformat/mpegts.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
Wolfgang Haupt Oct. 31, 2022, 12:55 p.m. UTC | #2
This fixes mpegts infinitely parsing streams that contain
only stuffed PATs, which apparently happens on some dead
livetv sources.

Wolfgang Haupt (1):
  Add "no packet" timeout option for mpegts

 libavformat/mpegts.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
diff mbox series

Patch

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);