[FFmpeg-devel,1/2] avformat/avio: add new_http_request callback

Submitted by Aman Gupta on Dec. 29, 2017, 10:06 p.m.

Details

Message ID 20171229220606.29155-1-ffmpeg@tmm1.net
State New
Headers show

Commit Message

Aman Gupta Dec. 29, 2017, 10:06 p.m.
From: Aman Gupta <aman@tmm1.net>

This callback is implemented on AVIOContext which are backed by an
HTTP URLContext. This allows the owner of the IO context to both
discover that it is capable of making additional http requests, and
to make such requests without including http.h. In particular this
avoids the use of internal functions to fetch the URLContext, and
prevents accidentally calling ff_http_do_new_request on a URLContext
which is not actually part of an HTTPContext.

Signed-off-by: Aman Gupta <aman@tmm1.net>
Signed-off-by: wm4 <nfxjfg@googlemail.com>
---
 libavformat/avio.h    |  6 ++++++
 libavformat/aviobuf.c | 10 ++++++++++
 libavformat/http.c    |  2 ++
 libavformat/url.h     |  1 +
 4 files changed, 19 insertions(+)

Patch hide | download patch | download mbox

diff --git a/libavformat/avio.h b/libavformat/avio.h
index 75912ce6be..640ca9f9e2 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -349,6 +349,12 @@  typedef struct AVIOContext {
      * Try to buffer at least this amount of data before flushing it
      */
     int min_packet_size;
+
+    /**
+     * Make a new request on the underlying http connection.
+     * This is internal only, do not use from outside.
+     */
+    int (*new_http_request)(struct AVIOContext *pb, const char *uri);
 } AVIOContext;
 
 /**
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 86eb6579f4..8530c78823 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -922,6 +922,14 @@  static int64_t io_read_seek(void *opaque, int stream_index, int64_t timestamp, i
     return internal->h->prot->url_read_seek(internal->h, stream_index, timestamp, flags);
 }
 
+static int io_new_http_request(AVIOContext *s, const char *uri)
+{
+    URLContext *uc = ffio_geturlcontext(s);
+    if (!uc)
+        return AVERROR(ENOSYS);
+    return uc->prot->url_new_http_request(uc, uri);
+}
+
 int ffio_fdopen(AVIOContext **s, URLContext *h)
 {
     AVIOInternal *internal = NULL;
@@ -970,6 +978,8 @@  int ffio_fdopen(AVIOContext **s, URLContext *h)
 
         if (h->prot->url_read_seek)
             (*s)->seekable |= AVIO_SEEKABLE_TIME;
+        if (h->prot->url_new_http_request)
+            (*s)->new_http_request = io_new_http_request;
     }
     (*s)->short_seek_get = io_short_seek;
     (*s)->av_class = &ff_avio_class;
diff --git a/libavformat/http.c b/libavformat/http.c
index a376f1a488..1dad129bdd 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -1712,6 +1712,7 @@  const URLProtocol ff_http_protocol = {
     .url_write           = http_write,
     .url_seek            = http_seek,
     .url_close           = http_close,
+    .url_new_http_request= ff_http_do_new_request,
     .url_get_file_handle = http_get_file_handle,
     .url_get_short_seek  = http_get_short_seek,
     .url_shutdown        = http_shutdown,
@@ -1732,6 +1733,7 @@  const URLProtocol ff_https_protocol = {
     .url_write           = http_write,
     .url_seek            = http_seek,
     .url_close           = http_close,
+    .url_new_http_request= ff_http_do_new_request,
     .url_get_file_handle = http_get_file_handle,
     .url_get_short_seek  = http_get_short_seek,
     .url_shutdown        = http_shutdown,
diff --git a/libavformat/url.h b/libavformat/url.h
index 4750bfff82..2f2d62e54f 100644
--- a/libavformat/url.h
+++ b/libavformat/url.h
@@ -62,6 +62,7 @@  typedef struct URLProtocol {
     int     (*url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options);
     int     (*url_accept)(URLContext *s, URLContext **c);
     int     (*url_handshake)(URLContext *c);
+    int     (*url_new_http_request)(URLContext *h, const char *uri);
 
     /**
      * Read data from the protocol.