Hi, attached patches are the current state of work. It's probably still a bit rough around the edges, but I think I made some progress. The sample code in doc/examples does not write any data as of yet, but the HTTP handshake works.
From b43aeaa27f6ca7df476aa194b2f78aa1b49516d0 Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:06:16 +0200 Subject: [PATCH 01/10] lavf/network: split ff_listen_bind into ff_listen and ff_accept
Signed-off-by: Stephan Holljes <klaxa1...@googlemail.com> --- libavformat/network.c | 27 +++++++++++++++++++++------ libavformat/network.h | 4 ++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/libavformat/network.c b/libavformat/network.c index 47ade8c..8d61746 100644 --- a/libavformat/network.c +++ b/libavformat/network.c @@ -187,12 +187,11 @@ int ff_socket(int af, int type, int proto) return fd; } -int ff_listen_bind(int fd, const struct sockaddr *addr, - socklen_t addrlen, int timeout, URLContext *h) +int ff_listen(int fd, const struct sockaddr *addr, + socklen_t addrlen) { int ret; int reuse = 1; - struct pollfd lp = { fd, POLLIN, 0 }; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) { av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n"); } @@ -203,6 +202,13 @@ int ff_listen_bind(int fd, const struct sockaddr *addr, ret = listen(fd, 1); if (ret) return ff_neterrno(); + return ret; +} + +int ff_accept(int fd, int timeout, URLContext *h) +{ + int ret; + struct pollfd lp = { fd, POLLIN, 0 }; ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback); if (ret < 0) @@ -211,15 +217,24 @@ int ff_listen_bind(int fd, const struct sockaddr *addr, ret = accept(fd, NULL, NULL); if (ret < 0) return ff_neterrno(); - - closesocket(fd); - if (ff_socket_nonblock(ret, 1) < 0) av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n"); return ret; } +int ff_listen_bind(int fd, const struct sockaddr *addr, + socklen_t addrlen, int timeout, URLContext *h) +{ + int ret; + if ((ret = ff_listen(fd, addr, addrlen)) < 0) + return ret; + ret = ff_accept(fd, timeout, h); + closesocket(fd); + return ret; + +} + int ff_listen_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next) diff --git a/libavformat/network.h b/libavformat/network.h index 86fb656..44e109c 100644 --- a/libavformat/network.h +++ b/libavformat/network.h @@ -254,6 +254,10 @@ int ff_listen_bind(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h); +int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen); + +int ff_accept(int fd, int timeout, URLContext *h); + /** * Connect to a file descriptor and poll for result. * -- 2.1.0
From 39faa1ea315bb51452446e291fd5d93d7eb3a988 Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:12:37 +0200 Subject: [PATCH 02/10] lavf/avio: add ffurl_accept and ffurl_handshake Signed-off-by: Stephan Holljes <klaxa1...@googlemail.com> --- libavformat/avio.c | 15 +++++++++++++++ libavformat/url.h | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/libavformat/avio.c b/libavformat/avio.c index aff8d10..cb8126d 100644 --- a/libavformat/avio.c +++ b/libavformat/avio.c @@ -211,6 +211,21 @@ int ffurl_connect(URLContext *uc, AVDictionary **options) return 0; } +int ffurl_accept(URLContext *s, URLContext *c) +{ + int ret; + if ((ret = s->prot->url_accept(s, c)) < 0) + return ret; + c->is_connected = 1; + return ret; + +} + +int ffurl_handshake(URLContext *s, URLContext *c) +{ + return s->prot->url_handshake(s, c); +} + #define URL_SCHEME_CHARS \ "abcdefghijklmnopqrstuvwxyz" \ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ diff --git a/libavformat/url.h b/libavformat/url.h index 99a3201..78613c5 100644 --- a/libavformat/url.h +++ b/libavformat/url.h @@ -58,6 +58,8 @@ typedef struct URLProtocol { * for those nested protocols. */ int (*url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options); + int (*url_accept)(URLContext *s, URLContext *c); + int (*url_handshake)(URLContext *s, URLContext *c); /** * Read data from the protocol. @@ -140,6 +142,16 @@ int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options); /** + * Accept an URLContext c on an URLContext s + * @param s server context + * @param c client context + * @return the accepted filedescriptor on success, ff_neterrno() on failure. + */ +int ffurl_accept(URLContext *s, URLContext *c); + +int ffurl_handshake(URLContext *s, URLContext *c); + +/** * Read up to size bytes from the resource accessed by h, and store * the read bytes in buf. * -- 2.1.0
From 8b473ba9acffecf98f8252eeccb413bcfbbf38c5 Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:13:36 +0200 Subject: [PATCH 03/10] lavf/avio: add avio_accept and avio_handshake Signed-off-by: Stephan Holljes <klaxa1...@googlemail.com> --- libavformat/avio.h | 2 ++ libavformat/aviobuf.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/libavformat/avio.h b/libavformat/avio.h index 5ac5d38..7407ab0 100644 --- a/libavformat/avio.h +++ b/libavformat/avio.h @@ -648,4 +648,6 @@ struct AVBPrint; */ int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size); +int avio_accept(AVIOContext *s, AVIOContext **c); +int avio_handshake(AVIOContext *s, AVIOContext *c); #endif /* AVFORMAT_AVIO_H */ diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index ff85081..9be5131 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -1021,6 +1021,27 @@ int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size) return 0; } +int avio_accept(AVIOContext *s, AVIOContext **c) +{ + int ret; + URLContext *sc = s->opaque; + URLContext *cc; + if ((ret = ffurl_alloc(&cc, sc->filename, AVIO_FLAG_READ_WRITE, &sc->interrupt_callback)) < 0) + return ret; + ret = ffurl_accept(sc, cc); + if (ret > 0) + ret = ffio_fdopen(c, cc); + return ret; +} + +int avio_handshake(AVIOContext *s, AVIOContext *c) +{ + int ret; + URLContext *sc = s->opaque; + URLContext *cc = c->opaque; + return ffurl_handshake(sc, cc); +} + /* output in a dynamic buffer */ typedef struct DynBuffer { -- 2.1.0
From 8ba3d1ef528cdd9209764b0f696b8df81ea46870 Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:16:02 +0200 Subject: [PATCH 04/10] lavf/http: add http_accept and http_handshake Signed-off-by: Stephan Holljes <klaxa1...@googlemail.com> --- libavformat/http.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/libavformat/http.c b/libavformat/http.c index 676bfd5..7219f08 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -382,6 +382,38 @@ static int http_open(URLContext *h, const char *uri, int flags, return ret; } +static int http_accept(URLContext *s, URLContext *c) +{ + int ret; + HTTPContext *sh = s->priv_data; + HTTPContext *ch = c->priv_data; + URLContext *sl = sh->hd; + URLContext *cl; + if ((ret = ffurl_alloc(&cl, sl->filename, AVIO_FLAG_READ_WRITE, &sl->interrupt_callback)) < 0) + goto fail; + if ((ret = ffurl_accept(sl, cl)) < 0) + goto fail; + ch->hd = cl; +fail: + return ret; +} + +static int http_handshake(URLContext *s, URLContext *c) { + int ret, err, new_location; + HTTPContext *sh = s->priv_data; + HTTPContext *ch = c->priv_data; + URLContext *sl = sh->hd; + URLContext *cl = ch->hd; + static const char header[] = "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nTransfer-Encoding: chunked\r\n\r\n"; + if ((err = http_read_header(c, &new_location)) < 0) + goto fail; + if ((ret = ffurl_write(cl, header, strlen(header))) < 0) + goto fail; +fail: + handle_http_errors(c, err); + return ret; +} + static int http_getc(HTTPContext *s) { int len; @@ -1346,6 +1378,8 @@ HTTP_CLASS(http); URLProtocol ff_http_protocol = { .name = "http", .url_open2 = http_open, + .url_accept = http_accept, + .url_handshake = http_handshake, .url_read = http_read, .url_write = http_write, .url_seek = http_seek, @@ -1364,6 +1398,8 @@ HTTP_CLASS(https); URLProtocol ff_https_protocol = { .name = "https", .url_open2 = http_open, + .url_accept = http_accept, + .url_handshake = http_handshake, .url_read = http_read, .url_write = http_write, .url_seek = http_seek, @@ -1477,6 +1513,8 @@ static int http_proxy_write(URLContext *h, const uint8_t *buf, int size) URLProtocol ff_httpproxy_protocol = { .name = "httpproxy", .url_open = http_proxy_open, + .url_accept = http_accept, + .url_handshake = http_handshake, .url_read = http_buf_read, .url_write = http_proxy_write, .url_close = http_proxy_close, -- 2.1.0
From cfd27b21cf9fae39d881608a3ba379e6fb75848c Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:17:12 +0200 Subject: [PATCH 05/10] lavf/tcp: make tcp_open return with a listening socket without calling accept() --- libavformat/tcp.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libavformat/tcp.c b/libavformat/tcp.c index f24cad2..04210b3 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -126,11 +126,9 @@ static int tcp_open(URLContext *h, const char *uri, int flags) } if (s->listen) { - if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen, - s->listen_timeout, h)) < 0) { + if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0) { goto fail1; } - fd = ret; } else { if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen, s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) { -- 2.1.0
From dd197651d205b2dece97798e933974ecef3a2b7f Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:20:17 +0200 Subject: [PATCH 06/10] lavf/tcp: add tcp_accept Signed-off-by: Stephan Holljes <klaxa1...@googlemail.com> --- libavformat/tcp.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libavformat/tcp.c b/libavformat/tcp.c index 04210b3..9f7a388 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -161,6 +161,19 @@ static int tcp_open(URLContext *h, const char *uri, int flags) return ret; } +static int tcp_accept(URLContext *s, URLContext *c) +{ + TCPContext *sc = s->priv_data; + TCPContext *cc = c->priv_data; + int ret; + ret = accept(sc->fd, NULL, NULL); + if (ret < 0) { + return ff_neterrno(); + } + cc->fd = ret; + return ret; +} + static int tcp_read(URLContext *h, uint8_t *buf, int size) { TCPContext *s = h->priv_data; @@ -221,6 +234,7 @@ static int tcp_get_file_handle(URLContext *h) URLProtocol ff_tcp_protocol = { .name = "tcp", .url_open = tcp_open, + .url_accept = tcp_accept, .url_read = tcp_read, .url_write = tcp_write, .url_close = tcp_close, -- 2.1.0
From 5ab3661637c1ba571bc7f7bf365e3f3c8bc4ae89 Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:25:35 +0200 Subject: [PATCH 07/10] lavf/http: remove connection logic from http_listen() Signed-off-by: Stephan Holljes <klaxa1...@googlemail.com> --- libavformat/http.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/libavformat/http.c b/libavformat/http.c index 7219f08..dbf0374 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -320,11 +320,10 @@ static int http_listen(URLContext *h, const char *uri, int flags, AVDictionary **options) { HTTPContext *s = h->priv_data; int ret; - static const char header[] = "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nTransfer-Encoding: chunked\r\n\r\n"; char hostname[1024], proto[10]; char lower_url[100]; const char *lower_proto = "tcp"; - int port, new_location; + int port; s->chunked_post = 1; av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); @@ -336,14 +335,7 @@ static int http_listen(URLContext *h, const char *uri, int flags, if ((ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE, &h->interrupt_callback, options)) < 0) goto fail; - if ((ret = http_read_header(h, &new_location)) < 0) - goto fail; - if ((ret = ffurl_write(s->hd, header, strlen(header))) < 0) - goto fail; - return 0; - fail: - handle_http_errors(h, ret); av_dict_free(&s->chained_options); return ret; } -- 2.1.0
From b835a248bba5004ad8f8a598992fa959881b2376 Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:26:43 +0200 Subject: [PATCH 08/10] lavf/http: ignore 0 in handle_http_errors() Signed-off-by: Stephan Holljes <klaxa1...@googlemail.com> --- libavformat/http.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libavformat/http.c b/libavformat/http.c index dbf0374..33d1203 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -306,6 +306,8 @@ static void handle_http_errors(URLContext *h, int error) HTTPContext *s = h->priv_data; if (h->is_connected) { switch(error) { + case 0: + break; case AVERROR_HTTP_BAD_REQUEST: ffurl_write(s->hd, bad_request, strlen(bad_request)); break; -- 2.1.0
From 127b0ef456d203bc295ef017737019c0f8329515 Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:39:13 +0200 Subject: [PATCH 09/10] lavf/http: only shut down the connection when it's a client. Signed-off-by: Stephan Holljes <klaxa1...@googlemail.com> --- libavformat/http.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/http.c b/libavformat/http.c index 33d1203..528e1a1 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -1289,7 +1289,7 @@ static int http_close(URLContext *h) av_freep(&s->inflate_buffer); #endif /* CONFIG_ZLIB */ - if (!s->end_chunked_post) + if ((!s->listen && !s->end_chunked_post)) /* Close the write direction by sending the end of chunked encoding. */ ret = http_shutdown(h, h->flags); -- 2.1.0
From df4b466693b8d8627cca59a17d9e7ab5fd5e843e Mon Sep 17 00:00:00 2001 From: Stephan Holljes <klaxa1...@googlemail.com> Date: Sun, 28 Jun 2015 06:39:56 +0200 Subject: [PATCH 10/10] doc/examples: WIP: add http_multiclient example code. Signed-off-by: Stephan Holljes <klaxa1...@googlemail.com> --- doc/examples/Makefile | 1 + doc/examples/http_multiclient.c | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 doc/examples/http_multiclient.c diff --git a/doc/examples/Makefile b/doc/examples/Makefile index 9699f11..8c9501b 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -18,6 +18,7 @@ EXAMPLES= avio_list_dir \ extract_mvs \ filtering_video \ filtering_audio \ + http_multiclient \ metadata \ muxing \ remuxing \ diff --git a/doc/examples/http_multiclient.c b/doc/examples/http_multiclient.c new file mode 100644 index 0000000..215a8bb --- /dev/null +++ b/doc/examples/http_multiclient.c @@ -0,0 +1,60 @@ +#include <libavformat/avformat.h> + +int main(int argc, char **argv) +{ + AVOutputFormat *ofmt = NULL; + AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL; + AVDictionary *options = NULL; + AVPacket pkt; + AVIOContext *c = NULL; + const char *in_filename, *out_uri; + int ret; + + if (argc < 3) { + printf("usage: %s input http[s]://hostname[:port]\n" + "API example program to serve http to multiple clients.\n" + "The output format is guessed according to the input file extension.\n" + "\n", argv[0]); + return 1; + } + + in_filename = argv[1]; + out_uri = argv[2]; + + av_register_all(); + avformat_network_init(); + + if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) { + fprintf(stderr, "Could not open input file '%s'", in_filename); + goto end; + } + + if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) { + fprintf(stderr, "Failed to retrieve input stream information"); + goto end; + } + + avformat_alloc_output_context2(&ofmt_ctx, NULL, ifmt_ctx->iformat->name, out_uri); + if (!ofmt_ctx) { + fprintf(stderr, "Could not create output context\n"); + ret = AVERROR_UNKNOWN; + goto end; + } + + ofmt = ofmt_ctx->oformat; + av_dict_set(&options, "listen", "1", 0); + ret = avio_open2(&ofmt_ctx->pb, out_uri, AVIO_FLAG_READ_WRITE, NULL, &options); + avio_accept(ofmt_ctx->pb, &c); + avio_handshake(ofmt_ctx->pb, c); + avio_close(c); + +end: + avformat_close_input(&ifmt_ctx); + + if (ofmt_ctx) + avio_closep(&ofmt_ctx->pb); + avformat_free_context(ofmt_ctx); + if (ret < 0 && ret != AVERROR_EOF) + return 1; + return 0; +} -- 2.1.0
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel