From patchwork Thu Aug 4 13:04:15 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: 91 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.67 with SMTP id o64csp1189284vsd; Thu, 4 Aug 2016 06:04:38 -0700 (PDT) X-Received: by 10.28.227.11 with SMTP id a11mr74268974wmh.29.1470315878565; Thu, 04 Aug 2016 06:04:38 -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 u186si3964958wmg.3.2016.08.04.06.04.36; Thu, 04 Aug 2016 06:04:38 -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 4391868A4AE; Thu, 4 Aug 2016 16:04:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9AD9268A48B for ; Thu, 4 Aug 2016 16:04:19 +0300 (EEST) Received: by mail-wm0-f68.google.com with SMTP id x83so44123121wma.3 for ; Thu, 04 Aug 2016 06:04:26 -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=RIkNnH6gBOTtem0g39Dfq7cRxHp2NdrspdoaHLWRgBo=; b=j1rigJt3lrTknriXgoSpwvZrBuAyMDdTCF4P9LeJ8wK3TIvS0kqVfhXQynbQzEwSsK 4KKFkHs+fct4rAkn/1Ro+18F3qHXHO/84IYo0sg+2PPEMppIlno5m/UjZBW9ibGqZBUj 5mce4C5BxfFMQTHA/l/oX9HmaG+hCDOhToU/OjP8P1gIhWELNFMQ4m1ubfERljYq5+dR peOsmvqBH+05ojZ/YMT3kSzc+TblP7MgUaVcLRMEo28TtffEWNvBNduYkWdHOLFVBUaD emIv96Ktc2EGfkCjgHTKOnIPxymDYu3GuYb/mzD6IlxG5LfidkL/pV0QdMctHw6AKdjC WgOA== 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=RIkNnH6gBOTtem0g39Dfq7cRxHp2NdrspdoaHLWRgBo=; b=gaD+yAQnNcfp+Ay7rpfwlP/g5phhRRoVFnAZi08C+YsIGxvAHqL4NICgNkhCIAcprE Dd5mVIJAnz1/2trE8FZ4L7FWi329qlcn2b8iKo5U495me80I6oS9qFhRG8zsh2vRgXP3 +ziMRoWyv9RiFd3VqiANZtkJxy4te6Q9N2mqA923qHHSCBD4bTobcs+GNoqPefUEYerP wTfpR6EP11UYyDd8YeQPilIbusIqiNzLf/sCa9W4hkvNaDc17k0L9nkOnost9DAAbtJp WWz7f8eM1JdR33gq4SUMio1mX2G2aOfRO5o40arcpZjWawcjB6EqmdJapp/jZZgulYeH 2UMA== X-Gm-Message-State: AEkoousIIh5ZWFcM+yOGV/gqWPsx9iijD3S8QsL57Z4DaH0c3LcrSNwy63ujWWqxga/HxQ== X-Received: by 10.28.153.70 with SMTP id b67mr30690043wme.84.1470315865992; Thu, 04 Aug 2016 06:04:25 -0700 (PDT) Received: from localhost.localdomain (157.174.broadband3.iol.cz. [85.70.174.157]) by smtp.gmail.com with ESMTPSA id i7sm12782715wjg.42.2016.08.04.06.04.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 04 Aug 2016 06:04:25 -0700 (PDT) From: sebechlebskyjan@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Thu, 4 Aug 2016 15:04:15 +0200 Message-Id: <1470315855-19209-1-git-send-email-sebechlebskyjan@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <20160803142353.GC5460@nb4> References: <20160803142353.GC5460@nb4> Subject: [FFmpeg-devel] [PATCH v2 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 --- Changes from the last version: - boolean flags accessed from both threads are ints now and are accessed with atomic operations. - pthread_tryjoin_np is replaced by flag set before fifo_consumer_thread returns libavformat/fifo.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/libavformat/fifo.c b/libavformat/fifo.c index bd9d934..9b92eab 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/threadmessage.h" #include "avformat.h" #include "internal.h" #include "pthread.h" +#include "url.h" #define FIFO_DEFAULT_QUEUE_SIZE 60 #define FIFO_DEFAULT_MAX_RECOVERY_ATTEMPTS 16 @@ -77,6 +79,17 @@ typedef struct FifoContext { /* Value > 0 signalizes 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 { @@ -110,6 +123,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; @@ -442,12 +465,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; @@ -458,7 +486,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) @@ -543,7 +572,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); @@ -552,15 +581,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 |= AVFMT_FLAG_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) + return ret; /* 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; @@ -588,6 +623,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", @@ -603,6 +642,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);