From patchwork Tue Dec 22 18:09:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 24623 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 676BB44AFF5 for ; Tue, 22 Dec 2020 21:08:03 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4E25968AA53; Tue, 22 Dec 2020 21:08:03 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B266C6891CE for ; Tue, 22 Dec 2020 21:07:57 +0200 (EET) Received: by mail-ed1-f50.google.com with SMTP id u19so13942430edx.2 for ; Tue, 22 Dec 2020 11:07:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=e0ldp5YuPG4EFxyINRY9yCf4Hx5uBHx5EKJvd4yi5w8=; b=imd4Y6MkmX0nptm6pOL4dVU8buWP9elxSIZDJwVjQ8I4zwHC2O9g37nklarNLn74Vp 2jHjKMY9H4XsT//Q374kwDv7L2RdYdxywRipF1OEmFJ2FnPjbzvRbkNf/8XWOOsSCB9j dMmYcEu4ptEl5LP5mgRqPL5mtEfu9ubULThFsretlPI9t2i/xMzupmPkq5rJD5glue8e kOzkElNHC0DyVlxJBRCq+zLeNetGcJs+J44XW0oGIiOJdBAjcGdrZMrXVsIFVlgADUn6 TeoHPWylWxLzCSjE7pThbj3HsNYIxiKoPFzbGj0bRaDr9+q+QMrR3RkvTHlxcs31Sl10 rYSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=e0ldp5YuPG4EFxyINRY9yCf4Hx5uBHx5EKJvd4yi5w8=; b=ZPMcAbf8wgZu8qxj+xRcukmemCyhXO7AiENBg5MiY3a5E50GIJBiDC/WgR2Sr1Vnb+ aGLClAUYoND6N+YqV+/pVNvBqOzWBSpNxYqkDjmj/D+8N+wA4iWKECs+de0PxAEkxfhv Ux6ATcmgED/Z2ZsyZgVshRnEXbQRIlvhtMlsmqyaBsuodCEXp175DG/KONUYvPeXakp7 Q8cqNIbBg0n+t27FElFRlRbT7X8IK/gq5bfhcN1/4qwYKv3bVDsNA1RNo4AV6/++v5nZ Tr8kQhCzgamUKAE1Oq1FTc1KIIK/jPDn/bcB1GU2Si+Tw8mwbO4CzkYVwYJvxt5X4Uf6 q09w== X-Gm-Message-State: AOAM533bgIOei51PcLDQDFDxA3fsmpZLiagPXzlviJ9hh+mLSM+Nxb0h I18YeMDO863uLvrbqehDPnze/3jWVt4TKA== X-Google-Smtp-Source: ABdhPJxY9R0pTGWapkbt6mxRMlB9RGX9tVyIEsPRwU5hhJftFIxEYRYnzFyRAH/55WDp6K3/u7eKOQ== X-Received: by 2002:a05:6402:254a:: with SMTP id l10mr14299870edb.264.1608660568939; Tue, 22 Dec 2020 10:09:28 -0800 (PST) Received: from localhost.localdomain ([31.45.254.141]) by smtp.gmail.com with ESMTPSA id qu21sm10567500ejb.95.2020.12.22.10.09.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Dec 2020 10:09:28 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 22 Dec 2020 19:09:20 +0100 Message-Id: <20201222180920.11145-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [RFC][PATCH] libavformat: add librist protocol 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- TODO: What about logging messages? Return codes? File handles? Add support for password? Advanced stuff? --- configure | 5 ++ libavformat/Makefile | 1 + libavformat/librist.c | 164 ++++++++++++++++++++++++++++++++++++++++ libavformat/protocols.c | 1 + 4 files changed, 171 insertions(+) create mode 100644 libavformat/librist.c diff --git a/configure b/configure index 90914752f1..462f2dcf64 100755 --- a/configure +++ b/configure @@ -259,6 +259,7 @@ External library support: --enable-libpulse enable Pulseaudio input via libpulse [no] --enable-librabbitmq enable RabbitMQ library [no] --enable-librav1e enable AV1 encoding via rav1e [no] + --enable-librist enable RIST via librist [no] --enable-librsvg enable SVG rasterization via librsvg [no] --enable-librubberband enable rubberband needed for rubberband filter [no] --enable-librtmp enable RTMP[E] support via librtmp [no] @@ -1797,6 +1798,7 @@ EXTERNAL_LIBRARY_LIST=" libpulse librabbitmq librav1e + librist librsvg librtmp libshine @@ -3488,6 +3490,8 @@ unix_protocol_select="network" # external library protocols libamqp_protocol_deps="librabbitmq" libamqp_protocol_select="network" +librist_protocol_deps="librist" +librist_protocol_select="network" librtmp_protocol_deps="librtmp" librtmpe_protocol_deps="librtmp" librtmps_protocol_deps="librtmp" @@ -6404,6 +6408,7 @@ enabled libopus && { enabled libpulse && require_pkg_config libpulse libpulse pulse/pulseaudio.h pa_context_new enabled librabbitmq && require_pkg_config librabbitmq "librabbitmq >= 0.7.1" amqp.h amqp_new_connection enabled librav1e && require_pkg_config librav1e "rav1e >= 0.1.0" rav1e.h rav1e_context_new +enabled librist && require_pkg_config librist "librist >= 0.2" librist/librist.h rist_receiver_create enabled librsvg && require_pkg_config librsvg librsvg-2.0 librsvg-2.0/librsvg/rsvg.h rsvg_handle_render_cairo enabled librtmp && require_pkg_config librtmp librtmp librtmp/rtmp.h RTMP_Socket enabled librubberband && require_pkg_config librubberband "rubberband >= 1.8.1" rubberband/rubberband-c.h rubberband_new -lstdc++ && append librubberband_extralibs "-lstdc++" diff --git a/libavformat/Makefile b/libavformat/Makefile index 97d868081b..799e16c59e 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -652,6 +652,7 @@ OBJS-$(CONFIG_UNIX_PROTOCOL) += unix.o # external library protocols OBJS-$(CONFIG_LIBAMQP_PROTOCOL) += libamqp.o +OBJS-$(CONFIG_LIBRIST_PROTOCOL) += librist.o OBJS-$(CONFIG_LIBRTMP_PROTOCOL) += librtmp.o OBJS-$(CONFIG_LIBRTMPE_PROTOCOL) += librtmp.o OBJS-$(CONFIG_LIBRTMPS_PROTOCOL) += librtmp.o diff --git a/libavformat/librist.c b/libavformat/librist.c new file mode 100644 index 0000000000..b07f1ab673 --- /dev/null +++ b/libavformat/librist.c @@ -0,0 +1,164 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Reliable Internet Streaming Transport protocol + */ + +#include "libavutil/avassert.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "libavutil/time.h" + +#include "avformat.h" +#include "internal.h" +#include "network.h" +#include "os_support.h" +#include "url.h" + +#include + +typedef struct RISTContext { + const AVClass *class; + int fd; + + int profile; + + const struct rist_peer_config *peer_config; + struct rist_peer *peer; + struct rist_ctx *rist_ctx; +} RISTContext; + +#define D AV_OPT_FLAG_DECODING_PARAM +#define E AV_OPT_FLAG_ENCODING_PARAM +#define OFFSET(x) offsetof(RISTContext, x) +static const AVOption librist_options[] = { + { "profile", "set RIST profile", OFFSET(profile), AV_OPT_TYPE_INT, {.i64=RIST_PROFILE_SIMPLE}, 0, 2, .flags = D|E }, + { NULL } +}; + +static int librist_open(URLContext *h, const char *uri, int flags) +{ + RISTContext *s = h->priv_data; + int ret; + + if (flags & AVIO_FLAG_WRITE) { + ret = rist_sender_create(&s->rist_ctx, s->profile, 0, NULL); + if (ret < 0) + return ret; + } else { + ret = rist_receiver_create(&s->rist_ctx, s->profile, NULL); + if (ret < 0) + return ret; + } + + ret = rist_parse_address(uri, &s->peer_config); + if (ret < 0) + return ret; + + ret = rist_peer_create(s->rist_ctx, &s->peer, s->peer_config); + if (ret < 0) + return ret; + + ret = rist_start(s->rist_ctx); + if (ret < 0) + return ret; + + return 0; +} + +static int librist_read(URLContext *h, uint8_t *buf, int size) +{ + RISTContext *s = h->priv_data; + const struct rist_data_block *data_block; + int ret; + + ret = rist_receiver_data_read(s->rist_ctx, &data_block, 5); + if (ret < 0) + return ret; + + if (ret > 0) { + memcpy(buf, data_block->payload, data_block->payload_len); + return data_block->payload_len; + } + + return 0; +} + +static int librist_write(URLContext *h, const uint8_t *buf, int size) +{ + RISTContext *s = h->priv_data; + struct rist_data_block data_block = { 0 }; + int ret; + + data_block.ts_ntp = 0; + data_block.payload = buf; + data_block.payload_len = size; + ret = rist_sender_data_write(s->rist_ctx, &data_block); + if (ret < 0) + return ret; + return size; // XXX should be ret actually but librist is buggy, got OOM otherwise +} + +static int librist_close(URLContext *h) +{ + RISTContext *s = h->priv_data; + int ret; + + free((void *)s->peer_config); + s->peer_config = NULL; + + ret = rist_peer_destroy(s->rist_ctx, s->peer); + if (ret < 0) + return ret; + s->peer = NULL; + + ret = rist_destroy(s->rist_ctx); + if (ret < 0) + return ret; + s->rist_ctx = NULL; + + return 0; +} + +static int librist_get_file_handle(URLContext *h) +{ + RISTContext *s = h->priv_data; + + return s->fd; +} + +static const AVClass librist_class = { + .class_name = "librist", + .item_name = av_default_item_name, + .option = librist_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +const URLProtocol ff_librist_protocol = { + .name = "rist", + .url_open = librist_open, + .url_read = librist_read, + .url_write = librist_write, + .url_close = librist_close, + .url_get_file_handle = librist_get_file_handle, + .priv_data_size = sizeof(RISTContext), + .flags = URL_PROTOCOL_FLAG_NETWORK, + .priv_data_class = &librist_class, +}; diff --git a/libavformat/protocols.c b/libavformat/protocols.c index 7df18fbb3b..c4fc446bb6 100644 --- a/libavformat/protocols.c +++ b/libavformat/protocols.c @@ -61,6 +61,7 @@ extern const URLProtocol ff_udp_protocol; extern const URLProtocol ff_udplite_protocol; extern const URLProtocol ff_unix_protocol; extern const URLProtocol ff_libamqp_protocol; +extern const URLProtocol ff_librist_protocol; extern const URLProtocol ff_librtmp_protocol; extern const URLProtocol ff_librtmpe_protocol; extern const URLProtocol ff_librtmps_protocol;