]> git.sesse.net Git - vlc/blobdiff - modules/access_output/livehttp.c
livehttp: try to follow draft 11 section 6.2.2
[vlc] / modules / access_output / livehttp.c
index 0c85457765b1b8c371b7b95cd603b300491d7ce5..9149a946abd662002bb6c6b2ad18114143854e41 100644 (file)
@@ -236,6 +236,7 @@ static int Open( vlc_object_t *p_this )
     p_sys->segments_t = vlc_array_new();
 
     p_sys->stuffing_size = 0;
+    p_sys->i_opendts = VLC_TS_INVALID;
 
     p_sys->psz_indexPath = NULL;
     psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
@@ -489,6 +490,26 @@ static void destroySegment( output_segment_t *segment )
     free( segment );
 }
 
+/************************************************************************
+ * segmentAmountNeeded: check that playlist has atleast 3*p_sys->i_seglength of segments
+ * return how many segments are needed for that (max of p_sys->i_segment )
+ ************************************************************************/
+static uint32_t segmentAmountNeeded( sout_access_out_sys_t *p_sys )
+{
+    float duration = .0f;
+    for( unsigned index = 1; index <= vlc_array_count( p_sys->segments_t ) ; index++ )
+    {
+        output_segment_t* segment = vlc_array_item_at_index( p_sys->segments_t, vlc_array_count( p_sys->segments_t ) - index );
+        duration += segment->f_seglength;
+
+        if( duration >= (float)( 3 * p_sys->i_seglen ) )
+            return __MAX(index, p_sys->i_numsegs);
+    }
+    return vlc_array_count( p_sys->segments_t )-1;
+
+}
+
+
 /************************************************************************
  * isFirstItemRemovable: Check for draft 11 section 6.2.2 
  * check that the first item has been around outside playlist 
@@ -524,8 +545,9 @@ static int updateIndexAndDel( sout_access_out_t *p_access, sout_access_out_sys_t
         i_firstseg = 1;
     else
     {
-        i_firstseg = ( p_sys->i_segment - p_sys->i_numsegs ) + 1;
-        i_index_offset = vlc_array_count( p_sys->segments_t ) - p_sys->i_numsegs;
+        unsigned numsegs = segmentAmountNeeded( p_sys );
+        i_firstseg = ( p_sys->i_segment - numsegs ) + 1;
+        i_index_offset = vlc_array_count( p_sys->segments_t ) - numsegs;
     }
 
     // First update index
@@ -897,6 +919,10 @@ static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
             if ( p_sys->i_handle < 0 )
             {
                 p_sys->i_opendts = output ? output->i_dts : p_buffer->i_dts;
+                //For first segment we can get negative duration otherwise...?
+                if( ( p_sys->i_opendts != VLC_TS_INVALID ) &&
+                    ( p_buffer->i_dts < p_sys->i_opendts ) )
+                    p_sys->i_opendts = p_buffer->i_dts;
                 if ( openNextFile( p_access, p_sys ) < 0 )
                    return -1;
             }