]> git.sesse.net Git - vlc/blobdiff - src/input/es_out_timeshift.c
Fixed typo in input.c
[vlc] / src / input / es_out_timeshift.c
index 9d0bf8388875ef26b20433acfb1ffe328a6f43e6..de9ab43d5a03d76fef76404e00182bdc4fbe7cd9 100644 (file)
  * Local prototypes
  *****************************************************************************/
 
+/* XXX attribute_packed is (and MUST be) used ONLY to reduce memory usage */
+#ifdef HAVE_ATTRIBUTE_PACKED
+#   define attribute_packed __attribute__((__packed__))
+#else
+#   define attribute_packed
+#endif
+
 enum
 {
     C_ADD,
     C_SEND,
     C_DEL,
     C_CONTROL,
-
-    C_MAX
 };
 
-typedef struct
+typedef struct attribute_packed
 {
     es_out_id_t *p_es;
     es_format_t *p_fmt;
 } ts_cmd_add_t;
 
-typedef struct
+typedef struct attribute_packed
 {
     es_out_id_t *p_es;
 } ts_cmd_del_t;
 
-typedef struct
+typedef struct attribute_packed
 {
     es_out_id_t *p_es;
     block_t *p_block;
-    off_t i_offset;
+    int     i_offset;  /* We do not use file > INT_MAX */
 } ts_cmd_send_t;
 
-typedef struct
+typedef struct attribute_packed
 {
     int  i_query;
 
@@ -116,12 +121,24 @@ typedef struct
             es_out_id_t *p_es;
             es_format_t *p_fmt;
         } es_fmt;
+        struct
+        {
+            /* FIXME Really too big (double make the whole thing too big) */
+            double  f_position;
+            mtime_t i_time;
+            mtime_t i_length;
+        } times;
+        struct
+        {
+            mtime_t i_pts_delay;
+            int     i_cr_average;
+        } jitter;
     };
 } ts_cmd_control_t;
 
-typedef struct
+typedef struct attribute_packed
 {
-    int     i_type;
+    int8_t  i_type;
     mtime_t i_date;
     union
     {
@@ -139,7 +156,7 @@ struct ts_storage_t
 
     /* */
     char    *psz_file;  /* Filename */
-    int64_t i_file_max; /* Max size in bytes */
+    size_t  i_file_max; /* Max size in bytes */
     int64_t i_file_size;/* Current size in bytes */
     FILE    *p_filew;   /* FILE handle for data writing */
     FILE    *p_filer;   /* FILE handle for data reading */
@@ -229,7 +246,7 @@ static void         TsAutoStop( es_out_t * );
 
 static void         TsStop( ts_thread_t * );
 static void         TsPushCmd( ts_thread_t *, ts_cmd_t * );
-static int          TsPopCmdLocked( ts_thread_t *, ts_cmd_t * );
+static int          TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
 static bool         TsHasCmd( ts_thread_t * );
 static bool         TsIsUnused( ts_thread_t * );
 static int          TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
@@ -243,7 +260,7 @@ static void         TsStoragePack( ts_storage_t *p_storage );
 static bool         TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
 static bool         TsStorageIsEmpty( ts_storage_t * );
 static void         TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
-static void         TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd );
+static void         TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
 
 static void CmdClean( ts_cmd_t * );
 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
@@ -308,12 +325,28 @@ es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out
 
     TAB_INIT( p_sys->i_es, p_sys->pp_es );
 
-    /* TODO config
-     * timeshift-granularity
-     * timeshift-path
-     */
-    p_sys->i_tmp_size_max = 50 * 1024*1024;
-    p_sys->psz_tmp_path = GetTmpPath( NULL );
+    /* */
+    const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
+    if( i_tmp_size_max < 0 )
+        p_sys->i_tmp_size_max = 50*1024*1024;
+    else
+        p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
+    msg_Dbg( p_input, "using timeshift granularity of %d bytes",
+             (int)p_sys->i_tmp_size_max );
+
+    char *psz_tmp_path = var_CreateGetNonEmptyString( p_input, "input-timeshift-path" );
+    p_sys->psz_tmp_path = GetTmpPath( psz_tmp_path );
+    msg_Dbg( p_input, "using timeshift  path '%s'", p_sys->psz_tmp_path );
+
+#if 0
+#define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
+    S(ts_cmd_t);
+    S(ts_cmd_control_t);
+    S(ts_cmd_send_t);
+    S(ts_cmd_del_t);
+    S(ts_cmd_add_t);
+#undef S
+#endif
 
     return p_out;
 }
@@ -429,7 +462,7 @@ static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
 
     if( p_sys->b_delayed )
     {
-        assert( !p_sys->p_input->b_can_pace_control );
+        assert( !p_sys->p_input->p->b_can_pace_control );
         *pi_wakeup = 0;
     }
     else
@@ -462,7 +495,7 @@ static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bo
     else
     {
         i_ret = VLC_EGENERIC;
-        if( !p_sys->p_input->b_can_pace_control )
+        if( !p_sys->p_input->p->b_can_pace_control )
         {
             if( !p_sys->b_delayed )
                 TsStart( p_out );
@@ -497,7 +530,7 @@ static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
     else
     {
         i_ret = VLC_EGENERIC;
-        if( !p_sys->p_input->b_can_pace_control )
+        if( !p_sys->p_input->p->b_can_pace_control )
         {
             if( !p_sys->b_delayed )
                 TsStart( p_out );
@@ -554,10 +587,6 @@ static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
         assert(0);
         return VLC_EGENERIC;
 
-    /* TODO ? or to remove ? */
-    case ES_OUT_GET_TS:
-        return VLC_EGENERIC;
-
     /* Pass-through control */
     case ES_OUT_SET_ACTIVE:
     case ES_OUT_SET_MODE:
@@ -568,12 +597,15 @@ static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
     case ES_OUT_SET_NEXT_DISPLAY_TIME:
     case ES_OUT_SET_GROUP_META:
     case ES_OUT_SET_GROUP_EPG:
+    case ES_OUT_SET_ES_SCRAMBLED_STATE:
     case ES_OUT_DEL_GROUP:
     case ES_OUT_SET_ES:
     case ES_OUT_RESTART_ES:
     case ES_OUT_SET_ES_DEFAULT:
     case ES_OUT_SET_ES_STATE:
     case ES_OUT_SET_ES_FMT:
+    case ES_OUT_SET_TIMES:
+    case ES_OUT_SET_JITTER:
     {
         ts_cmd_t cmd;
         if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
@@ -597,7 +629,7 @@ static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
             *pb_enabled = true;
             return VLC_SUCCESS;
         }
-        return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es, pb_enabled );
+        return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
     }
 
     /* Special internal input control */
@@ -707,7 +739,7 @@ static int TsStart( es_out_t *p_out )
 
     p_sys->b_delayed = true;
     if( vlc_thread_create( p_ts, "es out timeshift",
-                           TsRun, VLC_THREAD_PRIORITY_INPUT, false ) )
+                           TsRun, VLC_THREAD_PRIORITY_INPUT ) )
     {
         msg_Err( p_sys->p_input, "cannot create input thread" );
 
@@ -741,7 +773,7 @@ static void TsStop( ts_thread_t *p_ts )
     {
         ts_cmd_t cmd;
 
-        if( TsPopCmdLocked( p_ts, &cmd ) )
+        if( TsPopCmdLocked( p_ts, &cmd, true ) )
             break;
 
         CmdClean( &cmd );
@@ -764,6 +796,8 @@ static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
         if( !p_storage )
         {
             CmdClean( p_cmd );
+            vlc_mutex_unlock( &p_ts->lock );
+            /* TODO warn the user (but only once) */
             return;
         }
 
@@ -779,20 +813,21 @@ static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
         }
     }
 
+    /* TODO return error and warn the user (but only once) */
     TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
 
     vlc_cond_signal( &p_ts->wait );
 
     vlc_mutex_unlock( &p_ts->lock );
 }
-static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
+static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
 {
     vlc_assert_locked( &p_ts->lock );
 
     if( TsStorageIsEmpty( p_ts->p_storage_r ) )
         return VLC_EGENERIC;
 
-    TsStoragePopCmd( p_ts->p_storage_r, p_cmd );
+    TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
 
     while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
     {
@@ -898,7 +933,7 @@ static void *TsRun( vlc_object_t *p_thread )
             const int canc = vlc_savecancel();
             b_buffering = es_out_GetBuffering( p_ts->p_out );
 
-            if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd ) )
+            if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
             {
                 vlc_restorecancel( canc );
                 break;
@@ -1035,7 +1070,7 @@ static void TsStorageDelete( ts_storage_t *p_storage )
     {
         ts_cmd_t cmd;
 
-        TsStoragePopCmd( p_storage, &cmd );
+        TsStoragePopCmd( p_storage, &cmd, true );
 
         CmdClean( &cmd );
     }
@@ -1116,7 +1151,7 @@ static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bo
     }
     p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
 }
-static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd )
+static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
 {
     assert( !TsStorageIsEmpty( p_storage ) );
 
@@ -1125,7 +1160,8 @@ static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd )
     {
         block_t block;
 
-        if( !fseek( p_storage->p_filer, p_cmd->send.i_offset, SEEK_SET ) &&
+        if( !b_flush &&
+            !fseek( p_storage->p_filer, p_cmd->send.i_offset, SEEK_SET ) &&
             fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
         {
             block_t *p_block = block_Alloc( block.i_buffer );
@@ -1143,7 +1179,7 @@ static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd )
         }
         else
         {
-            fprintf( stderr, "----------------- 2: %m\n" );
+            //fprintf( stderr, "TsStoragePopCmd: %m\n" );
             p_cmd->send.p_block = block_Alloc( 1 );
         }
     }
@@ -1271,6 +1307,11 @@ static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_co
         p_cmd->control.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
         break;
 
+    case ES_OUT_SET_ES_SCRAMBLED_STATE:
+        p_cmd->control.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
+        p_cmd->control.es_bool.b_bool = (bool)va_arg( args, int );
+        break;
+
     case ES_OUT_RESET_PCR:           /* no arg */
         break;
 
@@ -1352,6 +1393,26 @@ static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_co
         }
         break;
     }
+    case ES_OUT_SET_TIMES:
+    {
+        double f_position = (double)va_arg( args, double );
+        mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
+        mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
+
+        p_cmd->control.times.f_position = f_position;
+        p_cmd->control.times.i_time = i_time;
+        p_cmd->control.times.i_length = i_length;
+        break;
+    }
+    case ES_OUT_SET_JITTER:
+    {
+        mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
+        int     i_cr_average = (int)va_arg( args, int );
+
+        p_cmd->control.jitter.i_pts_delay = i_pts_delay;
+        p_cmd->control.jitter.i_cr_average = i_cr_average;
+        break;
+    }
 
     default:
         assert(0);
@@ -1380,16 +1441,23 @@ static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
         return es_out_Control( p_out, i_query, p_cmd->control.i_i64 );
 
     case ES_OUT_SET_GROUP_PCR:          /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
-        return es_out_Control( p_out, i_query, p_cmd->control.int_i64.i_int, p_cmd->control.int_i64.i_i64 );
+        return es_out_Control( p_out, i_query, p_cmd->control.int_i64.i_int,
+                                               p_cmd->control.int_i64.i_i64 );
 
     case ES_OUT_RESET_PCR:           /* no arg */
         return es_out_Control( p_out, i_query );
 
     case ES_OUT_SET_GROUP_META:  /* arg1=int i_group arg2=vlc_meta_t* */
-        return es_out_Control( p_out, i_query, p_cmd->control.int_meta.i_int, p_cmd->control.int_meta.p_meta );
+        return es_out_Control( p_out, i_query, p_cmd->control.int_meta.i_int,
+                                               p_cmd->control.int_meta.p_meta );
 
     case ES_OUT_SET_GROUP_EPG:   /* arg1=int i_group arg2=vlc_epg_t* */
-        return es_out_Control( p_out, i_query, p_cmd->control.int_epg.i_int, p_cmd->control.int_epg.p_epg );
+        return es_out_Control( p_out, i_query, p_cmd->control.int_epg.i_int,
+                                               p_cmd->control.int_epg.p_epg );
+
+    case ES_OUT_SET_ES_SCRAMBLED_STATE: /* arg1=int es_out_id_t* arg2=bool */
+        return es_out_Control( p_out, i_query, p_cmd->control.es_bool.p_es->p_es,
+                                               p_cmd->control.es_bool.b_bool );
 
     /* Modified control */
     case ES_OUT_SET_ES:      /* arg1= es_out_id_t*                   */
@@ -1398,10 +1466,20 @@ static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
         return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es );
 
     case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool   */
-        return es_out_Control( p_out, i_query, p_cmd->control.es_bool.p_es->p_es, p_cmd->control.es_bool.b_bool );
+        return es_out_Control( p_out, i_query, p_cmd->control.es_bool.p_es->p_es,
+                                               p_cmd->control.es_bool.b_bool );
 
     case ES_OUT_SET_ES_FMT:     /* arg1= es_out_id_t* arg2=es_format_t* */
-        return es_out_Control( p_out, i_query, p_cmd->control.es_fmt.p_es->p_es, p_cmd->control.es_fmt.p_fmt );
+        return es_out_Control( p_out, i_query, p_cmd->control.es_fmt.p_es->p_es,
+                                               p_cmd->control.es_fmt.p_fmt );
+
+    case ES_OUT_SET_TIMES:
+        return es_out_Control( p_out, i_query, p_cmd->control.times.f_position,
+                                               p_cmd->control.times.i_time,
+                                               p_cmd->control.times.i_length );
+    case ES_OUT_SET_JITTER:
+        return es_out_Control( p_out, i_query, p_cmd->control.jitter.i_pts_delay,
+                                               p_cmd->control.jitter.i_cr_average );
 
     default:
         assert(0);
@@ -1495,14 +1573,14 @@ static FILE *GetTmpFile( char **ppsz_file, const char *psz_path )
         return NULL;
 
     /* */
-    fd = mkstemp( psz_name );
+    fd = utf8_mkstemp( psz_name );
     *ppsz_file = psz_name;
 
     if( fd < 0 )
         return NULL;
 
     /* */
-    f = fdopen( fd, "rw+" );
+    f = fdopen( fd, "w+b" );
     if( !f )
         close( fd );