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 *****************************************************************************/
34 #if defined (WIN32) && !defined (UNDER_CE)
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
41 #include <vlc_common.h>
44 # include <vlc_charset.h>
46 #include <vlc_input.h>
47 #include <vlc_es_out.h>
48 #include <vlc_block.h>
49 #include "input_internal.h"
51 #include "es_out_timeshift.h"
53 /*****************************************************************************
55 *****************************************************************************/
57 /* XXX attribute_packed is (and MUST be) used ONLY to reduce memory usage */
58 #ifdef HAVE_ATTRIBUTE_PACKED
59 # define attribute_packed __attribute__((__packed__))
61 # define attribute_packed
72 typedef struct attribute_packed
78 typedef struct attribute_packed
83 typedef struct attribute_packed
87 int i_offset; /* We do not use file > INT_MAX */
90 typedef struct attribute_packed
127 /* FIXME Really too big (double make the whole thing too big) */
135 mtime_t i_pts_jitter;
141 typedef struct attribute_packed
150 ts_cmd_control_t control;
154 typedef struct ts_storage_t ts_storage_t;
157 ts_storage_t *p_next;
160 char *psz_file; /* Filename */
161 size_t i_file_max; /* Max size in bytes */
162 int64_t i_file_size;/* Current size in bytes */
163 FILE *p_filew; /* FILE handle for data writing */
164 FILE *p_filer; /* FILE handle for data reading */
176 input_thread_t *p_input;
178 int64_t i_tmp_size_max;
179 const char *psz_tmp_path;
181 /* Lock for all following fields */
187 mtime_t i_pause_date;
193 mtime_t i_rate_delay;
196 mtime_t i_buffering_delay;
199 ts_storage_t *p_storage_r;
200 ts_storage_t *p_storage_w;
213 input_thread_t *p_input;
217 int64_t i_tmp_size_max; /* Maximal temporary file size in byte */
218 char *psz_tmp_path; /* Path for temporary files */
220 /* Lock for all following fields */
229 bool b_input_paused_source;
231 int i_input_rate_source;
238 static es_out_id_t *Add ( es_out_t *, const es_format_t * );
239 static int Send ( es_out_t *, es_out_id_t *, block_t * );
240 static void Del ( es_out_t *, es_out_id_t * );
241 static int Control( es_out_t *, int i_query, va_list );
242 static void Destroy( es_out_t * );
244 static int TsStart( es_out_t * );
245 static void TsAutoStop( es_out_t * );
247 static void TsStop( ts_thread_t * );
248 static void TsPushCmd( ts_thread_t *, ts_cmd_t * );
249 static int TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
250 static bool TsHasCmd( ts_thread_t * );
251 static bool TsIsUnused( ts_thread_t * );
252 static int TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
253 static int TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
255 static void *TsRun( void * );
257 static ts_storage_t *TsStorageNew( const char *psz_path, int64_t i_tmp_size_max );
258 static void TsStorageDelete( ts_storage_t * );
259 static void TsStoragePack( ts_storage_t *p_storage );
260 static bool TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
261 static bool TsStorageIsEmpty( ts_storage_t * );
262 static void TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
263 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
265 static void CmdClean( ts_cmd_t * );
266 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
268 static int CmdInitAdd ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
269 static void CmdInitSend ( ts_cmd_t *, es_out_id_t *, block_t * );
270 static int CmdInitDel ( ts_cmd_t *, es_out_id_t * );
271 static int CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
274 static void CmdCleanAdd ( ts_cmd_t * );
275 static void CmdCleanSend ( ts_cmd_t * );
276 static void CmdCleanControl( ts_cmd_t *p_cmd );
278 /* XXX these functions will take the destination es_out_t */
279 static void CmdExecuteAdd ( es_out_t *, ts_cmd_t * );
280 static int CmdExecuteSend ( es_out_t *, ts_cmd_t * );
281 static void CmdExecuteDel ( es_out_t *, ts_cmd_t * );
282 static int CmdExecuteControl( es_out_t *, ts_cmd_t * );
285 static char *GetTmpPath( char *psz_path );
286 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path );
288 /*****************************************************************************
289 * input_EsOutTimeshiftNew:
290 *****************************************************************************/
291 es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
293 es_out_t *p_out = malloc( sizeof(*p_out) );
297 es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
306 p_out->pf_send = Send;
308 p_out->pf_control = Control;
309 p_out->pf_destroy = Destroy;
310 p_out->p_sys = p_sys;
313 p_sys->b_input_paused = false;
314 p_sys->b_input_paused_source = false;
315 p_sys->p_input = p_input;
316 p_sys->i_input_rate = i_rate;
317 p_sys->i_input_rate_source = i_rate;
319 p_sys->p_out = p_next_out;
320 vlc_mutex_init_recursive( &p_sys->lock );
322 p_sys->b_delayed = false;
325 TAB_INIT( p_sys->i_es, p_sys->pp_es );
328 const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
329 if( i_tmp_size_max < 0 )
330 p_sys->i_tmp_size_max = 50*1024*1024;
332 p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
334 char *psz_tmp_path = var_CreateGetNonEmptyString( p_input, "input-timeshift-path" );
335 p_sys->psz_tmp_path = GetTmpPath( psz_tmp_path );
337 msg_Dbg( p_input, "using timeshift granularity of %d MiB, in path '%s'",
338 (int)p_sys->i_tmp_size_max/(1024*1024), p_sys->psz_tmp_path );
341 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
353 /*****************************************************************************
355 *****************************************************************************/
356 static void Destroy( es_out_t *p_out )
358 es_out_sys_t *p_sys = p_out->p_sys;
360 if( p_sys->b_delayed )
362 TsStop( p_sys->p_ts );
363 p_sys->b_delayed = false;
366 while( p_sys->i_es > 0 )
367 Del( p_out, p_sys->pp_es[0] );
368 TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
370 free( p_sys->psz_tmp_path );
371 vlc_mutex_destroy( &p_sys->lock );
376 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
378 es_out_sys_t *p_sys = p_out->p_sys;
381 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
385 vlc_mutex_lock( &p_sys->lock );
389 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
391 vlc_mutex_unlock( &p_sys->lock );
396 TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
398 if( p_sys->b_delayed )
399 TsPushCmd( p_sys->p_ts, &cmd );
401 CmdExecuteAdd( p_sys->p_out, &cmd );
403 vlc_mutex_unlock( &p_sys->lock );
407 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
409 es_out_sys_t *p_sys = p_out->p_sys;
411 int i_ret = VLC_SUCCESS;
413 vlc_mutex_lock( &p_sys->lock );
417 CmdInitSend( &cmd, p_es, p_block );
418 if( p_sys->b_delayed )
419 TsPushCmd( p_sys->p_ts, &cmd );
421 i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
423 vlc_mutex_unlock( &p_sys->lock );
427 static void Del( es_out_t *p_out, es_out_id_t *p_es )
429 es_out_sys_t *p_sys = p_out->p_sys;
432 vlc_mutex_lock( &p_sys->lock );
436 CmdInitDel( &cmd, p_es );
437 if( p_sys->b_delayed )
438 TsPushCmd( p_sys->p_ts, &cmd );
440 CmdExecuteDel( p_sys->p_out, &cmd );
442 TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
444 vlc_mutex_unlock( &p_sys->lock );
447 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
449 es_out_sys_t *p_sys = p_out->p_sys;
451 if( p_sys->b_delayed && TsHasCmd( p_sys->p_ts ) )
454 *pb_empty = es_out_GetEmpty( p_sys->p_out );
458 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
460 es_out_sys_t *p_sys = p_out->p_sys;
462 if( p_sys->b_delayed )
464 assert( !p_sys->p_input->p->b_can_pace_control );
469 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
474 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
476 es_out_sys_t *p_sys = p_out->p_sys;
478 if( p_sys->b_delayed )
479 *pb_buffering = true;
481 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
485 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
487 es_out_sys_t *p_sys = p_out->p_sys;
490 if( !p_sys->b_delayed && !b_source_paused == !b_paused )
492 i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
496 i_ret = VLC_EGENERIC;
497 if( !p_sys->p_input->p->b_can_pace_control )
499 if( !p_sys->b_delayed )
501 if( p_sys->b_delayed )
502 i_ret = TsChangePause( p_sys->p_ts, b_source_paused, b_paused, i_date );
506 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
507 * and so be able to advertize correctly pace control property in access
509 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
515 p_sys->b_input_paused_source = b_source_paused;
516 p_sys->b_input_paused = b_paused;
520 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
522 es_out_sys_t *p_sys = p_out->p_sys;
525 if( !p_sys->b_delayed && i_src_rate == i_rate )
527 i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
531 i_ret = VLC_EGENERIC;
532 if( !p_sys->p_input->p->b_can_pace_control )
534 if( !p_sys->b_delayed )
536 if( p_sys->b_delayed )
537 i_ret = TsChangeRate( p_sys->p_ts, i_src_rate, i_rate );
541 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
542 * and so be able to advertize correctly pace control property in access
544 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
551 p_sys->i_input_rate_source = i_src_rate;
552 p_sys->i_input_rate = i_rate;
556 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
558 es_out_sys_t *p_sys = p_out->p_sys;
560 if( !p_sys->b_delayed )
561 return es_out_SetTime( p_sys->p_out, i_date );
564 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
567 static int ControlLockedSetFrameNext( es_out_t *p_out )
569 es_out_sys_t *p_sys = p_out->p_sys;
571 return es_out_SetFrameNext( p_sys->p_out );
574 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
576 es_out_sys_t *p_sys = p_out->p_sys;
580 /* Invalid query for this es_out level */
581 case ES_OUT_SET_ES_BY_ID:
582 case ES_OUT_RESTART_ES_BY_ID:
583 case ES_OUT_SET_ES_DEFAULT_BY_ID:
584 case ES_OUT_GET_ES_OBJECTS_BY_ID:
585 case ES_OUT_SET_DELAY:
586 case ES_OUT_SET_RECORD_STATE:
590 /* Pass-through control */
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_SET_ES_SCRAMBLED_STATE:
600 case ES_OUT_DEL_GROUP:
601 case ES_OUT_SET_META:
603 case ES_OUT_RESTART_ES:
604 case ES_OUT_SET_ES_DEFAULT:
605 case ES_OUT_SET_ES_STATE:
606 case ES_OUT_SET_ES_FMT:
607 case ES_OUT_SET_TIMES:
608 case ES_OUT_SET_JITTER:
612 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
614 if( p_sys->b_delayed )
616 TsPushCmd( p_sys->p_ts, &cmd );
619 return CmdExecuteControl( p_sys->p_out, &cmd );
622 /* Special control when delayed */
623 case ES_OUT_GET_ES_STATE:
625 es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
626 bool *pb_enabled = (bool*)va_arg( args, bool* );
628 if( p_sys->b_delayed )
633 return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
635 /* Special internal input control */
636 case ES_OUT_GET_EMPTY:
638 bool *pb_empty = (bool*)va_arg( args, bool* );
639 return ControlLockedGetEmpty( p_out, pb_empty );
641 case ES_OUT_GET_WAKE_UP: /* TODO ? */
643 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
644 return ControlLockedGetWakeup( p_out, pi_wakeup );
646 case ES_OUT_GET_BUFFERING:
648 bool *pb_buffering = (bool *)va_arg( args, bool* );
649 return ControlLockedGetBuffering( p_out, pb_buffering );
651 case ES_OUT_SET_PAUSE_STATE:
653 const bool b_source_paused = (bool)va_arg( args, int );
654 const bool b_paused = (bool)va_arg( args, int );
655 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
657 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
659 case ES_OUT_SET_RATE:
661 const int i_src_rate = (int)va_arg( args, int );
662 const int i_rate = (int)va_arg( args, int );
664 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
666 case ES_OUT_SET_TIME:
668 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
670 return ControlLockedSetTime( p_out, i_date );
672 case ES_OUT_SET_FRAME_NEXT:
674 return ControlLockedSetFrameNext( p_out );
676 case ES_OUT_GET_PCR_SYSTEM:
678 if( p_sys->b_delayed )
681 mtime_t *pi_system = (mtime_t*)va_arg( args, mtime_t * );
682 mtime_t *pi_delay = (mtime_t*)va_arg( args, mtime_t * );
683 return es_out_ControlGetPcrSystem( p_sys->p_out, pi_system, pi_delay );
685 case ES_OUT_MODIFY_PCR_SYSTEM:
687 const bool b_absolute = va_arg( args, int );
688 const mtime_t i_system = va_arg( args, mtime_t );
690 if( b_absolute && p_sys->b_delayed )
693 return es_out_ControlModifyPcrSystem( p_sys->p_out, b_absolute, i_system );
695 case ES_OUT_GET_GROUP_FORCED:
697 int *pi_group = va_arg( args, int * );
698 return es_out_Control( p_sys->p_out, ES_OUT_GET_GROUP_FORCED, pi_group );
703 msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
708 static int Control( es_out_t *p_out, int i_query, va_list args )
710 es_out_sys_t *p_sys = p_out->p_sys;
713 vlc_mutex_lock( &p_sys->lock );
717 i_ret = ControlLocked( p_out, i_query, args );
719 vlc_mutex_unlock( &p_sys->lock );
724 /*****************************************************************************
726 *****************************************************************************/
727 static void TsDestroy( ts_thread_t *p_ts )
729 vlc_cond_destroy( &p_ts->wait );
730 vlc_mutex_destroy( &p_ts->lock );
733 static int TsStart( es_out_t *p_out )
735 es_out_sys_t *p_sys = p_out->p_sys;
738 assert( !p_sys->b_delayed );
740 p_sys->p_ts = p_ts = calloc(1, sizeof(*p_ts));
744 p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
745 p_ts->psz_tmp_path = p_sys->psz_tmp_path;
746 p_ts->p_input = p_sys->p_input;
747 p_ts->p_out = p_sys->p_out;
748 vlc_mutex_init( &p_ts->lock );
749 vlc_cond_init( &p_ts->wait );
750 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
751 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
752 p_ts->i_rate_source = p_sys->i_input_rate_source;
753 p_ts->i_rate = p_sys->i_input_rate;
754 p_ts->i_rate_date = -1;
755 p_ts->i_rate_delay = 0;
756 p_ts->i_buffering_delay = 0;
757 p_ts->i_cmd_delay = 0;
758 p_ts->p_storage_r = NULL;
759 p_ts->p_storage_w = NULL;
761 p_sys->b_delayed = true;
762 if( vlc_clone( &p_ts->thread, TsRun, p_ts, VLC_THREAD_PRIORITY_INPUT ) )
764 msg_Err( p_sys->p_input, "cannot create timeshift thread" );
768 p_sys->b_delayed = false;
774 static void TsAutoStop( es_out_t *p_out )
776 es_out_sys_t *p_sys = p_out->p_sys;
778 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_ts ) )
781 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
782 TsStop( p_sys->p_ts );
784 p_sys->b_delayed = false;
786 static void TsStop( ts_thread_t *p_ts )
788 vlc_cancel( p_ts->thread );
789 vlc_join( p_ts->thread, NULL );
791 vlc_mutex_lock( &p_ts->lock );
796 if( TsPopCmdLocked( p_ts, &cmd, true ) )
801 assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
802 if( p_ts->p_storage_r )
803 TsStorageDelete( p_ts->p_storage_r );
804 vlc_mutex_unlock( &p_ts->lock );
808 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
810 vlc_mutex_lock( &p_ts->lock );
812 if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
814 ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
819 vlc_mutex_unlock( &p_ts->lock );
820 /* TODO warn the user (but only once) */
824 if( !p_ts->p_storage_w )
826 p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
830 TsStoragePack( p_ts->p_storage_w );
831 p_ts->p_storage_w->p_next = p_storage;
832 p_ts->p_storage_w = p_storage;
836 /* TODO return error and warn the user (but only once) */
837 TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
839 vlc_cond_signal( &p_ts->wait );
841 vlc_mutex_unlock( &p_ts->lock );
843 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
845 vlc_assert_locked( &p_ts->lock );
847 if( TsStorageIsEmpty( p_ts->p_storage_r ) )
850 TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
852 while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
854 ts_storage_t *p_next = p_ts->p_storage_r->p_next;
858 TsStorageDelete( p_ts->p_storage_r );
859 p_ts->p_storage_r = p_next;
864 static bool TsHasCmd( ts_thread_t *p_ts )
868 vlc_mutex_lock( &p_ts->lock );
869 b_cmd = TsStorageIsEmpty( p_ts->p_storage_r );
870 vlc_mutex_unlock( &p_ts->lock );
874 static bool TsIsUnused( ts_thread_t *p_ts )
878 vlc_mutex_lock( &p_ts->lock );
879 b_unused = !p_ts->b_paused &&
880 p_ts->i_rate == p_ts->i_rate_source &&
881 TsStorageIsEmpty( p_ts->p_storage_r );
882 vlc_mutex_unlock( &p_ts->lock );
886 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
888 vlc_mutex_lock( &p_ts->lock );
893 assert( !b_source_paused );
894 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
898 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
905 assert( p_ts->i_pause_date > 0 );
907 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
910 p_ts->b_paused = b_paused;
911 p_ts->i_pause_date = i_date;
913 vlc_cond_signal( &p_ts->wait );
915 vlc_mutex_unlock( &p_ts->lock );
918 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
922 vlc_mutex_lock( &p_ts->lock );
923 p_ts->i_cmd_delay += p_ts->i_rate_delay;
925 p_ts->i_rate_date = -1;
926 p_ts->i_rate_delay = 0;
927 p_ts->i_rate = i_rate;
928 p_ts->i_rate_source = i_src_rate;
930 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
931 vlc_mutex_unlock( &p_ts->lock );
936 static void *TsRun( void *p_data )
938 ts_thread_t *p_ts = p_data;
939 mtime_t i_buffering_date = -1;
947 /* Pop a command to execute */
948 vlc_mutex_lock( &p_ts->lock );
949 mutex_cleanup_push( &p_ts->lock );
953 const int canc = vlc_savecancel();
954 b_buffering = es_out_GetBuffering( p_ts->p_out );
956 if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
958 vlc_restorecancel( canc );
961 vlc_restorecancel( canc );
963 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
966 if( b_buffering && i_buffering_date < 0 )
968 i_buffering_date = cmd.i_date;
970 else if( i_buffering_date > 0 )
972 p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
974 i_buffering_date = cmd.i_date;
976 i_buffering_date = -1;
979 if( p_ts->i_rate_date < 0 )
980 p_ts->i_rate_date = cmd.i_date;
982 p_ts->i_rate_delay = 0;
983 if( p_ts->i_rate_source != p_ts->i_rate )
985 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
986 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
988 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 )
990 const int canc = vlc_savecancel();
992 /* Auto reset to rate 1.0 */
993 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
995 p_ts->i_cmd_delay = 0;
996 p_ts->i_buffering_delay = 0;
998 p_ts->i_rate_delay = 0;
999 p_ts->i_rate_date = -1;
1000 p_ts->i_rate = p_ts->i_rate_source;
1002 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
1004 vlc_value_t val = { .i_int = p_ts->i_rate };
1006 * FIXME it is perfectly safe BUT it is ugly as it may hide a
1007 * rate change requested by user */
1008 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
1011 vlc_restorecancel( canc );
1013 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
1017 /* Regulate the speed of command processing to the same one than
1019 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
1021 mwait( i_deadline );
1025 /* Execute the command */
1026 const int canc = vlc_savecancel();
1027 switch( cmd.i_type )
1030 CmdExecuteAdd( p_ts->p_out, &cmd );
1031 CmdCleanAdd( &cmd );
1034 CmdExecuteSend( p_ts->p_out, &cmd );
1035 CmdCleanSend( &cmd );
1038 CmdExecuteControl( p_ts->p_out, &cmd );
1039 CmdCleanControl( &cmd );
1042 CmdExecuteDel( p_ts->p_out, &cmd );
1048 vlc_restorecancel( canc );
1054 /*****************************************************************************
1056 *****************************************************************************/
1057 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1059 ts_storage_t *p_storage = calloc( 1, sizeof(ts_storage_t) );
1064 p_storage->p_next = NULL;
1067 p_storage->i_file_max = i_tmp_size_max;
1068 p_storage->i_file_size = 0;
1069 p_storage->p_filew = GetTmpFile( &p_storage->psz_file, psz_tmp_path );
1070 if( p_storage->psz_file )
1071 p_storage->p_filer = vlc_fopen( p_storage->psz_file, "rb" );
1074 p_storage->i_cmd_w = 0;
1075 p_storage->i_cmd_r = 0;
1076 p_storage->i_cmd_max = 30000;
1077 p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1078 //fprintf( stderr, "\nSTORAGE name=%s size=%d KiB\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1080 if( !p_storage->p_cmd || !p_storage->p_filew || !p_storage->p_filer )
1082 TsStorageDelete( p_storage );
1087 static void TsStorageDelete( ts_storage_t *p_storage )
1089 while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1093 TsStoragePopCmd( p_storage, &cmd, true );
1097 free( p_storage->p_cmd );
1099 if( p_storage->p_filer )
1100 fclose( p_storage->p_filer );
1101 if( p_storage->p_filew )
1102 fclose( p_storage->p_filew );
1104 if( p_storage->psz_file )
1106 vlc_unlink( p_storage->psz_file );
1107 free( p_storage->psz_file );
1112 static void TsStoragePack( ts_storage_t *p_storage )
1114 /* Try to release a bit of memory */
1115 if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1118 p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1120 ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1122 p_storage->p_cmd = p_new;
1124 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1126 if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1128 size_t i_size = sizeof(*p_cmd->u.send.p_block) + p_cmd->u.send.p_block->i_buffer;
1130 if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1133 return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1135 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1137 return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1139 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1141 ts_cmd_t cmd = *p_cmd;
1143 assert( !TsStorageIsFull( p_storage, p_cmd ) );
1145 if( cmd.i_type == C_SEND )
1147 block_t *p_block = cmd.u.send.p_block;
1149 cmd.u.send.p_block = NULL;
1150 cmd.u.send.i_offset = ftell( p_storage->p_filew );
1152 if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1154 block_Release( p_block );
1157 p_storage->i_file_size += sizeof(*p_block);
1158 if( p_block->i_buffer > 0 )
1160 if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1162 block_Release( p_block );
1166 p_storage->i_file_size += p_block->i_buffer;
1167 block_Release( p_block );
1170 fflush( p_storage->p_filew );
1172 p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1174 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1176 assert( !TsStorageIsEmpty( p_storage ) );
1178 *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1179 if( p_cmd->i_type == C_SEND )
1184 !fseek( p_storage->p_filer, p_cmd->u.send.i_offset, SEEK_SET ) &&
1185 fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1187 block_t *p_block = block_Alloc( block.i_buffer );
1190 p_block->i_dts = block.i_dts;
1191 p_block->i_pts = block.i_pts;
1192 p_block->i_flags = block.i_flags;
1193 p_block->i_length = block.i_length;
1194 p_block->i_nb_samples = block.i_nb_samples;
1195 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1197 p_cmd->u.send.p_block = p_block;
1201 //fprintf( stderr, "TsStoragePopCmd: %m\n" );
1202 p_cmd->u.send.p_block = block_Alloc( 1 );
1207 /*****************************************************************************
1209 *****************************************************************************/
1210 static void CmdClean( ts_cmd_t *p_cmd )
1212 switch( p_cmd->i_type )
1215 CmdCleanAdd( p_cmd );
1218 CmdCleanSend( p_cmd );
1221 CmdCleanControl( p_cmd );
1231 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1233 p_cmd->i_type = C_ADD;
1234 p_cmd->i_date = mdate();
1235 p_cmd->u.add.p_es = p_es;
1238 p_cmd->u.add.p_fmt = malloc( sizeof(*p_fmt) );
1239 if( !p_cmd->u.add.p_fmt )
1240 return VLC_EGENERIC;
1241 es_format_Copy( p_cmd->u.add.p_fmt, p_fmt );
1245 p_cmd->u.add.p_fmt = (es_format_t*)p_fmt;
1249 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1251 p_cmd->u.add.p_es->p_es = es_out_Add( p_out, p_cmd->u.add.p_fmt );
1253 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1255 es_format_Clean( p_cmd->u.add.p_fmt );
1256 free( p_cmd->u.add.p_fmt );
1259 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1261 p_cmd->i_type = C_SEND;
1262 p_cmd->i_date = mdate();
1263 p_cmd->u.send.p_es = p_es;
1264 p_cmd->u.send.p_block = p_block;
1266 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1268 block_t *p_block = p_cmd->u.send.p_block;
1270 p_cmd->u.send.p_block = NULL;
1274 if( p_cmd->u.send.p_es->p_es )
1275 return es_out_Send( p_out, p_cmd->u.send.p_es->p_es, p_block );
1276 block_Release( p_block );
1278 return VLC_EGENERIC;
1280 static void CmdCleanSend( ts_cmd_t *p_cmd )
1282 if( p_cmd->u.send.p_block )
1283 block_Release( p_cmd->u.send.p_block );
1286 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1288 p_cmd->i_type = C_DEL;
1289 p_cmd->i_date = mdate();
1290 p_cmd->u.del.p_es = p_es;
1293 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1295 if( p_cmd->u.del.p_es->p_es )
1296 es_out_Del( p_out, p_cmd->u.del.p_es->p_es );
1297 free( p_cmd->u.del.p_es );
1300 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1302 p_cmd->i_type = C_CONTROL;
1303 p_cmd->i_date = mdate();
1304 p_cmd->u.control.i_query = i_query;
1308 /* Pass-through control */
1309 case ES_OUT_SET_MODE: /* arg1= int */
1310 case ES_OUT_SET_GROUP: /* arg1= int */
1311 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1312 p_cmd->u.control.u.i_int = (int)va_arg( args, int );
1315 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1316 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1317 p_cmd->u.control.u.i_i64 = (int64_t)va_arg( args, int64_t );
1320 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1321 p_cmd->u.control.u.int_i64.i_int = (int)va_arg( args, int );
1322 p_cmd->u.control.u.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1325 case ES_OUT_SET_ES_SCRAMBLED_STATE:
1326 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1327 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1330 case ES_OUT_RESET_PCR: /* no arg */
1331 case ES_OUT_SET_EOS:
1334 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1335 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1337 if( i_query == ES_OUT_SET_GROUP_META )
1338 p_cmd->u.control.u.int_meta.i_int = (int)va_arg( args, int );
1339 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
1343 p_cmd->u.control.u.int_meta.p_meta = vlc_meta_New();
1344 if( !p_cmd->u.control.u.int_meta.p_meta )
1345 return VLC_EGENERIC;
1346 vlc_meta_Merge( p_cmd->u.control.u.int_meta.p_meta, p_meta );
1350 /* The cast is only needed to avoid warning */
1351 p_cmd->u.control.u.int_meta.p_meta = (vlc_meta_t*)p_meta;
1356 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1358 p_cmd->u.control.u.int_epg.i_int = (int)va_arg( args, int );
1359 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
1363 p_cmd->u.control.u.int_epg.p_epg = vlc_epg_New( p_epg->psz_name );
1364 if( !p_cmd->u.control.u.int_epg.p_epg )
1365 return VLC_EGENERIC;
1366 for( int i = 0; i < p_epg->i_event; i++ )
1368 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1370 vlc_epg_AddEvent( p_cmd->u.control.u.int_epg.p_epg,
1371 p_evt->i_start, p_evt->i_duration,
1373 p_evt->psz_short_description, p_evt->psz_description );
1375 vlc_epg_SetCurrent( p_cmd->u.control.u.int_epg.p_epg,
1376 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1380 /* The cast is only needed to avoid warning */
1381 p_cmd->u.control.u.int_epg.p_epg = (vlc_epg_t*)p_epg;
1386 /* Modified control */
1387 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1388 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1389 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1390 p_cmd->u.control.u.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1393 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1394 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1395 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1398 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1400 p_cmd->u.control.u.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1401 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1405 p_cmd->u.control.u.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1406 if( !p_cmd->u.control.u.es_fmt.p_fmt )
1407 return VLC_EGENERIC;
1408 es_format_Copy( p_cmd->u.control.u.es_fmt.p_fmt, p_fmt );
1412 p_cmd->u.control.u.es_fmt.p_fmt = p_fmt;
1416 case ES_OUT_SET_TIMES:
1418 double f_position = (double)va_arg( args, double );
1419 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
1420 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
1422 p_cmd->u.control.u.times.f_position = f_position;
1423 p_cmd->u.control.u.times.i_time = i_time;
1424 p_cmd->u.control.u.times.i_length = i_length;
1427 case ES_OUT_SET_JITTER:
1429 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
1430 mtime_t i_pts_jitter = (mtime_t)va_arg( args, mtime_t );
1431 int i_cr_average = (int)va_arg( args, int );
1433 p_cmd->u.control.u.jitter.i_pts_delay = i_pts_delay;
1434 p_cmd->u.control.u.jitter.i_pts_jitter = i_pts_jitter;
1435 p_cmd->u.control.u.jitter.i_cr_average = i_cr_average;
1441 return VLC_EGENERIC;
1446 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1448 const int i_query = p_cmd->u.control.i_query;
1452 /* Pass-through control */
1453 case ES_OUT_SET_MODE: /* arg1= int */
1454 case ES_OUT_SET_GROUP: /* arg1= int */
1455 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1456 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_int );
1458 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1459 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1460 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_i64 );
1462 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1463 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_i64.i_int,
1464 p_cmd->u.control.u.int_i64.i_i64 );
1466 case ES_OUT_RESET_PCR: /* no arg */
1467 case ES_OUT_SET_EOS:
1468 return es_out_Control( p_out, i_query );
1470 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1471 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.i_int,
1472 p_cmd->u.control.u.int_meta.p_meta );
1474 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1475 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_epg.i_int,
1476 p_cmd->u.control.u.int_epg.p_epg );
1478 case ES_OUT_SET_ES_SCRAMBLED_STATE: /* arg1=int es_out_id_t* arg2=bool */
1479 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1480 p_cmd->u.control.u.es_bool.b_bool );
1482 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1483 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.p_meta );
1485 /* Modified control */
1486 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1487 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1488 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1489 return es_out_Control( p_out, i_query, p_cmd->u.control.u.p_es->p_es );
1491 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1492 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1493 p_cmd->u.control.u.es_bool.b_bool );
1495 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1496 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_fmt.p_es->p_es,
1497 p_cmd->u.control.u.es_fmt.p_fmt );
1499 case ES_OUT_SET_TIMES:
1500 return es_out_Control( p_out, i_query, p_cmd->u.control.u.times.f_position,
1501 p_cmd->u.control.u.times.i_time,
1502 p_cmd->u.control.u.times.i_length );
1503 case ES_OUT_SET_JITTER:
1504 return es_out_Control( p_out, i_query, p_cmd->u.control.u.jitter.i_pts_delay,
1505 p_cmd->u.control.u.jitter.i_pts_jitter,
1506 p_cmd->u.control.u.jitter.i_cr_average );
1510 return VLC_EGENERIC;
1513 static void CmdCleanControl( ts_cmd_t *p_cmd )
1515 if( ( p_cmd->u.control.i_query == ES_OUT_SET_GROUP_META ||
1516 p_cmd->u.control.i_query == ES_OUT_SET_META ) &&
1517 p_cmd->u.control.u.int_meta.p_meta )
1519 vlc_meta_Delete( p_cmd->u.control.u.int_meta.p_meta );
1521 else if( p_cmd->u.control.i_query == ES_OUT_SET_GROUP_EPG &&
1522 p_cmd->u.control.u.int_epg.p_epg )
1524 vlc_epg_Delete( p_cmd->u.control.u.int_epg.p_epg );
1526 else if( p_cmd->u.control.i_query == ES_OUT_SET_ES_FMT &&
1527 p_cmd->u.control.u.es_fmt.p_fmt )
1529 es_format_Clean( p_cmd->u.control.u.es_fmt.p_fmt );
1530 free( p_cmd->u.control.u.es_fmt.p_fmt );
1535 /*****************************************************************************
1537 *****************************************************************************/
1538 static char *GetTmpPath( char *psz_path )
1540 if( psz_path && *psz_path )
1542 /* Make sure that the path exists and is a directory */
1544 const int i_ret = vlc_stat( psz_path, &s );
1546 if( i_ret < 0 && !vlc_mkdir( psz_path, 0600 ) )
1548 else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1553 /* Create a suitable path */
1554 #if defined (WIN32) && !defined (UNDER_CE)
1555 const DWORD dwCount = GetTempPathW( 0, NULL );
1556 wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1559 if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1563 psw_path = _wgetcwd( NULL, 0 );
1570 psz_path = FromWide( psw_path );
1571 while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1572 psz_path[strlen( psz_path ) - 1] = '\0';
1577 if( !psz_path || *psz_path == '\0' )
1580 return strdup( "C:" );
1583 psz_path = strdup( DIR_SEP"tmp" );
1589 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path )
1597 if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1601 fd = vlc_mkstemp( psz_name );
1602 *ppsz_file = psz_name;
1608 f = fdopen( fd, "w+b" );