]> git.sesse.net Git - vlc/blobdiff - modules/access_output/livehttp.c
livehttp: take block length in account when checking segment length
[vlc] / modules / access_output / livehttp.c
index 805a6c58121c278812f3c41dc3a755d13eadb8d5..25db4a6c3f8c37178f72eb0d220c116a9e9d3b5f 100644 (file)
@@ -1,25 +1,24 @@
 /*****************************************************************************
  * livehttp.c: Live HTTP Streaming
  *****************************************************************************
- * Copyright (C) 2001, 2002 the VideoLAN team
- * Copyright (C) 2009-2010 by Keary Griffin
- * $Id$
+ * Copyright © 2001, 2002 VLC authors and VideoLAN
+ * Copyright © 2009-2010 by Keary Griffin
  *
  * Authors: Keary Griffin <kearygriffin at gmail.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -34,6 +33,9 @@
 #include <time.h>
 #include <fcntl.h>
 #include <errno.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
@@ -84,20 +86,20 @@ vlc_module_begin ()
     set_description( N_("HTTP Live streaming output") )
     set_shortname( N_("LiveHTTP" ))
     add_shortcut( "livehttp" )
-    set_capability( "sout access", 50 )
+    set_capability( "sout access", 0 )
     set_category( CAT_SOUT )
     set_subcategory( SUBCAT_SOUT_ACO )
-    add_integer( SOUT_CFG_PREFIX "seglen", 10, NULL, SEGLEN_TEXT, SEGLEN_LONGTEXT, true )
-    add_integer( SOUT_CFG_PREFIX "numsegs", 0, NULL, NUMSEGS_TEXT, NUMSEGS_LONGTEXT, true )
-    add_bool( SOUT_CFG_PREFIX "splitanywhere", false, NULL,
+    add_integer( SOUT_CFG_PREFIX "seglen", 10, SEGLEN_TEXT, SEGLEN_LONGTEXT, true )
+    add_integer( SOUT_CFG_PREFIX "numsegs", 0, NUMSEGS_TEXT, NUMSEGS_LONGTEXT, true )
+    add_bool( SOUT_CFG_PREFIX "splitanywhere", false,
               SPLITANYWHERE_TEXT, SPLITANYWHERE_LONGTEXT, true )
-    add_bool( SOUT_CFG_PREFIX "delsegs", true, NULL,
+    add_bool( SOUT_CFG_PREFIX "delsegs", true,
               DELSEGS_TEXT, DELSEGS_LONGTEXT, true )
-    add_bool( SOUT_CFG_PREFIX "ratecontrol", false, NULL,
+    add_bool( SOUT_CFG_PREFIX "ratecontrol", false,
               RATECONTROL_TEXT, RATECONTROL_TEXT, true )
-    add_string( SOUT_CFG_PREFIX "index", NULL, NULL,
+    add_string( SOUT_CFG_PREFIX "index", NULL,
                 INDEX_TEXT, INDEX_LONGTEXT, true )
-    add_string( SOUT_CFG_PREFIX "index-url", NULL, NULL,
+    add_string( SOUT_CFG_PREFIX "index-url", NULL,
                 INDEXURL_TEXT, INDEXURL_LONGTEXT, true )
     set_callbacks( Open, Close )
 vlc_module_end ()
@@ -130,6 +132,7 @@ struct sout_access_out_sys_t
     mtime_t  i_seglenm;
     uint32_t i_segment;
     size_t  i_seglen;
+    block_t *block_buffer;
     int i_handle;
     unsigned i_numsegs;
     bool b_delsegs;
@@ -158,7 +161,9 @@ static int Open( vlc_object_t *p_this )
         return VLC_ENOMEM;
 
     p_sys->i_seglen = var_GetInteger( p_access, SOUT_CFG_PREFIX "seglen" );
-    p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen;
+    /* Try to get within +-10% of asked segment length, so limit is +10% of segment length*/
+    p_sys->i_seglenm = CLOCK_FREQ * p_sys->i_seglen * 1.10;
+    p_sys->block_buffer = NULL;
 
     p_sys->i_numsegs = var_GetInteger( p_access, SOUT_CFG_PREFIX "numsegs" );
     p_sys->b_splitanywhere = var_GetBool( p_access, SOUT_CFG_PREFIX "splitanywhere" );
@@ -170,7 +175,7 @@ static int Open( vlc_object_t *p_this )
     if ( psz_idx )
     {
         char *psz_tmp;
-        psz_tmp = str_format( p_access, psz_idx );
+        psz_tmp = str_format_time( psz_idx );
         free( psz_idx );
         if ( !psz_tmp )
         {
@@ -200,12 +205,12 @@ static int Open( vlc_object_t *p_this )
 /*****************************************************************************
  * formatSegmentPath: create segment path name based on seg #
  *****************************************************************************/
-static char *formatSegmentPath( sout_access_out_t *p_access, char *psz_path, uint32_t i_seg, bool b_sanitize )
+static char *formatSegmentPath( char *psz_path, uint32_t i_seg, bool b_sanitize )
 {
     char *psz_result;
     char *psz_firstNumSign;
 
-    if ( ! ( psz_result  = str_format( p_access, psz_path ) ) )
+    if ( ! ( psz_result  = str_format_time( psz_path ) ) )
         return NULL;
 
     psz_firstNumSign = psz_result + strcspn( psz_result, SEG_NUMBER_PLACEHOLDER );
@@ -269,13 +274,13 @@ static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t
         for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
         {
             char *psz_name;
-            if ( ! ( psz_name = formatSegmentPath( p_access, psz_idxFormat, i, false ) ) )
+            if ( ! ( psz_name = formatSegmentPath( psz_idxFormat, i, false ) ) )
             {
                 free( psz_idxTmp );
                 fclose( fp );
                 return -1;
             }
-            val = fprintf( fp, "#EXTINF:%zu\n%s\n", p_sys->i_seglen, psz_name );
+            val = fprintf( fp, "#EXTINF:%zu,\n%s\n", p_sys->i_seglen, psz_name );
             free( psz_name );
             if ( val < 0 )
             {
@@ -313,7 +318,7 @@ static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t
     // Then take care of deletion
     if ( p_sys->b_delsegs && i_firstseg > 1 )
     {
-        char *psz_name = formatSegmentPath( p_access, p_access->psz_path, i_firstseg-1, true );
+        char *psz_name = formatSegmentPath( p_access->psz_path, i_firstseg-1, true );
          if ( psz_name )
          {
              vlc_unlink( psz_name );
@@ -350,6 +355,30 @@ static void Close( vlc_object_t * p_this )
     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
     sout_access_out_sys_t *p_sys = p_access->p_sys;
 
+    msg_Dbg( p_access, "Flushing buffer to last file");
+    while( p_sys->block_buffer )
+    {
+        ssize_t val = write( p_sys->i_handle, p_sys->block_buffer->p_buffer, p_sys->block_buffer->i_buffer );
+        if ( val == -1 )
+        {
+           if ( errno == EINTR )
+              continue;
+           block_ChainRelease ( p_sys->block_buffer);
+           break;
+        }
+
+        if ( (size_t)val >= p_sys->block_buffer->i_buffer )
+        {
+           block_t *p_next = p_sys->block_buffer->p_next;
+           block_Release (p_sys->block_buffer);
+           p_sys->block_buffer = p_next;
+        }
+        else
+        {
+           p_sys->block_buffer->p_buffer += val;
+           p_sys->block_buffer->i_buffer -= val;
+        }
+    }
 
     closeCurrentSegment( p_access, p_sys, true );
     free( p_sys->psz_indexUrl );
@@ -388,7 +417,7 @@ static ssize_t openNextFile( sout_access_out_t *p_access, sout_access_out_sys_t
 
     uint32_t i_newseg = p_sys->i_segment + 1;
 
-    char *psz_seg = formatSegmentPath( p_access, p_access->psz_path, i_newseg, true );
+    char *psz_seg = formatSegmentPath( p_access->psz_path, i_newseg, true );
     if ( !psz_seg )
         return -1;
 
@@ -417,42 +446,61 @@ static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
 {
     size_t i_write = 0;
     sout_access_out_sys_t *p_sys = p_access->p_sys;
+    block_t *p_temp;
 
     while( p_buffer )
     {
-        if ( p_sys->i_handle >= 0 && ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) ) && ( p_buffer->i_dts-p_sys->i_opendts ) > p_sys->i_seglenm )
-        {
-            closeCurrentSegment( p_access, p_sys, false );
-        }
-        if ( p_buffer->i_buffer > 0 && p_sys->i_handle < 0 )
+        if ( ( p_sys->b_splitanywhere || ( p_buffer->i_flags & BLOCK_FLAG_HEADER ) ) )
         {
-            p_sys->i_opendts = p_buffer->i_dts;
-            if ( openNextFile( p_access, p_sys ) < 0 )
-                return -1;
-        }
-        ssize_t val = write ( p_sys->i_handle,
-                             p_buffer->p_buffer, p_buffer->i_buffer );
-        if ( val == -1 )
-        {
-            if ( errno == EINTR )
-                continue;
-            block_ChainRelease ( p_buffer );
-            return -1;
-        }
+            block_t *output = p_sys->block_buffer;
+            p_sys->block_buffer = NULL;
 
-        if ( (size_t)val >= p_buffer->i_buffer )
-        {
-            block_t *p_next = p_buffer->p_next;
-            block_Release (p_buffer);
-            p_buffer = p_next;
-        }
-        else
-        {
-            p_buffer->p_buffer += val;
-            p_buffer->i_buffer -= val;
+
+            if( p_sys->i_handle > 0 &&
+                ( p_buffer->i_dts - p_sys->i_opendts +
+                  p_buffer->i_length * CLOCK_FREQ / INT64_C(1000000)
+                ) >= p_sys->i_seglenm )
+                closeCurrentSegment( p_access, p_sys, false );
+
+            if ( p_sys->i_handle < 0 )
+            {
+                p_sys->i_opendts = output ? output->i_dts : p_buffer->i_dts;
+                if ( openNextFile( p_access, p_sys ) < 0 )
+                   return -1;
+            }
+
+            while( output )
+            {
+                ssize_t val = write( p_sys->i_handle, output->p_buffer, output->i_buffer );
+                if ( val == -1 )
+                {
+                   if ( errno == EINTR )
+                      continue;
+                   block_ChainRelease ( p_buffer );
+                   return -1;
+                }
+
+                if ( (size_t)val >= output->i_buffer )
+                {
+                   block_t *p_next = output->p_next;
+                   block_Release (output);
+                   output = p_next;
+                }
+                else
+                {
+                   output->p_buffer += val;
+                   output->i_buffer -= val;
+                }
+                i_write += val;
+            }
         }
-        i_write += val;
+
+        p_temp = p_buffer->p_next;
+        p_buffer->p_next = NULL;
+        block_ChainAppend( &p_sys->block_buffer, p_buffer );
+        p_buffer = p_temp;
     }
+
     return i_write;
 }