From patchwork Mon Dec 10 12:05:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Semashev X-Patchwork-Id: 11362 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 A3EFF44CB2C for ; Mon, 10 Dec 2018 14:12:19 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E7F3068AB5C; Mon, 10 Dec 2018 14:12:09 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f68.google.com (mail-lf1-f68.google.com [209.85.167.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5FB0C68AB5B for ; Mon, 10 Dec 2018 14:12:04 +0200 (EET) Received: by mail-lf1-f68.google.com with SMTP id n18so7797992lfh.6 for ; Mon, 10 Dec 2018 04:12:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=JdlFhC27KibNSDv5sZKtneXnaowPfWXDy1i3iew4oJE=; b=A36wMv1tK86p/SUWLBM6M1GF9k5RQw+t6R5nPHH8XmiaW2F/b9ougSAPvPDfKq9u0G miLvoNlDHDVUmXZJw7flXniG9/eHGYXLa6uEhGMosAlQpxPm8DPUKqV0c4rOKxhOliJD YoWuJO4Kj00K7eUO6DRzu+DcX3gdFdVZCikdJI+gO5aE7K2AAjrCcQKUZxa0X9nXMSmr GJjmHBDeuHogQtsRgYrcir5pqiC+MsFT6I2ZkF5PMUQmcqOP8m6pSyZJUcDCls1i3wJm DZDcDRxpR8YywEUgluNzJIREKSfi0fubBzOK5xtaPXePE6aVjEdCdmjb23tG3sdLbQaz Eu1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=JdlFhC27KibNSDv5sZKtneXnaowPfWXDy1i3iew4oJE=; b=REV7Ea2BHWpCPHKFbMCKTIBRpKRdx30PHZKFDnPZInBKuv39Lpj3AOqp2hxn6JGdyg QYGi9rsemw7z6EhC5zJEwKmNehFaM6cPIY/U5flY7TvMAzZk3VqgnorMfAoPkQgSLPux EhcOeRovhHMR+4/GUbqY+25vsiZjZkwq+iq8+sKNknge+nSoAVi5CchLcbYgbs0K+OVV lx+H648XQYHgxfGQ0OBGUTIoqjR5N+mxepplIZB5W06qOBbFG97qJWAAMWvDPtqH3pFH 0CMmchWe03Ksotg58vlW3AZuUm06O0ft2M/FcdpksHIZn1mYVDP7PegG6zKK/3D3zKFd BSfw== X-Gm-Message-State: AA+aEWbWlA9ptkepms+RHfSaKXldrQsvXVoszSkTUfJt4pYxsVkC3Gn+ iJtpVa1k9/plyDhoLOSmiqzb8f3i X-Google-Smtp-Source: AFSGD/X3bkBH/Jm7/z1Yxu3KEapaY/cwxy3Fxgl7dD3OmVoMB4Alj9/tg21tgTApDWjiFn+S5cSwbg== X-Received: by 2002:a19:1901:: with SMTP id 1mr5730230lfz.99.1544443554908; Mon, 10 Dec 2018 04:05:54 -0800 (PST) Received: from localhost.localdomain (broadband-37-110-31-10.ip.moscow.rt.ru. [37.110.31.10]) by smtp.gmail.com with ESMTPSA id h85-v6sm2133577ljf.68.2018.12.10.04.05.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Dec 2018 04:05:54 -0800 (PST) From: Andrey Semashev To: ffmpeg-devel@ffmpeg.org Date: Mon, 10 Dec 2018 15:05:48 +0300 Message-Id: <20181210120549.17179-1-andrey.semashev@gmail.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 1/2] lavf: Add general API for IO buffer synchronization. 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: Andrey Semashev Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This commit adds a new set of functions to avio and url subsystems, which allow users to invoke IO buffer synchronization with the underlying media. The most obvious target for this extension if the filesystem streams. Invoking IO synchronization allows user applications to ensure that all written content has reached the filesystem on the media and can be observed by other processes. The public API for this is avio_sync() function, which can be called on AVIOContext. The internal, lower layer API is ffurl_sync(), which works directly on the underlying URLContext. URLContext backends can add support for this new API by setting the sync handler to the new url_sync member of URLProtocol. When no handler is set then the sync operation is a no-op, the result code is AVERROR(ENOSYS). --- libavformat/avio.c | 7 +++++++ libavformat/avio.h | 20 ++++++++++++++++++++ libavformat/aviobuf.c | 17 +++++++++++++++++ libavformat/url.h | 12 ++++++++++++ 4 files changed, 56 insertions(+) diff --git a/libavformat/avio.c b/libavformat/avio.c index 663789ec02..5754a7c20d 100644 --- a/libavformat/avio.c +++ b/libavformat/avio.c @@ -623,6 +623,13 @@ int64_t ffurl_size(URLContext *h) return size; } +int ffurl_sync(URLContext *h) +{ + if (h->prot->url_sync) + return h->prot->url_sync(h); + return AVERROR(ENOSYS); +} + int ffurl_get_file_handle(URLContext *h) { if (!h || !h->prot || !h->prot->url_get_file_handle) diff --git a/libavformat/avio.h b/libavformat/avio.h index 75912ce6be..9e2ef14e60 100644 --- a/libavformat/avio.h +++ b/libavformat/avio.h @@ -349,6 +349,14 @@ typedef struct AVIOContext { * Try to buffer at least this amount of data before flushing it */ int min_packet_size; + + /** + * A callback for synchronizing buffers with the media state. + * + * @return 0 on success, AVERROR(ENOSYS) if the operation is not supported + * and ignored, or other AVERROR < 0 on error. + */ + int (*sync)(void *opaque); } AVIOContext; /** @@ -586,6 +594,18 @@ int avio_printf(AVIOContext *s, const char *fmt, ...) av_printf_format(2, 3); */ void avio_flush(AVIOContext *s); +/** + * Flush internal buffers and ensure the synchronized state of the + * resource associated with the context s. This call may be expensive. + * Not all resources support syncing, this operation is a no-op + * if sync is not supported or needed. + * This function can only be used if s was opened by avio_open(). + * + * @return 0 on success, AVERROR(ENOSYS) if the operation is not supported + * and ignored, or other AVERROR < 0 on error. + */ +int avio_sync(AVIOContext *s); + /** * Read size bytes from AVIOContext into buf. * @return number of bytes read or AVERROR diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 5a33f82950..c1c9334719 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -243,6 +243,14 @@ void avio_flush(AVIOContext *s) avio_seek(s, seekback, SEEK_CUR); } +int avio_sync(AVIOContext *s) +{ + avio_flush(s); + if (s->sync) + return s->sync(s->opaque); + return AVERROR(ENOSYS); +} + int64_t avio_seek(AVIOContext *s, int64_t offset, int whence) { int64_t offset1; @@ -978,6 +986,12 @@ static int64_t io_read_seek(void *opaque, int stream_index, int64_t timestamp, i return internal->h->prot->url_read_seek(internal->h, stream_index, timestamp, flags); } +static int io_sync(void *opaque) +{ + AVIOInternal *internal = opaque; + return ffurl_sync(internal->h); +} + int ffio_fdopen(AVIOContext **s, URLContext *h) { AVIOInternal *internal = NULL; @@ -1026,6 +1040,9 @@ int ffio_fdopen(AVIOContext **s, URLContext *h) if (h->prot->url_read_seek) (*s)->seekable |= AVIO_SEEKABLE_TIME; + + if (h->prot->url_sync) + (*s)->sync = io_sync; } (*s)->short_seek_get = io_short_seek; (*s)->av_class = &ff_avio_class; diff --git a/libavformat/url.h b/libavformat/url.h index 4750bfff82..5b8cd22e5a 100644 --- a/libavformat/url.h +++ b/libavformat/url.h @@ -97,6 +97,7 @@ typedef struct URLProtocol { int (*url_delete)(URLContext *h); int (*url_move)(URLContext *h_src, URLContext *h_dst); const char *default_whitelist; + int (*url_sync)(URLContext *h); } URLProtocol; /** @@ -228,6 +229,17 @@ int64_t ffurl_seek(URLContext *h, int64_t pos, int whence); int ffurl_closep(URLContext **h); int ffurl_close(URLContext *h); +/** + * Flush any buffered data and synchronize the resource accessed + * by the URLContext h. This call may be expensive. + * Not all types of resources support syncing, the call is a no-op + * if sync is not supported or needed. + * + * @return 0 on success, AVERROR(ENOSYS) if the operation is not supported + * and ignored, or other AVERROR < 0 on error. + */ +int ffurl_sync(URLContext *h); + /** * Return the filesize of the resource accessed by h, AVERROR(ENOSYS) * if the operation is not supported by h, or another negative value