From patchwork Sun Aug 14 21:15:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sebechlebskyjan@gmail.com X-Patchwork-Id: 167 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.67 with SMTP id o64csp1240507vsd; Sun, 14 Aug 2016 14:15:37 -0700 (PDT) X-Received: by 10.194.32.73 with SMTP id g9mr32636991wji.80.1471209337495; Sun, 14 Aug 2016 14:15:37 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ne1si16856297wjb.47.2016.08.14.14.15.35; Sun, 14 Aug 2016 14:15:37 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B8132689A84; Mon, 15 Aug 2016 00:15:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0724E68981C for ; Mon, 15 Aug 2016 00:15:20 +0300 (EEST) Received: by mail-wm0-f67.google.com with SMTP id i138so7999183wmf.3 for ; Sun, 14 Aug 2016 14:15:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GV8VOKdY0uOgv1aNmb9DHrDd+c7YCD4O80kbqAOOwKs=; b=iPNzVTHBtKT/nhIy491wO6blHvF26TJPsU8JWJYVPuZARs57KQQZijcs1k3302s38y o/KXF71gGXKP8RT5P4GlpNWE87yvNfuvFWqP3sfCdPPf89ftSlzxWt+yqY2OgNEQb4KA dpqaw1IOk3BAAw+zbJhhdrNzvZUVqx9jaRLuLYf8NqJw98sdzEHi5QjSiTAL+DsntQOe 0CXU23SzTTp1Ltta+1MRI+JxpIKnb/cSc5HTiE1pjdmy46smh4ggod1KevRApLr2rpMk WCQNrguXKNBC0CyAo96VDhidOwnnSwUQxdMgHq7cb9+xx2dGQqOrOIVYCQv5qu6C3Gus NtLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=GV8VOKdY0uOgv1aNmb9DHrDd+c7YCD4O80kbqAOOwKs=; b=MpLCCvh2jzL69ESIk36vi0Nf9PD+NvD0m4SNN8ed2jLVQsbgSYCiVIIFoiZ5vRIaiO KqvzMwU1u5uIIi15IQxD8xZRxL0lBWQxQTZ2qEHme638DiEmMjJw2+2mbl+3NNdRH83S 9Vdrti+1dLgwXFa6bDTwe21MEaX6hwvj0CABV/2dhWmY8OwDCuUCzMKI4H90VWzHrnpH 9YOnsQpgJ8mTC11gw0OoJLXgDLA1TWZwJKZfyjrR3vWGjJc/g6XLOSrWxWEWDpWkicdj 3q3VHOg9mAzgE4erK09EezK68FE58dJkjIxa+Dds3ve+TYyuMwMfWs0yzu2bQFjCKJrH H3ZQ== X-Gm-Message-State: AEkoouvdLHOnPhfxWXbiF2aQ1eKegp+PaN+OTy847dxGsx6qd054IxXlW9/bae7pVAHRPQ== X-Received: by 10.194.81.137 with SMTP id a9mr27422326wjy.106.1471209320918; Sun, 14 Aug 2016 14:15:20 -0700 (PDT) Received: from localhost.localdomain (157.174.broadband3.iol.cz. [85.70.174.157]) by smtp.gmail.com with ESMTPSA id yz6sm18292200wjb.35.2016.08.14.14.15.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 14 Aug 2016 14:15:20 -0700 (PDT) From: sebechlebskyjan@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Sun, 14 Aug 2016 23:15:08 +0200 Message-Id: <1471209308-25838-2-git-send-email-sebechlebskyjan@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1471209308-25838-1-git-send-email-sebechlebskyjan@gmail.com> References: <1471209308-25838-1-git-send-email-sebechlebskyjan@gmail.com> Subject: [FFmpeg-devel] [PATCH v5 10/11] avformat/fifo: Add AVFMT_FLAG_NONBLOCK support 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 Cc: Jan Sebechlebsky MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Jan Sebechlebsky Add support for nonblocking calls. Signed-off-by: Jan Sebechlebsky --- No changes since the last version of the patch, just rebased because of changes in previous patch. libavformat/fifo.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/libavformat/fifo.c b/libavformat/fifo.c index 3748fa9..ef0fda4 100644 --- a/libavformat/fifo.c +++ b/libavformat/fifo.c @@ -19,12 +19,14 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/atomic.h" #include "libavutil/opt.h" #include "libavutil/time.h" #include "libavutil/thread.h" #include "libavutil/threadmessage.h" #include "avformat.h" #include "internal.h" +#include "url.h" #define FIFO_DEFAULT_QUEUE_SIZE 60 #define FIFO_DEFAULT_MAX_RECOVERY_ATTEMPTS 0 @@ -77,6 +79,17 @@ typedef struct FifoContext { /* Value > 0 signals queue overflow */ volatile uint8_t overflow_flag; + /* Whether termination was requested by invoking deinit + * before the thread was finished. Used only in non-blocking + * mode - when AVFMT_FLAG_NONBLOCK is set. */ + volatile int termination_requested; + + /* Initially 0, set to 1 immediately before thread function + * returns */ + volatile int thread_finished_flag; + + /* Original interrupt callback of the underlying muxer. */ + AVIOInterruptCB orig_interrupt_callback; } FifoContext; typedef struct FifoThreadContext { @@ -111,6 +124,16 @@ typedef struct FifoMessage { AVPacket pkt; } FifoMessage; +static int fifo_interrupt_callback_wrapper(void *arg) +{ + FifoContext *ctx = arg; + + if (avpriv_atomic_int_get(&ctx->termination_requested)) + return 1; + + return ff_check_interrupt(&ctx->orig_interrupt_callback); +} + static int fifo_thread_write_header(FifoThreadContext *ctx) { AVFormatContext *avf = ctx->avf; @@ -430,12 +453,17 @@ static void *fifo_consumer_thread(void *data) fifo->write_trailer_ret = fifo_thread_write_trailer(&fifo_thread_ctx); + /* This must be only return path from fifo_consumer_thread function, + * so the thread_finised_flag is set. */ + avpriv_atomic_int_set(&fifo->thread_finished_flag, 1); return NULL; } static int fifo_mux_init(AVFormatContext *avf) { FifoContext *fifo = avf->priv_data; + AVIOInterruptCB interrupt_cb = {.callback = fifo_interrupt_callback_wrapper, + .opaque = fifo}; AVFormatContext *avf2; int ret = 0, i; @@ -446,7 +474,8 @@ static int fifo_mux_init(AVFormatContext *avf) fifo->avf = avf2; - avf2->interrupt_callback = avf->interrupt_callback; + fifo->orig_interrupt_callback = avf->interrupt_callback; + avf2->interrupt_callback = interrupt_cb; avf2->max_delay = avf->max_delay; ret = av_dict_copy(&avf2->metadata, avf->metadata, 0); if (ret < 0) @@ -533,7 +562,7 @@ static int fifo_write_packet(AVFormatContext *avf, AVPacket *pkt) { FifoContext *fifo = avf->priv_data; FifoMessage msg = {.type = pkt ? FIFO_WRITE_PACKET : FIFO_FLUSH_OUTPUT}; - int ret; + int ret, queue_flags = 0; if (pkt) { av_init_packet(&msg.pkt); @@ -542,15 +571,21 @@ static int fifo_write_packet(AVFormatContext *avf, AVPacket *pkt) return ret; } - ret = av_thread_message_queue_send(fifo->queue, &msg, - fifo->drop_pkts_on_overflow ? - AV_THREAD_MESSAGE_NONBLOCK : 0); + if (fifo->drop_pkts_on_overflow || (avf->flags & AVFMT_FLAG_NONBLOCK)) + queue_flags |= AV_THREAD_MESSAGE_NONBLOCK; + + ret = av_thread_message_queue_send(fifo->queue, &msg, queue_flags); + if (ret == AVERROR(EAGAIN)) { - uint8_t overflow_set = 0; + uint8_t overflow_set; + + if (avf->flags & AVFMT_FLAG_NONBLOCK) + goto fail; /* Queue is full, set fifo->overflow_flag to 1 * to let consumer thread know the queue should * be flushed. */ + overflow_set = 0; pthread_mutex_lock(&fifo->overflow_flag_lock); if (!fifo->overflow_flag) fifo->overflow_flag = overflow_set = 1; @@ -578,6 +613,10 @@ static int fifo_write_trailer(AVFormatContext *avf) av_thread_message_queue_set_err_recv(fifo->queue, AVERROR_EOF); + if ((avf->flags & AVFMT_FLAG_NONBLOCK) && + !avpriv_atomic_int_get(&fifo->thread_finished_flag)) + return AVERROR(EAGAIN); + ret = pthread_join( fifo->writer_thread, NULL); if (ret < 0) { av_log(avf, AV_LOG_ERROR, "pthread join error: %s\n", @@ -593,6 +632,16 @@ static void fifo_deinit(AVFormatContext *avf) { FifoContext *fifo = avf->priv_data; + if (avf->flags & AVFMT_FLAG_NONBLOCK) { + int ret; + avpriv_atomic_int_set(&fifo->termination_requested, 1); + ret = pthread_join( fifo->writer_thread, NULL); + if (ret < 0) { + av_log(avf, AV_LOG_ERROR, "pthread join error: %s\n", + av_err2str(AVERROR(ret))); + } + } + if (fifo->format_options) av_dict_free(&fifo->format_options);