From patchwork Thu May 7 06:16:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: leozhang X-Patchwork-Id: 19522 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2E7C14496D2 for ; Thu, 7 May 2020 09:17:03 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 10E78689D89; Thu, 7 May 2020 09:17:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smg-sh-01.qiyi.com (unknown [101.227.12.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0B717688399 for ; Thu, 7 May 2020 09:16:55 +0300 (EEST) X-AuditID: 65e30cac-a51ff70000002643-b3-5eb3a7d7ed20 Received: from mail.iqiyi.com (Unknown_Domain [10.16.130.5]) by smg-sh-01.qiyi.com (Qiyi mail Gateway) with SMTP id 3A.B8.09795.7D7A3BE5; Thu, 7 May 2020 14:16:55 +0800 (HKT) From: leozhang To: Date: Thu, 7 May 2020 14:16:48 +0800 Message-ID: <1588832208-63370-1-git-send-email-leozhang@qiyi.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.13.40.147] X-ClientProxiedBy: BJ-CAS25.iqiyi.pps (10.11.50.117) To EXCH28B.iqiyi.pps (10.16.148.55) Subject: [FFmpeg-devel] [PATCH v2 1/3] avformat/fifo: add options to slow down writing packets to match real time approximately X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Suggested-by: Nicolas George Reviewed-by: Nicolas George Reviewed-by: Marton Balint Reviewed-by: Andreas Rheinhardt Signed-off-by: leozhang --- doc/muxers.texi | 21 +++++++++++++++++++++ libavformat/fifo.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/doc/muxers.texi b/doc/muxers.texi index 536433b..14528f1 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -2274,6 +2274,17 @@ certain (usually permanent) errors the recovery is not attempted even when Specify whether to wait for the keyframe after recovering from queue overflow or failure. This option is set to 0 (false) by default. +@item realtime @var{bool} +If set to 1 (true), slow down writing packets to match real time approximately. +This is similar to @ref{the realtime or arealtime filters,,the "realtime_002c-arealtime" section in the ffmpeg-filters manual,ffmpeg-filters}. +Please note that in some cases without filtering, such as stream copy, you can also use it. + +@item realtime_speed +It is the same as the speed option to realtime or arealtime filters. + +@item realtime_limit @var{duration} +It is the same as the limit option to realtime or arealtime filters. + @end table @subsection Examples @@ -2291,6 +2302,16 @@ ffmpeg -re -i ... -c:v libx264 -c:a aac -f fifo -fifo_format flv -map 0:v -map 0 @end itemize +@itemize + +@item +Stream something to rtmp server, instead of using -re option. +@example +ffmpeg -i your_input_file -c copy -map 0:v -map 0:a -f fifo -fifo_format flv -realtime 1 rtmp://example.com/live/stream_name +@end example + +@end itemize + @anchor{tee} @section tee diff --git a/libavformat/fifo.c b/libavformat/fifo.c index d11dc66..7acc420 100644 --- a/libavformat/fifo.c +++ b/libavformat/fifo.c @@ -26,6 +26,7 @@ #include "libavutil/threadmessage.h" #include "avformat.h" #include "internal.h" +#include #define FIFO_DEFAULT_QUEUE_SIZE 60 #define FIFO_DEFAULT_MAX_RECOVERY_ATTEMPTS 0 @@ -77,6 +78,17 @@ typedef struct FifoContext { /* Value > 0 signals queue overflow */ volatile uint8_t overflow_flag; + /* Slow down writing packets to match real time approximately */ + int realtime; + + /* Speed factor for the processing when realtime */ + double realtime_speed; + + /* Time limit for the pauses when realtime */ + int64_t realtime_limit; + + int64_t delta; + unsigned inited; } FifoContext; typedef struct FifoThreadContext { @@ -183,6 +195,31 @@ static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt) dst_tb = avf2->streams[s_idx]->time_base; av_packet_rescale_ts(pkt, src_tb, dst_tb); + if (fifo->realtime) { + int64_t pts = av_rescale_q(pkt->dts, dst_tb, AV_TIME_BASE_Q) / fifo->realtime_speed; + int64_t now = av_gettime_relative(); + int64_t sleep = pts - now + fifo->delta; + + if (!fifo->inited) { + sleep = 0; + fifo->delta = now - pts; + fifo->inited = 1; + } + + if (FFABS(sleep) > fifo->realtime_limit / fifo->realtime_speed) { + av_log(avf, AV_LOG_WARNING, "time discontinuity detected: %"PRIi64" us, resetting\n", sleep); + sleep = 0; + fifo->delta = now - pts; + } + + if (sleep > 0) { + av_log(avf, AV_LOG_DEBUG, "sleeping %"PRIi64" us\n", sleep); + for (; sleep > 600000000; sleep -= 600000000) + av_usleep(600000000); + av_usleep(sleep); + } + } + ret = av_write_frame(avf2, pkt); if (ret >= 0) av_packet_unref(pkt); @@ -630,6 +667,15 @@ static const AVOption options[] = { {"recover_any_error", "Attempt recovery regardless of type of the error", OFFSET(recover_any_error), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, + {"realtime", "Slow down writing packets to match real time approximately", OFFSET(realtime), + AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, + + {"realtime_speed", "Speed factor for the processing when realtime", OFFSET(realtime_speed), + AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, DBL_MIN, DBL_MAX, AV_OPT_FLAG_ENCODING_PARAM}, + + {"realtime_limit", "Time limit for the pauses when realtime", OFFSET(realtime_limit), + AV_OPT_TYPE_DURATION, {.i64 = 2000000}, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM}, + {NULL}, }; From patchwork Thu May 7 06:17:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: leozhang X-Patchwork-Id: 19523 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 433754496D2 for ; Thu, 7 May 2020 09:17:20 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 28312689E33; Thu, 7 May 2020 09:17:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smg-bj-02.qiyi.com (unknown [202.108.14.100]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7C4D5689CDF for ; Thu, 7 May 2020 09:17:12 +0300 (EEST) X-AuditID: ca6c0e64-3bfff70000008de1-ae-5eb3a7e5db51 Received: from mail.iqiyi.com (Unknown_Domain [10.16.130.3]) by smg-bj-02.qiyi.com (Qiyi mail Gateway) with SMTP id 31.C9.36321.5E7A3BE5; Thu, 7 May 2020 14:17:09 +0800 (HKT) From: leozhang To: Date: Thu, 7 May 2020 14:17:05 +0800 Message-ID: <1588832225-63654-1-git-send-email-leozhang@qiyi.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.13.40.147] X-ClientProxiedBy: BJ-CAS25.iqiyi.pps (10.11.50.117) To EXCH28B.iqiyi.pps (10.16.148.55) Subject: [FFmpeg-devel] [PATCH v2 2/3] avformat/fifo: add a seperate function which sleeps any specified duration X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Suggested-by: Nicolas George Reviewed-by: Nicolas George Reviewed-by: Marton Balint Reviewed-by: Andreas Rheinhardt Signed-off-by: leozhang --- libavformat/fifo.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libavformat/fifo.c b/libavformat/fifo.c index 7acc420..b819aeb 100644 --- a/libavformat/fifo.c +++ b/libavformat/fifo.c @@ -123,6 +123,13 @@ typedef struct FifoMessage { AVPacket pkt; } FifoMessage; +static void fifo_sleep(int64_t duration) +{ + for (; duration > 600000000; duration -= 600000000) + av_usleep(600000000); + av_usleep(duration); +} + static int fifo_thread_write_header(FifoThreadContext *ctx) { AVFormatContext *avf = ctx->avf; @@ -214,9 +221,7 @@ static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt) if (sleep > 0) { av_log(avf, AV_LOG_DEBUG, "sleeping %"PRIi64" us\n", sleep); - for (; sleep > 600000000; sleep -= 600000000) - av_usleep(600000000); - av_usleep(sleep); + fifo_sleep(sleep); } } From patchwork Thu May 7 06:17:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: leozhang X-Patchwork-Id: 19524 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 782064496D2 for ; Thu, 7 May 2020 09:17:34 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 58E8F689E53; Thu, 7 May 2020 09:17:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smg-bj-02.qiyi.com (unknown [202.108.14.100]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 48048688399 for ; Thu, 7 May 2020 09:17:26 +0300 (EEST) X-AuditID: ca6c0e64-3d7ff70000008de1-af-5eb3a7f530d3 Received: from mail.iqiyi.com (Unknown_Domain [10.16.130.5]) by smg-bj-02.qiyi.com (Qiyi mail Gateway) with SMTP id 51.C9.36321.5F7A3BE5; Thu, 7 May 2020 14:17:25 +0800 (HKT) From: leozhang To: Date: Thu, 7 May 2020 14:17:21 +0800 Message-ID: <1588832241-63824-1-git-send-email-leozhang@qiyi.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.13.40.147] X-ClientProxiedBy: BJ-CAS24.iqiyi.pps (10.15.221.33) To EXCH28B.iqiyi.pps (10.16.148.55) Subject: [FFmpeg-devel] [PATCH v2 3/3] avformat/fifo: add option to delay output X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Suggested-by: Nicolas George Reviewed-by: Nicolas George Reviewed-by: Marton Balint Reviewed-by: Andreas Rheinhardt Signed-off-by: leozhang --- doc/muxers.texi | 14 ++++++++++++++ libavformat/fifo.c | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/doc/muxers.texi b/doc/muxers.texi index 14528f1..272cf24 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -2285,6 +2285,10 @@ It is the same as the speed option to realtime or arealtime filters. @item realtime_limit @var{duration} It is the same as the limit option to realtime or arealtime filters. +@item output_delay @var{duration} +Time to delay output. This will block every output packet for the extra specified duration. Default value is 0. +This option is useful when user wouldn't want broadcast what's comming next immediately in live streaming applications. + @end table @subsection Examples @@ -2312,6 +2316,16 @@ ffmpeg -i your_input_file -c copy -map 0:v -map 0:a -f fifo -fifo_format flv -re @end itemize +@itemize + +@item +Add 20 seconds delay to rtmp stream. +@example +ffmpeg -i your_input_stream_address -c copy -map 0:a -map 0:v -f fifo -realtime 1 -queue_size 6000000 -output_delay 20 -fifo_format flv rtmp://example.com/live/delayed_stream_name +@end example + +@end itemize + @anchor{tee} @section tee diff --git a/libavformat/fifo.c b/libavformat/fifo.c index b819aeb..ba463e4 100644 --- a/libavformat/fifo.c +++ b/libavformat/fifo.c @@ -89,6 +89,9 @@ typedef struct FifoContext { int64_t delta; unsigned inited; + + /* Time to delay output */ + int64_t output_delay; } FifoContext; typedef struct FifoThreadContext { @@ -439,6 +442,8 @@ static void *fifo_consumer_thread(void *data) memset(&fifo_thread_ctx, 0, sizeof(FifoThreadContext)); fifo_thread_ctx.avf = avf; + fifo_sleep(fifo->output_delay); + while (1) { uint8_t just_flushed = 0; @@ -681,6 +686,9 @@ static const AVOption options[] = { {"realtime_limit", "Time limit for the pauses when realtime", OFFSET(realtime_limit), AV_OPT_TYPE_DURATION, {.i64 = 2000000}, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM}, + {"output_delay", "Time to delay output", OFFSET(output_delay), + AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM}, + {NULL}, };