[vlc-devel] [PATCH 2/2] srt: parse SRT parameters from incoming URL

Aaron Boxer boxerab at gmail.com
Fri Mar 15 01:19:20 CET 2019


>From 12dc89d8bbed8bdd9c768785596e6a60785e9862 Mon Sep 17 00:00:00 2001
From: Aaron Boxer <aaron.boxer at collabora.com>
Date: Tue, 5 Mar 2019 15:25:31 -0500
Subject: [PATCH 2/2] srt: parse SRT parameters from incoming URL

---
 modules/access/srt.c | 115 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 108 insertions(+), 7 deletions(-)

diff --git a/modules/access/srt.c b/modules/access/srt.c
index d8d8cbe659..c36d8a2dc7 100644
--- a/modules/access/srt.c
+++ b/modules/access/srt.c
@@ -37,6 +37,9 @@

 #include <srt/srt.h>

+const char* SRT_PARAM_PASSPHRASE     = "passphrase";
+const char* SRT_PARAM_LATENCY         = "latency";
+
 /* libsrt defines default packet size as 1316 internally
  * so srt module takes same value. */
 #define SRT_DEFAULT_CHUNK_SIZE 1316
@@ -70,6 +73,79 @@ typedef struct
     int         i_chunks; /* Number of chunks to allocate in the next read
*/
 } stream_sys_t;

+
+
+struct parsed_param {
+    char *key;
+    char *val;
+};
+
+static inline char *
+find(char *str, char find)
+{
+    str = strchr(str, find);
+    return str != NULL ? str + 1 : NULL;
+}
+
+/**
+ * Parse a query string into an array of key/value structs.
+ *
+ * The query string should be a null terminated string of parameters
separated by
+ * a delimiter. Each parameter are checked for the equal sign character.
If it
+ * appears in the parameter, it will be used as a null terminator and the
part
+ * that comes after it will be the value of the parameter.
+ *
+ *
+ * param: query:         the query string to parse. The string will be
modified.
+ * param: delimiter:      the character that separates the key/value pairs
from each other.
+ * param: params:       an array of parsed_param structs to hold the
result.
+ * param: max_params:     maximum number of parameters to parse.
+ *
+ * Return:                the number of parsed items. -1 if there was an
error.
+ */
+static int
+srt_url_parse_query(char *query, const char* delimiter, struct
parsed_param *params, int max_params)
+{
+    int i = 0;
+    char *token = NULL;
+
+    if (!query ||  *query == '\0')
+    {
+        return -1;
+    }
+    if (!params || max_params  == 0)
+    {
+        return 0;
+    }
+
+    token = strtok(query, delimiter);
+    while (token != NULL && i < max_params)
+    {
+        params[i].key = token;
+        params[i].val = NULL;
+        if ((params[i].val = strchr(params[i].key, '=')) != NULL)
+        {
+            size_t val_len = strlen(params[i].val);
+
+            /* make key into a zero-delimited string */
+            *(params[i].val) = '\0';
+
+            /* make sure val is not empty */
+            if (val_len > 1)
+            {
+                params[i].val++;
+
+                /* make sure key is not empty */
+                if (params[i].key[0])
+                    i++;
+            };
+        }
+        token = strtok(NULL, delimiter);
+    }
+    return i;
+}
+
+
 static void srt_wait_interrupted(void *p_data)
 {
     stream_t *p_stream = p_data;
@@ -115,9 +191,10 @@ static int Control(stream_t *p_stream, int i_query,
va_list args)

 static bool srt_schedule_reconnect(stream_t *p_stream)
 {
-    int         i_latency;
+    int         i_latency=-1;
     int         stat;
     char        *psz_passphrase = NULL;
+    char        *url = NULL;

     struct addrinfo hints = {
         .ai_socktype = SOCK_DGRAM,
@@ -153,6 +230,29 @@ static bool srt_schedule_reconnect(stream_t *p_stream)
         goto out;
     }

+    /* Parse URL parameters */
+    if (p_stream->psz_url)
+    {
+        char* query = NULL;
+        struct parsed_param params[32];
+        int num_params = 0;
+        int i=0;
+        url = strdup((const char*)p_stream->psz_url );
+           query = find(url,'?');
+        if (query)
+        {
+            num_params = srt_url_parse_query(query,"&", params,
sizeof(params)/sizeof(struct parsed_param) );
+            if (num_params > 0) {
+                for (i = 0; i < num_params; ++i)
+                {
+                    if (strcmp(params[i].key, SRT_PARAM_LATENCY) == 0 &&
params[i].val)
+                        i_latency = atoi(params[i].val);
+                }
+            }
+        }
+    }
+
+
     /* Make SRT non-blocking */
     srt_setsockopt( p_sys->sock, 0, SRTO_SNDSYN,
         &(bool) { false }, sizeof( bool ) );
@@ -168,11 +268,12 @@ static bool srt_schedule_reconnect(stream_t *p_stream)
         &(int) { 0 }, sizeof( int ) );

     /* Set latency */
-    i_latency = var_InheritInteger( p_stream, "latency" );
+    if (i_latency == -1)
+        i_latency = var_InheritInteger( p_stream, SRT_PARAM_LATENCY );
     srt_setsockopt( p_sys->sock, 0, SRTO_TSBPDDELAY,
-        &i_latency, sizeof( int ) );
+        &i_latency, sizeof( i_latency ) );

-    psz_passphrase = var_InheritString( p_stream, "passphrase" );
+       psz_passphrase = var_InheritString( p_stream, SRT_PARAM_PASSPHRASE
);
     if ( psz_passphrase != NULL && psz_passphrase[0] != '\0')
     {
         int i_key_length = var_InheritInteger( p_stream, "key-length" );
@@ -211,7 +312,7 @@ out:
     }

     freeaddrinfo( res );
-    free( psz_passphrase );
+       free(url);

     return !failed;
 }
@@ -422,8 +523,8 @@ vlc_module_begin ()
             N_("SRT chunk size (bytes)"), NULL, true )
     add_integer( "poll-timeout", SRT_DEFAULT_POLL_TIMEOUT,
             N_("Return poll wait after timeout milliseconds (-1 =
infinite)"), NULL, true )
-    add_integer( "latency", SRT_DEFAULT_LATENCY, N_("SRT latency (ms)"),
NULL, true )
-    add_password("passphrase", "", N_("Password for stream encryption"),
NULL)
+    add_integer( SRT_PARAM_LATENCY, SRT_DEFAULT_LATENCY, N_("SRT latency
(ms)"), NULL, true )
+    add_password(SRT_PARAM_PASSPHRASE, "", N_("Password for stream
encryption"), NULL)
     add_integer( "key-length", SRT_DEFAULT_KEY_LENGTH,
             SRT_KEY_LENGTH_TEXT, SRT_KEY_LENGTH_TEXT, false )
         change_integer_list( srt_key_lengths, srt_key_length_names )
-- 
2.17.1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20190314/ee131a6e/attachment.html>


More information about the vlc-devel mailing list