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
126 /* FIXME Really too big (double make the whole thing too big) */
134 typedef struct attribute_packed
143 ts_cmd_control_t control;
147 typedef struct ts_storage_t ts_storage_t;
150 ts_storage_t *p_next;
153 char *psz_file; /* Filename */
154 size_t i_file_max; /* Max size in bytes */
155 int64_t i_file_size;/* Current size in bytes */
156 FILE *p_filew; /* FILE handle for data writing */
157 FILE *p_filer; /* FILE handle for data reading */
171 input_thread_t *p_input;
173 int64_t i_tmp_size_max;
174 const char *psz_tmp_path;
176 /* Lock for all following fields */
182 mtime_t i_pause_date;
188 mtime_t i_rate_delay;
191 mtime_t i_buffering_delay;
194 ts_storage_t *p_storage_r;
195 ts_storage_t *p_storage_w;
208 input_thread_t *p_input;
212 int64_t i_tmp_size_max; /* Maximal temporary file size in byte */
213 char *psz_tmp_path; /* Path for temporary files */
215 /* Lock for all following fields */
220 ts_thread_t *p_thread;
224 bool b_input_paused_source;
226 int i_input_rate_source;
233 static es_out_id_t *Add ( es_out_t *, const es_format_t * );
234 static int Send ( es_out_t *, es_out_id_t *, block_t * );
235 static void Del ( es_out_t *, es_out_id_t * );
236 static int Control( es_out_t *, int i_query, va_list );
237 static void Destroy( es_out_t * );
239 static int TsStart( es_out_t * );
240 static void TsAutoStop( es_out_t * );
242 static void TsStop( ts_thread_t * );
243 static void TsPushCmd( ts_thread_t *, ts_cmd_t * );
244 static int TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
245 static bool TsHasCmd( ts_thread_t * );
246 static bool TsIsUnused( ts_thread_t * );
247 static int TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
248 static int TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
250 static void *TsRun( vlc_object_t * );
252 static ts_storage_t *TsStorageNew( const char *psz_path, int64_t i_tmp_size_max );
253 static void TsStorageDelete( ts_storage_t * );
254 static void TsStoragePack( ts_storage_t *p_storage );
255 static bool TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
256 static bool TsStorageIsEmpty( ts_storage_t * );
257 static void TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
258 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
260 static void CmdClean( ts_cmd_t * );
261 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
263 static int CmdInitAdd ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
264 static void CmdInitSend ( ts_cmd_t *, es_out_id_t *, block_t * );
265 static int CmdInitDel ( ts_cmd_t *, es_out_id_t * );
266 static int CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
269 static void CmdCleanAdd ( ts_cmd_t * );
270 static void CmdCleanSend ( ts_cmd_t * );
271 static void CmdCleanControl( ts_cmd_t *p_cmd );
273 /* XXX these functions will take the destination es_out_t */
274 static void CmdExecuteAdd ( es_out_t *, ts_cmd_t * );
275 static int CmdExecuteSend ( es_out_t *, ts_cmd_t * );
276 static void CmdExecuteDel ( es_out_t *, ts_cmd_t * );
277 static int CmdExecuteControl( es_out_t *, ts_cmd_t * );
280 static char *GetTmpPath( char *psz_path );
281 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path );
283 /*****************************************************************************
284 * input_EsOutTimeshiftNew:
285 *****************************************************************************/
286 es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
288 es_out_t *p_out = malloc( sizeof(*p_out) );
292 es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
301 p_out->pf_send = Send;
303 p_out->pf_control = Control;
304 p_out->pf_destroy = Destroy;
305 p_out->p_sys = p_sys;
306 p_out->b_sout = p_input->p->p_sout != NULL;
309 p_sys->b_input_paused = false;
310 p_sys->b_input_paused_source = false;
311 p_sys->p_input = p_input;
312 p_sys->i_input_rate = i_rate;
313 p_sys->i_input_rate_source = i_rate;
315 p_sys->p_out = p_next_out;
316 vlc_mutex_init_recursive( &p_sys->lock );
318 p_sys->b_delayed = false;
319 p_sys->p_thread = NULL;
321 TAB_INIT( p_sys->i_es, p_sys->pp_es );
324 const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
325 if( i_tmp_size_max < 0 )
326 p_sys->i_tmp_size_max = 50*1024*1024;
328 p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
329 msg_Dbg( p_input, "using timeshift granularity of %d bytes",
330 (int)p_sys->i_tmp_size_max );
332 char *psz_tmp_path = var_CreateGetNonEmptyString( p_input, "input-timeshift-path" );
333 p_sys->psz_tmp_path = GetTmpPath( psz_tmp_path );
334 msg_Dbg( p_input, "using timeshift path '%s'", p_sys->psz_tmp_path );
337 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
349 /*****************************************************************************
351 *****************************************************************************/
352 static void Destroy( es_out_t *p_out )
354 es_out_sys_t *p_sys = p_out->p_sys;
356 if( p_sys->b_delayed )
358 TsStop( p_sys->p_thread );
359 p_sys->b_delayed = false;
362 while( p_sys->i_es > 0 )
363 Del( p_out, p_sys->pp_es[0] );
364 TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
366 free( p_sys->psz_tmp_path );
367 vlc_mutex_destroy( &p_sys->lock );
372 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
374 es_out_sys_t *p_sys = p_out->p_sys;
377 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
381 vlc_mutex_lock( &p_sys->lock );
385 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
387 vlc_mutex_unlock( &p_sys->lock );
392 TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
394 if( p_sys->b_delayed )
395 TsPushCmd( p_sys->p_thread, &cmd );
397 CmdExecuteAdd( p_sys->p_out, &cmd );
399 vlc_mutex_unlock( &p_sys->lock );
403 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
405 es_out_sys_t *p_sys = p_out->p_sys;
407 int i_ret = VLC_SUCCESS;
409 vlc_mutex_lock( &p_sys->lock );
413 CmdInitSend( &cmd, p_es, p_block );
414 if( p_sys->b_delayed )
415 TsPushCmd( p_sys->p_thread, &cmd );
417 i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
419 vlc_mutex_unlock( &p_sys->lock );
423 static void Del( es_out_t *p_out, es_out_id_t *p_es )
425 es_out_sys_t *p_sys = p_out->p_sys;
428 vlc_mutex_lock( &p_sys->lock );
432 CmdInitDel( &cmd, p_es );
433 if( p_sys->b_delayed )
434 TsPushCmd( p_sys->p_thread, &cmd );
436 CmdExecuteDel( p_sys->p_out, &cmd );
438 TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
440 vlc_mutex_unlock( &p_sys->lock );
443 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
445 es_out_sys_t *p_sys = p_out->p_sys;
447 if( p_sys->b_delayed && TsHasCmd( p_sys->p_thread ) )
450 *pb_empty = es_out_GetEmpty( p_sys->p_out );
454 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
456 es_out_sys_t *p_sys = p_out->p_sys;
458 if( p_sys->b_delayed )
460 assert( !p_sys->p_input->b_can_pace_control );
465 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
470 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
472 es_out_sys_t *p_sys = p_out->p_sys;
474 if( p_sys->b_delayed )
475 *pb_buffering = true;
477 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
481 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
483 es_out_sys_t *p_sys = p_out->p_sys;
486 if( !p_sys->b_delayed && !b_source_paused == !b_paused )
488 i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
492 i_ret = VLC_EGENERIC;
493 if( !p_sys->p_input->b_can_pace_control )
495 if( !p_sys->b_delayed )
497 if( p_sys->b_delayed )
498 i_ret = TsChangePause( p_sys->p_thread, b_source_paused, b_paused, i_date );
502 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
503 * and so be able to advertize correctly pace control property in access
505 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
511 p_sys->b_input_paused_source = b_source_paused;
512 p_sys->b_input_paused = b_paused;
516 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
518 es_out_sys_t *p_sys = p_out->p_sys;
521 if( !p_sys->b_delayed && i_src_rate == i_rate )
523 i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
527 i_ret = VLC_EGENERIC;
528 if( !p_sys->p_input->b_can_pace_control )
530 if( !p_sys->b_delayed )
532 if( p_sys->b_delayed )
533 i_ret = TsChangeRate( p_sys->p_thread, i_src_rate, i_rate );
537 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
538 * and so be able to advertize correctly pace control property in access
540 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
547 p_sys->i_input_rate_source = i_src_rate;
548 p_sys->i_input_rate = i_rate;
552 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
554 es_out_sys_t *p_sys = p_out->p_sys;
556 if( !p_sys->b_delayed )
557 return es_out_SetTime( p_sys->p_out, i_date );
560 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
563 static int ControlLockedSetFrameNext( es_out_t *p_out )
565 es_out_sys_t *p_sys = p_out->p_sys;
567 return es_out_SetFrameNext( p_sys->p_out );
570 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
572 es_out_sys_t *p_sys = p_out->p_sys;
576 /* Invalid query for this es_out level */
577 case ES_OUT_SET_ES_BY_ID:
578 case ES_OUT_RESTART_ES_BY_ID:
579 case ES_OUT_SET_ES_DEFAULT_BY_ID:
580 case ES_OUT_SET_DELAY:
581 case ES_OUT_SET_RECORD_STATE:
585 /* TODO ? or to remove ? */
589 /* Pass-through control */
590 case ES_OUT_SET_ACTIVE:
591 case ES_OUT_SET_MODE:
592 case ES_OUT_SET_GROUP:
594 case ES_OUT_SET_GROUP_PCR:
595 case ES_OUT_RESET_PCR:
596 case ES_OUT_SET_NEXT_DISPLAY_TIME:
597 case ES_OUT_SET_GROUP_META:
598 case ES_OUT_SET_GROUP_EPG:
599 case ES_OUT_DEL_GROUP:
601 case ES_OUT_RESTART_ES:
602 case ES_OUT_SET_ES_DEFAULT:
603 case ES_OUT_SET_ES_STATE:
604 case ES_OUT_SET_ES_FMT:
605 case ES_OUT_SET_TIMES:
608 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
610 if( p_sys->b_delayed )
612 TsPushCmd( p_sys->p_thread, &cmd );
615 return CmdExecuteControl( p_sys->p_out, &cmd );
618 /* Special control when delayed */
619 case ES_OUT_GET_ES_STATE:
621 es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
622 bool *pb_enabled = (bool*)va_arg( args, bool* );
624 if( p_sys->b_delayed )
629 return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
632 /* Special internal input control */
633 case ES_OUT_GET_EMPTY:
635 bool *pb_empty = (bool*)va_arg( args, bool* );
636 return ControlLockedGetEmpty( p_out, pb_empty );
638 case ES_OUT_GET_WAKE_UP: /* TODO ? */
640 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
641 return ControlLockedGetWakeup( p_out, pi_wakeup );
643 case ES_OUT_GET_BUFFERING:
645 bool *pb_buffering = (bool *)va_arg( args, bool* );
646 return ControlLockedGetBuffering( p_out, pb_buffering );
648 case ES_OUT_SET_PAUSE_STATE:
650 const bool b_source_paused = (bool)va_arg( args, int );
651 const bool b_paused = (bool)va_arg( args, int );
652 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
654 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
656 case ES_OUT_SET_RATE:
658 const int i_src_rate = (int)va_arg( args, int );
659 const int i_rate = (int)va_arg( args, int );
661 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
663 case ES_OUT_SET_TIME:
665 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
667 return ControlLockedSetTime( p_out, i_date );
669 case ES_OUT_SET_FRAME_NEXT:
671 return ControlLockedSetFrameNext( p_out );
675 msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
680 static int Control( es_out_t *p_out, int i_query, va_list args )
682 es_out_sys_t *p_sys = p_out->p_sys;
685 vlc_mutex_lock( &p_sys->lock );
689 i_ret = ControlLocked( p_out, i_query, args );
691 vlc_mutex_unlock( &p_sys->lock );
696 /*****************************************************************************
698 *****************************************************************************/
699 static void TsDestructor( vlc_object_t *p_this )
701 ts_thread_t *p_ts = (ts_thread_t*)p_this;
703 vlc_cond_destroy( &p_ts->wait );
704 vlc_mutex_destroy( &p_ts->lock );
706 static int TsStart( es_out_t *p_out )
708 es_out_sys_t *p_sys = p_out->p_sys;
711 assert( !p_sys->b_delayed );
713 p_sys->p_thread = p_ts = vlc_custom_create( p_sys->p_input, sizeof(ts_thread_t),
714 VLC_OBJECT_GENERIC, "es out timeshift" );
718 p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
719 p_ts->psz_tmp_path = p_sys->psz_tmp_path;
720 p_ts->p_input = p_sys->p_input;
721 p_ts->p_out = p_sys->p_out;
722 vlc_mutex_init( &p_ts->lock );
723 vlc_cond_init( &p_ts->wait );
724 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
725 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
726 p_ts->i_rate_source = p_sys->i_input_rate_source;
727 p_ts->i_rate = p_sys->i_input_rate;
728 p_ts->i_rate_date = -1;
729 p_ts->i_rate_delay = 0;
730 p_ts->i_buffering_delay = 0;
731 p_ts->i_cmd_delay = 0;
732 p_ts->p_storage_r = NULL;
733 p_ts->p_storage_w = NULL;
735 vlc_object_set_destructor( p_ts, TsDestructor );
737 p_sys->b_delayed = true;
738 if( vlc_thread_create( p_ts, "es out timeshift",
739 TsRun, VLC_THREAD_PRIORITY_INPUT, false ) )
741 msg_Err( p_sys->p_input, "cannot create input thread" );
743 vlc_object_release( p_ts );
745 p_sys->b_delayed = false;
751 static void TsAutoStop( es_out_t *p_out )
753 es_out_sys_t *p_sys = p_out->p_sys;
755 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_thread ) )
758 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
759 TsStop( p_sys->p_thread );
761 p_sys->b_delayed = false;
763 static void TsStop( ts_thread_t *p_ts )
765 vlc_object_kill( p_ts );
766 vlc_thread_join( p_ts );
768 vlc_mutex_lock( &p_ts->lock );
773 if( TsPopCmdLocked( p_ts, &cmd, true ) )
778 assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
779 if( p_ts->p_storage_r )
780 TsStorageDelete( p_ts->p_storage_r );
781 vlc_mutex_unlock( &p_ts->lock );
783 vlc_object_release( p_ts );
785 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
787 vlc_mutex_lock( &p_ts->lock );
789 if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
791 ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
796 vlc_mutex_unlock( &p_ts->lock );
797 /* TODO warn the user (but only once) */
801 if( !p_ts->p_storage_w )
803 p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
807 TsStoragePack( p_ts->p_storage_w );
808 p_ts->p_storage_w->p_next = p_storage;
809 p_ts->p_storage_w = p_storage;
813 /* TODO return error and warn the user (but only once) */
814 TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
816 vlc_cond_signal( &p_ts->wait );
818 vlc_mutex_unlock( &p_ts->lock );
820 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
822 vlc_assert_locked( &p_ts->lock );
824 if( TsStorageIsEmpty( p_ts->p_storage_r ) )
827 TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
829 while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
831 ts_storage_t *p_next = p_ts->p_storage_r->p_next;
835 TsStorageDelete( p_ts->p_storage_r );
836 p_ts->p_storage_r = p_next;
841 static bool TsHasCmd( ts_thread_t *p_ts )
845 vlc_mutex_lock( &p_ts->lock );
846 b_cmd = TsStorageIsEmpty( p_ts->p_storage_r );
847 vlc_mutex_unlock( &p_ts->lock );
851 static bool TsIsUnused( ts_thread_t *p_ts )
855 vlc_mutex_lock( &p_ts->lock );
856 b_unused = !p_ts->b_paused &&
857 p_ts->i_rate == p_ts->i_rate_source &&
858 TsStorageIsEmpty( p_ts->p_storage_r );
859 vlc_mutex_unlock( &p_ts->lock );
863 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
865 vlc_mutex_lock( &p_ts->lock );
870 assert( !b_source_paused );
871 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
875 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
882 assert( p_ts->i_pause_date > 0 );
884 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
887 p_ts->b_paused = b_paused;
888 p_ts->i_pause_date = i_date;
890 vlc_cond_signal( &p_ts->wait );
892 vlc_mutex_unlock( &p_ts->lock );
895 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
899 vlc_mutex_lock( &p_ts->lock );
900 p_ts->i_cmd_delay += p_ts->i_rate_delay;
902 p_ts->i_rate_date = -1;
903 p_ts->i_rate_delay = 0;
904 p_ts->i_rate = i_rate;
905 p_ts->i_rate_source = i_src_rate;
907 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
908 vlc_mutex_unlock( &p_ts->lock );
913 static void *TsRun( vlc_object_t *p_thread )
915 ts_thread_t *p_ts = (ts_thread_t*)p_thread;
916 mtime_t i_buffering_date = -1;
924 /* Pop a command to execute */
925 vlc_mutex_lock( &p_ts->lock );
926 mutex_cleanup_push( &p_ts->lock );
930 const int canc = vlc_savecancel();
931 b_buffering = es_out_GetBuffering( p_ts->p_out );
933 if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
935 vlc_restorecancel( canc );
938 vlc_restorecancel( canc );
940 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
943 if( b_buffering && i_buffering_date < 0 )
945 i_buffering_date = cmd.i_date;
947 else if( i_buffering_date > 0 )
949 p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
951 i_buffering_date = cmd.i_date;
953 i_buffering_date = -1;
956 if( p_ts->i_rate_date < 0 )
957 p_ts->i_rate_date = cmd.i_date;
959 p_ts->i_rate_delay = 0;
960 if( p_ts->i_rate_source != p_ts->i_rate )
962 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
963 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
965 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 )
967 const int canc = vlc_savecancel();
969 /* Auto reset to rate 1.0 */
970 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
972 p_ts->i_cmd_delay = 0;
973 p_ts->i_buffering_delay = 0;
975 p_ts->i_rate_delay = 0;
976 p_ts->i_rate_date = -1;
977 p_ts->i_rate = p_ts->i_rate_source;
979 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
981 vlc_value_t val = { .i_int = p_ts->i_rate };
983 * FIXME it is perfectly safe BUT it is ugly as it may hide a
984 * rate change requested by user */
985 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
988 vlc_restorecancel( canc );
990 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
994 /* Regulate the speed of command processing to the same one than
996 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
1002 /* Execute the command */
1003 const int canc = vlc_savecancel();
1004 switch( cmd.i_type )
1007 CmdExecuteAdd( p_ts->p_out, &cmd );
1008 CmdCleanAdd( &cmd );
1011 CmdExecuteSend( p_ts->p_out, &cmd );
1012 CmdCleanSend( &cmd );
1015 CmdExecuteControl( p_ts->p_out, &cmd );
1016 CmdCleanControl( &cmd );
1019 CmdExecuteDel( p_ts->p_out, &cmd );
1025 vlc_restorecancel( canc );
1031 /*****************************************************************************
1033 *****************************************************************************/
1034 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1036 ts_storage_t *p_storage = calloc( 1, sizeof(ts_storage_t) );
1041 p_storage->p_next = NULL;
1044 p_storage->i_file_max = i_tmp_size_max;
1045 p_storage->i_file_size = 0;
1046 p_storage->p_filew = GetTmpFile( &p_storage->psz_file, psz_tmp_path );
1047 if( p_storage->psz_file )
1048 p_storage->p_filer = utf8_fopen( p_storage->psz_file, "rb" );
1051 p_storage->i_cmd_w = 0;
1052 p_storage->i_cmd_r = 0;
1053 p_storage->i_cmd_max = 30000;
1054 p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1055 //fprintf( stderr, "\nSTORAGE name=%s size=%d kbytes\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1057 if( !p_storage->p_cmd || !p_storage->p_filew || !p_storage->p_filer )
1059 TsStorageDelete( p_storage );
1064 static void TsStorageDelete( ts_storage_t *p_storage )
1066 while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1070 TsStoragePopCmd( p_storage, &cmd, true );
1074 free( p_storage->p_cmd );
1076 if( p_storage->p_filer )
1077 fclose( p_storage->p_filer );
1078 if( p_storage->p_filew )
1079 fclose( p_storage->p_filew );
1081 if( p_storage->psz_file )
1083 utf8_unlink( p_storage->psz_file );
1084 free( p_storage->psz_file );
1089 static void TsStoragePack( ts_storage_t *p_storage )
1091 /* Try to release a bit of memory */
1092 if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1095 p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1097 ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1099 p_storage->p_cmd = p_new;
1101 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1103 if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1105 size_t i_size = sizeof(*p_cmd->send.p_block) + p_cmd->send.p_block->i_buffer;
1107 if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1110 return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1112 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1114 return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1116 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1118 ts_cmd_t cmd = *p_cmd;
1120 assert( !TsStorageIsFull( p_storage, p_cmd ) );
1122 if( cmd.i_type == C_SEND )
1124 block_t *p_block = cmd.send.p_block;
1126 cmd.send.p_block = NULL;
1127 cmd.send.i_offset = ftell( p_storage->p_filew );
1129 if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1131 block_Release( p_block );
1134 p_storage->i_file_size += sizeof(*p_block);
1135 if( p_block->i_buffer > 0 )
1137 if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1139 block_Release( p_block );
1143 p_storage->i_file_size += p_block->i_buffer;
1144 block_Release( p_block );
1147 fflush( p_storage->p_filew );
1149 p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1151 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1153 assert( !TsStorageIsEmpty( p_storage ) );
1155 *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1156 if( p_cmd->i_type == C_SEND )
1161 !fseek( p_storage->p_filer, p_cmd->send.i_offset, SEEK_SET ) &&
1162 fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1164 block_t *p_block = block_Alloc( block.i_buffer );
1167 p_block->i_dts = block.i_dts;
1168 p_block->i_pts = block.i_pts;
1169 p_block->i_flags = block.i_flags;
1170 p_block->i_length = block.i_length;
1171 p_block->i_rate = block.i_rate;
1172 p_block->i_samples = block.i_samples;
1173 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1175 p_cmd->send.p_block = p_block;
1179 //fprintf( stderr, "TsStoragePopCmd: %m\n" );
1180 p_cmd->send.p_block = block_Alloc( 1 );
1185 /*****************************************************************************
1187 *****************************************************************************/
1188 static void CmdClean( ts_cmd_t *p_cmd )
1190 switch( p_cmd->i_type )
1193 CmdCleanAdd( p_cmd );
1196 CmdCleanSend( p_cmd );
1199 CmdCleanControl( p_cmd );
1209 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1211 p_cmd->i_type = C_ADD;
1212 p_cmd->i_date = mdate();
1213 p_cmd->add.p_es = p_es;
1216 p_cmd->add.p_fmt = malloc( sizeof(*p_fmt) );
1217 if( !p_cmd->add.p_fmt )
1218 return VLC_EGENERIC;
1219 es_format_Copy( p_cmd->add.p_fmt, p_fmt );
1223 p_cmd->add.p_fmt = (es_format_t*)p_fmt;
1227 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1229 p_cmd->add.p_es->p_es = es_out_Add( p_out, p_cmd->add.p_fmt );
1231 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1233 es_format_Clean( p_cmd->add.p_fmt );
1234 free( p_cmd->add.p_fmt );
1237 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1239 p_cmd->i_type = C_SEND;
1240 p_cmd->i_date = mdate();
1241 p_cmd->send.p_es = p_es;
1242 p_cmd->send.p_block = p_block;
1244 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1246 block_t *p_block = p_cmd->send.p_block;
1248 p_cmd->send.p_block = NULL;
1252 if( p_cmd->send.p_es->p_es )
1253 return es_out_Send( p_out, p_cmd->send.p_es->p_es, p_block );
1254 block_Release( p_block );
1256 return VLC_EGENERIC;
1258 static void CmdCleanSend( ts_cmd_t *p_cmd )
1260 if( p_cmd->send.p_block )
1261 block_Release( p_cmd->send.p_block );
1264 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1266 p_cmd->i_type = C_DEL;
1267 p_cmd->i_date = mdate();
1268 p_cmd->del.p_es = p_es;
1271 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1273 if( p_cmd->del.p_es->p_es )
1274 es_out_Del( p_out, p_cmd->del.p_es->p_es );
1275 free( p_cmd->del.p_es );
1278 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1280 p_cmd->i_type = C_CONTROL;
1281 p_cmd->i_date = mdate();
1282 p_cmd->control.i_query = i_query;
1286 /* Pass-through control */
1287 case ES_OUT_SET_ACTIVE: /* arg1= bool */
1288 p_cmd->control.b_bool = (bool)va_arg( args, int );
1291 case ES_OUT_SET_MODE: /* arg1= int */
1292 case ES_OUT_SET_GROUP: /* arg1= int */
1293 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1294 p_cmd->control.i_int = (int)va_arg( args, int );
1297 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1298 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1299 p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
1302 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1303 p_cmd->control.int_i64.i_int = (int)va_arg( args, int );
1304 p_cmd->control.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1307 case ES_OUT_RESET_PCR: /* no arg */
1310 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1312 p_cmd->control.int_meta.i_int = (int)va_arg( args, int );
1313 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1317 p_cmd->control.int_meta.p_meta = vlc_meta_New();
1318 if( !p_cmd->control.int_meta.p_meta )
1319 return VLC_EGENERIC;
1320 vlc_meta_Merge( p_cmd->control.int_meta.p_meta, p_meta );
1324 p_cmd->control.int_meta.p_meta = p_meta;
1329 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1331 p_cmd->control.int_epg.i_int = (int)va_arg( args, int );
1332 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1336 p_cmd->control.int_epg.p_epg = vlc_epg_New( p_epg->psz_name );
1337 if( !p_cmd->control.int_epg.p_epg )
1338 return VLC_EGENERIC;
1339 for( int i = 0; i < p_epg->i_event; i++ )
1341 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1343 vlc_epg_AddEvent( p_cmd->control.int_epg.p_epg,
1344 p_evt->i_start, p_evt->i_duration,
1346 p_evt->psz_short_description, p_evt->psz_description );
1348 vlc_epg_SetCurrent( p_cmd->control.int_epg.p_epg,
1349 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1353 p_cmd->control.int_epg.p_epg = p_epg;
1358 /* Modified control */
1359 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1360 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1361 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1362 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1365 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1366 p_cmd->control.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1367 p_cmd->control.es_bool.b_bool = (bool)va_arg( args, int );
1370 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1372 p_cmd->control.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1373 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1377 p_cmd->control.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1378 if( !p_cmd->control.es_fmt.p_fmt )
1379 return VLC_EGENERIC;
1380 es_format_Copy( p_cmd->control.es_fmt.p_fmt, p_fmt );
1384 p_cmd->control.es_fmt.p_fmt = p_fmt;
1388 case ES_OUT_SET_TIMES:
1390 double f_position = (double)va_arg( args, double );
1391 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
1392 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
1394 p_cmd->control.times.f_position = f_position;
1395 p_cmd->control.times.i_time = i_time;
1396 p_cmd->control.times.i_length = i_length;
1402 return VLC_EGENERIC;
1407 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1409 const int i_query = p_cmd->control.i_query;
1413 /* Pass-through control */
1414 case ES_OUT_SET_ACTIVE: /* arg1= bool */
1415 return es_out_Control( p_out, i_query, p_cmd->control.b_bool );
1417 case ES_OUT_SET_MODE: /* arg1= int */
1418 case ES_OUT_SET_GROUP: /* arg1= int */
1419 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1420 return es_out_Control( p_out, i_query, p_cmd->control.i_int );
1422 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1423 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1424 return es_out_Control( p_out, i_query, p_cmd->control.i_i64 );
1426 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1427 return es_out_Control( p_out, i_query, p_cmd->control.int_i64.i_int,
1428 p_cmd->control.int_i64.i_i64 );
1430 case ES_OUT_RESET_PCR: /* no arg */
1431 return es_out_Control( p_out, i_query );
1433 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1434 return es_out_Control( p_out, i_query, p_cmd->control.int_meta.i_int,
1435 p_cmd->control.int_meta.p_meta );
1437 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1438 return es_out_Control( p_out, i_query, p_cmd->control.int_epg.i_int,
1439 p_cmd->control.int_epg.p_epg );
1441 /* Modified control */
1442 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1443 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1444 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1445 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es );
1447 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1448 return es_out_Control( p_out, i_query, p_cmd->control.es_bool.p_es->p_es,
1449 p_cmd->control.es_bool.b_bool );
1451 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1452 return es_out_Control( p_out, i_query, p_cmd->control.es_fmt.p_es->p_es,
1453 p_cmd->control.es_fmt.p_fmt );
1455 case ES_OUT_SET_TIMES:
1456 return es_out_Control( p_out, i_query, p_cmd->control.times.f_position,
1457 p_cmd->control.times.i_time,
1458 p_cmd->control.times.i_length );
1462 return VLC_EGENERIC;
1465 static void CmdCleanControl( ts_cmd_t *p_cmd )
1467 if( p_cmd->control.i_query == ES_OUT_SET_GROUP_META &&
1468 p_cmd->control.int_meta.p_meta )
1470 vlc_meta_Delete( p_cmd->control.int_meta.p_meta );
1472 else if( p_cmd->control.i_query == ES_OUT_SET_GROUP_EPG &&
1473 p_cmd->control.int_epg.p_epg )
1475 vlc_epg_Delete( p_cmd->control.int_epg.p_epg );
1477 else if( p_cmd->control.i_query == ES_OUT_SET_ES_FMT &&
1478 p_cmd->control.es_fmt.p_fmt )
1480 es_format_Clean( p_cmd->control.es_fmt.p_fmt );
1481 free( p_cmd->control.es_fmt.p_fmt );
1486 /*****************************************************************************
1488 *****************************************************************************/
1489 static char *GetTmpPath( char *psz_path )
1491 if( psz_path && *psz_path )
1493 /* Make sure that the path exists and is a directory */
1495 const int i_ret = utf8_stat( psz_path, &s );
1497 if( i_ret < 0 && !utf8_mkdir( psz_path, 0600 ) )
1499 else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1504 /* Create a suitable path */
1505 #if defined (WIN32) && !defined (UNDER_CE)
1506 const DWORD dwCount = GetTempPathW( 0, NULL );
1507 wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1510 if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1514 psw_path = _wgetcwd( NULL, 0 );
1521 psz_path = FromWide( psw_path );
1522 while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1523 psz_path[strlen( psz_path ) - 1] = '\0';
1528 if( !psz_path || *psz_path == '\0' )
1531 return strdup( "C:" );
1534 psz_path = strdup( "/tmp" );
1540 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path )
1548 if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1552 fd = utf8_mkstemp( psz_name );
1553 *ppsz_file = psz_name;
1559 f = fdopen( fd, "rw+" );