[FFmpeg-devel] [PATCH 6/8] ffserver.c: Add config file reading

2018-05-31 Thread Stephan Holljes
Signed-off-by: Stephan Holljes 
---
 ffserver.c | 248 ++---
 1 file changed, 172 insertions(+), 76 deletions(-)

diff --git a/ffserver.c b/ffserver.c
index b80a7f8..1363cdc 100644
--- a/ffserver.c
+++ b/ffserver.c
@@ -38,6 +38,7 @@
 #include "segment.h"
 #include "publisher.h"
 #include "httpd.h"
+#include "configreader.h"
 
 #define BUFFER_SECS 30
 #define LISTEN_TIMEOUT_MSEC 1000
@@ -54,9 +55,11 @@ struct WriteInfo {
 };
 
 struct AcceptInfo {
-struct PublisherContext *pub;
+struct PublisherContext **pubs;
 struct HTTPDInterface *httpd;
-AVFormatContext *ifmt_ctx;
+AVFormatContext **ifmt_ctxs;
+struct HTTPDConfig *config;
+int nb_pub; /* number of publishers (streams) equal to number of ifmt_ctx 
*/
 };
 
 
@@ -286,52 +289,77 @@ void *accept_thread(void *arg)
 {
 struct AcceptInfo *info = (struct AcceptInfo*) arg;
 struct FFServerInfo *ffinfo = NULL;
+struct PublisherContext *pub;
 char status[4096];
+char *stream_name;
 struct HTTPClient *client = NULL;
 void *server = NULL;
 AVIOContext *client_ctx = NULL;
 AVFormatContext *ofmt_ctx = NULL;
+AVFormatContext *ifmt_ctx;
 unsigned char *avio_buffer;
 AVOutputFormat *ofmt;
 AVDictionary *mkvopts = NULL;
 AVStream *in_stream, *out_stream;
 int ret, i, reply_code;
-struct HTTPDConfig config = {
-.bind_address = "0",
-.port = 8080,
-.accept_timeout = LISTEN_TIMEOUT_MSEC,
-};
-
-info->httpd->init(, config);
-
-
+int shutdown;
+struct HTTPDConfig *config = info->config;
+
+info->httpd->init(, *config);
+
 for (;;) {
-if (info->pub->shutdown)
+shutdown = 1;
+for (i = 0; i < config->nb_streams; i++) {
+if (info->pubs[i] && !info->pubs[i]->shutdown)
+shutdown = 0;
+}
+if (shutdown)
 break;
-publisher_gen_status_json(info->pub, status);
-av_log(server, AV_LOG_INFO, status);
+for (i = 0; i < config->nb_streams; i++) {
+publisher_gen_status_json(info->pubs[i], status);
+av_log(server, AV_LOG_INFO, status);
+}
 client = NULL;
 av_log(server, AV_LOG_DEBUG, "Accepting new clients.\n");
 reply_code = 200;
-if (publisher_reserve_client(info->pub)) {
-av_log(client, AV_LOG_WARNING, "No more client slots free, 
Returning 503.\n");
-reply_code = 503;
-}
-
+
 if ((ret = info->httpd->accept(server, , reply_code)) < 0) {
 if (ret == HTTPD_LISTEN_TIMEOUT) {
-publisher_cancel_reserve(info->pub);
 continue;
 } else if (ret == HTTPD_CLIENT_ERROR) {
 info->httpd->close(server, client);
 }
 av_log(server, AV_LOG_WARNING, "Error during accept, retrying.\n");
-publisher_cancel_reserve(info->pub);
 continue;
 }
-
+
+pub = NULL;
+ifmt_ctx = NULL;
+for (i = 0; i < config->nb_streams; i++) {
+stream_name = info->pubs[i]->stream_name;
+//   skip leading '/'  ---v
+if(!strncmp(client->resource + 1, stream_name, 
strlen(stream_name))) {
+pub = info->pubs[i];
+ifmt_ctx = info->ifmt_ctxs[i];
+break;
+}
+}
+
+if (!pub || !ifmt_ctx) {
+av_log(client_ctx, AV_LOG_WARNING, "No suitable publisher found 
for resource: %s.\n",
+client->resource ? 
client->resource : "(null)");
+reply_code = 404;
+}
+
+
+if (pub && ifmt_ctx && publisher_reserve_client(pub)) {
+av_log(client_ctx, AV_LOG_WARNING, "No more client slots free, 
Returning 503.\n");
+reply_code = 503;
+}
+
 if (reply_code != 200) {
-publisher_cancel_reserve(info->pub);
+if (pub && ifmt_ctx)
+publisher_cancel_reserve(pub);
 info->httpd->close(server, client);
 continue;
 }
@@ -344,7 +372,7 @@ void *accept_thread(void *arg)
 client_ctx = avio_alloc_context(avio_buffer, AV_BUFSIZE, 1, ffinfo, 
NULL, _write, NULL);
 if (!client_ctx) {
 av_log(client, AV_LOG_ERROR, "Could not allocate output format 
context.\n");
-publisher_cancel_reserve(info->pub);
+publisher_cancel_reserve(pub);
 info->httpd->close(server, client);
 av_free(client_ctx->buffer);
 avio_context_free(_ctx);
@@ -354,7 +382,7 @@ void *accept_thread(void *arg)
 avformat_alloc_output_context2(_ctx, NULL, "matroska", NULL);
 if (!ofmt_ctx) {
 av_log(client, AV_LOG_ERROR, "Could not allocate output format 
context.\n");
-publisher_cancel_reserve(info->pub);
+

[FFmpeg-devel] [PATCH 6/8] ffserver.c: Add config file reading

2018-05-28 Thread Stephan Holljes
Signed-off-by: Stephan Holljes 
---
 ffserver.c | 248 ++---
 1 file changed, 172 insertions(+), 76 deletions(-)

diff --git a/ffserver.c b/ffserver.c
index 402e710..12c257f 100644
--- a/ffserver.c
+++ b/ffserver.c
@@ -38,6 +38,7 @@
 #include "segment.h"
 #include "publisher.h"
 #include "httpd.h"
+#include "configreader.h"
 
 #define BUFFER_SECS 30
 #define LISTEN_TIMEOUT_MSEC 1000
@@ -54,9 +55,11 @@ struct WriteInfo {
 };
 
 struct AcceptInfo {
-struct PublisherContext *pub;
+struct PublisherContext **pubs;
 struct HTTPDInterface *httpd;
-AVFormatContext *ifmt_ctx;
+AVFormatContext **ifmt_ctxs;
+struct HTTPDConfig *config;
+int nb_pub; /* number of publishers (streams) equal to number of ifmt_ctx 
*/
 };
 
 
@@ -290,52 +293,77 @@ void *accept_thread(void *arg)
 {
 struct AcceptInfo *info = (struct AcceptInfo*) arg;
 struct FFServerInfo *ffinfo = NULL;
+struct PublisherContext *pub;
 char status[4096];
+char *stream_name;
 struct HTTPClient *client = NULL;
 void *server = NULL;
 AVIOContext *client_ctx = NULL;
 AVFormatContext *ofmt_ctx = NULL;
+AVFormatContext *ifmt_ctx;
 unsigned char *avio_buffer;
 AVOutputFormat *ofmt;
 AVDictionary *mkvopts = NULL;
 AVStream *in_stream, *out_stream;
 int ret, i, reply_code;
-struct HTTPDConfig config = {
-.bind_address = "0",
-.port = 8080,
-.accept_timeout = LISTEN_TIMEOUT_MSEC,
-};
-
-info->httpd->init(, config);
-
-
+int shutdown;
+struct HTTPDConfig *config = info->config;
+
+info->httpd->init(, *config);
+
 for (;;) {
-if (info->pub->shutdown)
+shutdown = 1;
+for (i = 0; i < config->nb_streams; i++) {
+if (info->pubs[i] && !info->pubs[i]->shutdown)
+shutdown = 0;
+}
+if (shutdown)
 break;
-publisher_gen_status_json(info->pub, status);
-av_log(server, AV_LOG_INFO, status);
+for (i = 0; i < config->nb_streams; i++) {
+publisher_gen_status_json(info->pubs[i], status);
+av_log(server, AV_LOG_INFO, status);
+}
 client = NULL;
 av_log(server, AV_LOG_DEBUG, "Accepting new clients.\n");
 reply_code = 200;
-if (publisher_reserve_client(info->pub)) {
-av_log(client, AV_LOG_WARNING, "No more client slots free, 
Returning 503.\n");
-reply_code = 503;
-}
-
+
 if ((ret = info->httpd->accept(server, , reply_code)) < 0) {
 if (ret == HTTPD_LISTEN_TIMEOUT) {
-publisher_cancel_reserve(info->pub);
 continue;
 } else if (ret == HTTPD_CLIENT_ERROR) {
 info->httpd->close(server, client);
 }
 av_log(server, AV_LOG_WARNING, "Error during accept, retrying.\n");
-publisher_cancel_reserve(info->pub);
 continue;
 }
-
+
+pub = NULL;
+ifmt_ctx = NULL;
+for (i = 0; i < config->nb_streams; i++) {
+stream_name = info->pubs[i]->stream_name;
+//   skip leading '/'  ---v
+if(!strncmp(client->resource + 1, stream_name, 
strlen(stream_name))) {
+pub = info->pubs[i];
+ifmt_ctx = info->ifmt_ctxs[i];
+break;
+}
+}
+
+if (!pub || !ifmt_ctx) {
+av_log(client_ctx, AV_LOG_WARNING, "No suitable publisher found 
for resource: %s.\n",
+client->resource ? 
client->resource : "(null)");
+reply_code = 404;
+}
+
+
+if (pub && ifmt_ctx && publisher_reserve_client(pub)) {
+av_log(client_ctx, AV_LOG_WARNING, "No more client slots free, 
Returning 503.\n");
+reply_code = 503;
+}
+
 if (reply_code != 200) {
-publisher_cancel_reserve(info->pub);
+if (pub && ifmt_ctx)
+publisher_cancel_reserve(pub);
 info->httpd->close(server, client);
 continue;
 }
@@ -348,7 +376,7 @@ void *accept_thread(void *arg)
 client_ctx = avio_alloc_context(avio_buffer, AV_BUFSIZE, 1, ffinfo, 
NULL, _write, NULL);
 if (!client_ctx) {
 av_log(client, AV_LOG_ERROR, "Could not allocate output format 
context.\n");
-publisher_cancel_reserve(info->pub);
+publisher_cancel_reserve(pub);
 info->httpd->close(server, client);
 av_free(client_ctx->buffer);
 avio_context_free(_ctx);
@@ -358,7 +386,7 @@ void *accept_thread(void *arg)
 avformat_alloc_output_context2(_ctx, NULL, "matroska", NULL);
 if (!ofmt_ctx) {
 av_log(client, AV_LOG_ERROR, "Could not allocate output format 
context.\n");
-publisher_cancel_reserve(info->pub);
+

Re: [FFmpeg-devel] [PATCH 6/8] ffserver.c: Add config file reading

2018-05-20 Thread Michael Niedermayer
On Sun, May 20, 2018 at 08:54:02PM +0200, Stephan Holljes wrote:
> Signed-off-by: Stephan Holljes 
> ---
>  ffserver.c | 248 
> ++---
>  1 file changed, 172 insertions(+), 76 deletions(-)
> 
> diff --git a/ffserver.c b/ffserver.c
> index 44fc263..3d842d8 100644
> --- a/ffserver.c
> +++ b/ffserver.c
> @@ -38,6 +38,7 @@
>  #include "segment.h"
>  #include "publisher.h"
>  #include "httpd.h"
> +#include "configreader.h"
>  
>  #define BUFFER_SECS 30
>  #define LISTEN_TIMEOUT_MSEC 1000
> @@ -54,9 +55,11 @@ struct WriteInfo {
>  };
>  
>  struct AcceptInfo {
> -struct PublisherContext *pub;
> +struct PublisherContext **pubs;
>  struct HTTPDInterface *httpd;
> -AVFormatContext *ifmt_ctx;
> +AVFormatContext **ifmt_ctxs;
> +struct HTTPDConfig *config;
> +int nb_pub; /* number of publishers (streams) equal to number of 
> ifmt_ctx */
>  };
>  
>  
> @@ -287,52 +290,77 @@ void *accept_thread(void *arg)
>  {
>  struct AcceptInfo *info = (struct AcceptInfo*) arg;
>  struct FFServerInfo *ffinfo = NULL;
> +struct PublisherContext *pub;
>  char status[4096];
> +char *stream_name;
>  struct HTTPClient *client = NULL;
>  void *server = NULL;
>  AVIOContext *client_ctx = NULL;
>  AVFormatContext *ofmt_ctx = NULL;
> +AVFormatContext *ifmt_ctx;
>  unsigned char *avio_buffer;
>  AVOutputFormat *ofmt;
>  AVDictionary *mkvopts = NULL;
>  AVStream *in_stream, *out_stream;
>  int ret, i, reply_code;
> -struct HTTPDConfig config = {
> -.bind_address = "0",
> -.port = 8080,
> -.accept_timeout = LISTEN_TIMEOUT_MSEC,
> -};
> -
> -info->httpd->init(, config);
> -
> -
> +int shutdown;
> +struct HTTPDConfig *config = info->config;
> +
> +info->httpd->init(, *config);
> +
>  for (;;) {
> -if (info->pub->shutdown)
> +shutdown = 1;
> +for (i = 0; i < config->nb_streams; i++) {
> +if (info->pubs[i] && !info->pubs[i]->shutdown)
> +shutdown = 0;
> +}
> +if (shutdown)
>  break;
> -publisher_gen_status_json(info->pub, status);
> -av_log(server, AV_LOG_INFO, status);
> +for (i = 0; i < config->nb_streams; i++) {
> +publisher_gen_status_json(info->pubs[i], status);
> +av_log(server, AV_LOG_INFO, status);
> +}
>  client = NULL;
>  av_log(server, AV_LOG_DEBUG, "Accepting new clients.\n");
>  reply_code = 200;
> -if (publisher_reserve_client(info->pub)) {
> -av_log(client, AV_LOG_WARNING, "No more client slots free, 
> Returning 503.\n");
> -reply_code = 503;
> -}
> -
> +
>  if ((ret = info->httpd->accept(server, , reply_code)) < 0) {
>  if (ret == HTTPD_LISTEN_TIMEOUT) {
> -publisher_cancel_reserve(info->pub);
>  continue;
>  } else if (ret == HTTPD_CLIENT_ERROR) {
>  info->httpd->close(server, client);
>  }
>  av_log(server, AV_LOG_WARNING, "Error during accept, 
> retrying.\n");
> -publisher_cancel_reserve(info->pub);
>  continue;
>  }
> -
> +
> +pub = NULL;
> +ifmt_ctx = NULL;
> +for (i = 0; i < config->nb_streams; i++) {
> +stream_name = info->pubs[i]->stream_name;
> +//   skip leading '/'  ---v
> +if(!strncmp(client->resource + 1, stream_name, 
> strlen(stream_name))) {
> +pub = info->pubs[i];
> +ifmt_ctx = info->ifmt_ctxs[i];
> +break;
> +}
> +}
> +
> +if (!pub || !ifmt_ctx) {
> +av_log(client_ctx, AV_LOG_WARNING, "No suitable publisher found 
> for resource: %s.\n",
> +client->resource ? 
> client->resource : "(null)");
> +reply_code = 404;
> +}
> +
> +
> +if (pub && ifmt_ctx && publisher_reserve_client(pub)) {
> +av_log(client_ctx, AV_LOG_WARNING, "No more client slots free, 
> Returning 503.\n");
> +reply_code = 503;
> +}
> +
>  if (reply_code != 200) {
> -publisher_cancel_reserve(info->pub);
> +if (pub && ifmt_ctx)
> +publisher_cancel_reserve(pub);
>  info->httpd->close(server, client);
>  continue;
>  }
> @@ -345,7 +373,7 @@ void *accept_thread(void *arg)
>  client_ctx = avio_alloc_context(avio_buffer, AV_BUFSIZE, 1, ffinfo, 
> NULL, _write, NULL);
>  if (!client_ctx) {
>  av_log(client, AV_LOG_ERROR, "Could not allocate output format 
> context.\n");
> -publisher_cancel_reserve(info->pub);
> +publisher_cancel_reserve(pub);
>  info->httpd->close(server, 

[FFmpeg-devel] [PATCH 6/8] ffserver.c: Add config file reading

2018-05-20 Thread Stephan Holljes
Signed-off-by: Stephan Holljes 
---
 ffserver.c | 248 ++---
 1 file changed, 172 insertions(+), 76 deletions(-)

diff --git a/ffserver.c b/ffserver.c
index 44fc263..3d842d8 100644
--- a/ffserver.c
+++ b/ffserver.c
@@ -38,6 +38,7 @@
 #include "segment.h"
 #include "publisher.h"
 #include "httpd.h"
+#include "configreader.h"
 
 #define BUFFER_SECS 30
 #define LISTEN_TIMEOUT_MSEC 1000
@@ -54,9 +55,11 @@ struct WriteInfo {
 };
 
 struct AcceptInfo {
-struct PublisherContext *pub;
+struct PublisherContext **pubs;
 struct HTTPDInterface *httpd;
-AVFormatContext *ifmt_ctx;
+AVFormatContext **ifmt_ctxs;
+struct HTTPDConfig *config;
+int nb_pub; /* number of publishers (streams) equal to number of ifmt_ctx 
*/
 };
 
 
@@ -287,52 +290,77 @@ void *accept_thread(void *arg)
 {
 struct AcceptInfo *info = (struct AcceptInfo*) arg;
 struct FFServerInfo *ffinfo = NULL;
+struct PublisherContext *pub;
 char status[4096];
+char *stream_name;
 struct HTTPClient *client = NULL;
 void *server = NULL;
 AVIOContext *client_ctx = NULL;
 AVFormatContext *ofmt_ctx = NULL;
+AVFormatContext *ifmt_ctx;
 unsigned char *avio_buffer;
 AVOutputFormat *ofmt;
 AVDictionary *mkvopts = NULL;
 AVStream *in_stream, *out_stream;
 int ret, i, reply_code;
-struct HTTPDConfig config = {
-.bind_address = "0",
-.port = 8080,
-.accept_timeout = LISTEN_TIMEOUT_MSEC,
-};
-
-info->httpd->init(, config);
-
-
+int shutdown;
+struct HTTPDConfig *config = info->config;
+
+info->httpd->init(, *config);
+
 for (;;) {
-if (info->pub->shutdown)
+shutdown = 1;
+for (i = 0; i < config->nb_streams; i++) {
+if (info->pubs[i] && !info->pubs[i]->shutdown)
+shutdown = 0;
+}
+if (shutdown)
 break;
-publisher_gen_status_json(info->pub, status);
-av_log(server, AV_LOG_INFO, status);
+for (i = 0; i < config->nb_streams; i++) {
+publisher_gen_status_json(info->pubs[i], status);
+av_log(server, AV_LOG_INFO, status);
+}
 client = NULL;
 av_log(server, AV_LOG_DEBUG, "Accepting new clients.\n");
 reply_code = 200;
-if (publisher_reserve_client(info->pub)) {
-av_log(client, AV_LOG_WARNING, "No more client slots free, 
Returning 503.\n");
-reply_code = 503;
-}
-
+
 if ((ret = info->httpd->accept(server, , reply_code)) < 0) {
 if (ret == HTTPD_LISTEN_TIMEOUT) {
-publisher_cancel_reserve(info->pub);
 continue;
 } else if (ret == HTTPD_CLIENT_ERROR) {
 info->httpd->close(server, client);
 }
 av_log(server, AV_LOG_WARNING, "Error during accept, retrying.\n");
-publisher_cancel_reserve(info->pub);
 continue;
 }
-
+
+pub = NULL;
+ifmt_ctx = NULL;
+for (i = 0; i < config->nb_streams; i++) {
+stream_name = info->pubs[i]->stream_name;
+//   skip leading '/'  ---v
+if(!strncmp(client->resource + 1, stream_name, 
strlen(stream_name))) {
+pub = info->pubs[i];
+ifmt_ctx = info->ifmt_ctxs[i];
+break;
+}
+}
+
+if (!pub || !ifmt_ctx) {
+av_log(client_ctx, AV_LOG_WARNING, "No suitable publisher found 
for resource: %s.\n",
+client->resource ? 
client->resource : "(null)");
+reply_code = 404;
+}
+
+
+if (pub && ifmt_ctx && publisher_reserve_client(pub)) {
+av_log(client_ctx, AV_LOG_WARNING, "No more client slots free, 
Returning 503.\n");
+reply_code = 503;
+}
+
 if (reply_code != 200) {
-publisher_cancel_reserve(info->pub);
+if (pub && ifmt_ctx)
+publisher_cancel_reserve(pub);
 info->httpd->close(server, client);
 continue;
 }
@@ -345,7 +373,7 @@ void *accept_thread(void *arg)
 client_ctx = avio_alloc_context(avio_buffer, AV_BUFSIZE, 1, ffinfo, 
NULL, _write, NULL);
 if (!client_ctx) {
 av_log(client, AV_LOG_ERROR, "Could not allocate output format 
context.\n");
-publisher_cancel_reserve(info->pub);
+publisher_cancel_reserve(pub);
 info->httpd->close(server, client);
 av_free(client_ctx->buffer);
 avio_context_free(_ctx);
@@ -355,7 +383,7 @@ void *accept_thread(void *arg)
 avformat_alloc_output_context2(_ctx, NULL, "matroska", NULL);
 if (!ofmt_ctx) {
 av_log(client, AV_LOG_ERROR, "Could not allocate output format 
context.\n");
-