1 /*****************************************************************************
2 * es_out_timeshift.c: Es Out timeshift.
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
7 * Authors: Laurent Aimar < fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #if defined (WIN32) && !defined (UNDER_CE)
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
42 #include <vlc_common.h>
43 #include <vlc_charset.h>
45 #include <vlc_input.h>
46 #include <vlc_es_out.h>
47 #include <vlc_block.h>
48 #include "input_internal.h"
50 #include "es_out_timeshift.h"
52 /*****************************************************************************
54 *****************************************************************************/
56 /* XXX attribute_packed is (and MUST be) used ONLY to reduce memory usage */
57 #ifdef HAVE_ATTRIBUTE_PACKED
58 # define attribute_packed __attribute__((__packed__))
60 # define attribute_packed
71 typedef struct attribute_packed
77 typedef struct attribute_packed
82 typedef struct attribute_packed
86 int i_offset; /* We do not use file > INT_MAX */
89 typedef struct attribute_packed
127 typedef struct attribute_packed
136 ts_cmd_control_t control;
140 typedef struct ts_storage_t ts_storage_t;
143 ts_storage_t *p_next;
146 char *psz_file; /* Filename */
147 size_t i_file_max; /* Max size in bytes */
148 int64_t i_file_size;/* Current size in bytes */
149 FILE *p_filew; /* FILE handle for data writing */
150 FILE *p_filer; /* FILE handle for data reading */
164 input_thread_t *p_input;
166 int64_t i_tmp_size_max;
167 const char *psz_tmp_path;
169 /* Lock for all following fields */
175 mtime_t i_pause_date;
181 mtime_t i_rate_delay;
184 mtime_t i_buffering_delay;
187 ts_storage_t *p_storage_r;
188 ts_storage_t *p_storage_w;
201 input_thread_t *p_input;
205 int64_t i_tmp_size_max; /* Maximal temporary file size in byte */
206 char *psz_tmp_path; /* Path for temporary files */
208 /* Lock for all following fields */
213 ts_thread_t *p_thread;
217 bool b_input_paused_source;
219 int i_input_rate_source;
226 static es_out_id_t *Add ( es_out_t *, const es_format_t * );
227 static int Send ( es_out_t *, es_out_id_t *, block_t * );
228 static void Del ( es_out_t *, es_out_id_t * );
229 static int Control( es_out_t *, int i_query, va_list );
230 static void Destroy( es_out_t * );
232 static int TsStart( es_out_t * );
233 static void TsAutoStop( es_out_t * );
235 static void TsStop( ts_thread_t * );
236 static void TsPushCmd( ts_thread_t *, ts_cmd_t * );
237 static int TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
238 static bool TsHasCmd( ts_thread_t * );
239 static bool TsIsUnused( ts_thread_t * );
240 static int TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
241 static int TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
243 static void *TsRun( vlc_object_t * );
245 static ts_storage_t *TsStorageNew( const char *psz_path, int64_t i_tmp_size_max );
246 static void TsStorageDelete( ts_storage_t * );
247 static void TsStoragePack( ts_storage_t *p_storage );
248 static bool TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
249 static bool TsStorageIsEmpty( ts_storage_t * );
250 static void TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
251 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
253 static void CmdClean( ts_cmd_t * );
254 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
256 static int CmdInitAdd ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
257 static void CmdInitSend ( ts_cmd_t *, es_out_id_t *, block_t * );
258 static int CmdInitDel ( ts_cmd_t *, es_out_id_t * );
259 static int CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
262 static void CmdCleanAdd ( ts_cmd_t * );
263 static void CmdCleanSend ( ts_cmd_t * );
264 static void CmdCleanControl( ts_cmd_t *p_cmd );
266 /* XXX these functions will take the destination es_out_t */
267 static void CmdExecuteAdd ( es_out_t *, ts_cmd_t * );
268 static int CmdExecuteSend ( es_out_t *, ts_cmd_t * );
269 static void CmdExecuteDel ( es_out_t *, ts_cmd_t * );
270 static int CmdExecuteControl( es_out_t *, ts_cmd_t * );
273 static char *GetTmpPath( char *psz_path );
274 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path );
276 /*****************************************************************************
277 * input_EsOutTimeshiftNew:
278 *****************************************************************************/
279 es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
281 es_out_t *p_out = malloc( sizeof(*p_out) );
285 es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
294 p_out->pf_send = Send;
296 p_out->pf_control = Control;
297 p_out->pf_destroy = Destroy;
298 p_out->p_sys = p_sys;
299 p_out->b_sout = p_input->p->p_sout != NULL;
302 p_sys->b_input_paused = false;
303 p_sys->b_input_paused_source = false;
304 p_sys->p_input = p_input;
305 p_sys->i_input_rate = i_rate;
306 p_sys->i_input_rate_source = i_rate;
308 p_sys->p_out = p_next_out;
309 vlc_mutex_init_recursive( &p_sys->lock );
311 p_sys->b_delayed = false;
312 p_sys->p_thread = NULL;
314 TAB_INIT( p_sys->i_es, p_sys->pp_es );
317 const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
318 if( i_tmp_size_max < 0 )
319 p_sys->i_tmp_size_max = 50*1024*1024;
321 p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
322 msg_Dbg( p_input, "using timeshift granularity of %d bytes",
323 (int)p_sys->i_tmp_size_max );
325 char *psz_tmp_path = var_CreateGetNonEmptyString( p_input, "input-timeshift-path" );
326 p_sys->psz_tmp_path = GetTmpPath( psz_tmp_path );
327 msg_Dbg( p_input, "using timeshift path '%s'", p_sys->psz_tmp_path );
330 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
342 /*****************************************************************************
344 *****************************************************************************/
345 static void Destroy( es_out_t *p_out )
347 es_out_sys_t *p_sys = p_out->p_sys;
349 if( p_sys->b_delayed )
351 TsStop( p_sys->p_thread );
352 p_sys->b_delayed = false;
355 while( p_sys->i_es > 0 )
356 Del( p_out, p_sys->pp_es[0] );
357 TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
359 free( p_sys->psz_tmp_path );
360 vlc_mutex_destroy( &p_sys->lock );
365 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
367 es_out_sys_t *p_sys = p_out->p_sys;
370 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
374 vlc_mutex_lock( &p_sys->lock );
378 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
380 vlc_mutex_unlock( &p_sys->lock );
385 TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
387 if( p_sys->b_delayed )
388 TsPushCmd( p_sys->p_thread, &cmd );
390 CmdExecuteAdd( p_sys->p_out, &cmd );
392 vlc_mutex_unlock( &p_sys->lock );
396 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
398 es_out_sys_t *p_sys = p_out->p_sys;
400 int i_ret = VLC_SUCCESS;
402 vlc_mutex_lock( &p_sys->lock );
406 CmdInitSend( &cmd, p_es, p_block );
407 if( p_sys->b_delayed )
408 TsPushCmd( p_sys->p_thread, &cmd );
410 i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
412 vlc_mutex_unlock( &p_sys->lock );
416 static void Del( es_out_t *p_out, es_out_id_t *p_es )
418 es_out_sys_t *p_sys = p_out->p_sys;
421 vlc_mutex_lock( &p_sys->lock );
425 CmdInitDel( &cmd, p_es );
426 if( p_sys->b_delayed )
427 TsPushCmd( p_sys->p_thread, &cmd );
429 CmdExecuteDel( p_sys->p_out, &cmd );
431 TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
433 vlc_mutex_unlock( &p_sys->lock );
436 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
438 es_out_sys_t *p_sys = p_out->p_sys;
440 if( p_sys->b_delayed && TsHasCmd( p_sys->p_thread ) )
443 *pb_empty = es_out_GetEmpty( p_sys->p_out );
447 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
449 es_out_sys_t *p_sys = p_out->p_sys;
451 if( p_sys->b_delayed )
453 assert( !p_sys->p_input->b_can_pace_control );
458 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
463 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
465 es_out_sys_t *p_sys = p_out->p_sys;
467 if( p_sys->b_delayed )
468 *pb_buffering = true;
470 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
474 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
476 es_out_sys_t *p_sys = p_out->p_sys;
479 if( !p_sys->b_delayed && !b_source_paused == !b_paused )
481 i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
485 i_ret = VLC_EGENERIC;
486 if( !p_sys->p_input->b_can_pace_control )
488 if( !p_sys->b_delayed )
490 if( p_sys->b_delayed )
491 i_ret = TsChangePause( p_sys->p_thread, b_source_paused, b_paused, i_date );
495 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
496 * and so be able to advertize correctly pace control property in access
498 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
504 p_sys->b_input_paused_source = b_source_paused;
505 p_sys->b_input_paused = b_paused;
509 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
511 es_out_sys_t *p_sys = p_out->p_sys;
514 if( !p_sys->b_delayed && i_src_rate == i_rate )
516 i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
520 i_ret = VLC_EGENERIC;
521 if( !p_sys->p_input->b_can_pace_control )
523 if( !p_sys->b_delayed )
525 if( p_sys->b_delayed )
526 i_ret = TsChangeRate( p_sys->p_thread, i_src_rate, i_rate );
530 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
531 * and so be able to advertize correctly pace control property in access
533 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
540 p_sys->i_input_rate_source = i_src_rate;
541 p_sys->i_input_rate = i_rate;
545 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
547 es_out_sys_t *p_sys = p_out->p_sys;
549 if( !p_sys->b_delayed )
550 return es_out_SetTime( p_sys->p_out, i_date );
553 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
556 static int ControlLockedSetFrameNext( es_out_t *p_out )
558 es_out_sys_t *p_sys = p_out->p_sys;
560 return es_out_SetFrameNext( p_sys->p_out );
563 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
565 es_out_sys_t *p_sys = p_out->p_sys;
569 /* Invalid query for this es_out level */
570 case ES_OUT_SET_ES_BY_ID:
571 case ES_OUT_RESTART_ES_BY_ID:
572 case ES_OUT_SET_ES_DEFAULT_BY_ID:
573 case ES_OUT_SET_DELAY:
574 case ES_OUT_SET_RECORD_STATE:
578 /* TODO ? or to remove ? */
582 /* Pass-through control */
583 case ES_OUT_SET_ACTIVE:
584 case ES_OUT_SET_MODE:
585 case ES_OUT_SET_GROUP:
587 case ES_OUT_SET_GROUP_PCR:
588 case ES_OUT_RESET_PCR:
589 case ES_OUT_SET_NEXT_DISPLAY_TIME:
590 case ES_OUT_SET_GROUP_META:
591 case ES_OUT_SET_GROUP_EPG:
592 case ES_OUT_DEL_GROUP:
594 case ES_OUT_RESTART_ES:
595 case ES_OUT_SET_ES_DEFAULT:
596 case ES_OUT_SET_ES_STATE:
597 case ES_OUT_SET_ES_FMT:
600 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
602 if( p_sys->b_delayed )
604 TsPushCmd( p_sys->p_thread, &cmd );
607 return CmdExecuteControl( p_sys->p_out, &cmd );
610 /* Special control when delayed */
611 case ES_OUT_GET_ES_STATE:
613 es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
614 bool *pb_enabled = (bool*)va_arg( args, bool* );
616 if( p_sys->b_delayed )
621 return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
624 /* Special internal input control */
625 case ES_OUT_GET_EMPTY:
627 bool *pb_empty = (bool*)va_arg( args, bool* );
628 return ControlLockedGetEmpty( p_out, pb_empty );
630 case ES_OUT_GET_WAKE_UP: /* TODO ? */
632 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
633 return ControlLockedGetWakeup( p_out, pi_wakeup );
635 case ES_OUT_GET_BUFFERING:
637 bool *pb_buffering = (bool *)va_arg( args, bool* );
638 return ControlLockedGetBuffering( p_out, pb_buffering );
640 case ES_OUT_SET_PAUSE_STATE:
642 const bool b_source_paused = (bool)va_arg( args, int );
643 const bool b_paused = (bool)va_arg( args, int );
644 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
646 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
648 case ES_OUT_SET_RATE:
650 const int i_src_rate = (int)va_arg( args, int );
651 const int i_rate = (int)va_arg( args, int );
653 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
655 case ES_OUT_SET_TIME:
657 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
659 return ControlLockedSetTime( p_out, i_date );
661 case ES_OUT_SET_FRAME_NEXT:
663 return ControlLockedSetFrameNext( p_out );
667 msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
672 static int Control( es_out_t *p_out, int i_query, va_list args )
674 es_out_sys_t *p_sys = p_out->p_sys;
677 vlc_mutex_lock( &p_sys->lock );
681 i_ret = ControlLocked( p_out, i_query, args );
683 vlc_mutex_unlock( &p_sys->lock );
688 /*****************************************************************************
690 *****************************************************************************/
691 static void TsDestructor( vlc_object_t *p_this )
693 ts_thread_t *p_ts = (ts_thread_t*)p_this;
695 vlc_cond_destroy( &p_ts->wait );
696 vlc_mutex_destroy( &p_ts->lock );
698 static int TsStart( es_out_t *p_out )
700 es_out_sys_t *p_sys = p_out->p_sys;
703 assert( !p_sys->b_delayed );
705 p_sys->p_thread = p_ts = vlc_custom_create( p_sys->p_input, sizeof(ts_thread_t),
706 VLC_OBJECT_GENERIC, "es out timeshift" );
710 p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
711 p_ts->psz_tmp_path = p_sys->psz_tmp_path;
712 p_ts->p_input = p_sys->p_input;
713 p_ts->p_out = p_sys->p_out;
714 vlc_mutex_init( &p_ts->lock );
715 vlc_cond_init( &p_ts->wait );
716 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
717 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
718 p_ts->i_rate_source = p_sys->i_input_rate_source;
719 p_ts->i_rate = p_sys->i_input_rate;
720 p_ts->i_rate_date = -1;
721 p_ts->i_rate_delay = 0;
722 p_ts->i_buffering_delay = 0;
723 p_ts->i_cmd_delay = 0;
724 p_ts->p_storage_r = NULL;
725 p_ts->p_storage_w = NULL;
727 vlc_object_set_destructor( p_ts, TsDestructor );
729 p_sys->b_delayed = true;
730 if( vlc_thread_create( p_ts, "es out timeshift",
731 TsRun, VLC_THREAD_PRIORITY_INPUT, false ) )
733 msg_Err( p_sys->p_input, "cannot create input thread" );
735 vlc_object_release( p_ts );
737 p_sys->b_delayed = false;
743 static void TsAutoStop( es_out_t *p_out )
745 es_out_sys_t *p_sys = p_out->p_sys;
747 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_thread ) )
750 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
751 TsStop( p_sys->p_thread );
753 p_sys->b_delayed = false;
755 static void TsStop( ts_thread_t *p_ts )
757 vlc_object_kill( p_ts );
758 vlc_thread_join( p_ts );
760 vlc_mutex_lock( &p_ts->lock );
765 if( TsPopCmdLocked( p_ts, &cmd, true ) )
770 assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
771 if( p_ts->p_storage_r )
772 TsStorageDelete( p_ts->p_storage_r );
773 vlc_mutex_unlock( &p_ts->lock );
775 vlc_object_release( p_ts );
777 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
779 vlc_mutex_lock( &p_ts->lock );
781 if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
783 ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
788 vlc_mutex_unlock( &p_ts->lock );
789 /* TODO warn the user (but only once) */
793 if( !p_ts->p_storage_w )
795 p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
799 TsStoragePack( p_ts->p_storage_w );
800 p_ts->p_storage_w->p_next = p_storage;
801 p_ts->p_storage_w = p_storage;
805 /* TODO return error and warn the user (but only once) */
806 TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
808 vlc_cond_signal( &p_ts->wait );
810 vlc_mutex_unlock( &p_ts->lock );
812 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
814 vlc_assert_locked( &p_ts->lock );
816 if( TsStorageIsEmpty( p_ts->p_storage_r ) )
819 TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
821 while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
823 ts_storage_t *p_next = p_ts->p_storage_r->p_next;
827 TsStorageDelete( p_ts->p_storage_r );
828 p_ts->p_storage_r = p_next;
833 static bool TsHasCmd( ts_thread_t *p_ts )
837 vlc_mutex_lock( &p_ts->lock );
838 b_cmd = TsStorageIsEmpty( p_ts->p_storage_r );
839 vlc_mutex_unlock( &p_ts->lock );
843 static bool TsIsUnused( ts_thread_t *p_ts )
847 vlc_mutex_lock( &p_ts->lock );
848 b_unused = !p_ts->b_paused &&
849 p_ts->i_rate == p_ts->i_rate_source &&
850 TsStorageIsEmpty( p_ts->p_storage_r );
851 vlc_mutex_unlock( &p_ts->lock );
855 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
857 vlc_mutex_lock( &p_ts->lock );
862 assert( !b_source_paused );
863 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
867 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
874 assert( p_ts->i_pause_date > 0 );
876 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
879 p_ts->b_paused = b_paused;
880 p_ts->i_pause_date = i_date;
882 vlc_cond_signal( &p_ts->wait );
884 vlc_mutex_unlock( &p_ts->lock );
887 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
891 vlc_mutex_lock( &p_ts->lock );
892 p_ts->i_cmd_delay += p_ts->i_rate_delay;
894 p_ts->i_rate_date = -1;
895 p_ts->i_rate_delay = 0;
896 p_ts->i_rate = i_rate;
897 p_ts->i_rate_source = i_src_rate;
899 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
900 vlc_mutex_unlock( &p_ts->lock );
905 static void *TsRun( vlc_object_t *p_thread )
907 ts_thread_t *p_ts = (ts_thread_t*)p_thread;
908 mtime_t i_buffering_date = -1;
916 /* Pop a command to execute */
917 vlc_mutex_lock( &p_ts->lock );
918 mutex_cleanup_push( &p_ts->lock );
922 const int canc = vlc_savecancel();
923 b_buffering = es_out_GetBuffering( p_ts->p_out );
925 if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
927 vlc_restorecancel( canc );
930 vlc_restorecancel( canc );
932 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
935 if( b_buffering && i_buffering_date < 0 )
937 i_buffering_date = cmd.i_date;
939 else if( i_buffering_date > 0 )
941 p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
943 i_buffering_date = cmd.i_date;
945 i_buffering_date = -1;
948 if( p_ts->i_rate_date < 0 )
949 p_ts->i_rate_date = cmd.i_date;
951 p_ts->i_rate_delay = 0;
952 if( p_ts->i_rate_source != p_ts->i_rate )
954 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
955 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
957 if( p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay < 0 && p_ts->i_rate != p_ts->i_rate_source )
959 const int canc = vlc_savecancel();
961 /* Auto reset to rate 1.0 */
962 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
964 p_ts->i_cmd_delay = 0;
965 p_ts->i_buffering_delay = 0;
967 p_ts->i_rate_delay = 0;
968 p_ts->i_rate_date = -1;
969 p_ts->i_rate = p_ts->i_rate_source;
971 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
973 vlc_value_t val = { .i_int = p_ts->i_rate };
975 * FIXME it is perfectly safe BUT it is ugly as it may hide a
976 * rate change requested by user */
977 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
980 vlc_restorecancel( canc );
982 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
986 /* Regulate the speed of command processing to the same one than
988 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
994 /* Execute the command */
995 const int canc = vlc_savecancel();
999 CmdExecuteAdd( p_ts->p_out, &cmd );
1000 CmdCleanAdd( &cmd );
1003 CmdExecuteSend( p_ts->p_out, &cmd );
1004 CmdCleanSend( &cmd );
1007 CmdExecuteControl( p_ts->p_out, &cmd );
1008 CmdCleanControl( &cmd );
1011 CmdExecuteDel( p_ts->p_out, &cmd );
1017 vlc_restorecancel( canc );
1023 /*****************************************************************************
1025 *****************************************************************************/
1026 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1028 ts_storage_t *p_storage = calloc( 1, sizeof(ts_storage_t) );
1033 p_storage->p_next = NULL;
1036 p_storage->i_file_max = i_tmp_size_max;
1037 p_storage->i_file_size = 0;
1038 p_storage->p_filew = GetTmpFile( &p_storage->psz_file, psz_tmp_path );
1039 if( p_storage->psz_file )
1040 p_storage->p_filer = utf8_fopen( p_storage->psz_file, "rb" );
1043 p_storage->i_cmd_w = 0;
1044 p_storage->i_cmd_r = 0;
1045 p_storage->i_cmd_max = 30000;
1046 p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1047 //fprintf( stderr, "\nSTORAGE name=%s size=%d kbytes\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1049 if( !p_storage->p_cmd || !p_storage->p_filew || !p_storage->p_filer )
1051 TsStorageDelete( p_storage );
1056 static void TsStorageDelete( ts_storage_t *p_storage )
1058 while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1062 TsStoragePopCmd( p_storage, &cmd, true );
1066 free( p_storage->p_cmd );
1068 if( p_storage->p_filer )
1069 fclose( p_storage->p_filer );
1070 if( p_storage->p_filew )
1071 fclose( p_storage->p_filew );
1073 if( p_storage->psz_file )
1075 utf8_unlink( p_storage->psz_file );
1076 free( p_storage->psz_file );
1081 static void TsStoragePack( ts_storage_t *p_storage )
1083 /* Try to release a bit of memory */
1084 if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1087 p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1089 ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1091 p_storage->p_cmd = p_new;
1093 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1095 if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1097 size_t i_size = sizeof(*p_cmd->send.p_block) + p_cmd->send.p_block->i_buffer;
1099 if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1102 return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1104 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1106 return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1108 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1110 ts_cmd_t cmd = *p_cmd;
1112 assert( !TsStorageIsFull( p_storage, p_cmd ) );
1114 if( cmd.i_type == C_SEND )
1116 block_t *p_block = cmd.send.p_block;
1118 cmd.send.p_block = NULL;
1119 cmd.send.i_offset = ftell( p_storage->p_filew );
1121 if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1123 block_Release( p_block );
1126 p_storage->i_file_size += sizeof(*p_block);
1127 if( p_block->i_buffer > 0 )
1129 if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1131 block_Release( p_block );
1135 p_storage->i_file_size += p_block->i_buffer;
1136 block_Release( p_block );
1139 fflush( p_storage->p_filew );
1141 p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1143 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1145 assert( !TsStorageIsEmpty( p_storage ) );
1147 *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1148 if( p_cmd->i_type == C_SEND )
1153 !fseek( p_storage->p_filer, p_cmd->send.i_offset, SEEK_SET ) &&
1154 fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1156 block_t *p_block = block_Alloc( block.i_buffer );
1159 p_block->i_dts = block.i_dts;
1160 p_block->i_pts = block.i_pts;
1161 p_block->i_flags = block.i_flags;
1162 p_block->i_length = block.i_length;
1163 p_block->i_rate = block.i_rate;
1164 p_block->i_samples = block.i_samples;
1165 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1167 p_cmd->send.p_block = p_block;
1171 //fprintf( stderr, "TsStoragePopCmd: %m\n" );
1172 p_cmd->send.p_block = block_Alloc( 1 );
1177 /*****************************************************************************
1179 *****************************************************************************/
1180 static void CmdClean( ts_cmd_t *p_cmd )
1182 switch( p_cmd->i_type )
1185 CmdCleanAdd( p_cmd );
1188 CmdCleanSend( p_cmd );
1191 CmdCleanControl( p_cmd );
1201 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1203 p_cmd->i_type = C_ADD;
1204 p_cmd->i_date = mdate();
1205 p_cmd->add.p_es = p_es;
1208 p_cmd->add.p_fmt = malloc( sizeof(*p_fmt) );
1209 if( !p_cmd->add.p_fmt )
1210 return VLC_EGENERIC;
1211 es_format_Copy( p_cmd->add.p_fmt, p_fmt );
1215 p_cmd->add.p_fmt = (es_format_t*)p_fmt;
1219 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1221 p_cmd->add.p_es->p_es = es_out_Add( p_out, p_cmd->add.p_fmt );
1223 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1225 es_format_Clean( p_cmd->add.p_fmt );
1226 free( p_cmd->add.p_fmt );
1229 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1231 p_cmd->i_type = C_SEND;
1232 p_cmd->i_date = mdate();
1233 p_cmd->send.p_es = p_es;
1234 p_cmd->send.p_block = p_block;
1236 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1238 block_t *p_block = p_cmd->send.p_block;
1240 p_cmd->send.p_block = NULL;
1244 if( p_cmd->send.p_es->p_es )
1245 return es_out_Send( p_out, p_cmd->send.p_es->p_es, p_block );
1246 block_Release( p_block );
1248 return VLC_EGENERIC;
1250 static void CmdCleanSend( ts_cmd_t *p_cmd )
1252 if( p_cmd->send.p_block )
1253 block_Release( p_cmd->send.p_block );
1256 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1258 p_cmd->i_type = C_DEL;
1259 p_cmd->i_date = mdate();
1260 p_cmd->del.p_es = p_es;
1263 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1265 if( p_cmd->del.p_es->p_es )
1266 es_out_Del( p_out, p_cmd->del.p_es->p_es );
1267 free( p_cmd->del.p_es );
1270 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1272 p_cmd->i_type = C_CONTROL;
1273 p_cmd->i_date = mdate();
1274 p_cmd->control.i_query = i_query;
1278 /* Pass-through control */
1279 case ES_OUT_SET_ACTIVE: /* arg1= bool */
1280 p_cmd->control.b_bool = (bool)va_arg( args, int );
1283 case ES_OUT_SET_MODE: /* arg1= int */
1284 case ES_OUT_SET_GROUP: /* arg1= int */
1285 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1286 p_cmd->control.i_int = (int)va_arg( args, int );
1289 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1290 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1291 p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
1294 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1295 p_cmd->control.int_i64.i_int = (int)va_arg( args, int );
1296 p_cmd->control.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1299 case ES_OUT_RESET_PCR: /* no arg */
1302 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1304 p_cmd->control.int_meta.i_int = (int)va_arg( args, int );
1305 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1309 p_cmd->control.int_meta.p_meta = vlc_meta_New();
1310 if( !p_cmd->control.int_meta.p_meta )
1311 return VLC_EGENERIC;
1312 vlc_meta_Merge( p_cmd->control.int_meta.p_meta, p_meta );
1316 p_cmd->control.int_meta.p_meta = p_meta;
1321 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1323 p_cmd->control.int_epg.i_int = (int)va_arg( args, int );
1324 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1328 p_cmd->control.int_epg.p_epg = vlc_epg_New( p_epg->psz_name );
1329 if( !p_cmd->control.int_epg.p_epg )
1330 return VLC_EGENERIC;
1331 for( int i = 0; i < p_epg->i_event; i++ )
1333 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1335 vlc_epg_AddEvent( p_cmd->control.int_epg.p_epg,
1336 p_evt->i_start, p_evt->i_duration,
1338 p_evt->psz_short_description, p_evt->psz_description );
1340 vlc_epg_SetCurrent( p_cmd->control.int_epg.p_epg,
1341 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1345 p_cmd->control.int_epg.p_epg = p_epg;
1350 /* Modified control */
1351 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1352 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1353 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1354 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1357 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1358 p_cmd->control.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1359 p_cmd->control.es_bool.b_bool = (bool)va_arg( args, int );
1362 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1364 p_cmd->control.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1365 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1369 p_cmd->control.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1370 if( !p_cmd->control.es_fmt.p_fmt )
1371 return VLC_EGENERIC;
1372 es_format_Copy( p_cmd->control.es_fmt.p_fmt, p_fmt );
1376 p_cmd->control.es_fmt.p_fmt = p_fmt;
1383 return VLC_EGENERIC;
1388 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1390 const int i_query = p_cmd->control.i_query;
1394 /* Pass-through control */
1395 case ES_OUT_SET_ACTIVE: /* arg1= bool */
1396 return es_out_Control( p_out, i_query, p_cmd->control.b_bool );
1398 case ES_OUT_SET_MODE: /* arg1= int */
1399 case ES_OUT_SET_GROUP: /* arg1= int */
1400 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1401 return es_out_Control( p_out, i_query, p_cmd->control.i_int );
1403 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1404 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1405 return es_out_Control( p_out, i_query, p_cmd->control.i_i64 );
1407 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1408 return es_out_Control( p_out, i_query, p_cmd->control.int_i64.i_int, p_cmd->control.int_i64.i_i64 );
1410 case ES_OUT_RESET_PCR: /* no arg */
1411 return es_out_Control( p_out, i_query );
1413 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1414 return es_out_Control( p_out, i_query, p_cmd->control.int_meta.i_int, p_cmd->control.int_meta.p_meta );
1416 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1417 return es_out_Control( p_out, i_query, p_cmd->control.int_epg.i_int, p_cmd->control.int_epg.p_epg );
1419 /* Modified control */
1420 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1421 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1422 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1423 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es );
1425 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1426 return es_out_Control( p_out, i_query, p_cmd->control.es_bool.p_es->p_es, p_cmd->control.es_bool.b_bool );
1428 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1429 return es_out_Control( p_out, i_query, p_cmd->control.es_fmt.p_es->p_es, p_cmd->control.es_fmt.p_fmt );
1433 return VLC_EGENERIC;
1436 static void CmdCleanControl( ts_cmd_t *p_cmd )
1438 if( p_cmd->control.i_query == ES_OUT_SET_GROUP_META &&
1439 p_cmd->control.int_meta.p_meta )
1441 vlc_meta_Delete( p_cmd->control.int_meta.p_meta );
1443 else if( p_cmd->control.i_query == ES_OUT_SET_GROUP_EPG &&
1444 p_cmd->control.int_epg.p_epg )
1446 vlc_epg_Delete( p_cmd->control.int_epg.p_epg );
1448 else if( p_cmd->control.i_query == ES_OUT_SET_ES_FMT &&
1449 p_cmd->control.es_fmt.p_fmt )
1451 es_format_Clean( p_cmd->control.es_fmt.p_fmt );
1452 free( p_cmd->control.es_fmt.p_fmt );
1457 /*****************************************************************************
1459 *****************************************************************************/
1460 static char *GetTmpPath( char *psz_path )
1462 if( psz_path && *psz_path )
1464 /* Make sure that the path exists and is a directory */
1466 const int i_ret = utf8_stat( psz_path, &s );
1468 if( i_ret < 0 && !utf8_mkdir( psz_path, 0600 ) )
1470 else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1475 /* Create a suitable path */
1476 #if defined (WIN32) && !defined (UNDER_CE)
1477 const DWORD dwCount = GetTempPathW( 0, NULL );
1478 wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1481 if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1485 psw_path = _wgetcwd( NULL, 0 );
1492 psz_path = FromWide( psw_path );
1493 while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1494 psz_path[strlen( psz_path ) - 1] = '\0';
1499 if( !psz_path || *psz_path == '\0' )
1502 return strdup( "C:" );
1505 psz_path = strdup( "/tmp" );
1511 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path )
1519 if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1523 fd = utf8_mkstemp( psz_name );
1524 *ppsz_file = psz_name;
1530 f = fdopen( fd, "rw+" );