diff mbox series

[FFmpeg-devel] libavformat/fifo: avoid repeated failure and recovery

Message ID 20211121170620.1756-1-gy.cft4@gmail.com
State New
Headers show
Series [FFmpeg-devel] libavformat/fifo: avoid repeated failure and recovery
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Ryoji Gyoda Nov. 21, 2021, 5:06 p.m. UTC
If fifo_thread_recover() succeeds immediately after
fifo_thread_dispatch_message() fails, the dts of the packet is scaled
twice, causing cur_dts to be abnormally large and "Application provided
invalid, non monotonically increasing dts to muxer in stream" to occur
repeatedly.

Steps to reproduce:
1. ffmpeg -f lavfi -i testsrc -c:v libx264 -map 0:v -flags +global_header -f fifo -fifo_format flv -attempt_recovery 1 -recover_any_error 1 rtmp://example.com/livekey
2. set a breakpoint on fifo_thread_recover
3. force disconnect from the rtmp server
4. wait for break
5. reconnect to the rtmp server
6. resume execution of ffmpeg

Signed-off-by: Ryoji Gyoda <gy.cft4@gmail.com>
---
 libavformat/fifo.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

Comments

Marton Balint Nov. 29, 2021, 9:25 p.m. UTC | #1
On Mon, 22 Nov 2021, Ryoji Gyoda wrote:

> If fifo_thread_recover() succeeds immediately after
> fifo_thread_dispatch_message() fails, the dts of the packet is scaled
> twice, causing cur_dts to be abnormally large and "Application provided
> invalid, non monotonically increasing dts to muxer in stream" to occur
> repeatedly.
>
> Steps to reproduce:
> 1. ffmpeg -f lavfi -i testsrc -c:v libx264 -map 0:v -flags +global_header -f fifo -fifo_format flv -attempt_recovery 1 -recover_any_error 1 rtmp://example.com/livekey
> 2. set a breakpoint on fifo_thread_recover
> 3. force disconnect from the rtmp server
> 4. wait for break
> 5. reconnect to the rtmp server
> 6. resume execution of ffmpeg
>
> Signed-off-by: Ryoji Gyoda <gy.cft4@gmail.com>
> ---
> libavformat/fifo.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/libavformat/fifo.c b/libavformat/fifo.c
> index 51d7fa5b94..e8caa6ec65 100644
> --- a/libavformat/fifo.c
> +++ b/libavformat/fifo.c
> @@ -183,6 +183,7 @@ static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt)
>     AVFormatContext *avf2 = fifo->avf;
>     AVRational src_tb, dst_tb;
>     int ret, s_idx;
> +    int64_t orig_pts, orig_dts, orig_duration;
>
>     if (fifo->timeshift && pkt->dts != AV_NOPTS_VALUE)
>         atomic_fetch_sub_explicit(&fifo->queue_duration, next_duration(avf, pkt, &ctx->last_received_dts), memory_order_relaxed);
> @@ -198,14 +199,24 @@ static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt)
>         }
>     }
>
> +    orig_pts = pkt->pts;
> +    orig_dts = pkt->dts;
> +    orig_duration = pkt->duration;
>     s_idx = pkt->stream_index;
>     src_tb = avf->streams[s_idx]->time_base;
>     dst_tb = avf2->streams[s_idx]->time_base;
>     av_packet_rescale_ts(pkt, src_tb, dst_tb);
>
>     ret = av_write_frame(avf2, pkt);
> -    if (ret >= 0)
> +    if (ret >= 0) {
>         av_packet_unref(pkt);
> +    }
> +    else {
> +        // avoid scaling twice
> +        pkt->pts = orig_pts;
> +        pkt->dts = orig_dts;
> +        pkt->duration = orig_duration;
> +    }
>     return ret;
> }
>

Thanks, applied.

Regards,
Marton
diff mbox series

Patch

diff --git a/libavformat/fifo.c b/libavformat/fifo.c
index 51d7fa5b94..e8caa6ec65 100644
--- a/libavformat/fifo.c
+++ b/libavformat/fifo.c
@@ -183,6 +183,7 @@  static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt)
     AVFormatContext *avf2 = fifo->avf;
     AVRational src_tb, dst_tb;
     int ret, s_idx;
+    int64_t orig_pts, orig_dts, orig_duration;
 
     if (fifo->timeshift && pkt->dts != AV_NOPTS_VALUE)
         atomic_fetch_sub_explicit(&fifo->queue_duration, next_duration(avf, pkt, &ctx->last_received_dts), memory_order_relaxed);
@@ -198,14 +199,24 @@  static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt)
         }
     }
 
+    orig_pts = pkt->pts;
+    orig_dts = pkt->dts;
+    orig_duration = pkt->duration;
     s_idx = pkt->stream_index;
     src_tb = avf->streams[s_idx]->time_base;
     dst_tb = avf2->streams[s_idx]->time_base;
     av_packet_rescale_ts(pkt, src_tb, dst_tb);
 
     ret = av_write_frame(avf2, pkt);
-    if (ret >= 0)
+    if (ret >= 0) {
         av_packet_unref(pkt);
+    }
+    else {
+        // avoid scaling twice
+        pkt->pts = orig_pts;
+        pkt->dts = orig_dts;
+        pkt->duration = orig_duration;
+    }
     return ret;
 }