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 /* Pass-through control */
586 case ES_OUT_SET_ACTIVE:
587 case ES_OUT_SET_MODE:
588 case ES_OUT_SET_GROUP:
590 case ES_OUT_SET_GROUP_PCR:
591 case ES_OUT_RESET_PCR:
592 case ES_OUT_SET_NEXT_DISPLAY_TIME:
593 case ES_OUT_SET_GROUP_META:
594 case ES_OUT_SET_GROUP_EPG:
595 case ES_OUT_DEL_GROUP:
597 case ES_OUT_RESTART_ES:
598 case ES_OUT_SET_ES_DEFAULT:
599 case ES_OUT_SET_ES_STATE:
600 case ES_OUT_SET_ES_FMT:
601 case ES_OUT_SET_TIMES:
604 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
606 if( p_sys->b_delayed )
608 TsPushCmd( p_sys->p_thread, &cmd );
611 return CmdExecuteControl( p_sys->p_out, &cmd );
614 /* Special control when delayed */
615 case ES_OUT_GET_ES_STATE:
617 es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
618 bool *pb_enabled = (bool*)va_arg( args, bool* );
620 if( p_sys->b_delayed )
625 return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
628 /* Special internal input control */
629 case ES_OUT_GET_EMPTY:
631 bool *pb_empty = (bool*)va_arg( args, bool* );
632 return ControlLockedGetEmpty( p_out, pb_empty );
634 case ES_OUT_GET_WAKE_UP: /* TODO ? */
636 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
637 return ControlLockedGetWakeup( p_out, pi_wakeup );
639 case ES_OUT_GET_BUFFERING:
641 bool *pb_buffering = (bool *)va_arg( args, bool* );
642 return ControlLockedGetBuffering( p_out, pb_buffering );
644 case ES_OUT_SET_PAUSE_STATE:
646 const bool b_source_paused = (bool)va_arg( args, int );
647 const bool b_paused = (bool)va_arg( args, int );
648 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
650 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
652 case ES_OUT_SET_RATE:
654 const int i_src_rate = (int)va_arg( args, int );
655 const int i_rate = (int)va_arg( args, int );
657 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
659 case ES_OUT_SET_TIME:
661 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
663 return ControlLockedSetTime( p_out, i_date );
665 case ES_OUT_SET_FRAME_NEXT:
667 return ControlLockedSetFrameNext( p_out );
671 msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
676 static int Control( es_out_t *p_out, int i_query, va_list args )
678 es_out_sys_t *p_sys = p_out->p_sys;
681 vlc_mutex_lock( &p_sys->lock );
685 i_ret = ControlLocked( p_out, i_query, args );
687 vlc_mutex_unlock( &p_sys->lock );
692 /*****************************************************************************
694 *****************************************************************************/
695 static void TsDestructor( vlc_object_t *p_this )
697 ts_thread_t *p_ts = (ts_thread_t*)p_this;
699 vlc_cond_destroy( &p_ts->wait );
700 vlc_mutex_destroy( &p_ts->lock );
702 static int TsStart( es_out_t *p_out )
704 es_out_sys_t *p_sys = p_out->p_sys;
707 assert( !p_sys->b_delayed );
709 p_sys->p_thread = p_ts = vlc_custom_create( p_sys->p_input, sizeof(ts_thread_t),
710 VLC_OBJECT_GENERIC, "es out timeshift" );
714 p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
715 p_ts->psz_tmp_path = p_sys->psz_tmp_path;
716 p_ts->p_input = p_sys->p_input;
717 p_ts->p_out = p_sys->p_out;
718 vlc_mutex_init( &p_ts->lock );
719 vlc_cond_init( &p_ts->wait );
720 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
721 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
722 p_ts->i_rate_source = p_sys->i_input_rate_source;
723 p_ts->i_rate = p_sys->i_input_rate;
724 p_ts->i_rate_date = -1;
725 p_ts->i_rate_delay = 0;
726 p_ts->i_buffering_delay = 0;
727 p_ts->i_cmd_delay = 0;
728 p_ts->p_storage_r = NULL;
729 p_ts->p_storage_w = NULL;
731 vlc_object_set_destructor( p_ts, TsDestructor );
733 p_sys->b_delayed = true;
734 if( vlc_thread_create( p_ts, "es out timeshift",
735 TsRun, VLC_THREAD_PRIORITY_INPUT, false ) )
737 msg_Err( p_sys->p_input, "cannot create input thread" );
739 vlc_object_release( p_ts );
741 p_sys->b_delayed = false;
747 static void TsAutoStop( es_out_t *p_out )
749 es_out_sys_t *p_sys = p_out->p_sys;
751 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_thread ) )
754 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
755 TsStop( p_sys->p_thread );
757 p_sys->b_delayed = false;
759 static void TsStop( ts_thread_t *p_ts )
761 vlc_object_kill( p_ts );
762 vlc_thread_join( p_ts );
764 vlc_mutex_lock( &p_ts->lock );
769 if( TsPopCmdLocked( p_ts, &cmd, true ) )
774 assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
775 if( p_ts->p_storage_r )
776 TsStorageDelete( p_ts->p_storage_r );
777 vlc_mutex_unlock( &p_ts->lock );
779 vlc_object_release( p_ts );
781 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
783 vlc_mutex_lock( &p_ts->lock );
785 if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
787 ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
792 vlc_mutex_unlock( &p_ts->lock );
793 /* TODO warn the user (but only once) */
797 if( !p_ts->p_storage_w )
799 p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
803 TsStoragePack( p_ts->p_storage_w );
804 p_ts->p_storage_w->p_next = p_storage;
805 p_ts->p_storage_w = p_storage;
809 /* TODO return error and warn the user (but only once) */
810 TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
812 vlc_cond_signal( &p_ts->wait );
814 vlc_mutex_unlock( &p_ts->lock );
816 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
818 vlc_assert_locked( &p_ts->lock );
820 if( TsStorageIsEmpty( p_ts->p_storage_r ) )
823 TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
825 while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
827 ts_storage_t *p_next = p_ts->p_storage_r->p_next;
831 TsStorageDelete( p_ts->p_storage_r );
832 p_ts->p_storage_r = p_next;
837 static bool TsHasCmd( ts_thread_t *p_ts )
841 vlc_mutex_lock( &p_ts->lock );
842 b_cmd = TsStorageIsEmpty( p_ts->p_storage_r );
843 vlc_mutex_unlock( &p_ts->lock );
847 static bool TsIsUnused( ts_thread_t *p_ts )
851 vlc_mutex_lock( &p_ts->lock );
852 b_unused = !p_ts->b_paused &&
853 p_ts->i_rate == p_ts->i_rate_source &&
854 TsStorageIsEmpty( p_ts->p_storage_r );
855 vlc_mutex_unlock( &p_ts->lock );
859 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
861 vlc_mutex_lock( &p_ts->lock );
866 assert( !b_source_paused );
867 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
871 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
878 assert( p_ts->i_pause_date > 0 );
880 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
883 p_ts->b_paused = b_paused;
884 p_ts->i_pause_date = i_date;
886 vlc_cond_signal( &p_ts->wait );
888 vlc_mutex_unlock( &p_ts->lock );
891 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
895 vlc_mutex_lock( &p_ts->lock );
896 p_ts->i_cmd_delay += p_ts->i_rate_delay;
898 p_ts->i_rate_date = -1;
899 p_ts->i_rate_delay = 0;
900 p_ts->i_rate = i_rate;
901 p_ts->i_rate_source = i_src_rate;
903 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
904 vlc_mutex_unlock( &p_ts->lock );
909 static void *TsRun( vlc_object_t *p_thread )
911 ts_thread_t *p_ts = (ts_thread_t*)p_thread;
912 mtime_t i_buffering_date = -1;
920 /* Pop a command to execute */
921 vlc_mutex_lock( &p_ts->lock );
922 mutex_cleanup_push( &p_ts->lock );
926 const int canc = vlc_savecancel();
927 b_buffering = es_out_GetBuffering( p_ts->p_out );
929 if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
931 vlc_restorecancel( canc );
934 vlc_restorecancel( canc );
936 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
939 if( b_buffering && i_buffering_date < 0 )
941 i_buffering_date = cmd.i_date;
943 else if( i_buffering_date > 0 )
945 p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
947 i_buffering_date = cmd.i_date;
949 i_buffering_date = -1;
952 if( p_ts->i_rate_date < 0 )
953 p_ts->i_rate_date = cmd.i_date;
955 p_ts->i_rate_delay = 0;
956 if( p_ts->i_rate_source != p_ts->i_rate )
958 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
959 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
961 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 )
963 const int canc = vlc_savecancel();
965 /* Auto reset to rate 1.0 */
966 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
968 p_ts->i_cmd_delay = 0;
969 p_ts->i_buffering_delay = 0;
971 p_ts->i_rate_delay = 0;
972 p_ts->i_rate_date = -1;
973 p_ts->i_rate = p_ts->i_rate_source;
975 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
977 vlc_value_t val = { .i_int = p_ts->i_rate };
979 * FIXME it is perfectly safe BUT it is ugly as it may hide a
980 * rate change requested by user */
981 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
984 vlc_restorecancel( canc );
986 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
990 /* Regulate the speed of command processing to the same one than
992 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
998 /* Execute the command */
999 const int canc = vlc_savecancel();
1000 switch( cmd.i_type )
1003 CmdExecuteAdd( p_ts->p_out, &cmd );
1004 CmdCleanAdd( &cmd );
1007 CmdExecuteSend( p_ts->p_out, &cmd );
1008 CmdCleanSend( &cmd );
1011 CmdExecuteControl( p_ts->p_out, &cmd );
1012 CmdCleanControl( &cmd );
1015 CmdExecuteDel( p_ts->p_out, &cmd );
1021 vlc_restorecancel( canc );
1027 /*****************************************************************************
1029 *****************************************************************************/
1030 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1032 ts_storage_t *p_storage = calloc( 1, sizeof(ts_storage_t) );
1037 p_storage->p_next = NULL;
1040 p_storage->i_file_max = i_tmp_size_max;
1041 p_storage->i_file_size = 0;
1042 p_storage->p_filew = GetTmpFile( &p_storage->psz_file, psz_tmp_path );
1043 if( p_storage->psz_file )
1044 p_storage->p_filer = utf8_fopen( p_storage->psz_file, "rb" );
1047 p_storage->i_cmd_w = 0;
1048 p_storage->i_cmd_r = 0;
1049 p_storage->i_cmd_max = 30000;
1050 p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1051 //fprintf( stderr, "\nSTORAGE name=%s size=%d kbytes\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1053 if( !p_storage->p_cmd || !p_storage->p_filew || !p_storage->p_filer )
1055 TsStorageDelete( p_storage );
1060 static void TsStorageDelete( ts_storage_t *p_storage )
1062 while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1066 TsStoragePopCmd( p_storage, &cmd, true );
1070 free( p_storage->p_cmd );
1072 if( p_storage->p_filer )
1073 fclose( p_storage->p_filer );
1074 if( p_storage->p_filew )
1075 fclose( p_storage->p_filew );
1077 if( p_storage->psz_file )
1079 utf8_unlink( p_storage->psz_file );
1080 free( p_storage->psz_file );
1085 static void TsStoragePack( ts_storage_t *p_storage )
1087 /* Try to release a bit of memory */
1088 if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1091 p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1093 ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1095 p_storage->p_cmd = p_new;
1097 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1099 if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1101 size_t i_size = sizeof(*p_cmd->send.p_block) + p_cmd->send.p_block->i_buffer;
1103 if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1106 return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1108 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1110 return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1112 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1114 ts_cmd_t cmd = *p_cmd;
1116 assert( !TsStorageIsFull( p_storage, p_cmd ) );
1118 if( cmd.i_type == C_SEND )
1120 block_t *p_block = cmd.send.p_block;
1122 cmd.send.p_block = NULL;
1123 cmd.send.i_offset = ftell( p_storage->p_filew );
1125 if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1127 block_Release( p_block );
1130 p_storage->i_file_size += sizeof(*p_block);
1131 if( p_block->i_buffer > 0 )
1133 if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1135 block_Release( p_block );
1139 p_storage->i_file_size += p_block->i_buffer;
1140 block_Release( p_block );
1143 fflush( p_storage->p_filew );
1145 p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1147 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1149 assert( !TsStorageIsEmpty( p_storage ) );
1151 *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1152 if( p_cmd->i_type == C_SEND )
1157 !fseek( p_storage->p_filer, p_cmd->send.i_offset, SEEK_SET ) &&
1158 fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1160 block_t *p_block = block_Alloc( block.i_buffer );
1163 p_block->i_dts = block.i_dts;
1164 p_block->i_pts = block.i_pts;
1165 p_block->i_flags = block.i_flags;
1166 p_block->i_length = block.i_length;
1167 p_block->i_rate = block.i_rate;
1168 p_block->i_samples = block.i_samples;
1169 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1171 p_cmd->send.p_block = p_block;
1175 //fprintf( stderr, "TsStoragePopCmd: %m\n" );
1176 p_cmd->send.p_block = block_Alloc( 1 );
1181 /*****************************************************************************
1183 *****************************************************************************/
1184 static void CmdClean( ts_cmd_t *p_cmd )
1186 switch( p_cmd->i_type )
1189 CmdCleanAdd( p_cmd );
1192 CmdCleanSend( p_cmd );
1195 CmdCleanControl( p_cmd );
1205 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1207 p_cmd->i_type = C_ADD;
1208 p_cmd->i_date = mdate();
1209 p_cmd->add.p_es = p_es;
1212 p_cmd->add.p_fmt = malloc( sizeof(*p_fmt) );
1213 if( !p_cmd->add.p_fmt )
1214 return VLC_EGENERIC;
1215 es_format_Copy( p_cmd->add.p_fmt, p_fmt );
1219 p_cmd->add.p_fmt = (es_format_t*)p_fmt;
1223 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1225 p_cmd->add.p_es->p_es = es_out_Add( p_out, p_cmd->add.p_fmt );
1227 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1229 es_format_Clean( p_cmd->add.p_fmt );
1230 free( p_cmd->add.p_fmt );
1233 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1235 p_cmd->i_type = C_SEND;
1236 p_cmd->i_date = mdate();
1237 p_cmd->send.p_es = p_es;
1238 p_cmd->send.p_block = p_block;
1240 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1242 block_t *p_block = p_cmd->send.p_block;
1244 p_cmd->send.p_block = NULL;
1248 if( p_cmd->send.p_es->p_es )
1249 return es_out_Send( p_out, p_cmd->send.p_es->p_es, p_block );
1250 block_Release( p_block );
1252 return VLC_EGENERIC;
1254 static void CmdCleanSend( ts_cmd_t *p_cmd )
1256 if( p_cmd->send.p_block )
1257 block_Release( p_cmd->send.p_block );
1260 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1262 p_cmd->i_type = C_DEL;
1263 p_cmd->i_date = mdate();
1264 p_cmd->del.p_es = p_es;
1267 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1269 if( p_cmd->del.p_es->p_es )
1270 es_out_Del( p_out, p_cmd->del.p_es->p_es );
1271 free( p_cmd->del.p_es );
1274 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1276 p_cmd->i_type = C_CONTROL;
1277 p_cmd->i_date = mdate();
1278 p_cmd->control.i_query = i_query;
1282 /* Pass-through control */
1283 case ES_OUT_SET_ACTIVE: /* arg1= bool */
1284 p_cmd->control.b_bool = (bool)va_arg( args, int );
1287 case ES_OUT_SET_MODE: /* arg1= int */
1288 case ES_OUT_SET_GROUP: /* arg1= int */
1289 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1290 p_cmd->control.i_int = (int)va_arg( args, int );
1293 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1294 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1295 p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
1298 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1299 p_cmd->control.int_i64.i_int = (int)va_arg( args, int );
1300 p_cmd->control.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1303 case ES_OUT_RESET_PCR: /* no arg */
1306 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1308 p_cmd->control.int_meta.i_int = (int)va_arg( args, int );
1309 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1313 p_cmd->control.int_meta.p_meta = vlc_meta_New();
1314 if( !p_cmd->control.int_meta.p_meta )
1315 return VLC_EGENERIC;
1316 vlc_meta_Merge( p_cmd->control.int_meta.p_meta, p_meta );
1320 p_cmd->control.int_meta.p_meta = p_meta;
1325 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1327 p_cmd->control.int_epg.i_int = (int)va_arg( args, int );
1328 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1332 p_cmd->control.int_epg.p_epg = vlc_epg_New( p_epg->psz_name );
1333 if( !p_cmd->control.int_epg.p_epg )
1334 return VLC_EGENERIC;
1335 for( int i = 0; i < p_epg->i_event; i++ )
1337 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1339 vlc_epg_AddEvent( p_cmd->control.int_epg.p_epg,
1340 p_evt->i_start, p_evt->i_duration,
1342 p_evt->psz_short_description, p_evt->psz_description );
1344 vlc_epg_SetCurrent( p_cmd->control.int_epg.p_epg,
1345 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1349 p_cmd->control.int_epg.p_epg = p_epg;
1354 /* Modified control */
1355 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1356 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1357 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1358 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1361 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1362 p_cmd->control.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1363 p_cmd->control.es_bool.b_bool = (bool)va_arg( args, int );
1366 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1368 p_cmd->control.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1369 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1373 p_cmd->control.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1374 if( !p_cmd->control.es_fmt.p_fmt )
1375 return VLC_EGENERIC;
1376 es_format_Copy( p_cmd->control.es_fmt.p_fmt, p_fmt );
1380 p_cmd->control.es_fmt.p_fmt = p_fmt;
1384 case ES_OUT_SET_TIMES:
1386 double f_position = (double)va_arg( args, double );
1387 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
1388 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
1390 p_cmd->control.times.f_position = f_position;
1391 p_cmd->control.times.i_time = i_time;
1392 p_cmd->control.times.i_length = i_length;
1398 return VLC_EGENERIC;
1403 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1405 const int i_query = p_cmd->control.i_query;
1409 /* Pass-through control */
1410 case ES_OUT_SET_ACTIVE: /* arg1= bool */
1411 return es_out_Control( p_out, i_query, p_cmd->control.b_bool );
1413 case ES_OUT_SET_MODE: /* arg1= int */
1414 case ES_OUT_SET_GROUP: /* arg1= int */
1415 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1416 return es_out_Control( p_out, i_query, p_cmd->control.i_int );
1418 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1419 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1420 return es_out_Control( p_out, i_query, p_cmd->control.i_i64 );
1422 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1423 return es_out_Control( p_out, i_query, p_cmd->control.int_i64.i_int,
1424 p_cmd->control.int_i64.i_i64 );
1426 case ES_OUT_RESET_PCR: /* no arg */
1427 return es_out_Control( p_out, i_query );
1429 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1430 return es_out_Control( p_out, i_query, p_cmd->control.int_meta.i_int,
1431 p_cmd->control.int_meta.p_meta );
1433 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1434 return es_out_Control( p_out, i_query, p_cmd->control.int_epg.i_int,
1435 p_cmd->control.int_epg.p_epg );
1437 /* Modified control */
1438 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1439 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1440 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1441 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es );
1443 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1444 return es_out_Control( p_out, i_query, p_cmd->control.es_bool.p_es->p_es,
1445 p_cmd->control.es_bool.b_bool );
1447 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1448 return es_out_Control( p_out, i_query, p_cmd->control.es_fmt.p_es->p_es,
1449 p_cmd->control.es_fmt.p_fmt );
1451 case ES_OUT_SET_TIMES:
1452 return es_out_Control( p_out, i_query, p_cmd->control.times.f_position,
1453 p_cmd->control.times.i_time,
1454 p_cmd->control.times.i_length );
1458 return VLC_EGENERIC;
1461 static void CmdCleanControl( ts_cmd_t *p_cmd )
1463 if( p_cmd->control.i_query == ES_OUT_SET_GROUP_META &&
1464 p_cmd->control.int_meta.p_meta )
1466 vlc_meta_Delete( p_cmd->control.int_meta.p_meta );
1468 else if( p_cmd->control.i_query == ES_OUT_SET_GROUP_EPG &&
1469 p_cmd->control.int_epg.p_epg )
1471 vlc_epg_Delete( p_cmd->control.int_epg.p_epg );
1473 else if( p_cmd->control.i_query == ES_OUT_SET_ES_FMT &&
1474 p_cmd->control.es_fmt.p_fmt )
1476 es_format_Clean( p_cmd->control.es_fmt.p_fmt );
1477 free( p_cmd->control.es_fmt.p_fmt );
1482 /*****************************************************************************
1484 *****************************************************************************/
1485 static char *GetTmpPath( char *psz_path )
1487 if( psz_path && *psz_path )
1489 /* Make sure that the path exists and is a directory */
1491 const int i_ret = utf8_stat( psz_path, &s );
1493 if( i_ret < 0 && !utf8_mkdir( psz_path, 0600 ) )
1495 else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1500 /* Create a suitable path */
1501 #if defined (WIN32) && !defined (UNDER_CE)
1502 const DWORD dwCount = GetTempPathW( 0, NULL );
1503 wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1506 if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1510 psw_path = _wgetcwd( NULL, 0 );
1517 psz_path = FromWide( psw_path );
1518 while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1519 psz_path[strlen( psz_path ) - 1] = '\0';
1524 if( !psz_path || *psz_path == '\0' )
1527 return strdup( "C:" );
1530 psz_path = strdup( "/tmp" );
1536 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path )
1544 if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1548 fd = utf8_mkstemp( psz_name );
1549 *ppsz_file = psz_name;
1555 f = fdopen( fd, "rw+" );