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 *****************************************************************************/
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
118 vlc_epg_event_t
*p_evt
;
137 /* FIXME Really too big (double make the whole thing too big) */
145 mtime_t i_pts_jitter
;
151 typedef struct attribute_packed
160 ts_cmd_control_t control
;
164 typedef struct ts_storage_t ts_storage_t
;
167 ts_storage_t
*p_next
;
171 char *psz_file
; /* Filename */
173 size_t i_file_max
; /* Max size in bytes */
174 int64_t i_file_size
;/* Current size in bytes */
175 FILE *p_filew
; /* FILE handle for data writing */
176 FILE *p_filer
; /* FILE handle for data reading */
188 input_thread_t
*p_input
;
190 int64_t i_tmp_size_max
;
191 const char *psz_tmp_path
;
193 /* Lock for all following fields */
199 mtime_t i_pause_date
;
205 mtime_t i_rate_delay
;
208 mtime_t i_buffering_delay
;
211 ts_storage_t
*p_storage_r
;
212 ts_storage_t
*p_storage_w
;
225 input_thread_t
*p_input
;
229 int64_t i_tmp_size_max
; /* Maximal temporary file size in byte */
230 char *psz_tmp_path
; /* Path for temporary files */
232 /* Lock for all following fields */
241 bool b_input_paused_source
;
243 int i_input_rate_source
;
250 static es_out_id_t
*Add ( es_out_t
*, const es_format_t
* );
251 static int Send ( es_out_t
*, es_out_id_t
*, block_t
* );
252 static void Del ( es_out_t
*, es_out_id_t
* );
253 static int Control( es_out_t
*, int i_query
, va_list );
254 static void Destroy( es_out_t
* );
256 static int TsStart( es_out_t
* );
257 static void TsAutoStop( es_out_t
* );
259 static void TsStop( ts_thread_t
* );
260 static void TsPushCmd( ts_thread_t
*, ts_cmd_t
* );
261 static int TsPopCmdLocked( ts_thread_t
*, ts_cmd_t
*, bool b_flush
);
262 static bool TsHasCmd( ts_thread_t
* );
263 static bool TsIsUnused( ts_thread_t
* );
264 static int TsChangePause( ts_thread_t
*, bool b_source_paused
, bool b_paused
, mtime_t i_date
);
265 static int TsChangeRate( ts_thread_t
*, int i_src_rate
, int i_rate
);
267 static void *TsRun( void * );
269 static ts_storage_t
*TsStorageNew( const char *psz_path
, int64_t i_tmp_size_max
);
270 static void TsStorageDelete( ts_storage_t
* );
271 static void TsStoragePack( ts_storage_t
*p_storage
);
272 static bool TsStorageIsFull( ts_storage_t
*, const ts_cmd_t
*p_cmd
);
273 static bool TsStorageIsEmpty( ts_storage_t
* );
274 static void TsStoragePushCmd( ts_storage_t
*, const ts_cmd_t
*p_cmd
, bool b_flush
);
275 static void TsStoragePopCmd( ts_storage_t
*p_storage
, ts_cmd_t
*p_cmd
, bool b_flush
);
277 static void CmdClean( ts_cmd_t
* );
278 static void cmd_cleanup_routine( void *p
) { CmdClean( p
); }
280 static int CmdInitAdd ( ts_cmd_t
*, es_out_id_t
*, const es_format_t
*, bool b_copy
);
281 static void CmdInitSend ( ts_cmd_t
*, es_out_id_t
*, block_t
* );
282 static int CmdInitDel ( ts_cmd_t
*, es_out_id_t
* );
283 static int CmdInitControl( ts_cmd_t
*, int i_query
, va_list, bool b_copy
);
286 static void CmdCleanAdd ( ts_cmd_t
* );
287 static void CmdCleanSend ( ts_cmd_t
* );
288 static void CmdCleanControl( ts_cmd_t
*p_cmd
);
290 /* XXX these functions will take the destination es_out_t */
291 static void CmdExecuteAdd ( es_out_t
*, ts_cmd_t
* );
292 static int CmdExecuteSend ( es_out_t
*, ts_cmd_t
* );
293 static void CmdExecuteDel ( es_out_t
*, ts_cmd_t
* );
294 static int CmdExecuteControl( es_out_t
*, ts_cmd_t
* );
297 static int GetTmpFile( char **ppsz_file
, const char *psz_path
);
299 /*****************************************************************************
300 * input_EsOutTimeshiftNew:
301 *****************************************************************************/
302 es_out_t
*input_EsOutTimeshiftNew( input_thread_t
*p_input
, es_out_t
*p_next_out
, int i_rate
)
304 es_out_t
*p_out
= malloc( sizeof(*p_out
) );
308 es_out_sys_t
*p_sys
= malloc( sizeof(*p_sys
) );
317 p_out
->pf_send
= Send
;
319 p_out
->pf_control
= Control
;
320 p_out
->pf_destroy
= Destroy
;
321 p_out
->p_sys
= p_sys
;
324 p_sys
->b_input_paused
= false;
325 p_sys
->b_input_paused_source
= false;
326 p_sys
->p_input
= p_input
;
327 p_sys
->i_input_rate
= i_rate
;
328 p_sys
->i_input_rate_source
= i_rate
;
330 p_sys
->p_out
= p_next_out
;
331 vlc_mutex_init_recursive( &p_sys
->lock
);
333 p_sys
->b_delayed
= false;
336 TAB_INIT( p_sys
->i_es
, p_sys
->pp_es
);
339 const int i_tmp_size_max
= var_CreateGetInteger( p_input
, "input-timeshift-granularity" );
340 if( i_tmp_size_max
< 0 )
341 p_sys
->i_tmp_size_max
= 50*1024*1024;
343 p_sys
->i_tmp_size_max
= __MAX( i_tmp_size_max
, 1*1024*1024 );
344 msg_Dbg( p_input
, "using timeshift granularity of %d MiB",
345 (int)p_sys
->i_tmp_size_max
/(1024*1024) );
347 p_sys
->psz_tmp_path
= var_InheritString( p_input
, "input-timeshift-path" );
348 #if defined (_WIN32) && !VLC_WINSTORE_APP
349 if( p_sys
->psz_tmp_path
== NULL
)
351 const DWORD count
= GetTempPath( 0, NULL
);
354 TCHAR
*path
= vlc_alloc( count
+ 1, sizeof(TCHAR
) );
357 DWORD ret
= GetTempPath( count
+ 1, path
);
358 if( ret
!= 0 && ret
<= count
)
359 p_sys
->psz_tmp_path
= FromT( path
);
364 if( p_sys
->psz_tmp_path
== NULL
)
366 wchar_t *wpath
= _wgetcwd( NULL
, 0 );
369 p_sys
->psz_tmp_path
= FromWide( wpath
);
373 if( p_sys
->psz_tmp_path
== NULL
)
374 p_sys
->psz_tmp_path
= strdup( "C:" );
376 if( p_sys
->psz_tmp_path
!= NULL
)
378 size_t len
= strlen( p_sys
->psz_tmp_path
);
380 while( len
> 0 && p_sys
->psz_tmp_path
[len
- 1] == DIR_SEP_CHAR
)
383 p_sys
->psz_tmp_path
[len
] = '\0';
386 if( p_sys
->psz_tmp_path
!= NULL
)
387 msg_Dbg( p_input
, "using timeshift path: %s", p_sys
->psz_tmp_path
);
389 msg_Dbg( p_input
, "using default timeshift path" );
392 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
404 /*****************************************************************************
406 *****************************************************************************/
407 static void Destroy( es_out_t
*p_out
)
409 es_out_sys_t
*p_sys
= p_out
->p_sys
;
411 if( p_sys
->b_delayed
)
413 TsStop( p_sys
->p_ts
);
414 p_sys
->b_delayed
= false;
417 while( p_sys
->i_es
> 0 )
418 Del( p_out
, p_sys
->pp_es
[0] );
419 TAB_CLEAN( p_sys
->i_es
, p_sys
->pp_es
);
421 free( p_sys
->psz_tmp_path
);
422 vlc_mutex_destroy( &p_sys
->lock
);
427 static es_out_id_t
*Add( es_out_t
*p_out
, const es_format_t
*p_fmt
)
429 es_out_sys_t
*p_sys
= p_out
->p_sys
;
432 es_out_id_t
*p_es
= malloc( sizeof( *p_es
) );
436 vlc_mutex_lock( &p_sys
->lock
);
440 if( CmdInitAdd( &cmd
, p_es
, p_fmt
, p_sys
->b_delayed
) )
442 vlc_mutex_unlock( &p_sys
->lock
);
447 TAB_APPEND( p_sys
->i_es
, p_sys
->pp_es
, p_es
);
449 if( p_sys
->b_delayed
)
450 TsPushCmd( p_sys
->p_ts
, &cmd
);
452 CmdExecuteAdd( p_sys
->p_out
, &cmd
);
454 vlc_mutex_unlock( &p_sys
->lock
);
458 static int Send( es_out_t
*p_out
, es_out_id_t
*p_es
, block_t
*p_block
)
460 es_out_sys_t
*p_sys
= p_out
->p_sys
;
462 int i_ret
= VLC_SUCCESS
;
464 vlc_mutex_lock( &p_sys
->lock
);
468 CmdInitSend( &cmd
, p_es
, p_block
);
469 if( p_sys
->b_delayed
)
470 TsPushCmd( p_sys
->p_ts
, &cmd
);
472 i_ret
= CmdExecuteSend( p_sys
->p_out
, &cmd
) ;
474 vlc_mutex_unlock( &p_sys
->lock
);
478 static void Del( es_out_t
*p_out
, es_out_id_t
*p_es
)
480 es_out_sys_t
*p_sys
= p_out
->p_sys
;
483 vlc_mutex_lock( &p_sys
->lock
);
487 CmdInitDel( &cmd
, p_es
);
488 if( p_sys
->b_delayed
)
489 TsPushCmd( p_sys
->p_ts
, &cmd
);
491 CmdExecuteDel( p_sys
->p_out
, &cmd
);
493 TAB_REMOVE( p_sys
->i_es
, p_sys
->pp_es
, p_es
);
495 vlc_mutex_unlock( &p_sys
->lock
);
498 static int ControlLockedGetEmpty( es_out_t
*p_out
, bool *pb_empty
)
500 es_out_sys_t
*p_sys
= p_out
->p_sys
;
502 if( p_sys
->b_delayed
&& TsHasCmd( p_sys
->p_ts
) )
505 *pb_empty
= es_out_GetEmpty( p_sys
->p_out
);
509 static int ControlLockedGetWakeup( es_out_t
*p_out
, mtime_t
*pi_wakeup
)
511 es_out_sys_t
*p_sys
= p_out
->p_sys
;
513 if( p_sys
->b_delayed
)
515 assert( !input_priv(p_sys
->p_input
)->b_can_pace_control
);
520 *pi_wakeup
= es_out_GetWakeup( p_sys
->p_out
);
525 static int ControlLockedGetBuffering( es_out_t
*p_out
, bool *pb_buffering
)
527 es_out_sys_t
*p_sys
= p_out
->p_sys
;
529 if( p_sys
->b_delayed
)
530 *pb_buffering
= true;
532 *pb_buffering
= es_out_GetBuffering( p_sys
->p_out
);
536 static int ControlLockedSetPauseState( es_out_t
*p_out
, bool b_source_paused
, bool b_paused
, mtime_t i_date
)
538 es_out_sys_t
*p_sys
= p_out
->p_sys
;
541 if( !p_sys
->b_delayed
&& !b_source_paused
== !b_paused
)
543 i_ret
= es_out_SetPauseState( p_sys
->p_out
, b_source_paused
, b_paused
, i_date
);
547 i_ret
= VLC_EGENERIC
;
548 if( !input_priv(p_sys
->p_input
)->b_can_pace_control
)
550 if( !p_sys
->b_delayed
)
552 if( p_sys
->b_delayed
)
553 i_ret
= TsChangePause( p_sys
->p_ts
, b_source_paused
, b_paused
, i_date
);
557 /* XXX we may do it BUT it would be better to finish the clock clean up+improvements
558 * and so be able to advertize correctly pace control property in access
560 msg_Err( p_sys
->p_input
, "EsOutTimeshift does not work with streams that have pace control" );
566 p_sys
->b_input_paused_source
= b_source_paused
;
567 p_sys
->b_input_paused
= b_paused
;
571 static int ControlLockedSetRate( es_out_t
*p_out
, int i_src_rate
, int i_rate
)
573 es_out_sys_t
*p_sys
= p_out
->p_sys
;
576 if( !p_sys
->b_delayed
&& i_src_rate
== i_rate
)
578 i_ret
= es_out_SetRate( p_sys
->p_out
, i_src_rate
, i_rate
);
582 i_ret
= VLC_EGENERIC
;
583 if( !input_priv(p_sys
->p_input
)->b_can_pace_control
)
585 if( !p_sys
->b_delayed
)
587 if( p_sys
->b_delayed
)
588 i_ret
= TsChangeRate( p_sys
->p_ts
, i_src_rate
, i_rate
);
592 /* XXX we may do it BUT it would be better to finish the clock clean up+improvements
593 * and so be able to advertize correctly pace control property in access
595 msg_Err( p_sys
->p_input
, "EsOutTimeshift does not work with streams that have pace control" );
602 p_sys
->i_input_rate_source
= i_src_rate
;
603 p_sys
->i_input_rate
= i_rate
;
607 static int ControlLockedSetTime( es_out_t
*p_out
, mtime_t i_date
)
609 es_out_sys_t
*p_sys
= p_out
->p_sys
;
611 if( !p_sys
->b_delayed
)
612 return es_out_SetTime( p_sys
->p_out
, i_date
);
615 msg_Err( p_sys
->p_input
, "EsOutTimeshift does not yet support time change" );
618 static int ControlLockedSetFrameNext( es_out_t
*p_out
)
620 es_out_sys_t
*p_sys
= p_out
->p_sys
;
622 return es_out_SetFrameNext( p_sys
->p_out
);
625 static int ControlLocked( es_out_t
*p_out
, int i_query
, va_list args
)
627 es_out_sys_t
*p_sys
= p_out
->p_sys
;
631 /* Pass-through control */
632 case ES_OUT_SET_MODE
:
633 case ES_OUT_SET_GROUP
:
635 case ES_OUT_SET_GROUP_PCR
:
636 case ES_OUT_RESET_PCR
:
637 case ES_OUT_SET_NEXT_DISPLAY_TIME
:
638 case ES_OUT_SET_GROUP_META
:
639 case ES_OUT_SET_GROUP_EPG
:
640 case ES_OUT_SET_GROUP_EPG_EVENT
:
641 case ES_OUT_SET_EPG_TIME
:
642 case ES_OUT_SET_ES_SCRAMBLED_STATE
:
643 case ES_OUT_DEL_GROUP
:
644 case ES_OUT_SET_META
:
646 case ES_OUT_RESTART_ES
:
647 case ES_OUT_RESTART_ALL_ES
:
648 case ES_OUT_SET_ES_DEFAULT
:
649 case ES_OUT_SET_ES_STATE
:
650 case ES_OUT_SET_ES_CAT_POLICY
:
651 case ES_OUT_SET_ES_FMT
:
652 case ES_OUT_SET_TIMES
:
653 case ES_OUT_SET_JITTER
:
657 if( CmdInitControl( &cmd
, i_query
, args
, p_sys
->b_delayed
) )
659 if( p_sys
->b_delayed
)
661 TsPushCmd( p_sys
->p_ts
, &cmd
);
664 return CmdExecuteControl( p_sys
->p_out
, &cmd
);
667 /* Special control when delayed */
668 case ES_OUT_GET_ES_STATE
:
670 es_out_id_t
*p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
671 bool *pb_enabled
= (bool*)va_arg( args
, bool* );
673 if( p_sys
->b_delayed
)
678 return es_out_Control( p_sys
->p_out
, ES_OUT_GET_ES_STATE
, p_es
->p_es
, pb_enabled
);
680 /* Special internal input control */
681 case ES_OUT_GET_EMPTY
:
683 bool *pb_empty
= (bool*)va_arg( args
, bool* );
684 return ControlLockedGetEmpty( p_out
, pb_empty
);
686 case ES_OUT_GET_WAKE_UP
: /* TODO ? */
688 mtime_t
*pi_wakeup
= (mtime_t
*)va_arg( args
, mtime_t
* );
689 return ControlLockedGetWakeup( p_out
, pi_wakeup
);
691 case ES_OUT_GET_BUFFERING
:
693 bool *pb_buffering
= (bool *)va_arg( args
, bool* );
694 return ControlLockedGetBuffering( p_out
, pb_buffering
);
696 case ES_OUT_SET_PAUSE_STATE
:
698 const bool b_source_paused
= (bool)va_arg( args
, int );
699 const bool b_paused
= (bool)va_arg( args
, int );
700 const mtime_t i_date
= (mtime_t
) va_arg( args
, mtime_t
);
702 return ControlLockedSetPauseState( p_out
, b_source_paused
, b_paused
, i_date
);
704 case ES_OUT_SET_RATE
:
706 const int i_src_rate
= (int)va_arg( args
, int );
707 const int i_rate
= (int)va_arg( args
, int );
709 return ControlLockedSetRate( p_out
, i_src_rate
, i_rate
);
711 case ES_OUT_SET_TIME
:
713 const mtime_t i_date
= (mtime_t
)va_arg( args
, mtime_t
);
715 return ControlLockedSetTime( p_out
, i_date
);
717 case ES_OUT_SET_FRAME_NEXT
:
719 return ControlLockedSetFrameNext( p_out
);
722 case ES_OUT_GET_PCR_SYSTEM
:
723 if( p_sys
->b_delayed
)
726 case ES_OUT_GET_GROUP_FORCED
:
727 case ES_OUT_POST_SUBNODE
:
728 return es_out_vaControl( p_sys
->p_out
, i_query
, args
);
730 case ES_OUT_MODIFY_PCR_SYSTEM
:
732 const bool b_absolute
= va_arg( args
, int );
733 const mtime_t i_system
= va_arg( args
, mtime_t
);
735 if( b_absolute
&& p_sys
->b_delayed
)
738 return es_out_ControlModifyPcrSystem( p_sys
->p_out
, b_absolute
, i_system
);
741 /* Invalid queries for this es_out level */
742 case ES_OUT_SET_ES_BY_ID
:
743 case ES_OUT_RESTART_ES_BY_ID
:
744 case ES_OUT_SET_ES_DEFAULT_BY_ID
:
745 case ES_OUT_GET_ES_OBJECTS_BY_ID
:
746 case ES_OUT_SET_DELAY
:
747 case ES_OUT_SET_RECORD_STATE
:
749 vlc_assert_unreachable();
753 static int Control( es_out_t
*p_out
, int i_query
, va_list args
)
755 es_out_sys_t
*p_sys
= p_out
->p_sys
;
758 vlc_mutex_lock( &p_sys
->lock
);
762 i_ret
= ControlLocked( p_out
, i_query
, args
);
764 vlc_mutex_unlock( &p_sys
->lock
);
769 /*****************************************************************************
771 *****************************************************************************/
772 static void TsDestroy( ts_thread_t
*p_ts
)
774 vlc_cond_destroy( &p_ts
->wait
);
775 vlc_mutex_destroy( &p_ts
->lock
);
778 static int TsStart( es_out_t
*p_out
)
780 es_out_sys_t
*p_sys
= p_out
->p_sys
;
783 assert( !p_sys
->b_delayed
);
785 p_sys
->p_ts
= p_ts
= calloc(1, sizeof(*p_ts
));
789 p_ts
->i_tmp_size_max
= p_sys
->i_tmp_size_max
;
790 p_ts
->psz_tmp_path
= p_sys
->psz_tmp_path
;
791 p_ts
->p_input
= p_sys
->p_input
;
792 p_ts
->p_out
= p_sys
->p_out
;
793 vlc_mutex_init( &p_ts
->lock
);
794 vlc_cond_init( &p_ts
->wait
);
795 p_ts
->b_paused
= p_sys
->b_input_paused
&& !p_sys
->b_input_paused_source
;
796 p_ts
->i_pause_date
= p_ts
->b_paused
? mdate() : -1;
797 p_ts
->i_rate_source
= p_sys
->i_input_rate_source
;
798 p_ts
->i_rate
= p_sys
->i_input_rate
;
799 p_ts
->i_rate_date
= -1;
800 p_ts
->i_rate_delay
= 0;
801 p_ts
->i_buffering_delay
= 0;
802 p_ts
->i_cmd_delay
= 0;
803 p_ts
->p_storage_r
= NULL
;
804 p_ts
->p_storage_w
= NULL
;
806 p_sys
->b_delayed
= true;
807 if( vlc_clone( &p_ts
->thread
, TsRun
, p_ts
, VLC_THREAD_PRIORITY_INPUT
) )
809 msg_Err( p_sys
->p_input
, "cannot create timeshift thread" );
813 p_sys
->b_delayed
= false;
819 static void TsAutoStop( es_out_t
*p_out
)
821 es_out_sys_t
*p_sys
= p_out
->p_sys
;
823 if( !p_sys
->b_delayed
|| !TsIsUnused( p_sys
->p_ts
) )
826 msg_Warn( p_sys
->p_input
, "es out timeshift: auto stop" );
827 TsStop( p_sys
->p_ts
);
829 p_sys
->b_delayed
= false;
831 static void TsStop( ts_thread_t
*p_ts
)
833 vlc_cancel( p_ts
->thread
);
834 vlc_join( p_ts
->thread
, NULL
);
836 vlc_mutex_lock( &p_ts
->lock
);
841 if( TsPopCmdLocked( p_ts
, &cmd
, true ) )
846 assert( !p_ts
->p_storage_r
|| !p_ts
->p_storage_r
->p_next
);
847 if( p_ts
->p_storage_r
)
848 TsStorageDelete( p_ts
->p_storage_r
);
849 vlc_mutex_unlock( &p_ts
->lock
);
853 static void TsPushCmd( ts_thread_t
*p_ts
, ts_cmd_t
*p_cmd
)
855 vlc_mutex_lock( &p_ts
->lock
);
857 if( !p_ts
->p_storage_w
|| TsStorageIsFull( p_ts
->p_storage_w
, p_cmd
) )
859 ts_storage_t
*p_storage
= TsStorageNew( p_ts
->psz_tmp_path
, p_ts
->i_tmp_size_max
);
864 vlc_mutex_unlock( &p_ts
->lock
);
865 /* TODO warn the user (but only once) */
869 if( !p_ts
->p_storage_w
)
871 p_ts
->p_storage_r
= p_ts
->p_storage_w
= p_storage
;
875 TsStoragePack( p_ts
->p_storage_w
);
876 p_ts
->p_storage_w
->p_next
= p_storage
;
877 p_ts
->p_storage_w
= p_storage
;
881 /* TODO return error and warn the user (but only once) */
882 TsStoragePushCmd( p_ts
->p_storage_w
, p_cmd
, p_ts
->p_storage_r
== p_ts
->p_storage_w
);
884 vlc_cond_signal( &p_ts
->wait
);
886 vlc_mutex_unlock( &p_ts
->lock
);
888 static int TsPopCmdLocked( ts_thread_t
*p_ts
, ts_cmd_t
*p_cmd
, bool b_flush
)
890 vlc_assert_locked( &p_ts
->lock
);
892 if( TsStorageIsEmpty( p_ts
->p_storage_r
) )
895 TsStoragePopCmd( p_ts
->p_storage_r
, p_cmd
, b_flush
);
897 while( TsStorageIsEmpty( p_ts
->p_storage_r
) )
899 ts_storage_t
*p_next
= p_ts
->p_storage_r
->p_next
;
903 TsStorageDelete( p_ts
->p_storage_r
);
904 p_ts
->p_storage_r
= p_next
;
909 static bool TsHasCmd( ts_thread_t
*p_ts
)
913 vlc_mutex_lock( &p_ts
->lock
);
914 b_cmd
= TsStorageIsEmpty( p_ts
->p_storage_r
);
915 vlc_mutex_unlock( &p_ts
->lock
);
919 static bool TsIsUnused( ts_thread_t
*p_ts
)
923 vlc_mutex_lock( &p_ts
->lock
);
924 b_unused
= !p_ts
->b_paused
&&
925 p_ts
->i_rate
== p_ts
->i_rate_source
&&
926 TsStorageIsEmpty( p_ts
->p_storage_r
);
927 vlc_mutex_unlock( &p_ts
->lock
);
931 static int TsChangePause( ts_thread_t
*p_ts
, bool b_source_paused
, bool b_paused
, mtime_t i_date
)
933 vlc_mutex_lock( &p_ts
->lock
);
938 assert( !b_source_paused
);
939 i_ret
= es_out_SetPauseState( p_ts
->p_out
, true, true, i_date
);
943 i_ret
= es_out_SetPauseState( p_ts
->p_out
, false, false, i_date
);
950 assert( p_ts
->i_pause_date
> 0 );
952 p_ts
->i_cmd_delay
+= i_date
- p_ts
->i_pause_date
;
955 p_ts
->b_paused
= b_paused
;
956 p_ts
->i_pause_date
= i_date
;
958 vlc_cond_signal( &p_ts
->wait
);
960 vlc_mutex_unlock( &p_ts
->lock
);
963 static int TsChangeRate( ts_thread_t
*p_ts
, int i_src_rate
, int i_rate
)
967 vlc_mutex_lock( &p_ts
->lock
);
968 p_ts
->i_cmd_delay
+= p_ts
->i_rate_delay
;
970 p_ts
->i_rate_date
= -1;
971 p_ts
->i_rate_delay
= 0;
972 p_ts
->i_rate
= i_rate
;
973 p_ts
->i_rate_source
= i_src_rate
;
975 i_ret
= es_out_SetRate( p_ts
->p_out
, i_rate
, i_rate
);
976 vlc_mutex_unlock( &p_ts
->lock
);
981 static void *TsRun( void *p_data
)
983 ts_thread_t
*p_ts
= p_data
;
984 mtime_t i_buffering_date
= -1;
992 /* Pop a command to execute */
993 vlc_mutex_lock( &p_ts
->lock
);
994 mutex_cleanup_push( &p_ts
->lock
);
998 const int canc
= vlc_savecancel();
999 b_buffering
= es_out_GetBuffering( p_ts
->p_out
);
1001 if( ( !p_ts
->b_paused
|| b_buffering
) && !TsPopCmdLocked( p_ts
, &cmd
, false ) )
1003 vlc_restorecancel( canc
);
1006 vlc_restorecancel( canc
);
1008 vlc_cond_wait( &p_ts
->wait
, &p_ts
->lock
);
1011 if( b_buffering
&& i_buffering_date
< 0 )
1013 i_buffering_date
= cmd
.i_date
;
1015 else if( i_buffering_date
> 0 )
1017 p_ts
->i_buffering_delay
+= i_buffering_date
- cmd
.i_date
; /* It is < 0 */
1019 i_buffering_date
= cmd
.i_date
;
1021 i_buffering_date
= -1;
1024 if( p_ts
->i_rate_date
< 0 )
1025 p_ts
->i_rate_date
= cmd
.i_date
;
1027 p_ts
->i_rate_delay
= 0;
1028 if( p_ts
->i_rate_source
!= p_ts
->i_rate
)
1030 const mtime_t i_duration
= cmd
.i_date
- p_ts
->i_rate_date
;
1031 p_ts
->i_rate_delay
= i_duration
* p_ts
->i_rate
/ p_ts
->i_rate_source
- i_duration
;
1033 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
)
1035 const int canc
= vlc_savecancel();
1037 /* Auto reset to rate 1.0 */
1038 msg_Warn( p_ts
->p_input
, "es out timeshift: auto reset rate to %d", p_ts
->i_rate_source
);
1040 p_ts
->i_cmd_delay
= 0;
1041 p_ts
->i_buffering_delay
= 0;
1043 p_ts
->i_rate_delay
= 0;
1044 p_ts
->i_rate_date
= -1;
1045 p_ts
->i_rate
= p_ts
->i_rate_source
;
1047 if( !es_out_SetRate( p_ts
->p_out
, p_ts
->i_rate_source
, p_ts
->i_rate
) )
1049 vlc_value_t val
= { .i_int
= p_ts
->i_rate
};
1051 * FIXME it is perfectly safe BUT it is ugly as it may hide a
1052 * rate change requested by user */
1053 input_ControlPush( p_ts
->p_input
, INPUT_CONTROL_SET_RATE
, &val
);
1056 vlc_restorecancel( canc
);
1058 i_deadline
= cmd
.i_date
+ p_ts
->i_cmd_delay
+ p_ts
->i_rate_delay
+ p_ts
->i_buffering_delay
;
1061 vlc_mutex_unlock( &p_ts
->lock
);
1063 /* Regulate the speed of command processing to the same one than
1065 vlc_cleanup_push( cmd_cleanup_routine
, &cmd
);
1067 mwait( i_deadline
);
1071 /* Execute the command */
1072 const int canc
= vlc_savecancel();
1073 switch( cmd
.i_type
)
1076 CmdExecuteAdd( p_ts
->p_out
, &cmd
);
1077 CmdCleanAdd( &cmd
);
1080 CmdExecuteSend( p_ts
->p_out
, &cmd
);
1081 CmdCleanSend( &cmd
);
1084 CmdExecuteControl( p_ts
->p_out
, &cmd
);
1085 CmdCleanControl( &cmd
);
1088 CmdExecuteDel( p_ts
->p_out
, &cmd
);
1091 vlc_assert_unreachable();
1094 vlc_restorecancel( canc
);
1100 /*****************************************************************************
1102 *****************************************************************************/
1103 static ts_storage_t
*TsStorageNew( const char *psz_tmp_path
, int64_t i_tmp_size_max
)
1105 ts_storage_t
*p_storage
= malloc( sizeof (*p_storage
) );
1106 if( unlikely(p_storage
== NULL
) )
1110 int fd
= GetTmpFile( &psz_file
, psz_tmp_path
);
1117 p_storage
->p_filew
= fdopen( fd
, "w+b" );
1118 if( p_storage
->p_filew
== NULL
)
1121 vlc_unlink( psz_file
);
1125 p_storage
->p_filer
= vlc_fopen( psz_file
, "rb" );
1126 if( p_storage
->p_filer
== NULL
)
1128 fclose( p_storage
->p_filew
);
1129 vlc_unlink( psz_file
);
1134 vlc_unlink( psz_file
);
1137 p_storage
->psz_file
= psz_file
;
1139 p_storage
->p_next
= NULL
;
1142 p_storage
->i_file_max
= i_tmp_size_max
;
1143 p_storage
->i_file_size
= 0;
1146 p_storage
->i_cmd_w
= 0;
1147 p_storage
->i_cmd_r
= 0;
1148 p_storage
->i_cmd_max
= 30000;
1149 p_storage
->p_cmd
= vlc_alloc( p_storage
->i_cmd_max
, sizeof(*p_storage
->p_cmd
) );
1150 //fprintf( stderr, "\nSTORAGE name=%s size=%d KiB\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1152 if( !p_storage
->p_cmd
)
1154 TsStorageDelete( p_storage
);
1164 static void TsStorageDelete( ts_storage_t
*p_storage
)
1166 while( p_storage
->i_cmd_r
< p_storage
->i_cmd_w
)
1170 TsStoragePopCmd( p_storage
, &cmd
, true );
1174 free( p_storage
->p_cmd
);
1176 fclose( p_storage
->p_filer
);
1177 fclose( p_storage
->p_filew
);
1179 vlc_unlink( p_storage
->psz_file
);
1180 free( p_storage
->psz_file
);
1185 static void TsStoragePack( ts_storage_t
*p_storage
)
1187 /* Try to release a bit of memory */
1188 if( p_storage
->i_cmd_w
>= p_storage
->i_cmd_max
)
1191 p_storage
->i_cmd_max
= __MAX( p_storage
->i_cmd_w
, 1 );
1193 ts_cmd_t
*p_new
= realloc( p_storage
->p_cmd
, p_storage
->i_cmd_max
* sizeof(*p_storage
->p_cmd
) );
1195 p_storage
->p_cmd
= p_new
;
1197 static bool TsStorageIsFull( ts_storage_t
*p_storage
, const ts_cmd_t
*p_cmd
)
1199 if( p_cmd
&& p_cmd
->i_type
== C_SEND
&& p_storage
->i_cmd_w
> 0 )
1201 size_t i_size
= sizeof(*p_cmd
->u
.send
.p_block
) + p_cmd
->u
.send
.p_block
->i_buffer
;
1203 if( p_storage
->i_file_size
+ i_size
>= p_storage
->i_file_max
)
1206 return p_storage
->i_cmd_w
>= p_storage
->i_cmd_max
;
1208 static bool TsStorageIsEmpty( ts_storage_t
*p_storage
)
1210 return !p_storage
|| p_storage
->i_cmd_r
>= p_storage
->i_cmd_w
;
1212 static void TsStoragePushCmd( ts_storage_t
*p_storage
, const ts_cmd_t
*p_cmd
, bool b_flush
)
1214 ts_cmd_t cmd
= *p_cmd
;
1216 assert( !TsStorageIsFull( p_storage
, p_cmd
) );
1218 if( cmd
.i_type
== C_SEND
)
1220 block_t
*p_block
= cmd
.u
.send
.p_block
;
1222 cmd
.u
.send
.p_block
= NULL
;
1223 cmd
.u
.send
.i_offset
= ftell( p_storage
->p_filew
);
1225 if( fwrite( p_block
, sizeof(*p_block
), 1, p_storage
->p_filew
) != 1 )
1227 block_Release( p_block
);
1230 p_storage
->i_file_size
+= sizeof(*p_block
);
1231 if( p_block
->i_buffer
> 0 )
1233 if( fwrite( p_block
->p_buffer
, p_block
->i_buffer
, 1, p_storage
->p_filew
) != 1 )
1235 block_Release( p_block
);
1239 p_storage
->i_file_size
+= p_block
->i_buffer
;
1240 block_Release( p_block
);
1243 fflush( p_storage
->p_filew
);
1245 p_storage
->p_cmd
[p_storage
->i_cmd_w
++] = cmd
;
1247 static void TsStoragePopCmd( ts_storage_t
*p_storage
, ts_cmd_t
*p_cmd
, bool b_flush
)
1249 assert( !TsStorageIsEmpty( p_storage
) );
1251 *p_cmd
= p_storage
->p_cmd
[p_storage
->i_cmd_r
++];
1252 if( p_cmd
->i_type
== C_SEND
)
1257 !fseek( p_storage
->p_filer
, p_cmd
->u
.send
.i_offset
, SEEK_SET
) &&
1258 fread( &block
, sizeof(block
), 1, p_storage
->p_filer
) == 1 )
1260 block_t
*p_block
= block_Alloc( block
.i_buffer
);
1263 p_block
->i_dts
= block
.i_dts
;
1264 p_block
->i_pts
= block
.i_pts
;
1265 p_block
->i_flags
= block
.i_flags
;
1266 p_block
->i_length
= block
.i_length
;
1267 p_block
->i_nb_samples
= block
.i_nb_samples
;
1268 p_block
->i_buffer
= fread( p_block
->p_buffer
, 1, block
.i_buffer
, p_storage
->p_filer
);
1270 p_cmd
->u
.send
.p_block
= p_block
;
1274 //perror( "TsStoragePopCmd" );
1275 p_cmd
->u
.send
.p_block
= block_Alloc( 1 );
1280 /*****************************************************************************
1282 *****************************************************************************/
1283 static void CmdClean( ts_cmd_t
*p_cmd
)
1285 switch( p_cmd
->i_type
)
1288 CmdCleanAdd( p_cmd
);
1291 CmdCleanSend( p_cmd
);
1294 CmdCleanControl( p_cmd
);
1299 vlc_assert_unreachable();
1304 static int CmdInitAdd( ts_cmd_t
*p_cmd
, es_out_id_t
*p_es
, const es_format_t
*p_fmt
, bool b_copy
)
1306 p_cmd
->i_type
= C_ADD
;
1307 p_cmd
->i_date
= mdate();
1308 p_cmd
->u
.add
.p_es
= p_es
;
1311 p_cmd
->u
.add
.p_fmt
= malloc( sizeof(*p_fmt
) );
1312 if( !p_cmd
->u
.add
.p_fmt
)
1313 return VLC_EGENERIC
;
1314 es_format_Copy( p_cmd
->u
.add
.p_fmt
, p_fmt
);
1318 p_cmd
->u
.add
.p_fmt
= (es_format_t
*)p_fmt
;
1322 static void CmdExecuteAdd( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1324 p_cmd
->u
.add
.p_es
->p_es
= es_out_Add( p_out
, p_cmd
->u
.add
.p_fmt
);
1326 static void CmdCleanAdd( ts_cmd_t
*p_cmd
)
1328 es_format_Clean( p_cmd
->u
.add
.p_fmt
);
1329 free( p_cmd
->u
.add
.p_fmt
);
1332 static void CmdInitSend( ts_cmd_t
*p_cmd
, es_out_id_t
*p_es
, block_t
*p_block
)
1334 p_cmd
->i_type
= C_SEND
;
1335 p_cmd
->i_date
= mdate();
1336 p_cmd
->u
.send
.p_es
= p_es
;
1337 p_cmd
->u
.send
.p_block
= p_block
;
1339 static int CmdExecuteSend( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1341 block_t
*p_block
= p_cmd
->u
.send
.p_block
;
1343 p_cmd
->u
.send
.p_block
= NULL
;
1347 if( p_cmd
->u
.send
.p_es
->p_es
)
1348 return es_out_Send( p_out
, p_cmd
->u
.send
.p_es
->p_es
, p_block
);
1349 block_Release( p_block
);
1351 return VLC_EGENERIC
;
1353 static void CmdCleanSend( ts_cmd_t
*p_cmd
)
1355 if( p_cmd
->u
.send
.p_block
)
1356 block_Release( p_cmd
->u
.send
.p_block
);
1359 static int CmdInitDel( ts_cmd_t
*p_cmd
, es_out_id_t
*p_es
)
1361 p_cmd
->i_type
= C_DEL
;
1362 p_cmd
->i_date
= mdate();
1363 p_cmd
->u
.del
.p_es
= p_es
;
1366 static void CmdExecuteDel( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1368 if( p_cmd
->u
.del
.p_es
->p_es
)
1369 es_out_Del( p_out
, p_cmd
->u
.del
.p_es
->p_es
);
1370 free( p_cmd
->u
.del
.p_es
);
1373 static int CmdInitControl( ts_cmd_t
*p_cmd
, int i_query
, va_list args
, bool b_copy
)
1375 p_cmd
->i_type
= C_CONTROL
;
1376 p_cmd
->i_date
= mdate();
1377 p_cmd
->u
.control
.i_query
= i_query
;
1381 /* Pass-through control */
1382 case ES_OUT_SET_MODE
: /* arg1= int */
1383 case ES_OUT_SET_GROUP
: /* arg1= int */
1384 case ES_OUT_DEL_GROUP
: /* arg1=int i_group */
1385 p_cmd
->u
.control
.u
.i_int
= (int)va_arg( args
, int );
1388 case ES_OUT_SET_PCR
: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1389 case ES_OUT_SET_NEXT_DISPLAY_TIME
: /* arg1=int64_t i_pts(microsecond) */
1390 p_cmd
->u
.control
.u
.i_i64
= (int64_t)va_arg( args
, int64_t );
1393 case ES_OUT_SET_GROUP_PCR
: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1394 p_cmd
->u
.control
.u
.int_i64
.i_int
= (int)va_arg( args
, int );
1395 p_cmd
->u
.control
.u
.int_i64
.i_i64
= (int64_t)va_arg( args
, int64_t );
1398 case ES_OUT_SET_ES_SCRAMBLED_STATE
:
1399 p_cmd
->u
.control
.u
.es_bool
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1400 p_cmd
->u
.control
.u
.es_bool
.b_bool
= (bool)va_arg( args
, int );
1403 case ES_OUT_RESET_PCR
: /* no arg */
1404 case ES_OUT_SET_EOS
:
1405 case ES_OUT_RESTART_ALL_ES
:
1408 case ES_OUT_SET_META
: /* arg1=const vlc_meta_t* */
1409 case ES_OUT_SET_GROUP_META
: /* arg1=int i_group arg2=const vlc_meta_t* */
1411 if( i_query
== ES_OUT_SET_GROUP_META
)
1412 p_cmd
->u
.control
.u
.int_meta
.i_int
= (int)va_arg( args
, int );
1413 const vlc_meta_t
*p_meta
= va_arg( args
, const vlc_meta_t
* );
1417 p_cmd
->u
.control
.u
.int_meta
.p_meta
= vlc_meta_New();
1418 if( !p_cmd
->u
.control
.u
.int_meta
.p_meta
)
1419 return VLC_EGENERIC
;
1420 vlc_meta_Merge( p_cmd
->u
.control
.u
.int_meta
.p_meta
, p_meta
);
1424 /* The cast is only needed to avoid warning */
1425 p_cmd
->u
.control
.u
.int_meta
.p_meta
= (vlc_meta_t
*)p_meta
;
1430 case ES_OUT_SET_GROUP_EPG
: /* arg1=int i_group arg2=const vlc_epg_t* */
1432 p_cmd
->u
.control
.u
.int_epg
.i_int
= (int)va_arg( args
, int );
1433 const vlc_epg_t
*p_epg
= va_arg( args
, const vlc_epg_t
* );
1437 p_cmd
->u
.control
.u
.int_epg
.p_epg
= vlc_epg_Duplicate( p_epg
);
1438 if( !p_cmd
->u
.control
.u
.int_epg
.p_epg
)
1439 return VLC_EGENERIC
;
1443 /* The cast is only needed to avoid warning */
1444 p_cmd
->u
.control
.u
.int_epg
.p_epg
= (vlc_epg_t
*)p_epg
;
1448 case ES_OUT_SET_GROUP_EPG_EVENT
: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1450 p_cmd
->u
.control
.u
.int_epg_evt
.i_int
= (int)va_arg( args
, int );
1451 const vlc_epg_event_t
*p_evt
= va_arg( args
, const vlc_epg_event_t
* );
1455 p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
= vlc_epg_event_Duplicate( p_evt
);
1456 if( !p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
)
1457 return VLC_EGENERIC
;
1461 /* The cast is only needed to avoid warning */
1462 p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
= (vlc_epg_event_t
*)p_evt
;
1466 case ES_OUT_SET_EPG_TIME
: /* arg1=int64_t (seconds) */
1467 p_cmd
->u
.control
.u
.i_i64
= (int64_t)va_arg( args
, int64_t );
1470 /* Modified control */
1471 case ES_OUT_SET_ES
: /* arg1= es_out_id_t* */
1472 case ES_OUT_RESTART_ES
: /* arg1= es_out_id_t* */
1473 case ES_OUT_SET_ES_DEFAULT
: /* arg1= es_out_id_t* */
1474 p_cmd
->u
.control
.u
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1477 case ES_OUT_SET_ES_CAT_POLICY
:
1478 p_cmd
->u
.control
.u
.es_policy
.i_cat
= (int) va_arg( args
, int );
1479 p_cmd
->u
.control
.u
.es_policy
.i_policy
= (int) va_arg( args
, int );
1482 case ES_OUT_SET_ES_STATE
:/* arg1= es_out_id_t* arg2=bool */
1483 p_cmd
->u
.control
.u
.es_bool
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1484 p_cmd
->u
.control
.u
.es_bool
.b_bool
= (bool)va_arg( args
, int );
1487 case ES_OUT_SET_ES_FMT
: /* arg1= es_out_id_t* arg2=es_format_t* */
1489 p_cmd
->u
.control
.u
.es_fmt
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1490 es_format_t
*p_fmt
= (es_format_t
*)va_arg( args
, es_format_t
* );
1494 p_cmd
->u
.control
.u
.es_fmt
.p_fmt
= malloc( sizeof(*p_fmt
) );
1495 if( !p_cmd
->u
.control
.u
.es_fmt
.p_fmt
)
1496 return VLC_EGENERIC
;
1497 es_format_Copy( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
, p_fmt
);
1501 p_cmd
->u
.control
.u
.es_fmt
.p_fmt
= p_fmt
;
1505 case ES_OUT_SET_TIMES
:
1507 double f_position
= (double)va_arg( args
, double );
1508 mtime_t i_time
= (mtime_t
)va_arg( args
, mtime_t
);
1509 mtime_t i_length
= (mtime_t
)va_arg( args
, mtime_t
);
1511 p_cmd
->u
.control
.u
.times
.f_position
= f_position
;
1512 p_cmd
->u
.control
.u
.times
.i_time
= i_time
;
1513 p_cmd
->u
.control
.u
.times
.i_length
= i_length
;
1516 case ES_OUT_SET_JITTER
:
1518 mtime_t i_pts_delay
= (mtime_t
)va_arg( args
, mtime_t
);
1519 mtime_t i_pts_jitter
= (mtime_t
)va_arg( args
, mtime_t
);
1520 int i_cr_average
= (int)va_arg( args
, int );
1522 p_cmd
->u
.control
.u
.jitter
.i_pts_delay
= i_pts_delay
;
1523 p_cmd
->u
.control
.u
.jitter
.i_pts_jitter
= i_pts_jitter
;
1524 p_cmd
->u
.control
.u
.jitter
.i_cr_average
= i_cr_average
;
1529 vlc_assert_unreachable();
1530 return VLC_EGENERIC
;
1535 static int CmdExecuteControl( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1537 const int i_query
= p_cmd
->u
.control
.i_query
;
1541 /* Pass-through control */
1542 case ES_OUT_SET_MODE
: /* arg1= int */
1543 case ES_OUT_SET_GROUP
: /* arg1= int */
1544 case ES_OUT_DEL_GROUP
: /* arg1=int i_group */
1545 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.i_int
);
1547 case ES_OUT_SET_PCR
: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1548 case ES_OUT_SET_NEXT_DISPLAY_TIME
: /* arg1=int64_t i_pts(microsecond) */
1549 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.i_i64
);
1551 case ES_OUT_SET_GROUP_PCR
: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1552 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_i64
.i_int
,
1553 p_cmd
->u
.control
.u
.int_i64
.i_i64
);
1555 case ES_OUT_RESET_PCR
: /* no arg */
1556 case ES_OUT_SET_EOS
:
1557 case ES_OUT_RESTART_ALL_ES
:
1558 return es_out_Control( p_out
, i_query
);
1560 case ES_OUT_SET_GROUP_META
: /* arg1=int i_group arg2=const vlc_meta_t* */
1561 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_meta
.i_int
,
1562 p_cmd
->u
.control
.u
.int_meta
.p_meta
);
1564 case ES_OUT_SET_GROUP_EPG
: /* arg1=int i_group arg2=const vlc_epg_t* */
1565 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_epg
.i_int
,
1566 p_cmd
->u
.control
.u
.int_epg
.p_epg
);
1568 case ES_OUT_SET_GROUP_EPG_EVENT
: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1569 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_epg_evt
.i_int
,
1570 p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
);
1572 case ES_OUT_SET_EPG_TIME
: /* arg1=int64_t */
1573 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.i_i64
);
1575 case ES_OUT_SET_ES_SCRAMBLED_STATE
: /* arg1=int es_out_id_t* arg2=bool */
1576 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_bool
.p_es
->p_es
,
1577 p_cmd
->u
.control
.u
.es_bool
.b_bool
);
1579 case ES_OUT_SET_META
: /* arg1=const vlc_meta_t* */
1580 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_meta
.p_meta
);
1582 /* Modified control */
1583 case ES_OUT_SET_ES
: /* arg1= es_out_id_t* */
1584 case ES_OUT_RESTART_ES
: /* arg1= es_out_id_t* */
1585 case ES_OUT_SET_ES_DEFAULT
: /* arg1= es_out_id_t* */
1586 return es_out_Control( p_out
, i_query
, !p_cmd
->u
.control
.u
.p_es
? NULL
:
1587 p_cmd
->u
.control
.u
.p_es
->p_es
);
1589 case ES_OUT_SET_ES_STATE
:/* arg1= es_out_id_t* arg2=bool */
1590 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_bool
.p_es
->p_es
,
1591 p_cmd
->u
.control
.u
.es_bool
.b_bool
);
1593 case ES_OUT_SET_ES_CAT_POLICY
:
1594 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_policy
.i_cat
,
1595 p_cmd
->u
.control
.u
.es_policy
.i_policy
);
1597 case ES_OUT_SET_ES_FMT
: /* arg1= es_out_id_t* arg2=es_format_t* */
1598 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_fmt
.p_es
->p_es
,
1599 p_cmd
->u
.control
.u
.es_fmt
.p_fmt
);
1601 case ES_OUT_SET_TIMES
:
1602 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.times
.f_position
,
1603 p_cmd
->u
.control
.u
.times
.i_time
,
1604 p_cmd
->u
.control
.u
.times
.i_length
);
1605 case ES_OUT_SET_JITTER
:
1606 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.jitter
.i_pts_delay
,
1607 p_cmd
->u
.control
.u
.jitter
.i_pts_jitter
,
1608 p_cmd
->u
.control
.u
.jitter
.i_cr_average
);
1611 vlc_assert_unreachable();
1612 return VLC_EGENERIC
;
1615 static void CmdCleanControl( ts_cmd_t
*p_cmd
)
1617 switch( p_cmd
->u
.control
.i_query
)
1619 case ES_OUT_SET_GROUP_META
:
1620 case ES_OUT_SET_META
:
1621 if( p_cmd
->u
.control
.u
.int_meta
.p_meta
)
1622 vlc_meta_Delete( p_cmd
->u
.control
.u
.int_meta
.p_meta
);
1624 case ES_OUT_SET_GROUP_EPG
:
1625 if( p_cmd
->u
.control
.u
.int_epg
.p_epg
)
1626 vlc_epg_Delete( p_cmd
->u
.control
.u
.int_epg
.p_epg
);
1628 case ES_OUT_SET_GROUP_EPG_EVENT
:
1629 if( p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
)
1630 vlc_epg_event_Delete( p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
);
1632 case ES_OUT_SET_ES_FMT
:
1633 if( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
)
1635 es_format_Clean( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
);
1636 free( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
);
1642 static int GetTmpFile( char **filename
, const char *dirname
)
1645 && asprintf( filename
, "%s"DIR_SEP PACKAGE_NAME
"-timeshift.XXXXXX",
1648 vlc_mkdir( dirname
, 0700 );
1650 int fd
= vlc_mkstemp( *filename
);
1657 *filename
= strdup( DIR_SEP
"tmp"DIR_SEP PACKAGE_NAME
"-timeshift.XXXXXX" );
1658 if( unlikely(*filename
== NULL
) )
1661 int fd
= vlc_mkstemp( *filename
);