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 it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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>
44 #include <vlc_common.h>
47 # include <vlc_charset.h>
49 #include <vlc_input.h>
50 #include <vlc_es_out.h>
51 #include <vlc_block.h>
52 #include "input_internal.h"
54 #include "es_out_timeshift.h"
56 /*****************************************************************************
58 *****************************************************************************/
60 /* XXX attribute_packed is (and MUST be) used ONLY to reduce memory usage */
61 #ifdef HAVE_ATTRIBUTE_PACKED
62 # define attribute_packed __attribute__((__packed__))
64 # define attribute_packed
75 typedef struct attribute_packed
81 typedef struct attribute_packed
86 typedef struct attribute_packed
90 int i_offset; /* We do not use file > INT_MAX */
93 typedef struct attribute_packed
130 /* FIXME Really too big (double make the whole thing too big) */
138 mtime_t i_pts_jitter;
144 typedef struct attribute_packed
153 ts_cmd_control_t control;
157 typedef struct ts_storage_t ts_storage_t;
160 ts_storage_t *p_next;
163 char *psz_file; /* Filename */
164 size_t i_file_max; /* Max size in bytes */
165 int64_t i_file_size;/* Current size in bytes */
166 FILE *p_filew; /* FILE handle for data writing */
167 FILE *p_filer; /* FILE handle for data reading */
179 input_thread_t *p_input;
181 int64_t i_tmp_size_max;
182 const char *psz_tmp_path;
184 /* Lock for all following fields */
190 mtime_t i_pause_date;
196 mtime_t i_rate_delay;
199 mtime_t i_buffering_delay;
202 ts_storage_t *p_storage_r;
203 ts_storage_t *p_storage_w;
216 input_thread_t *p_input;
220 int64_t i_tmp_size_max; /* Maximal temporary file size in byte */
221 char *psz_tmp_path; /* Path for temporary files */
223 /* Lock for all following fields */
232 bool b_input_paused_source;
234 int i_input_rate_source;
241 static es_out_id_t *Add ( es_out_t *, const es_format_t * );
242 static int Send ( es_out_t *, es_out_id_t *, block_t * );
243 static void Del ( es_out_t *, es_out_id_t * );
244 static int Control( es_out_t *, int i_query, va_list );
245 static void Destroy( es_out_t * );
247 static int TsStart( es_out_t * );
248 static void TsAutoStop( es_out_t * );
250 static void TsStop( ts_thread_t * );
251 static void TsPushCmd( ts_thread_t *, ts_cmd_t * );
252 static int TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
253 static bool TsHasCmd( ts_thread_t * );
254 static bool TsIsUnused( ts_thread_t * );
255 static int TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
256 static int TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
258 static void *TsRun( void * );
260 static ts_storage_t *TsStorageNew( const char *psz_path, int64_t i_tmp_size_max );
261 static void TsStorageDelete( ts_storage_t * );
262 static void TsStoragePack( ts_storage_t *p_storage );
263 static bool TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
264 static bool TsStorageIsEmpty( ts_storage_t * );
265 static void TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
266 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
268 static void CmdClean( ts_cmd_t * );
269 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
271 static int CmdInitAdd ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
272 static void CmdInitSend ( ts_cmd_t *, es_out_id_t *, block_t * );
273 static int CmdInitDel ( ts_cmd_t *, es_out_id_t * );
274 static int CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
277 static void CmdCleanAdd ( ts_cmd_t * );
278 static void CmdCleanSend ( ts_cmd_t * );
279 static void CmdCleanControl( ts_cmd_t *p_cmd );
281 /* XXX these functions will take the destination es_out_t */
282 static void CmdExecuteAdd ( es_out_t *, ts_cmd_t * );
283 static int CmdExecuteSend ( es_out_t *, ts_cmd_t * );
284 static void CmdExecuteDel ( es_out_t *, ts_cmd_t * );
285 static int CmdExecuteControl( es_out_t *, ts_cmd_t * );
288 static char *GetTmpPath( char *psz_path );
289 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path );
291 /*****************************************************************************
292 * input_EsOutTimeshiftNew:
293 *****************************************************************************/
294 es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
296 es_out_t *p_out = malloc( sizeof(*p_out) );
300 es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
309 p_out->pf_send = Send;
311 p_out->pf_control = Control;
312 p_out->pf_destroy = Destroy;
313 p_out->p_sys = p_sys;
316 p_sys->b_input_paused = false;
317 p_sys->b_input_paused_source = false;
318 p_sys->p_input = p_input;
319 p_sys->i_input_rate = i_rate;
320 p_sys->i_input_rate_source = i_rate;
322 p_sys->p_out = p_next_out;
323 vlc_mutex_init_recursive( &p_sys->lock );
325 p_sys->b_delayed = false;
328 TAB_INIT( p_sys->i_es, p_sys->pp_es );
331 const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
332 if( i_tmp_size_max < 0 )
333 p_sys->i_tmp_size_max = 50*1024*1024;
335 p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
337 char *psz_tmp_path = var_CreateGetNonEmptyString( p_input, "input-timeshift-path" );
338 p_sys->psz_tmp_path = GetTmpPath( psz_tmp_path );
340 msg_Dbg( p_input, "using timeshift granularity of %d MiB, in path '%s'",
341 (int)p_sys->i_tmp_size_max/(1024*1024), p_sys->psz_tmp_path );
344 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
356 /*****************************************************************************
358 *****************************************************************************/
359 static void Destroy( es_out_t *p_out )
361 es_out_sys_t *p_sys = p_out->p_sys;
363 if( p_sys->b_delayed )
365 TsStop( p_sys->p_ts );
366 p_sys->b_delayed = false;
369 while( p_sys->i_es > 0 )
370 Del( p_out, p_sys->pp_es[0] );
371 TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
373 free( p_sys->psz_tmp_path );
374 vlc_mutex_destroy( &p_sys->lock );
379 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
381 es_out_sys_t *p_sys = p_out->p_sys;
384 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
388 vlc_mutex_lock( &p_sys->lock );
392 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
394 vlc_mutex_unlock( &p_sys->lock );
399 TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
401 if( p_sys->b_delayed )
402 TsPushCmd( p_sys->p_ts, &cmd );
404 CmdExecuteAdd( p_sys->p_out, &cmd );
406 vlc_mutex_unlock( &p_sys->lock );
410 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
412 es_out_sys_t *p_sys = p_out->p_sys;
414 int i_ret = VLC_SUCCESS;
416 vlc_mutex_lock( &p_sys->lock );
420 CmdInitSend( &cmd, p_es, p_block );
421 if( p_sys->b_delayed )
422 TsPushCmd( p_sys->p_ts, &cmd );
424 i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
426 vlc_mutex_unlock( &p_sys->lock );
430 static void Del( es_out_t *p_out, es_out_id_t *p_es )
432 es_out_sys_t *p_sys = p_out->p_sys;
435 vlc_mutex_lock( &p_sys->lock );
439 CmdInitDel( &cmd, p_es );
440 if( p_sys->b_delayed )
441 TsPushCmd( p_sys->p_ts, &cmd );
443 CmdExecuteDel( p_sys->p_out, &cmd );
445 TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
447 vlc_mutex_unlock( &p_sys->lock );
450 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
452 es_out_sys_t *p_sys = p_out->p_sys;
454 if( p_sys->b_delayed && TsHasCmd( p_sys->p_ts ) )
457 *pb_empty = es_out_GetEmpty( p_sys->p_out );
461 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
463 es_out_sys_t *p_sys = p_out->p_sys;
465 if( p_sys->b_delayed )
467 assert( !p_sys->p_input->p->b_can_pace_control );
472 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
477 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
479 es_out_sys_t *p_sys = p_out->p_sys;
481 if( p_sys->b_delayed )
482 *pb_buffering = true;
484 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
488 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
490 es_out_sys_t *p_sys = p_out->p_sys;
493 if( !p_sys->b_delayed && !b_source_paused == !b_paused )
495 i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
499 i_ret = VLC_EGENERIC;
500 if( !p_sys->p_input->p->b_can_pace_control )
502 if( !p_sys->b_delayed )
504 if( p_sys->b_delayed )
505 i_ret = TsChangePause( p_sys->p_ts, b_source_paused, b_paused, i_date );
509 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
510 * and so be able to advertize correctly pace control property in access
512 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
518 p_sys->b_input_paused_source = b_source_paused;
519 p_sys->b_input_paused = b_paused;
523 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
525 es_out_sys_t *p_sys = p_out->p_sys;
528 if( !p_sys->b_delayed && i_src_rate == i_rate )
530 i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
534 i_ret = VLC_EGENERIC;
535 if( !p_sys->p_input->p->b_can_pace_control )
537 if( !p_sys->b_delayed )
539 if( p_sys->b_delayed )
540 i_ret = TsChangeRate( p_sys->p_ts, i_src_rate, i_rate );
544 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
545 * and so be able to advertize correctly pace control property in access
547 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
554 p_sys->i_input_rate_source = i_src_rate;
555 p_sys->i_input_rate = i_rate;
559 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
561 es_out_sys_t *p_sys = p_out->p_sys;
563 if( !p_sys->b_delayed )
564 return es_out_SetTime( p_sys->p_out, i_date );
567 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
570 static int ControlLockedSetFrameNext( es_out_t *p_out )
572 es_out_sys_t *p_sys = p_out->p_sys;
574 return es_out_SetFrameNext( p_sys->p_out );
577 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
579 es_out_sys_t *p_sys = p_out->p_sys;
583 /* Invalid query for this es_out level */
584 case ES_OUT_SET_ES_BY_ID:
585 case ES_OUT_RESTART_ES_BY_ID:
586 case ES_OUT_SET_ES_DEFAULT_BY_ID:
587 case ES_OUT_GET_ES_OBJECTS_BY_ID:
588 case ES_OUT_SET_DELAY:
589 case ES_OUT_SET_RECORD_STATE:
593 /* Pass-through control */
594 case ES_OUT_SET_MODE:
595 case ES_OUT_SET_GROUP:
597 case ES_OUT_SET_GROUP_PCR:
598 case ES_OUT_RESET_PCR:
599 case ES_OUT_SET_NEXT_DISPLAY_TIME:
600 case ES_OUT_SET_GROUP_META:
601 case ES_OUT_SET_GROUP_EPG:
602 case ES_OUT_SET_ES_SCRAMBLED_STATE:
603 case ES_OUT_DEL_GROUP:
604 case ES_OUT_SET_META:
606 case ES_OUT_RESTART_ES:
607 case ES_OUT_SET_ES_DEFAULT:
608 case ES_OUT_SET_ES_STATE:
609 case ES_OUT_SET_ES_FMT:
610 case ES_OUT_SET_TIMES:
611 case ES_OUT_SET_JITTER:
615 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
617 if( p_sys->b_delayed )
619 TsPushCmd( p_sys->p_ts, &cmd );
622 return CmdExecuteControl( p_sys->p_out, &cmd );
625 /* Special control when delayed */
626 case ES_OUT_GET_ES_STATE:
628 es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
629 bool *pb_enabled = (bool*)va_arg( args, bool* );
631 if( p_sys->b_delayed )
636 return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
638 /* Special internal input control */
639 case ES_OUT_GET_EMPTY:
641 bool *pb_empty = (bool*)va_arg( args, bool* );
642 return ControlLockedGetEmpty( p_out, pb_empty );
644 case ES_OUT_GET_WAKE_UP: /* TODO ? */
646 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
647 return ControlLockedGetWakeup( p_out, pi_wakeup );
649 case ES_OUT_GET_BUFFERING:
651 bool *pb_buffering = (bool *)va_arg( args, bool* );
652 return ControlLockedGetBuffering( p_out, pb_buffering );
654 case ES_OUT_SET_PAUSE_STATE:
656 const bool b_source_paused = (bool)va_arg( args, int );
657 const bool b_paused = (bool)va_arg( args, int );
658 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
660 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
662 case ES_OUT_SET_RATE:
664 const int i_src_rate = (int)va_arg( args, int );
665 const int i_rate = (int)va_arg( args, int );
667 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
669 case ES_OUT_SET_TIME:
671 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
673 return ControlLockedSetTime( p_out, i_date );
675 case ES_OUT_SET_FRAME_NEXT:
677 return ControlLockedSetFrameNext( p_out );
679 case ES_OUT_GET_PCR_SYSTEM:
681 if( p_sys->b_delayed )
684 mtime_t *pi_system = (mtime_t*)va_arg( args, mtime_t * );
685 mtime_t *pi_delay = (mtime_t*)va_arg( args, mtime_t * );
686 return es_out_ControlGetPcrSystem( p_sys->p_out, pi_system, pi_delay );
688 case ES_OUT_MODIFY_PCR_SYSTEM:
690 const bool b_absolute = va_arg( args, int );
691 const mtime_t i_system = va_arg( args, mtime_t );
693 if( b_absolute && p_sys->b_delayed )
696 return es_out_ControlModifyPcrSystem( p_sys->p_out, b_absolute, i_system );
698 case ES_OUT_GET_GROUP_FORCED:
700 int *pi_group = va_arg( args, int * );
701 return es_out_Control( p_sys->p_out, ES_OUT_GET_GROUP_FORCED, pi_group );
706 msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
711 static int Control( es_out_t *p_out, int i_query, va_list args )
713 es_out_sys_t *p_sys = p_out->p_sys;
716 vlc_mutex_lock( &p_sys->lock );
720 i_ret = ControlLocked( p_out, i_query, args );
722 vlc_mutex_unlock( &p_sys->lock );
727 /*****************************************************************************
729 *****************************************************************************/
730 static void TsDestroy( ts_thread_t *p_ts )
732 vlc_cond_destroy( &p_ts->wait );
733 vlc_mutex_destroy( &p_ts->lock );
736 static int TsStart( es_out_t *p_out )
738 es_out_sys_t *p_sys = p_out->p_sys;
741 assert( !p_sys->b_delayed );
743 p_sys->p_ts = p_ts = calloc(1, sizeof(*p_ts));
747 p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
748 p_ts->psz_tmp_path = p_sys->psz_tmp_path;
749 p_ts->p_input = p_sys->p_input;
750 p_ts->p_out = p_sys->p_out;
751 vlc_mutex_init( &p_ts->lock );
752 vlc_cond_init( &p_ts->wait );
753 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
754 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
755 p_ts->i_rate_source = p_sys->i_input_rate_source;
756 p_ts->i_rate = p_sys->i_input_rate;
757 p_ts->i_rate_date = -1;
758 p_ts->i_rate_delay = 0;
759 p_ts->i_buffering_delay = 0;
760 p_ts->i_cmd_delay = 0;
761 p_ts->p_storage_r = NULL;
762 p_ts->p_storage_w = NULL;
764 p_sys->b_delayed = true;
765 if( vlc_clone( &p_ts->thread, TsRun, p_ts, VLC_THREAD_PRIORITY_INPUT ) )
767 msg_Err( p_sys->p_input, "cannot create timeshift thread" );
771 p_sys->b_delayed = false;
777 static void TsAutoStop( es_out_t *p_out )
779 es_out_sys_t *p_sys = p_out->p_sys;
781 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_ts ) )
784 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
785 TsStop( p_sys->p_ts );
787 p_sys->b_delayed = false;
789 static void TsStop( ts_thread_t *p_ts )
791 vlc_cancel( p_ts->thread );
792 vlc_join( p_ts->thread, NULL );
794 vlc_mutex_lock( &p_ts->lock );
799 if( TsPopCmdLocked( p_ts, &cmd, true ) )
804 assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
805 if( p_ts->p_storage_r )
806 TsStorageDelete( p_ts->p_storage_r );
807 vlc_mutex_unlock( &p_ts->lock );
811 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
813 vlc_mutex_lock( &p_ts->lock );
815 if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
817 ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
822 vlc_mutex_unlock( &p_ts->lock );
823 /* TODO warn the user (but only once) */
827 if( !p_ts->p_storage_w )
829 p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
833 TsStoragePack( p_ts->p_storage_w );
834 p_ts->p_storage_w->p_next = p_storage;
835 p_ts->p_storage_w = p_storage;
839 /* TODO return error and warn the user (but only once) */
840 TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
842 vlc_cond_signal( &p_ts->wait );
844 vlc_mutex_unlock( &p_ts->lock );
846 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
848 vlc_assert_locked( &p_ts->lock );
850 if( TsStorageIsEmpty( p_ts->p_storage_r ) )
853 TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
855 while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
857 ts_storage_t *p_next = p_ts->p_storage_r->p_next;
861 TsStorageDelete( p_ts->p_storage_r );
862 p_ts->p_storage_r = p_next;
867 static bool TsHasCmd( ts_thread_t *p_ts )
871 vlc_mutex_lock( &p_ts->lock );
872 b_cmd = TsStorageIsEmpty( p_ts->p_storage_r );
873 vlc_mutex_unlock( &p_ts->lock );
877 static bool TsIsUnused( ts_thread_t *p_ts )
881 vlc_mutex_lock( &p_ts->lock );
882 b_unused = !p_ts->b_paused &&
883 p_ts->i_rate == p_ts->i_rate_source &&
884 TsStorageIsEmpty( p_ts->p_storage_r );
885 vlc_mutex_unlock( &p_ts->lock );
889 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
891 vlc_mutex_lock( &p_ts->lock );
896 assert( !b_source_paused );
897 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
901 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
908 assert( p_ts->i_pause_date > 0 );
910 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
913 p_ts->b_paused = b_paused;
914 p_ts->i_pause_date = i_date;
916 vlc_cond_signal( &p_ts->wait );
918 vlc_mutex_unlock( &p_ts->lock );
921 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
925 vlc_mutex_lock( &p_ts->lock );
926 p_ts->i_cmd_delay += p_ts->i_rate_delay;
928 p_ts->i_rate_date = -1;
929 p_ts->i_rate_delay = 0;
930 p_ts->i_rate = i_rate;
931 p_ts->i_rate_source = i_src_rate;
933 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
934 vlc_mutex_unlock( &p_ts->lock );
939 static void *TsRun( void *p_data )
941 ts_thread_t *p_ts = p_data;
942 mtime_t i_buffering_date = -1;
950 /* Pop a command to execute */
951 vlc_mutex_lock( &p_ts->lock );
952 mutex_cleanup_push( &p_ts->lock );
956 const int canc = vlc_savecancel();
957 b_buffering = es_out_GetBuffering( p_ts->p_out );
959 if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
961 vlc_restorecancel( canc );
964 vlc_restorecancel( canc );
966 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
969 if( b_buffering && i_buffering_date < 0 )
971 i_buffering_date = cmd.i_date;
973 else if( i_buffering_date > 0 )
975 p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
977 i_buffering_date = cmd.i_date;
979 i_buffering_date = -1;
982 if( p_ts->i_rate_date < 0 )
983 p_ts->i_rate_date = cmd.i_date;
985 p_ts->i_rate_delay = 0;
986 if( p_ts->i_rate_source != p_ts->i_rate )
988 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
989 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
991 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 )
993 const int canc = vlc_savecancel();
995 /* Auto reset to rate 1.0 */
996 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
998 p_ts->i_cmd_delay = 0;
999 p_ts->i_buffering_delay = 0;
1001 p_ts->i_rate_delay = 0;
1002 p_ts->i_rate_date = -1;
1003 p_ts->i_rate = p_ts->i_rate_source;
1005 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
1007 vlc_value_t val = { .i_int = p_ts->i_rate };
1009 * FIXME it is perfectly safe BUT it is ugly as it may hide a
1010 * rate change requested by user */
1011 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
1014 vlc_restorecancel( canc );
1016 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
1020 /* Regulate the speed of command processing to the same one than
1022 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
1024 mwait( i_deadline );
1028 /* Execute the command */
1029 const int canc = vlc_savecancel();
1030 switch( cmd.i_type )
1033 CmdExecuteAdd( p_ts->p_out, &cmd );
1034 CmdCleanAdd( &cmd );
1037 CmdExecuteSend( p_ts->p_out, &cmd );
1038 CmdCleanSend( &cmd );
1041 CmdExecuteControl( p_ts->p_out, &cmd );
1042 CmdCleanControl( &cmd );
1045 CmdExecuteDel( p_ts->p_out, &cmd );
1051 vlc_restorecancel( canc );
1057 /*****************************************************************************
1059 *****************************************************************************/
1060 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1062 ts_storage_t *p_storage = calloc( 1, sizeof(ts_storage_t) );
1067 p_storage->p_next = NULL;
1070 p_storage->i_file_max = i_tmp_size_max;
1071 p_storage->i_file_size = 0;
1072 p_storage->p_filew = GetTmpFile( &p_storage->psz_file, psz_tmp_path );
1073 if( p_storage->psz_file )
1074 p_storage->p_filer = vlc_fopen( p_storage->psz_file, "rb" );
1077 p_storage->i_cmd_w = 0;
1078 p_storage->i_cmd_r = 0;
1079 p_storage->i_cmd_max = 30000;
1080 p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1081 //fprintf( stderr, "\nSTORAGE name=%s size=%d KiB\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1083 if( !p_storage->p_cmd || !p_storage->p_filew || !p_storage->p_filer )
1085 TsStorageDelete( p_storage );
1090 static void TsStorageDelete( ts_storage_t *p_storage )
1092 while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1096 TsStoragePopCmd( p_storage, &cmd, true );
1100 free( p_storage->p_cmd );
1102 if( p_storage->p_filer )
1103 fclose( p_storage->p_filer );
1104 if( p_storage->p_filew )
1105 fclose( p_storage->p_filew );
1107 if( p_storage->psz_file )
1109 vlc_unlink( p_storage->psz_file );
1110 free( p_storage->psz_file );
1115 static void TsStoragePack( ts_storage_t *p_storage )
1117 /* Try to release a bit of memory */
1118 if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1121 p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1123 ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1125 p_storage->p_cmd = p_new;
1127 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1129 if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1131 size_t i_size = sizeof(*p_cmd->u.send.p_block) + p_cmd->u.send.p_block->i_buffer;
1133 if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1136 return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1138 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1140 return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1142 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1144 ts_cmd_t cmd = *p_cmd;
1146 assert( !TsStorageIsFull( p_storage, p_cmd ) );
1148 if( cmd.i_type == C_SEND )
1150 block_t *p_block = cmd.u.send.p_block;
1152 cmd.u.send.p_block = NULL;
1153 cmd.u.send.i_offset = ftell( p_storage->p_filew );
1155 if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1157 block_Release( p_block );
1160 p_storage->i_file_size += sizeof(*p_block);
1161 if( p_block->i_buffer > 0 )
1163 if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1165 block_Release( p_block );
1169 p_storage->i_file_size += p_block->i_buffer;
1170 block_Release( p_block );
1173 fflush( p_storage->p_filew );
1175 p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1177 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1179 assert( !TsStorageIsEmpty( p_storage ) );
1181 *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1182 if( p_cmd->i_type == C_SEND )
1187 !fseek( p_storage->p_filer, p_cmd->u.send.i_offset, SEEK_SET ) &&
1188 fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1190 block_t *p_block = block_Alloc( block.i_buffer );
1193 p_block->i_dts = block.i_dts;
1194 p_block->i_pts = block.i_pts;
1195 p_block->i_flags = block.i_flags;
1196 p_block->i_length = block.i_length;
1197 p_block->i_nb_samples = block.i_nb_samples;
1198 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1200 p_cmd->u.send.p_block = p_block;
1204 //fprintf( stderr, "TsStoragePopCmd: %m\n" );
1205 p_cmd->u.send.p_block = block_Alloc( 1 );
1210 /*****************************************************************************
1212 *****************************************************************************/
1213 static void CmdClean( ts_cmd_t *p_cmd )
1215 switch( p_cmd->i_type )
1218 CmdCleanAdd( p_cmd );
1221 CmdCleanSend( p_cmd );
1224 CmdCleanControl( p_cmd );
1234 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1236 p_cmd->i_type = C_ADD;
1237 p_cmd->i_date = mdate();
1238 p_cmd->u.add.p_es = p_es;
1241 p_cmd->u.add.p_fmt = malloc( sizeof(*p_fmt) );
1242 if( !p_cmd->u.add.p_fmt )
1243 return VLC_EGENERIC;
1244 es_format_Copy( p_cmd->u.add.p_fmt, p_fmt );
1248 p_cmd->u.add.p_fmt = (es_format_t*)p_fmt;
1252 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1254 p_cmd->u.add.p_es->p_es = es_out_Add( p_out, p_cmd->u.add.p_fmt );
1256 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1258 es_format_Clean( p_cmd->u.add.p_fmt );
1259 free( p_cmd->u.add.p_fmt );
1262 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1264 p_cmd->i_type = C_SEND;
1265 p_cmd->i_date = mdate();
1266 p_cmd->u.send.p_es = p_es;
1267 p_cmd->u.send.p_block = p_block;
1269 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1271 block_t *p_block = p_cmd->u.send.p_block;
1273 p_cmd->u.send.p_block = NULL;
1277 if( p_cmd->u.send.p_es->p_es )
1278 return es_out_Send( p_out, p_cmd->u.send.p_es->p_es, p_block );
1279 block_Release( p_block );
1281 return VLC_EGENERIC;
1283 static void CmdCleanSend( ts_cmd_t *p_cmd )
1285 if( p_cmd->u.send.p_block )
1286 block_Release( p_cmd->u.send.p_block );
1289 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1291 p_cmd->i_type = C_DEL;
1292 p_cmd->i_date = mdate();
1293 p_cmd->u.del.p_es = p_es;
1296 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1298 if( p_cmd->u.del.p_es->p_es )
1299 es_out_Del( p_out, p_cmd->u.del.p_es->p_es );
1300 free( p_cmd->u.del.p_es );
1303 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1305 p_cmd->i_type = C_CONTROL;
1306 p_cmd->i_date = mdate();
1307 p_cmd->u.control.i_query = i_query;
1311 /* Pass-through control */
1312 case ES_OUT_SET_MODE: /* arg1= int */
1313 case ES_OUT_SET_GROUP: /* arg1= int */
1314 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1315 p_cmd->u.control.u.i_int = (int)va_arg( args, int );
1318 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1319 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1320 p_cmd->u.control.u.i_i64 = (int64_t)va_arg( args, int64_t );
1323 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1324 p_cmd->u.control.u.int_i64.i_int = (int)va_arg( args, int );
1325 p_cmd->u.control.u.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1328 case ES_OUT_SET_ES_SCRAMBLED_STATE:
1329 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1330 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1333 case ES_OUT_RESET_PCR: /* no arg */
1334 case ES_OUT_SET_EOS:
1337 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1338 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1340 if( i_query == ES_OUT_SET_GROUP_META )
1341 p_cmd->u.control.u.int_meta.i_int = (int)va_arg( args, int );
1342 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
1346 p_cmd->u.control.u.int_meta.p_meta = vlc_meta_New();
1347 if( !p_cmd->u.control.u.int_meta.p_meta )
1348 return VLC_EGENERIC;
1349 vlc_meta_Merge( p_cmd->u.control.u.int_meta.p_meta, p_meta );
1353 /* The cast is only needed to avoid warning */
1354 p_cmd->u.control.u.int_meta.p_meta = (vlc_meta_t*)p_meta;
1359 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1361 p_cmd->u.control.u.int_epg.i_int = (int)va_arg( args, int );
1362 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
1366 p_cmd->u.control.u.int_epg.p_epg = vlc_epg_New( p_epg->psz_name );
1367 if( !p_cmd->u.control.u.int_epg.p_epg )
1368 return VLC_EGENERIC;
1369 for( int i = 0; i < p_epg->i_event; i++ )
1371 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1373 vlc_epg_AddEvent( p_cmd->u.control.u.int_epg.p_epg,
1374 p_evt->i_start, p_evt->i_duration,
1376 p_evt->psz_short_description, p_evt->psz_description );
1378 vlc_epg_SetCurrent( p_cmd->u.control.u.int_epg.p_epg,
1379 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1383 /* The cast is only needed to avoid warning */
1384 p_cmd->u.control.u.int_epg.p_epg = (vlc_epg_t*)p_epg;
1389 /* Modified control */
1390 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1391 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1392 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1393 p_cmd->u.control.u.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1396 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1397 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1398 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1401 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1403 p_cmd->u.control.u.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1404 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1408 p_cmd->u.control.u.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1409 if( !p_cmd->u.control.u.es_fmt.p_fmt )
1410 return VLC_EGENERIC;
1411 es_format_Copy( p_cmd->u.control.u.es_fmt.p_fmt, p_fmt );
1415 p_cmd->u.control.u.es_fmt.p_fmt = p_fmt;
1419 case ES_OUT_SET_TIMES:
1421 double f_position = (double)va_arg( args, double );
1422 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
1423 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
1425 p_cmd->u.control.u.times.f_position = f_position;
1426 p_cmd->u.control.u.times.i_time = i_time;
1427 p_cmd->u.control.u.times.i_length = i_length;
1430 case ES_OUT_SET_JITTER:
1432 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
1433 mtime_t i_pts_jitter = (mtime_t)va_arg( args, mtime_t );
1434 int i_cr_average = (int)va_arg( args, int );
1436 p_cmd->u.control.u.jitter.i_pts_delay = i_pts_delay;
1437 p_cmd->u.control.u.jitter.i_pts_jitter = i_pts_jitter;
1438 p_cmd->u.control.u.jitter.i_cr_average = i_cr_average;
1444 return VLC_EGENERIC;
1449 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1451 const int i_query = p_cmd->u.control.i_query;
1455 /* Pass-through control */
1456 case ES_OUT_SET_MODE: /* arg1= int */
1457 case ES_OUT_SET_GROUP: /* arg1= int */
1458 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1459 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_int );
1461 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1462 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1463 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_i64 );
1465 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1466 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_i64.i_int,
1467 p_cmd->u.control.u.int_i64.i_i64 );
1469 case ES_OUT_RESET_PCR: /* no arg */
1470 case ES_OUT_SET_EOS:
1471 return es_out_Control( p_out, i_query );
1473 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1474 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.i_int,
1475 p_cmd->u.control.u.int_meta.p_meta );
1477 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1478 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_epg.i_int,
1479 p_cmd->u.control.u.int_epg.p_epg );
1481 case ES_OUT_SET_ES_SCRAMBLED_STATE: /* arg1=int es_out_id_t* arg2=bool */
1482 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1483 p_cmd->u.control.u.es_bool.b_bool );
1485 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1486 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.p_meta );
1488 /* Modified control */
1489 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1490 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1491 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1492 return es_out_Control( p_out, i_query, p_cmd->u.control.u.p_es->p_es );
1494 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1495 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1496 p_cmd->u.control.u.es_bool.b_bool );
1498 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1499 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_fmt.p_es->p_es,
1500 p_cmd->u.control.u.es_fmt.p_fmt );
1502 case ES_OUT_SET_TIMES:
1503 return es_out_Control( p_out, i_query, p_cmd->u.control.u.times.f_position,
1504 p_cmd->u.control.u.times.i_time,
1505 p_cmd->u.control.u.times.i_length );
1506 case ES_OUT_SET_JITTER:
1507 return es_out_Control( p_out, i_query, p_cmd->u.control.u.jitter.i_pts_delay,
1508 p_cmd->u.control.u.jitter.i_pts_jitter,
1509 p_cmd->u.control.u.jitter.i_cr_average );
1513 return VLC_EGENERIC;
1516 static void CmdCleanControl( ts_cmd_t *p_cmd )
1518 if( ( p_cmd->u.control.i_query == ES_OUT_SET_GROUP_META ||
1519 p_cmd->u.control.i_query == ES_OUT_SET_META ) &&
1520 p_cmd->u.control.u.int_meta.p_meta )
1522 vlc_meta_Delete( p_cmd->u.control.u.int_meta.p_meta );
1524 else if( p_cmd->u.control.i_query == ES_OUT_SET_GROUP_EPG &&
1525 p_cmd->u.control.u.int_epg.p_epg )
1527 vlc_epg_Delete( p_cmd->u.control.u.int_epg.p_epg );
1529 else if( p_cmd->u.control.i_query == ES_OUT_SET_ES_FMT &&
1530 p_cmd->u.control.u.es_fmt.p_fmt )
1532 es_format_Clean( p_cmd->u.control.u.es_fmt.p_fmt );
1533 free( p_cmd->u.control.u.es_fmt.p_fmt );
1538 /*****************************************************************************
1540 *****************************************************************************/
1541 static char *GetTmpPath( char *psz_path )
1543 if( psz_path && *psz_path )
1545 /* Make sure that the path exists and is a directory */
1547 const int i_ret = vlc_stat( psz_path, &s );
1549 if( i_ret < 0 && !vlc_mkdir( psz_path, 0600 ) )
1551 else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1556 /* Create a suitable path */
1557 #if defined (WIN32) && !defined (UNDER_CE)
1558 const DWORD dwCount = GetTempPathW( 0, NULL );
1559 wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1562 if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1566 psw_path = _wgetcwd( NULL, 0 );
1573 psz_path = FromWide( psw_path );
1574 while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1575 psz_path[strlen( psz_path ) - 1] = '\0';
1580 if( !psz_path || *psz_path == '\0' )
1583 return strdup( "C:" );
1586 psz_path = strdup( DIR_SEP"tmp" );
1592 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path )
1600 if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1604 fd = vlc_mkstemp( psz_name );
1605 *ppsz_file = psz_name;
1611 f = fdopen( fd, "w+b" );