[FFmpeg-devel] [PATCH 7/9] lavf/http: increase range for listen, handle connection closing accordingly, add http_handshake and move handshake logic there

Stephan Holljes klaxa1337 at googlemail.com
Wed Jul 8 19:03:56 CEST 2015


Signed-off-by: Stephan Holljes <klaxa1337 at googlemail.com>
---
 libavformat/http.c | 39 ++++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 11 deletions(-)
    Changes since first version:
      - Return from http_handshake() if no errors occured instead of falling through
        fail label
      - Add av_assert0() check for error in handle_http_errors()
      - Remove passthough if error == 0 (should never happen anymore)

diff --git a/libavformat/http.c b/libavformat/http.c
index 3c1ec35..813870e 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -129,7 +129,7 @@ static const AVOption options[] = {
     { "end_offset", "try to limit the request to bytes preceding this offset", OFFSET(end_off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
     { "method", "Override the HTTP method or set the expected HTTP method from a client", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
     { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
-    { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D | E },
+    { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
     { NULL }
 };
 
@@ -305,6 +305,7 @@ static void handle_http_errors(URLContext *h, int error)
     static const char bad_request[] = "HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n400 Bad Request\r\n";
     static const char internal_server_error[] = "HTTP/1.1 500 Internal server error\r\nContent-Type: text/plain\r\n\r\n500 Internal server error\r\n";
     HTTPContext *s = h->priv_data;
+    av_assert0(error < 0);
     if (h->is_connected) {
         switch(error) {
             case AVERROR_HTTP_BAD_REQUEST:
@@ -317,15 +318,33 @@ static void handle_http_errors(URLContext *h, int error)
     }
 }
 
+static int http_handshake(URLContext *c) {
+    int ret, err, new_location;
+    HTTPContext *ch = c->priv_data;
+    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 ((ret = ffurl_handshake(cl)) < 0)
+        return ret;
+    if ((err = http_read_header(c, &new_location)) < 0)
+        goto fail;
+    if ((ret = ffurl_write(cl, header, strlen(header))) < 0)
+        return ret;
+    // Avoid returning a positive value from ffurl_write()
+    ret = ret > 0 ? 0 : ret;
+    return ret;
+fail:
+    handle_http_errors(c, err);
+    return ret;
+}
+
 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);
@@ -333,18 +352,14 @@ static int http_listen(URLContext *h, const char *uri, int flags,
         lower_proto = "tls";
     ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
                 NULL);
-    av_dict_set(options, "listen", "1", 0);
+    if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
+        goto fail;
     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;
-
+    if (s->listen == 1) /* single client */
+        ret = http_handshake(h);
 fail:
-    handle_http_errors(h, ret);
     av_dict_free(&s->chained_options);
     return ret;
 }
@@ -1365,6 +1380,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,
-- 
2.1.0



More information about the ffmpeg-devel mailing list