]> git.sesse.net Git - vlc/blobdiff - src/input/input.c
Introduce realloc_or_free() to src/*, and add assert() to mark unhandled ENOMEM error...
[vlc] / src / input / input.c
index f179a6711ed4bec392191c6e0ea89b98d9e1f48a..3a3cf3c1efdafcdd56a5beb4876730db7d70e220 100644 (file)
 #endif
 
 #include <vlc_common.h>
+#include <vlc_memory.h>
 
 #include <ctype.h>
 #include <limits.h>
 #include <assert.h>
+#include <errno.h>
 
 #include "input_internal.h"
 #include "event.h"
 static void Destructor( input_thread_t * p_input );
 
 static  void *Run            ( vlc_object_t *p_this );
-static  void *RunAndDestroy  ( vlc_object_t *p_this );
 
 static input_thread_t * Create  ( vlc_object_t *, input_item_t *,
                                   const char *, bool, input_resource_t * );
 static  int             Init    ( input_thread_t *p_input );
 static void             End     ( input_thread_t *p_input );
-static void             MainLoop( input_thread_t *p_input );
+static void             MainLoop( input_thread_t *p_input, bool b_interactive );
 
 static void ObjectKillChildrens( input_thread_t *, vlc_object_t * );
 
-static inline int ControlPop( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline );
-static void       ControlReduce( input_thread_t * );
+static inline int ControlPop( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline, bool b_postpone_seek );
 static void       ControlRelease( int i_type, vlc_value_t val );
+static bool       ControlIsSeekRequest( int i_type );
 static bool       Control( input_thread_t *, int, vlc_value_t );
 
 static int  UpdateTitleSeekpointFromAccess( input_thread_t * );
@@ -155,39 +156,25 @@ input_thread_t *__input_CreateAndStart( vlc_object_t *p_parent,
 }
 
 /**
- * Initialize an input thread and run it. This thread will clean after itself,
- * you can forget about it. It can work either in blocking or non-blocking mode
+ * Initialize an input thread and run it until it stops by itself.
  *
  * \param p_parent a vlc_object
  * \param p_item an input item
- * \param b_block should we block until read is finished ?
  * \return an error code, VLC_SUCCESS on success
  */
-int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
-                   bool b_block )
+int __input_Read( vlc_object_t *p_parent, input_item_t *p_item )
 {
-    input_thread_t *p_input;
-
-    p_input = Create( p_parent, p_item, NULL, false, NULL );
+    input_thread_t *p_input = Create( p_parent, p_item, NULL, false, NULL );
     if( !p_input )
         return VLC_EGENERIC;
 
-    if( b_block )
-    {
-        RunAndDestroy( VLC_OBJECT(p_input) );
-        return VLC_SUCCESS;
-    }
-    else
+    if( !Init( p_input ) )
     {
-        if( vlc_thread_create( p_input, "input", RunAndDestroy,
-                               VLC_THREAD_PRIORITY_INPUT ) )
-        {
-            input_ChangeState( p_input, ERROR_S );
-            msg_Err( p_input, "cannot create input thread" );
-            vlc_object_release( p_input );
-            return VLC_EGENERIC;
-        }
+        MainLoop( p_input, false );
+        End( p_input );
     }
+
+    vlc_object_release( p_input );
     return VLC_SUCCESS;
 }
 
@@ -360,7 +347,11 @@ static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
     p_input->p->title = NULL;
     p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
     p_input->p->i_state = INIT_S;
-    p_input->p->i_rate = INPUT_RATE_DEFAULT;
+    p_input->p->i_rate = INPUT_RATE_DEFAULT
+                         / var_CreateGetFloat( p_input, "rate" );
+    /* Currently, the input rate variable is an integer. So we need to destroy
+     * the float variable inherited from the configuration. */
+    var_Destroy( p_input, "rate" );
     p_input->p->b_recording = false;
     memset( &p_input->p->bookmark, 0, sizeof(p_input->p->bookmark) );
     TAB_INIT( p_input->p->i_bookmark, p_input->p->pp_bookmark );
@@ -540,7 +531,7 @@ static void *Run( vlc_object_t *p_this )
     if( Init( p_input ) )
         goto exit;
 
-    MainLoop( p_input );
+    MainLoop( p_input, true ); /* FIXME it can be wrong (like with VLM) */
 
     /* Clean up */
     End( p_input );
@@ -559,31 +550,6 @@ exit:
     return NULL;
 }
 
-/*****************************************************************************
- * RunAndDestroy: main thread loop
- * This is the "just forget me" thread that spawns the input processing chain,
- * reads the stream, cleans up and releases memory
- *****************************************************************************/
-static void *RunAndDestroy( vlc_object_t *p_this )
-{
-    input_thread_t *p_input = (input_thread_t *)p_this;
-    const int canc = vlc_savecancel();
-
-    if( Init( p_input ) )
-        goto exit;
-
-    MainLoop( p_input );
-
-    /* Clean up */
-    End( p_input );
-
-exit:
-    /* Release memory */
-    vlc_object_release( p_input );
-    vlc_restorecancel( canc );
-    return NULL;
-}
-
 /*****************************************************************************
  * Main loop: Fill buffers from access, and demux
  *****************************************************************************/
@@ -741,11 +707,14 @@ static void MainLoopStatistic( input_thread_t *p_input )
  * MainLoop
  * The main input loop.
  */
-static void MainLoop( input_thread_t *p_input )
+static void MainLoop( input_thread_t *p_input, bool b_interactive )
 {
     mtime_t i_start_mdate = mdate();
     mtime_t i_intf_update = 0;
     mtime_t i_statistic_update = 0;
+    mtime_t i_last_seek_mdate = 0;
+    bool b_pause_after_eof = b_interactive &&
+                             var_CreateGetBool( p_input, "play-and-pause" );
 
     /* Start the timer */
     stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
@@ -753,10 +722,8 @@ static void MainLoop( input_thread_t *p_input )
     while( vlc_object_alive( p_input ) && !p_input->b_error )
     {
         bool b_force_update;
-        int i_type;
         vlc_value_t val;
         mtime_t i_current;
-        mtime_t i_deadline;
         mtime_t i_wakeup;
         bool b_paused;
         bool b_demux_polled;
@@ -768,7 +735,7 @@ static void MainLoop( input_thread_t *p_input )
          * is paused -> this may cause problem with some of them
          * The same problem can be seen when seeking while paused */
         b_paused = p_input->p->i_state == PAUSE_S &&
-                   !es_out_GetBuffering( p_input->p->p_es_out );
+                   ( !es_out_GetBuffering( p_input->p->p_es_out ) || p_input->p->input.b_eof );
 
         b_demux_polled = true;
         if( !b_paused )
@@ -779,33 +746,68 @@ static void MainLoop( input_thread_t *p_input )
 
                 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
             }
-            else if( !p_input->b_eof && !es_out_GetEmpty( p_input->p->p_es_out ) )
+            else if( !es_out_GetEmpty( p_input->p->p_es_out ) )
             {
                 msg_Dbg( p_input, "waiting decoder fifos to empty" );
                 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
             }
+            else if( b_pause_after_eof )
+            {
+                msg_Dbg( p_input, "pausing at EOF (pause after each)");
+                val.i_int = PAUSE_S;
+                Control( p_input, INPUT_CONTROL_SET_STATE, val );
+
+                b_pause_after_eof = false;
+                b_paused = true;
+            }
             else
             {
                 if( MainLoopTryRepeat( p_input, &i_start_mdate ) )
                     break;
+                b_pause_after_eof = var_GetBool( p_input, "play-and-pause" );
             }
         }
 
         /* */
         do {
-            i_deadline = i_wakeup;
+            mtime_t i_deadline = i_wakeup;
             if( b_paused || !b_demux_polled )
                 i_deadline = __MIN( i_intf_update, i_statistic_update );
 
             /* Handle control */
-            ControlReduce( p_input );
-            while( !ControlPop( p_input, &i_type, &val, i_deadline ) )
+            for( ;; )
             {
+                mtime_t i_limit = i_deadline;
+
+                /* We will postpone the execution of a seek until we have
+                 * finished the ES bufferisation (postpone is limited to
+                 * 125ms) */
+                bool b_buffering = es_out_GetBuffering( p_input->p->p_es_out ) &&
+                                   !p_input->p->input.b_eof;
+                if( b_buffering )
+                {
+                    /* When postpone is in order, check the ES level every 20ms */
+                    mtime_t i_current = mdate();
+                    if( i_last_seek_mdate + INT64_C(125000) >= i_current )
+                        i_limit = __MIN( i_deadline, i_current + INT64_C(20000) );
+                }
+
+                int i_type;
+                if( ControlPop( p_input, &i_type, &val, i_limit, b_buffering ) )
+                {
+                    if( b_buffering && i_limit < i_deadline )
+                        continue;
+                    break;
+                }
 
                 msg_Dbg( p_input, "control type=%d", i_type );
 
                 if( Control( p_input, i_type, val ) )
+                {
+                    if( ControlIsSeekRequest( i_type ) )
+                        i_last_seek_mdate = mdate();
                     b_force_update = true;
+                }
             }
 
             /* Update interface and statistics */
@@ -1171,9 +1173,6 @@ static int Init( input_thread_t * p_input )
     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
 
-    var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
-    var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
-
     /* */
     input_ChangeState( p_input, OPENING_S );
     input_SendEventCache( p_input, 0.0 );
@@ -1427,14 +1426,50 @@ void input_ControlPush( input_thread_t *p_input,
     vlc_mutex_unlock( &p_input->p->lock_control );
 }
 
+static int ControlGetReducedIndexLocked( input_thread_t *p_input )
+{
+    const int i_lt = p_input->p->control[0].i_type;
+    int i;
+    for( i = 1; i < p_input->p->i_control; i++ )
+    {
+        const int i_ct = p_input->p->control[i].i_type;
+
+        if( i_lt == i_ct &&
+            ( i_ct == INPUT_CONTROL_SET_STATE ||
+              i_ct == INPUT_CONTROL_SET_RATE ||
+              i_ct == INPUT_CONTROL_SET_POSITION ||
+              i_ct == INPUT_CONTROL_SET_TIME ||
+              i_ct == INPUT_CONTROL_SET_PROGRAM ||
+              i_ct == INPUT_CONTROL_SET_TITLE ||
+              i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
+              i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
+        {
+            continue;
+        }
+        else
+        {
+            /* TODO but that's not that important
+                - merge SET_X with SET_X_CMD
+                - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
+                - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
+                - ?
+                */
+            break;
+        }
+    }
+    return i - 1;
+}
+
+
 static inline int ControlPop( input_thread_t *p_input,
                               int *pi_type, vlc_value_t *p_val,
-                              mtime_t i_deadline )
+                              mtime_t i_deadline, bool b_postpone_seek )
 {
     input_thread_private_t *p_sys = p_input->p;
 
     vlc_mutex_lock( &p_sys->lock_control );
-    while( p_sys->i_control <= 0 )
+    while( p_sys->i_control <= 0 ||
+           ( b_postpone_seek && ControlIsSeekRequest( p_sys->control[0].i_type ) ) )
     {
         if( !vlc_object_alive( p_input ) || i_deadline < 0 )
         {
@@ -1451,59 +1486,37 @@ static inline int ControlPop( input_thread_t *p_input,
     }
 
     /* */
-    *pi_type = p_sys->control[0].i_type;
-    *p_val   = p_sys->control[0].val;
+    const int i_index = ControlGetReducedIndexLocked( p_input );
 
-    p_sys->i_control--;
+    /* */
+    *pi_type = p_sys->control[i_index].i_type;
+    *p_val   = p_sys->control[i_index].val;
+
+    p_sys->i_control -= i_index + 1;
     if( p_sys->i_control > 0 )
-        memmove( &p_sys->control[0], &p_sys->control[1],
+        memmove( &p_sys->control[0], &p_sys->control[i_index+1],
                  sizeof(*p_sys->control) * p_sys->i_control );
     vlc_mutex_unlock( &p_sys->lock_control );
 
     return VLC_SUCCESS;
 }
-
-static void ControlReduce( input_thread_t *p_input )
+static bool ControlIsSeekRequest( int i_type )
 {
-    vlc_mutex_lock( &p_input->p->lock_control );
-
-    for( int i = 1; i < p_input->p->i_control; i++ )
+    switch( i_type )
     {
-        const int i_lt = p_input->p->control[i-1].i_type;
-        const int i_ct = p_input->p->control[i].i_type;
-
-        /* XXX We can't merge INPUT_CONTROL_SET_ES */
-/*        msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
-                 i_lt, i_ct );
-*/
-        if( i_lt == i_ct &&
-            ( i_ct == INPUT_CONTROL_SET_STATE ||
-              i_ct == INPUT_CONTROL_SET_RATE ||
-              i_ct == INPUT_CONTROL_SET_POSITION ||
-              i_ct == INPUT_CONTROL_SET_TIME ||
-              i_ct == INPUT_CONTROL_SET_PROGRAM ||
-              i_ct == INPUT_CONTROL_SET_TITLE ||
-              i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
-              i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
-        {
-            int j;
-//            msg_Dbg( p_input, "merged at %d", i );
-            /* Remove the i-1 */
-            for( j = i; j <  p_input->p->i_control; j++ )
-                p_input->p->control[j-1] = p_input->p->control[j];
-            p_input->p->i_control--;
-        }
-        else
-        {
-            /* TODO but that's not that important
-                - merge SET_X with SET_X_CMD
-                - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
-                - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
-                - ?
-                */
-        }
+    case INPUT_CONTROL_SET_POSITION:
+    case INPUT_CONTROL_SET_TIME:
+    case INPUT_CONTROL_SET_TITLE:
+    case INPUT_CONTROL_SET_TITLE_NEXT:
+    case INPUT_CONTROL_SET_TITLE_PREV:
+    case INPUT_CONTROL_SET_SEEKPOINT:
+    case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
+    case INPUT_CONTROL_SET_SEEKPOINT_PREV:
+    case INPUT_CONTROL_SET_BOOKMARK:
+        return true;
+    default:
+        return false;
     }
-    vlc_mutex_unlock( &p_input->p->lock_control );
 }
 
 static void ControlRelease( int i_type, vlc_value_t val )
@@ -1604,7 +1617,6 @@ static bool Control( input_thread_t *p_input,
             break;
 
         case INPUT_CONTROL_SET_POSITION:
-        case INPUT_CONTROL_SET_POSITION_OFFSET:
         {
             double f_pos;
 
@@ -1640,7 +1652,6 @@ static bool Control( input_thread_t *p_input,
         }
 
         case INPUT_CONTROL_SET_TIME:
-        case INPUT_CONTROL_SET_TIME_OFFSET:
         {
             int64_t i_time;
             int i_ret;
@@ -1669,9 +1680,8 @@ static bool Control( input_thread_t *p_input,
                 int64_t i_length;
 
                 /* Emulate it with a SET_POS */
-                demux_Control( p_input->p->input.p_demux,
-                                DEMUX_GET_LENGTH, &i_length );
-                if( i_length > 0 )
+                if( !demux_Control( p_input->p->input.p_demux,
+                                    DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
                 {
                     double f_pos = (double)i_time / (double)i_length;
                     i_ret = demux_Control( p_input->p->input.p_demux,
@@ -2429,8 +2439,9 @@ static int InputSourceInit( input_thread_t *p_input,
     if( in->p_demux )
     {
         /* Get infos from access_demux */
-        demux_Control( in->p_demux,
-                        DEMUX_GET_PTS_DELAY, &in->i_pts_delay );
+        int i_ret = demux_Control( in->p_demux,
+                                   DEMUX_GET_PTS_DELAY, &in->i_pts_delay );
+        assert( !i_ret );
         in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
 
 
@@ -2915,8 +2926,9 @@ static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_atta
     input_attachment_t **attachment = *ppp_attachment;
     int i;
 
-    attachment = realloc( attachment,
-                          sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
+    attachment = realloc_or_free( attachment,
+                    sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
+    assert( attachment );
     for( i = 0; i < i_new; i++ )
         attachment[i_attachment++] = pp_new[i];
     free( pp_new );