[FFmpeg-devel] [PATCH 1/3] [RFC]lavf/avio: add custom authentication API

Lukasz Marek lukasz.m.luki2 at gmail.com
Wed Apr 8 01:04:56 CEST 2015


Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
---
 libavformat/avio.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/avio.h | 28 ++++++++++++++++++++++++++++
 libavformat/url.h  | 12 ++++++++++++
 3 files changed, 86 insertions(+)

diff --git a/libavformat/avio.c b/libavformat/avio.c
index a990ea2..9006cac 100644
--- a/libavformat/avio.c
+++ b/libavformat/avio.c
@@ -30,9 +30,12 @@
 #include "network.h"
 #endif
 #include "url.h"
+#include "internal.h"
 
 static URLProtocol *first_protocol = NULL;
 
+static AVIOCredentialsCB g_credentials_callback;
+
 URLProtocol *ffurl_protocol_next(const URLProtocol *prev)
 {
     return prev ? prev->next : first_protocol;
@@ -543,3 +546,46 @@ int ff_check_interrupt(AVIOInterruptCB *cb)
         return ret;
     return 0;
 }
+
+void avio_register_default_credentials_callback(const AVIOCredentialsCB *callback)
+{
+    if (callback)
+        g_credentials_callback = *callback;
+}
+
+int ff_url_get_credentials(const URLContext *h, AVIOCredentials *credentials)
+{
+    char url[MAX_URL_SIZE], proto[MAX_URL_SIZE], host[MAX_URL_SIZE];
+    int port, ret = 0;
+
+    if (!credentials) {
+        //probe
+        if (g_credentials_callback.callback || (h && h->credentials_callback.callback))
+            return 0;
+        return AVERROR(ENOSYS);
+    }
+
+    av_assert0(h);
+    av_url_split(proto, sizeof(proto), NULL, 0, host, sizeof(host), &port, NULL, 0, h->filename);
+    ff_url_join(url, sizeof(url), proto, NULL, host, port, NULL);
+
+    av_free(credentials->url);
+    credentials->url = av_strdup(url);
+    if (!credentials->url) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+    if (h->credentials_callback.callback)
+        ret = h->credentials_callback.callback(credentials, h->credentials_callback.opaque);
+    else if (g_credentials_callback.callback)
+        ret = g_credentials_callback.callback(credentials, g_credentials_callback.opaque);
+    else
+        ret = AVERROR(ENOSYS);
+  fail:
+    av_freep(&credentials->url);
+    if (ret < 0) {
+        av_freep(&credentials->username);
+        av_freep(&credentials->password);
+    }
+    return ret;
+}
diff --git a/libavformat/avio.h b/libavformat/avio.h
index 51913e3..60381c4 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -53,6 +53,27 @@ typedef struct AVIOInterruptCB {
 } AVIOInterruptCB;
 
 /**
+ * User provided URL credentials. Used by AVIOCredentialsCB.
+ * Protocol may ask for all fields or only for some of the (e.g. for password only).
+ * Protocol allocates required fields with default values (empty string for no default).
+ * Non-allocated fields must be skipped by the user.
+ * Allocated fields may be reallocated with their new values provided by the user.
+ */
+typedef struct AVIOCredentials {
+    char *url;                            /**< Read only, never include any credentials */
+    char *username;                       /**< User's name */
+    char *password;                       /**< User's password */
+} AVIOCredentials;
+
+/**
+ * Callback for asking user to enter URL credentials.
+ */
+typedef struct AVIOCredentialsCB {
+    int (*callback)(AVIOCredentials *credentials, void*);
+    void *opaque;
+} AVIOCredentialsCB;
+
+/**
  * Directory entry types.
  */
 enum AVIODirEntryType {
@@ -617,4 +638,11 @@ struct AVBPrint;
  */
 int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size);
 
+
+/**
+ * Register credentials callback
+ * @param callback callback data
+ */
+void avio_register_default_credentials_callback(const AVIOCredentialsCB *callback);
+
 #endif /* AVFORMAT_AVIO_H */
diff --git a/libavformat/url.h b/libavformat/url.h
index 1a845b7..f873734 100644
--- a/libavformat/url.h
+++ b/libavformat/url.h
@@ -47,6 +47,7 @@ typedef struct URLContext {
     int is_connected;
     AVIOInterruptCB interrupt_callback;
     int64_t rw_timeout;         /**< maximum time to wait for (network) read/write operation completion, in mcs */
+    AVIOCredentialsCB credentials_callback;
 } URLContext;
 
 typedef struct URLProtocol {
@@ -290,5 +291,16 @@ void ff_make_absolute_url(char *buf, int size, const char *base,
  */
 AVIODirEntry *ff_alloc_dir_entry(void);
 
+/**
+ * Get user provided credentials.
+ *
+ * May be called with NULL credentials (or even protocol context) to probe implementation.
+ * Must not be called with NULL context when credentials is not NULL.
+ *
+ * @param h                  protocol context
+ * @param[inout] credentials credentails data, may be NULL for implementation probing.
+ * @return                   >=0 on success, negative on error
+ */
+int ff_url_get_credentials(const URLContext *h, AVIOCredentials *credentials);
 
 #endif /* AVFORMAT_URL_H */
-- 
1.9.1



More information about the ffmpeg-devel mailing list