From patchwork Fri Mar 17 06:32:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Davy Durham X-Patchwork-Id: 40707 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp306034pzb; Thu, 16 Mar 2023 23:32:24 -0700 (PDT) X-Google-Smtp-Source: AK7set9mFInbN6z4CFNfd9ym8HKeyItQhBk5MWFHuv3D+4jq+IglxBO1D6rjeIr6Kegv6GEcVMIN X-Received: by 2002:a17:907:94d1:b0:92b:e1ff:be4e with SMTP id dn17-20020a17090794d100b0092be1ffbe4emr17914985ejc.1.1679034744546; Thu, 16 Mar 2023 23:32:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679034744; cv=none; d=google.com; s=arc-20160816; b=oEpiqqnOd2iENw0xUnPosCnJv6LrbGW1F9yH05Zf7ogpWDZiMvOU4z0Mjc2ihYHV5H JcxUTc5Q3CWU8FkVjMIn+Ott8LORo8dJbX9Usaq380k6+z653xLDBzpEwNXqpW2f4ldm yB8ot5Qps19N4smugu1OVo5zjXzEy93HO8UU8DXDQhvxcbKqdU/1K1di8AsdCV3kkv4U IT9WiT4IPHhhQ67tF+P3veW14Y/2ggT0+JwqrBZgGvzV7yU8L5NPJFWVv+oLIg7oDXJe gJWCjo/6EyMjntU2W4s8cAWQVrLzBkbgBMtG7RU/BN86HNFfmxV2f47bylpC+qs+/btO ACHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:to:content-language:from:user-agent:mime-version :date:message-id:dkim-signature:delivered-to; bh=U8az9+tn/hhY5bq1TMdW/Gah6sENT05iMEryjrZzJyY=; b=vXXd2m1gweadQRMys8uM6NvQUfGkWLHLt0Zxk5xjhZ2z+2vBCjeUW/aTQ+8w3pwDWp BykktePKyLOm9D2olylvMX9s2YZNJtPMZG6+d+K5iERR4KE63HA0da82llWZOsTSorKP kBiYAWDAM3mH7LA2aAYs/FxIpO7lnH0HUpY/UPca3GcDmeRzqUdDhYPqj1SYfDV2UIeh 9ESs2F3oF2Acq/eYnYjLl++/LCKgpoFdjsb2Mp1sFp1H2XpfZKzAMYnTyh6GUEDbzabh +8vJOAhx1Amp65D7TUHyuPC+LgxBv9OA4LTztQIGgTjgPb7Pyz5xJU4GDNuHIk22lVCi y4bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@davyandbeth-com.20210112.gappssmtp.com header.s=20210112 header.b=zpMUElg4; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id pw9-20020a17090720a900b0092bc0619e06si1551468ejb.619.2023.03.16.23.32.23; Thu, 16 Mar 2023 23:32:24 -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=@davyandbeth-com.20210112.gappssmtp.com header.s=20210112 header.b=zpMUElg4; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5B91B68C188; Fri, 17 Mar 2023 08:32:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f172.google.com (mail-qt1-f172.google.com [209.85.160.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3819468C0EB for ; Fri, 17 Mar 2023 08:32:14 +0200 (EET) Received: by mail-qt1-f172.google.com with SMTP id c19so4598695qtn.13 for ; Thu, 16 Mar 2023 23:32:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davyandbeth-com.20210112.gappssmtp.com; s=20210112; t=1679034732; h=content-transfer-encoding:to:content-language:subject:from :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=azbcBynD9SoFg3wm34+6MbC1KHMkSEUB+tN158ZHRb0=; b=zpMUElg4h5zCrJX3GyFNzXaqCe3OHrj1IqHaJ3FqY+HrNJauvoNSRUmvJ9r/M/Zewn un90f5iW9ITDprb2qrLJ9ju7jN9pHGPCdlylfnH+4A2OWVnTEJVQYak7u5CW3I4YScEX VuhtsvbiM29lBDkTQx8RSAGO6uIs9fqjZF2iEBAeX4b0c3V4fn2nghmf2FX5vtVL0zit dvOAVktbqCFGsgEACp2TYUUMmPWZxeazg18kuGuNm23zgwhM19cWB/3+wVhgl3uCDM9Z 6fHSTS+E9GtJ7B8DEEajVcVjX87u9r+4O4+xbVtTiazzO5r29xlsgnSB96CE2vfGvc7Y RkOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679034732; h=content-transfer-encoding:to:content-language:subject:from :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=azbcBynD9SoFg3wm34+6MbC1KHMkSEUB+tN158ZHRb0=; b=j3qyFTd9f9NoSAX7NhbP2LBdJ/OL0d/trG+lrqeGXqADafGFEhcsjaGPzK84gRqPal +QlYfviAoKmv/wSfxwlHYPKvm1G3AokOUwEp/28dVsY88alXPMHLgFsgpx8OuBxbDJNx tEX1Oe5orfWValBlyV+C6nGjotK8q+AxkvBGHUWJaQIUVwhCgiiZ08pdQ81dggvBC83r b2JTbGcsZCI1Y/R7MOg9JlQMY0eatdCrql9s0DYt5jPEmUSRE2toA2Hh8XcbU2mvQdfm nvgJRphUUOZLErArWYMmq1B+2khSrFdVXwrx+pvLx7ujQoISbjpbhj2aH2uJZqAEM891 iNzg== X-Gm-Message-State: AO0yUKVPzlD2RBn7a3R2tLiyaD4N2bXiEuUbLbuuWJGYp5ih8HlTvFxH EAEDiZg9yxVxdp4Q8Gl8Gs+HKcwGslAbDIsQOa70gQ== X-Received: by 2002:a05:622a:3c7:b0:3bf:c221:5871 with SMTP id k7-20020a05622a03c700b003bfc2215871mr11032748qtx.54.1679034732135; Thu, 16 Mar 2023 23:32:12 -0700 (PDT) Received: from [192.168.1.92] ([68.249.219.17]) by smtp.googlemail.com with ESMTPSA id t184-20020ae9dfc1000000b007424239e4casm1062702qkf.117.2023.03.16.23.32.11 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 16 Mar 2023 23:32:11 -0700 (PDT) Message-ID: <1731e457-2939-9406-ef33-88722444fc43@davyandbeth.com> Date: Fri, 17 Mar 2023 01:32:11 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 From: Davy Durham Content-Language: en-US To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] avformat/file: fd, pipe and file input/output now honor rw_timeout X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: 4S07zL/vWhbs File descriptors, pipes or even filenames (that may be fifos or reference other non-regular files) may have upstream or downstream processess that stall out just like any network channel. Hence ffmpeg should respect -rw_timeout for these types too. This is necessary when, for example, input from a pipe has stalled and we want ffmpeg to timeout in order to have a pipeline tear down. --- doc/protocols.texi | 4 ++-- libavformat/file.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) *buf, int size) FileContext *c = h->priv_data; int ret; size = FFMIN(size, c->blocksize); + if (h->rw_timeout > 0) { /* wait for something, anything to read */ + ret = ff_network_wait_fd_timeout(c->fd, 0, h->rw_timeout, NULL); + if (ret) + return ret; + } ret = read(c->fd, buf, size); if (ret == 0 && c->follow) return AVERROR(EAGAIN); @@ -152,6 +158,11 @@ static int file_write(URLContext *h, const unsigned char *buf, int size) { FileContext *c = h->priv_data; int ret; + if (h->rw_timeout > 0) { /* wait for something, anything to read */ + ret = ff_network_wait_fd_timeout(c->fd, 1, h->rw_timeout, NULL); + if (ret) + return ret; + } size = FFMIN(size, c->blocksize); ret = write(c->fd, buf, size); return (ret == -1) ? AVERROR(errno) : ret; @@ -304,6 +315,9 @@ static int file_open(URLContext *h, const char *filename, int flags) if (fd == -1) return AVERROR(errno); c->fd = fd; + if (h->rw_timeout > 0) { /* if we'll be doing reads w/ timeout, mark non-blocking */ + fcntl(c->fd, F_SETFL, fcntl(c->fd, F_GETFL, 0) | O_NONBLOCK); + } h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode); @@ -448,6 +462,9 @@ static int pipe_open(URLContext *h, const char *filename, int flags) c->fd = fd_dup(h, c->fd); if (c->fd == -1) return AVERROR(errno); + if (h->rw_timeout > 0) { /* if we'll be doing reads w/ timeout, mark non-blocking */ + fcntl(c->fd, F_SETFL, fcntl(c->fd, F_GETFL, 0) | O_NONBLOCK); + } h->is_streamed = 1; return 0; } @@ -493,6 +510,9 @@ static int fd_open(URLContext *h, const char *filename, int flags) c->fd = fd_dup(h, c->fd); if (c->fd == -1) return AVERROR(errno); + if (h->rw_timeout > 0) { /* if we'll be doing reads w/ timeout, mark non-blocking */ + fcntl(c->fd, F_SETFL, fcntl(c->fd, F_GETFL, 0) | O_NONBLOCK); + } return 0; } diff --git a/doc/protocols.texi b/doc/protocols.texi index 21ae6181a0..d75521ced2 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -45,8 +45,8 @@ All protocols accept the following options: @table @option @item rw_timeout -Maximum time to wait for (network) read/write operations to complete, -in microseconds. +Maximum time to wait for (network, file, fd, and pipe) read/write operations +to complete, in microseconds. @end table A description of the currently available protocols follows. diff --git a/libavformat/file.c b/libavformat/file.c index cbdf48de0a..fc1d15a672 100644 --- a/libavformat/file.c +++ b/libavformat/file.c @@ -40,6 +40,7 @@ #include #include "os_support.h" #include "url.h" +#include "network.h" /* Some systems may not have S_ISFIFO */ #ifndef S_ISFIFO @@ -140,6 +141,11 @@ static int file_read(URLContext *h, unsigned char