From patchwork Wed Apr 29 13:25:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: leozhang X-Patchwork-Id: 19370 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 2468244BA87 for ; Wed, 29 Apr 2020 16:25:17 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 09DCF68BFD5; Wed, 29 Apr 2020 16:25:17 +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 DCEBB68BE19 for ; Wed, 29 Apr 2020 16:25:09 +0300 (EEST) X-AuditID: ca6c0e64-3d7ff70000008de1-25-5ea9803176d3 Received: from mail.iqiyi.com (Unknown_Domain [10.16.130.3]) by smg-bj-02.qiyi.com (Qiyi mail Gateway) with SMTP id 81.3C.36321.13089AE5; Wed, 29 Apr 2020 21:25:05 +0800 (HKT) From: leozhang To: Date: Wed, 29 Apr 2020 21:25:00 +0800 Message-ID: <1588166700-76420-1-git-send-email-leozhang@qiyi.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.39.144.213] X-ClientProxiedBy: BJ-CAS31.iqiyi.pps (10.11.50.86) To EXCH28B.iqiyi.pps (10.16.148.55) Subject: [FFmpeg-devel] [PATCH 1/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" Signed-off-by: leozhang --- doc/muxers.texi | 3 +++ libavformat/fifo.c | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/doc/muxers.texi b/doc/muxers.texi index cb2bb42..a74cbc4 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -2271,6 +2271,9 @@ 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 output_delay +Time to delay output, in microseconds. Default value is 0. + @end table @subsection Examples diff --git a/libavformat/fifo.c b/libavformat/fifo.c index d11dc66..bdecf2d 100644 --- a/libavformat/fifo.c +++ b/libavformat/fifo.c @@ -77,6 +77,8 @@ typedef struct FifoContext { /* Value > 0 signals queue overflow */ volatile uint8_t overflow_flag; + /* Time to delay output, in microseconds */ + uint64_t output_delay; } FifoContext; typedef struct FifoThreadContext { @@ -397,6 +399,8 @@ static void *fifo_consumer_thread(void *data) memset(&fifo_thread_ctx, 0, sizeof(FifoThreadContext)); fifo_thread_ctx.avf = avf; + av_usleep(fifo->output_delay); + while (1) { uint8_t just_flushed = 0; @@ -630,6 +634,9 @@ 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}, + {"output_delay", "Time to delay output, in microseconds", OFFSET(output_delay), + AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM}, + {NULL}, }; From patchwork Wed Apr 29 13:25:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: leozhang X-Patchwork-Id: 19371 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 0E0F644BA87 for ; Wed, 29 Apr 2020 16:25:50 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E64A368BFF0; Wed, 29 Apr 2020 16:25:49 +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 8D22868BB52 for ; Wed, 29 Apr 2020 16:25:42 +0300 (EEST) X-AuditID: 65e30cac-a39ff70000002643-b2-5ea98055d47f Received: from mail.iqiyi.com (Unknown_Domain [10.16.130.5]) by smg-sh-01.qiyi.com (Qiyi mail Gateway) with SMTP id 7E.30.09795.55089AE5; Wed, 29 Apr 2020 21:25:41 +0800 (HKT) From: leozhang To: Date: Wed, 29 Apr 2020 21:25:36 +0800 Message-ID: <1588166736-82427-1-git-send-email-leozhang@qiyi.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.49.22.14] X-ClientProxiedBy: BJ-CAS24.iqiyi.pps (10.15.221.33) To EXCH28B.iqiyi.pps (10.16.148.55) Subject: [FFmpeg-devel] [PATCH 2/3] avformat/fifo: add option to write packets in paced way 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" Signed-off-by: leozhang --- doc/muxers.texi | 3 +++ libavformat/fifo.c | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/doc/muxers.texi b/doc/muxers.texi index a74cbc4..5140c00 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -2274,6 +2274,9 @@ queue overflow or failure. This option is set to 0 (false) by default. @item output_delay Time to delay output, in microseconds. Default value is 0. +@item paced +If set to 1 (true), write packets in paced way. Default value is 0 (false). + @end table @subsection Examples diff --git a/libavformat/fifo.c b/libavformat/fifo.c index bdecf2d..81b7e9e 100644 --- a/libavformat/fifo.c +++ b/libavformat/fifo.c @@ -79,6 +79,12 @@ typedef struct FifoContext { /* Time to delay output, in microseconds */ uint64_t output_delay; + + /* If set to 1, write packets in paced way */ + int paced; + + /* Time to start */ + uint64_t start_time; } FifoContext; typedef struct FifoThreadContext { @@ -184,6 +190,14 @@ static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt) 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); + if (fifo->paced) { + uint64_t pts = av_rescale_q(pkt->dts, dst_tb, AV_TIME_BASE_Q); + uint64_t now = av_gettime_relative() - fifo->start_time; + av_assert0(now >= fifo->output_delay); + if (pts > now - fifo->output_delay) { + av_usleep(pts - (now - fifo->output_delay)); + } + } ret = av_write_frame(avf2, pkt); if (ret >= 0) @@ -515,6 +529,8 @@ static int fifo_init(AVFormatContext *avf) return AVERROR(ret); fifo->overflow_flag_lock_initialized = 1; + fifo->start_time = av_gettime_relative(); + return 0; } @@ -637,6 +653,9 @@ static const AVOption options[] = { {"output_delay", "Time to delay output, in microseconds", OFFSET(output_delay), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM}, + {"paced", "Write packets in paced way", OFFSET(paced), + AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, + {NULL}, }; From patchwork Wed Apr 29 13:25:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: leozhang X-Patchwork-Id: 19372 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 DBE3644BDCD for ; Wed, 29 Apr 2020 16:26:08 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C48C968BFEA; Wed, 29 Apr 2020 16:26:08 +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 7527068BD89 for ; Wed, 29 Apr 2020 16:26:01 +0300 (EEST) X-AuditID: ca6c0e64-3bfff70000008de1-28-5ea98067fdc6 Received: from mail.iqiyi.com (Unknown_Domain [10.16.130.5]) by smg-bj-02.qiyi.com (Qiyi mail Gateway) with SMTP id 42.3C.36321.76089AE5; Wed, 29 Apr 2020 21:25:59 +0800 (HKT) From: leozhang To: Date: Wed, 29 Apr 2020 21:25:55 +0800 Message-ID: <1588166755-85064-1-git-send-email-leozhang@qiyi.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.41.132.132] X-ClientProxiedBy: BJ-CAS32.iqiyi.pps (10.11.50.121) To EXCH28B.iqiyi.pps (10.16.148.55) Subject: [FFmpeg-devel] [PATCH 3/3] doc/muxers: add command example how to delay output live stream 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" Signed-off-by: leozhang --- doc/muxers.texi | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/muxers.texi b/doc/muxers.texi index 5140c00..c4dbf7d 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -2294,6 +2294,17 @@ ffmpeg -re -i ... -c:v libx264 -c:a aac -f fifo -fifo_format flv -map 0:v -map 0 @end itemize +@itemize + +@item +Add 20 seconds delay to streaming rtmp. +@example +ffmpeg -i your_input_stream_address -c copy -map 0:a -map 0:v -f fifo -paced 1 -queue_size 6000000 + -output_delay 20000000 -fifo_format flv rtmp://example.com/live/delayed_stream_name +@end example + +@end itemize + @anchor{tee} @section tee