[vlc-devel] [PATCH] Naming patterns for HLS segments

Łukasz Korbel korbel85 at gmail.com
Thu Apr 12 20:39:06 CEST 2012


I have added naming pattern for segments in HLS broadcast.
Beside standard "index" pattern i've added "timestamp" which set
segment name to number of seconds since Epoch.

What is the advantage of new pattern?
When vlc crushes during streaming out it will start to name segments
again from 00...01 overwriting any existing segments. This will not
happen if segments are timestamps (and therefore are unique), what
makes broadcasting system more reliable.
Moreover such approach may be useful for someone who tend to use
segments later (to make e.g. some kind of history)


Patch body:

>From fcd05bc2cece380c182d63d015b68ad76b85fcf8 Mon Sep 17 00:00:00 2001
From: Lukasz Korbel <korbel85 at gmail.com>
Date: Thu, 12 Apr 2012 19:46:29 +0200
Subject: [PATCH] Additional naming convention for HLS segments

Segments names can be either indexes or timestamps.
---
 modules/access_output/livehttp.c |   46 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/modules/access_output/livehttp.c b/modules/access_output/livehttp.c
index d335a64..c0a26b8 100644
--- a/modules/access_output/livehttp.c
+++ b/modules/access_output/livehttp.c
@@ -82,6 +82,20 @@ static void Close( vlc_object_t * );

 #define RATECONTROL_TEXT N_("Use muxers rate control mechanism")

+#define PATTERN_TEXT N_("Pattern for segment names")
+#define PATTERN_LONGTEXT N_("There are following patterns for naming
segments:"\
+                            " index - name is 0-padded index of segment in "\
+                            "broadcast session\n"\
+                            "timestamp - name is time of broadcasting of "\
+                            "segment (number of seconds since Epoch) ")
+
+static const char *const ppsz_segpat[] = {
+    "index", "timestamp"
+};
+static const char *const ppsz_segpat_text[] = {
+    N_("index"), N_("timestamp")
+};
+
 vlc_module_begin ()
     set_description( N_("HTTP Live streaming output") )
     set_shortname( N_("LiveHTTP" ))
@@ -101,6 +115,9 @@ vlc_module_begin ()
                 INDEX_TEXT, INDEX_LONGTEXT, true )
     add_string( SOUT_CFG_PREFIX "index-url", NULL,
                 INDEXURL_TEXT, INDEXURL_LONGTEXT, true )
+    add_string( SOUT_CFG_PREFIX "name-pattern", "index",
+                PATTERN_TEXT, PATTERN_LONGTEXT, true )
+                change_string_list( ppsz_segpat, ppsz_segpat_text, 0)
     set_callbacks( Open, Close )
 vlc_module_end ()

@@ -116,6 +133,7 @@ static const char *const ppsz_sout_options[] = {
     "index",
     "index-url",
     "ratecontrol",
+    "name-pattern",
     NULL
 };

@@ -128,8 +146,10 @@ struct sout_access_out_sys_t
     char *psz_cursegPath;
     char *psz_indexPath;
     char *psz_indexUrl;
+    char *psz_namePattern;
     mtime_t i_opendts;
     mtime_t  i_seglenm;
+    time_t i_broadcastStart;
     uint32_t i_segment;
     size_t  i_seglen;
     int i_handle;
@@ -185,11 +205,16 @@ static int Open( vlc_object_t *p_this )
     }

     p_sys->psz_indexUrl = var_GetNonEmptyString( p_access,
SOUT_CFG_PREFIX "index-url" );
-
+    p_sys->psz_namePattern = var_GetNonEmptyString( p_access,
SOUT_CFG_PREFIX "name-pattern" );
     p_access->p_sys = p_sys;
     p_sys->i_handle = -1;
     p_sys->i_segment = 0;
     p_sys->psz_cursegPath = NULL;
+    /* arbitrary time of broadcast start in seconds */
+    p_sys->i_broadcastStart = time( NULL);
+    /* round down start time to multiple of segment length*/
+    if (p_sys->i_seglen > 1)
+        p_sys->i_broadcastStart = p_sys->i_broadcastStart -
p_sys->i_broadcastStart % p_sys->i_seglen;

     p_access->pf_write = Write;
     p_access->pf_seek  = Seek;
@@ -217,7 +242,24 @@ static char *formatSegmentPath( sout_access_out_t
*p_access, char *psz_path, uin
         int ret;

         *psz_firstNumSign = '\0';
-        ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result,
i_cnt, i_seg, psz_firstNumSign + i_cnt );
+
+        /* Select naming pattern*/
+        sout_access_out_sys_t *p_sys = p_access->p_sys;
+        if (strcmp( p_sys->psz_namePattern, "index") ==  0)
+            ret = asprintf( &psz_newResult, "%s%0*d%s", psz_result,
i_cnt, i_seg, psz_firstNumSign + i_cnt );
+        else if (strcmp( p_sys->psz_namePattern, "timestamp") ==  0)
+        {
+            int timestamp = p_sys->i_broadcastStart + i_seg * p_sys->i_seglen;
+            /* Write timestamp, ignore number of placeholders */
+            ret = asprintf( &psz_newResult, "%s%d%s", psz_result,
timestamp, psz_firstNumSign + i_cnt );
+        }
+        else
+        {
+            msg_Err( p_access, "Unrecognized value for %sname-pattern
(%s). Allowed values: %s",
+                               SOUT_CFG_PREFIX,
p_sys->psz_namePattern, ppsz_segpat);
+            return NULL;
+        }
+
         free ( psz_result );
         if ( ret < 0 )
             return NULL;
-- 
1.7.5.4

Regards,
Łukasz Korbel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Additional-naming-convention-for-HLS-segments.patch
Type: application/octet-stream
Size: 4275 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20120412/2db29bd6/attachment.obj>


More information about the vlc-devel mailing list