From patchwork Sun May 13 00:07:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephan Holljes X-Patchwork-Id: 8937 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:155:0:0:0:0:0 with SMTP id c82-v6csp2442144jad; Sat, 12 May 2018 17:16:16 -0700 (PDT) X-Google-Smtp-Source: AB8JxZq4TyAcrApNQtgiuXJcXYd5FS38qsdQBuA6qNcpXkH754zwYdSGE6VoIM+F8WKO6ocT95Py X-Received: by 2002:a1c:6d47:: with SMTP id i68-v6mr2115515wmc.154.1526170576238; Sat, 12 May 2018 17:16:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526170576; cv=none; d=google.com; s=arc-20160816; b=NU55Gx0Cdu5svWU4s95WxWNLd8OMu5fFDChI6w+OTG6bKs9EiCbT2MZcsC/WODEiFM h1BZgbYPWDRTZXtQeRTeK263fJcs867JfxQj/bAh0kl0eLbQI3fAdl0vHaOdj+tSpo/z OIBLf1ucSR8Uw2gu0h1nr8yjMG4IiodDS9yfynC1Eb8gEpqbHfK1lIe9ET6v6leGW4kv XXh8JMJO/MfOSDhYBL88UH451oYj5ua9B6ksuEpLx9V+o6XAKTuIMFzHn2IwsfVQj/e+ 8wybO4hEeTAqvcXIxdk6HtFq6WJN4qb29ogFuYTVb9sYFWCLKOhTa0489xJg5o9LmCW5 JHlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=P5oAzqoKvagCbzXls71VyMk0SSPghKZ57IBiyCo5NI4=; b=K8Cnp8QyOH0aS3Ia8TrCUOunKWrNr5OSAGdEFFnOo+28+j4kKSmUcwGhkkKru2kbfe iR+5BQerUdfAwxtWUJ0Gw/WlpHOYhA2CwOCyOl3INgb4nurby2Wb/SKHuNIy46xu2/oA LndmPsndddweSllx571HYqa5BnigF0ibMohid//EV73DqBbuNrsdFnKefusr8V09C3WY IxGUluNdKyhHUlxeiKvloMgebnTQVO+ojFMJoZnKsc7mW0sssYY6ZviMqqpwzssviqx1 TyEAVnFgX3c+cOoocAiwKjbV1M0tHMKcpGqs0+rGzS8073Kp6AOu9bZfGtJIm7uuEgRD gpzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@googlemail.com header.s=20161025 header.b=kZs4NkHJ; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e14-v6si5915129wrc.273.2018.05.12.17.16.15; Sat, 12 May 2018 17:16:16 -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=@googlemail.com header.s=20161025 header.b=kZs4NkHJ; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 563FD68A5EF; Sun, 13 May 2018 03:15:37 +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 564E4689FED for ; Sun, 13 May 2018 03:15:36 +0300 (EEST) Received: by mail-wm0-f68.google.com with SMTP id a137-v6so7168368wme.1 for ; Sat, 12 May 2018 17:16:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=aa1C49vdfayARuho8FVrjoDs+FQ77DLk5ChtTBZUG3I=; b=kZs4NkHJmQRYu/f6mFhoYikyfEHFI4/yQCBIGTSRF02E55a1qceTzSm1hYlC1EQ4QZ 1bl0KriQkQwvLKys1ecu6LMDI2Hw3aYuhZ8tTaTzWyx1gHttkk9l1nkQHF/DmtpLN3uZ 0UN0HYxzjE11q4qWw7Dq10IIGQSTYYR/lOUrclmeGxTYIkgUdD8HUIk3ivge7uaPOZOr fuO7BTBgAwSSjsj/BtDrypusAE7dKcoAfJtyuS5Z0+XngztBQn7dNNdGMWCtXqf+byR6 YvamX27Yj/qJSqd9GgW6PdmfJdCdcOfDJusYSm49Q3ZIaGqmt5q9gxscO+TI68JXuFoH D2ZQ== 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:in-reply-to :references; bh=aa1C49vdfayARuho8FVrjoDs+FQ77DLk5ChtTBZUG3I=; b=gO+4aT1dA3ZiZRSauwStM6az5dvu9puFMmMeZsRwT2STjBTg+seim2H28ZybNPecSp JYX7HpcNSVUm4E05XstcCCB+RVn6fBXTQ+YYvhG1shtD4FJ/H2+6/iFU9Y51QPgV6d1C JOjWqJkj+vXLGsc3+ROVgN4evAuFQw3UUoS2rjB8ffv85UMhFfM7qQZgj9Idzg9SgaqN Kc6z/Wl8CR7W2mmTDmSAXucG6x6PZ/7XRqgR3jc2MlA4as/9BnX/ODu1FrlK4lW7EL1T Cspa/0UOrPjvLDmRlW4W0/N2t7q+f5bNeS9GU6cmpBf/VMNMkdbEs4n9REQzSV6+OPmn qb7A== X-Gm-Message-State: ALKqPwcgkggLwGGB3SLHrM7IpVFPOuhId+R3x+1O0R9KyEY7Vnmrzr7Y xWGeYVkJ/d2ILcI0u4qQXQOGfg== X-Received: by 2002:a1c:8d90:: with SMTP id p138-v6mr2093965wmd.153.1526170085266; Sat, 12 May 2018 17:08:05 -0700 (PDT) Received: from localhost.localdomain ([46.5.2.0]) by smtp.gmail.com with ESMTPSA id r14-v6sm8245469wra.41.2018.05.12.17.08.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 12 May 2018 17:08:04 -0700 (PDT) From: Stephan Holljes To: ffmpeg-devel@ffmpeg.org Date: Sun, 13 May 2018 02:07:38 +0200 Message-Id: <20180513000740.12548-4-klaxa1337@googlemail.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180513000740.12548-1-klaxa1337@googlemail.com> References: <20180510154126.30789-1-klaxa1337@googlemail.com> <20180513000740.12548-1-klaxa1337@googlemail.com> Subject: [FFmpeg-devel] [PATCH 3/5] ffserver: Implement http interface and implementation 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: Stephan Holljes MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Stephan Holljes --- httpd.h | 58 +++++++++++++++++++++++ lavfhttpd.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 httpd.h create mode 100644 lavfhttpd.c diff --git a/httpd.h b/httpd.h new file mode 100644 index 0000000..6fb91bd --- /dev/null +++ b/httpd.h @@ -0,0 +1,58 @@ +/* + * 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 + */ + +#ifndef HTTPDINTERFACE_H +#define HTTPDINTERFACE_H + +#define HTTPD_OK 0 +#define HTTPD_LISTEN_TIMEOUT -1 +#define HTTPD_CLIENT_ERROR -2 +#define HTTPD_OTHER_ERROR -3 + +#include "publisher.h" + +/* HTTPD Config struct */ +struct HTTPDConfig { + char *bind_address; + int port; + int accept_timeout; +}; + +/* HTTPClient struct, this information is shared between ffserver and the httpd implementation */ +struct HTTPClient { + /* the method requested by the client, this field has to be set and freed by the httpd implementation */ + char *method; + /* the resource requested by the client, this field has to be set and freed by the httpd implementation */ + char *resource; + void *httpd_data; // httpd implementation specific data +}; + +/* HTTPDInterface that an httpd implementation must provide */ +struct HTTPDInterface { + int (*init) (void **server, struct HTTPDConfig config); + int (*free) (void *server); + int (*accept)(void *server, struct HTTPClient **client, int reply_code); + int (*write) (void *server, struct HTTPClient *client, const unsigned char *buf, int size); + int (*read) (void *server, struct HTTPClient *client, unsigned char *buf, int size); + void (*close)(void *server, struct HTTPClient *client); + void (*shutdown)(void *server); +}; + +/* Current HTTPDInterface implementation using lavformat */ +extern struct HTTPDInterface lavfhttpd; +#endif diff --git a/lavfhttpd.c b/lavfhttpd.c new file mode 100644 index 0000000..3cf9958 --- /dev/null +++ b/lavfhttpd.c @@ -0,0 +1,153 @@ +/* + * 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 + */ + +#ifndef LAVFHTTPD_H +#define LAVFHTTPD_H + +#include "httpd.h" +#include + + +int lavfhttpd_init(void **server, struct HTTPDConfig config) +{ + char out_uri[1024]; + int ret; + AVDictionary *opts = NULL; + AVIOContext *server_ctx = NULL; + + snprintf(out_uri, 1024, "http://%s:%d", config.bind_address, config.port); + + avformat_network_init(); + + if ((ret = av_dict_set(&opts, "listen", "2", 0)) < 0) { + av_log(opts, AV_LOG_ERROR, "Failed to set listen mode for server: %s\n", av_err2str(ret)); + av_free(opts); + return -1; + } + + if ((ret = av_dict_set_int(&opts, "listen_timeout", config.accept_timeout, 0)) < 0) { + av_log(opts, AV_LOG_ERROR, "Failed to set listen_timeout for server: %s\n", av_err2str(ret)); + av_free(opts); + return -1; + } + + if ((ret = avio_open2(&server_ctx, out_uri, AVIO_FLAG_WRITE, NULL, &opts)) < 0) { + av_log(server, AV_LOG_ERROR, "Failed to open server: %s\n", av_err2str(ret)); + av_free(opts); + return -1; + } + av_free(opts); + + *server = server_ctx; + return 0; +} + +int lavfhttpd_accept(void *server, struct HTTPClient **client, int reply_code) +{ + AVIOContext *server_ctx = (AVIOContext*) server; + AVIOContext *client_ctx = NULL; + struct HTTPClient *client_http = NULL; + int ret, ret2, handshake; + int reply_code2 = reply_code; + char *method, *resource; + if ((ret = avio_accept(server_ctx, &client_ctx)) < 0) { + if (ret == AVERROR(ETIMEDOUT)) + return HTTPD_LISTEN_TIMEOUT; + else + return HTTPD_OTHER_ERROR; + } + client_ctx->seekable = 0; + ret2 = HTTPD_OK; + client_http = av_malloc(sizeof(struct HTTPClient)); + client_http->method = NULL; + client_http->resource = NULL; + client_http->httpd_data = client_ctx; + while ((handshake = avio_handshake(client_ctx)) > 0) { + av_opt_get(client_ctx, "method", AV_OPT_SEARCH_CHILDREN, (uint8_t**) &method); + av_opt_get(client_ctx, "resource", AV_OPT_SEARCH_CHILDREN, (uint8_t**) &resource); + av_log(client_ctx, AV_LOG_DEBUG, "method: %s resource: %s\n", method, resource); + if (method && strlen(method) && strncmp("GET", method, 3)) { + ret2 = HTTPD_CLIENT_ERROR; + reply_code2 = 400; + if ((ret = av_opt_set_int(client_ctx, "reply_code", reply_code2, AV_OPT_SEARCH_CHILDREN)) < 0) { + av_log(client_ctx, AV_LOG_WARNING, "Failed to set reply_code: %s.\n", av_err2str(ret)); + } + } + if (client_http->method) + av_free(client_http->method); + if (client_http->resource) + av_free(client_http->resource); + client_http->method = av_strdup(method); + client_http->resource = av_strdup(resource); + av_free(method); + av_free(resource); + } + if (handshake < 0) { + ret2 = HTTPD_CLIENT_ERROR; + reply_code2 = 400; + } + + if ((ret = av_opt_set_int(client_ctx, "reply_code", reply_code2, AV_OPT_SEARCH_CHILDREN)) < 0) { + av_log(client_ctx, AV_LOG_WARNING, "Failed to set reply_code: %s.\n", av_err2str(ret)); + } + + *client = client_http; + return ret2; +} + +int lavfhttpd_write(void *server, struct HTTPClient *client, const unsigned char *buf, int size) +{ + AVIOContext *client_ctx = (AVIOContext*) client->httpd_data; + avio_write(client_ctx, buf, size); + avio_flush(client_ctx); + return size; +} + +int lavfhttpd_read(void *server, struct HTTPClient *client, unsigned char *buf, int size) +{ + AVIOContext *client_ctx = (AVIOContext*) client->httpd_data; + return avio_read(client_ctx, buf, size); +} + +void lavfhttpd_close(void *server, struct HTTPClient *client) +{ + AVIOContext *client_ctx = (AVIOContext*) client->httpd_data; + avio_close(client_ctx); + if (client->method) + av_free(client->method); + if (client->resource) + av_free(client->resource); + av_free(client); +} + +void lavfhttpd_shutdown(void *server) +{ + AVIOContext *server_ctx = (AVIOContext*) server; + avio_close(server_ctx); + avformat_network_deinit(); +} + +struct HTTPDInterface lavfhttpd = { + .init = lavfhttpd_init, + .accept = lavfhttpd_accept, + .write = lavfhttpd_write, + .close = lavfhttpd_close, + .shutdown = lavfhttpd_shutdown, +}; + +#endif