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
= malloc( (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_SET_ES_DEFAULT
:
648 case ES_OUT_SET_ES_STATE
:
649 case ES_OUT_SET_ES_CAT_POLICY
:
650 case ES_OUT_SET_ES_FMT
:
651 case ES_OUT_SET_TIMES
:
652 case ES_OUT_SET_JITTER
:
656 if( CmdInitControl( &cmd
, i_query
, args
, p_sys
->b_delayed
) )
658 if( p_sys
->b_delayed
)
660 TsPushCmd( p_sys
->p_ts
, &cmd
);
663 return CmdExecuteControl( p_sys
->p_out
, &cmd
);
666 /* Special control when delayed */
667 case ES_OUT_GET_ES_STATE
:
669 es_out_id_t
*p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
670 bool *pb_enabled
= (bool*)va_arg( args
, bool* );
672 if( p_sys
->b_delayed
)
677 return es_out_Control( p_sys
->p_out
, ES_OUT_GET_ES_STATE
, p_es
->p_es
, pb_enabled
);
679 /* Special internal input control */
680 case ES_OUT_GET_EMPTY
:
682 bool *pb_empty
= (bool*)va_arg( args
, bool* );
683 return ControlLockedGetEmpty( p_out
, pb_empty
);
685 case ES_OUT_GET_WAKE_UP
: /* TODO ? */
687 mtime_t
*pi_wakeup
= (mtime_t
*)va_arg( args
, mtime_t
* );
688 return ControlLockedGetWakeup( p_out
, pi_wakeup
);
690 case ES_OUT_GET_BUFFERING
:
692 bool *pb_buffering
= (bool *)va_arg( args
, bool* );
693 return ControlLockedGetBuffering( p_out
, pb_buffering
);
695 case ES_OUT_SET_PAUSE_STATE
:
697 const bool b_source_paused
= (bool)va_arg( args
, int );
698 const bool b_paused
= (bool)va_arg( args
, int );
699 const mtime_t i_date
= (mtime_t
) va_arg( args
, mtime_t
);
701 return ControlLockedSetPauseState( p_out
, b_source_paused
, b_paused
, i_date
);
703 case ES_OUT_SET_RATE
:
705 const int i_src_rate
= (int)va_arg( args
, int );
706 const int i_rate
= (int)va_arg( args
, int );
708 return ControlLockedSetRate( p_out
, i_src_rate
, i_rate
);
710 case ES_OUT_SET_TIME
:
712 const mtime_t i_date
= (mtime_t
)va_arg( args
, mtime_t
);
714 return ControlLockedSetTime( p_out
, i_date
);
716 case ES_OUT_SET_FRAME_NEXT
:
718 return ControlLockedSetFrameNext( p_out
);
721 case ES_OUT_GET_PCR_SYSTEM
:
722 if( p_sys
->b_delayed
)
725 case ES_OUT_GET_GROUP_FORCED
:
726 case ES_OUT_POST_SUBNODE
:
727 return es_out_vaControl( p_sys
->p_out
, i_query
, args
);
729 case ES_OUT_MODIFY_PCR_SYSTEM
:
731 const bool b_absolute
= va_arg( args
, int );
732 const mtime_t i_system
= va_arg( args
, mtime_t
);
734 if( b_absolute
&& p_sys
->b_delayed
)
737 return es_out_ControlModifyPcrSystem( p_sys
->p_out
, b_absolute
, i_system
);
740 /* Invalid queries for this es_out level */
741 case ES_OUT_SET_ES_BY_ID
:
742 case ES_OUT_RESTART_ES_BY_ID
:
743 case ES_OUT_SET_ES_DEFAULT_BY_ID
:
744 case ES_OUT_GET_ES_OBJECTS_BY_ID
:
745 case ES_OUT_SET_DELAY
:
746 case ES_OUT_SET_RECORD_STATE
:
748 vlc_assert_unreachable();
752 static int Control( es_out_t
*p_out
, int i_query
, va_list args
)
754 es_out_sys_t
*p_sys
= p_out
->p_sys
;
757 vlc_mutex_lock( &p_sys
->lock
);
761 i_ret
= ControlLocked( p_out
, i_query
, args
);
763 vlc_mutex_unlock( &p_sys
->lock
);
768 /*****************************************************************************
770 *****************************************************************************/
771 static void TsDestroy( ts_thread_t
*p_ts
)
773 vlc_cond_destroy( &p_ts
->wait
);
774 vlc_mutex_destroy( &p_ts
->lock
);
777 static int TsStart( es_out_t
*p_out
)
779 es_out_sys_t
*p_sys
= p_out
->p_sys
;
782 assert( !p_sys
->b_delayed
);
784 p_sys
->p_ts
= p_ts
= calloc(1, sizeof(*p_ts
));
788 p_ts
->i_tmp_size_max
= p_sys
->i_tmp_size_max
;
789 p_ts
->psz_tmp_path
= p_sys
->psz_tmp_path
;
790 p_ts
->p_input
= p_sys
->p_input
;
791 p_ts
->p_out
= p_sys
->p_out
;
792 vlc_mutex_init( &p_ts
->lock
);
793 vlc_cond_init( &p_ts
->wait
);
794 p_ts
->b_paused
= p_sys
->b_input_paused
&& !p_sys
->b_input_paused_source
;
795 p_ts
->i_pause_date
= p_ts
->b_paused
? mdate() : -1;
796 p_ts
->i_rate_source
= p_sys
->i_input_rate_source
;
797 p_ts
->i_rate
= p_sys
->i_input_rate
;
798 p_ts
->i_rate_date
= -1;
799 p_ts
->i_rate_delay
= 0;
800 p_ts
->i_buffering_delay
= 0;
801 p_ts
->i_cmd_delay
= 0;
802 p_ts
->p_storage_r
= NULL
;
803 p_ts
->p_storage_w
= NULL
;
805 p_sys
->b_delayed
= true;
806 if( vlc_clone( &p_ts
->thread
, TsRun
, p_ts
, VLC_THREAD_PRIORITY_INPUT
) )
808 msg_Err( p_sys
->p_input
, "cannot create timeshift thread" );
812 p_sys
->b_delayed
= false;
818 static void TsAutoStop( es_out_t
*p_out
)
820 es_out_sys_t
*p_sys
= p_out
->p_sys
;
822 if( !p_sys
->b_delayed
|| !TsIsUnused( p_sys
->p_ts
) )
825 msg_Warn( p_sys
->p_input
, "es out timeshift: auto stop" );
826 TsStop( p_sys
->p_ts
);
828 p_sys
->b_delayed
= false;
830 static void TsStop( ts_thread_t
*p_ts
)
832 vlc_cancel( p_ts
->thread
);
833 vlc_join( p_ts
->thread
, NULL
);
835 vlc_mutex_lock( &p_ts
->lock
);
840 if( TsPopCmdLocked( p_ts
, &cmd
, true ) )
845 assert( !p_ts
->p_storage_r
|| !p_ts
->p_storage_r
->p_next
);
846 if( p_ts
->p_storage_r
)
847 TsStorageDelete( p_ts
->p_storage_r
);
848 vlc_mutex_unlock( &p_ts
->lock
);
852 static void TsPushCmd( ts_thread_t
*p_ts
, ts_cmd_t
*p_cmd
)
854 vlc_mutex_lock( &p_ts
->lock
);
856 if( !p_ts
->p_storage_w
|| TsStorageIsFull( p_ts
->p_storage_w
, p_cmd
) )
858 ts_storage_t
*p_storage
= TsStorageNew( p_ts
->psz_tmp_path
, p_ts
->i_tmp_size_max
);
863 vlc_mutex_unlock( &p_ts
->lock
);
864 /* TODO warn the user (but only once) */
868 if( !p_ts
->p_storage_w
)
870 p_ts
->p_storage_r
= p_ts
->p_storage_w
= p_storage
;
874 TsStoragePack( p_ts
->p_storage_w
);
875 p_ts
->p_storage_w
->p_next
= p_storage
;
876 p_ts
->p_storage_w
= p_storage
;
880 /* TODO return error and warn the user (but only once) */
881 TsStoragePushCmd( p_ts
->p_storage_w
, p_cmd
, p_ts
->p_storage_r
== p_ts
->p_storage_w
);
883 vlc_cond_signal( &p_ts
->wait
);
885 vlc_mutex_unlock( &p_ts
->lock
);
887 static int TsPopCmdLocked( ts_thread_t
*p_ts
, ts_cmd_t
*p_cmd
, bool b_flush
)
889 vlc_assert_locked( &p_ts
->lock
);
891 if( TsStorageIsEmpty( p_ts
->p_storage_r
) )
894 TsStoragePopCmd( p_ts
->p_storage_r
, p_cmd
, b_flush
);
896 while( p_ts
->p_storage_r
&& TsStorageIsEmpty( p_ts
->p_storage_r
) )
898 ts_storage_t
*p_next
= p_ts
->p_storage_r
->p_next
;
902 TsStorageDelete( p_ts
->p_storage_r
);
903 p_ts
->p_storage_r
= p_next
;
908 static bool TsHasCmd( ts_thread_t
*p_ts
)
912 vlc_mutex_lock( &p_ts
->lock
);
913 b_cmd
= TsStorageIsEmpty( p_ts
->p_storage_r
);
914 vlc_mutex_unlock( &p_ts
->lock
);
918 static bool TsIsUnused( ts_thread_t
*p_ts
)
922 vlc_mutex_lock( &p_ts
->lock
);
923 b_unused
= !p_ts
->b_paused
&&
924 p_ts
->i_rate
== p_ts
->i_rate_source
&&
925 TsStorageIsEmpty( p_ts
->p_storage_r
);
926 vlc_mutex_unlock( &p_ts
->lock
);
930 static int TsChangePause( ts_thread_t
*p_ts
, bool b_source_paused
, bool b_paused
, mtime_t i_date
)
932 vlc_mutex_lock( &p_ts
->lock
);
937 assert( !b_source_paused
);
938 i_ret
= es_out_SetPauseState( p_ts
->p_out
, true, true, i_date
);
942 i_ret
= es_out_SetPauseState( p_ts
->p_out
, false, false, i_date
);
949 assert( p_ts
->i_pause_date
> 0 );
951 p_ts
->i_cmd_delay
+= i_date
- p_ts
->i_pause_date
;
954 p_ts
->b_paused
= b_paused
;
955 p_ts
->i_pause_date
= i_date
;
957 vlc_cond_signal( &p_ts
->wait
);
959 vlc_mutex_unlock( &p_ts
->lock
);
962 static int TsChangeRate( ts_thread_t
*p_ts
, int i_src_rate
, int i_rate
)
966 vlc_mutex_lock( &p_ts
->lock
);
967 p_ts
->i_cmd_delay
+= p_ts
->i_rate_delay
;
969 p_ts
->i_rate_date
= -1;
970 p_ts
->i_rate_delay
= 0;
971 p_ts
->i_rate
= i_rate
;
972 p_ts
->i_rate_source
= i_src_rate
;
974 i_ret
= es_out_SetRate( p_ts
->p_out
, i_rate
, i_rate
);
975 vlc_mutex_unlock( &p_ts
->lock
);
980 static void *TsRun( void *p_data
)
982 ts_thread_t
*p_ts
= p_data
;
983 mtime_t i_buffering_date
= -1;
991 /* Pop a command to execute */
992 vlc_mutex_lock( &p_ts
->lock
);
993 mutex_cleanup_push( &p_ts
->lock
);
997 const int canc
= vlc_savecancel();
998 b_buffering
= es_out_GetBuffering( p_ts
->p_out
);
1000 if( ( !p_ts
->b_paused
|| b_buffering
) && !TsPopCmdLocked( p_ts
, &cmd
, false ) )
1002 vlc_restorecancel( canc
);
1005 vlc_restorecancel( canc
);
1007 vlc_cond_wait( &p_ts
->wait
, &p_ts
->lock
);
1010 if( b_buffering
&& i_buffering_date
< 0 )
1012 i_buffering_date
= cmd
.i_date
;
1014 else if( i_buffering_date
> 0 )
1016 p_ts
->i_buffering_delay
+= i_buffering_date
- cmd
.i_date
; /* It is < 0 */
1018 i_buffering_date
= cmd
.i_date
;
1020 i_buffering_date
= -1;
1023 if( p_ts
->i_rate_date
< 0 )
1024 p_ts
->i_rate_date
= cmd
.i_date
;
1026 p_ts
->i_rate_delay
= 0;
1027 if( p_ts
->i_rate_source
!= p_ts
->i_rate
)
1029 const mtime_t i_duration
= cmd
.i_date
- p_ts
->i_rate_date
;
1030 p_ts
->i_rate_delay
= i_duration
* p_ts
->i_rate
/ p_ts
->i_rate_source
- i_duration
;
1032 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
)
1034 const int canc
= vlc_savecancel();
1036 /* Auto reset to rate 1.0 */
1037 msg_Warn( p_ts
->p_input
, "es out timeshift: auto reset rate to %d", p_ts
->i_rate_source
);
1039 p_ts
->i_cmd_delay
= 0;
1040 p_ts
->i_buffering_delay
= 0;
1042 p_ts
->i_rate_delay
= 0;
1043 p_ts
->i_rate_date
= -1;
1044 p_ts
->i_rate
= p_ts
->i_rate_source
;
1046 if( !es_out_SetRate( p_ts
->p_out
, p_ts
->i_rate_source
, p_ts
->i_rate
) )
1048 vlc_value_t val
= { .i_int
= p_ts
->i_rate
};
1050 * FIXME it is perfectly safe BUT it is ugly as it may hide a
1051 * rate change requested by user */
1052 input_ControlPush( p_ts
->p_input
, INPUT_CONTROL_SET_RATE
, &val
);
1055 vlc_restorecancel( canc
);
1057 i_deadline
= cmd
.i_date
+ p_ts
->i_cmd_delay
+ p_ts
->i_rate_delay
+ p_ts
->i_buffering_delay
;
1060 vlc_mutex_unlock( &p_ts
->lock
);
1062 /* Regulate the speed of command processing to the same one than
1064 vlc_cleanup_push( cmd_cleanup_routine
, &cmd
);
1066 mwait( i_deadline
);
1070 /* Execute the command */
1071 const int canc
= vlc_savecancel();
1072 switch( cmd
.i_type
)
1075 CmdExecuteAdd( p_ts
->p_out
, &cmd
);
1076 CmdCleanAdd( &cmd
);
1079 CmdExecuteSend( p_ts
->p_out
, &cmd
);
1080 CmdCleanSend( &cmd
);
1083 CmdExecuteControl( p_ts
->p_out
, &cmd
);
1084 CmdCleanControl( &cmd
);
1087 CmdExecuteDel( p_ts
->p_out
, &cmd
);
1090 vlc_assert_unreachable();
1093 vlc_restorecancel( canc
);
1099 /*****************************************************************************
1101 *****************************************************************************/
1102 static ts_storage_t
*TsStorageNew( const char *psz_tmp_path
, int64_t i_tmp_size_max
)
1104 ts_storage_t
*p_storage
= malloc( sizeof (*p_storage
) );
1105 if( unlikely(p_storage
== NULL
) )
1109 int fd
= GetTmpFile( &psz_file
, psz_tmp_path
);
1116 p_storage
->p_filew
= fdopen( fd
, "w+b" );
1117 if( p_storage
->p_filew
== NULL
)
1120 vlc_unlink( psz_file
);
1124 p_storage
->p_filer
= vlc_fopen( psz_file
, "rb" );
1125 if( p_storage
->p_filer
== NULL
)
1127 fclose( p_storage
->p_filew
);
1128 vlc_unlink( psz_file
);
1133 vlc_unlink( psz_file
);
1136 p_storage
->psz_file
= psz_file
;
1138 p_storage
->p_next
= NULL
;
1141 p_storage
->i_file_max
= i_tmp_size_max
;
1142 p_storage
->i_file_size
= 0;
1145 p_storage
->i_cmd_w
= 0;
1146 p_storage
->i_cmd_r
= 0;
1147 p_storage
->i_cmd_max
= 30000;
1148 p_storage
->p_cmd
= malloc( p_storage
->i_cmd_max
* sizeof(*p_storage
->p_cmd
) );
1149 //fprintf( stderr, "\nSTORAGE name=%s size=%d KiB\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1151 if( !p_storage
->p_cmd
)
1153 TsStorageDelete( p_storage
);
1163 static void TsStorageDelete( ts_storage_t
*p_storage
)
1165 while( p_storage
->i_cmd_r
< p_storage
->i_cmd_w
)
1169 TsStoragePopCmd( p_storage
, &cmd
, true );
1173 free( p_storage
->p_cmd
);
1175 fclose( p_storage
->p_filer
);
1176 fclose( p_storage
->p_filew
);
1178 vlc_unlink( p_storage
->psz_file
);
1179 free( p_storage
->psz_file
);
1184 static void TsStoragePack( ts_storage_t
*p_storage
)
1186 /* Try to release a bit of memory */
1187 if( p_storage
->i_cmd_w
>= p_storage
->i_cmd_max
)
1190 p_storage
->i_cmd_max
= __MAX( p_storage
->i_cmd_w
, 1 );
1192 ts_cmd_t
*p_new
= realloc( p_storage
->p_cmd
, p_storage
->i_cmd_max
* sizeof(*p_storage
->p_cmd
) );
1194 p_storage
->p_cmd
= p_new
;
1196 static bool TsStorageIsFull( ts_storage_t
*p_storage
, const ts_cmd_t
*p_cmd
)
1198 if( p_cmd
&& p_cmd
->i_type
== C_SEND
&& p_storage
->i_cmd_w
> 0 )
1200 size_t i_size
= sizeof(*p_cmd
->u
.send
.p_block
) + p_cmd
->u
.send
.p_block
->i_buffer
;
1202 if( p_storage
->i_file_size
+ i_size
>= p_storage
->i_file_max
)
1205 return p_storage
->i_cmd_w
>= p_storage
->i_cmd_max
;
1207 static bool TsStorageIsEmpty( ts_storage_t
*p_storage
)
1209 return !p_storage
|| p_storage
->i_cmd_r
>= p_storage
->i_cmd_w
;
1211 static void TsStoragePushCmd( ts_storage_t
*p_storage
, const ts_cmd_t
*p_cmd
, bool b_flush
)
1213 ts_cmd_t cmd
= *p_cmd
;
1215 assert( !TsStorageIsFull( p_storage
, p_cmd
) );
1217 if( cmd
.i_type
== C_SEND
)
1219 block_t
*p_block
= cmd
.u
.send
.p_block
;
1221 cmd
.u
.send
.p_block
= NULL
;
1222 cmd
.u
.send
.i_offset
= ftell( p_storage
->p_filew
);
1224 if( fwrite( p_block
, sizeof(*p_block
), 1, p_storage
->p_filew
) != 1 )
1226 block_Release( p_block
);
1229 p_storage
->i_file_size
+= sizeof(*p_block
);
1230 if( p_block
->i_buffer
> 0 )
1232 if( fwrite( p_block
->p_buffer
, p_block
->i_buffer
, 1, p_storage
->p_filew
) != 1 )
1234 block_Release( p_block
);
1238 p_storage
->i_file_size
+= p_block
->i_buffer
;
1239 block_Release( p_block
);
1242 fflush( p_storage
->p_filew
);
1244 p_storage
->p_cmd
[p_storage
->i_cmd_w
++] = cmd
;
1246 static void TsStoragePopCmd( ts_storage_t
*p_storage
, ts_cmd_t
*p_cmd
, bool b_flush
)
1248 assert( !TsStorageIsEmpty( p_storage
) );
1250 *p_cmd
= p_storage
->p_cmd
[p_storage
->i_cmd_r
++];
1251 if( p_cmd
->i_type
== C_SEND
)
1256 !fseek( p_storage
->p_filer
, p_cmd
->u
.send
.i_offset
, SEEK_SET
) &&
1257 fread( &block
, sizeof(block
), 1, p_storage
->p_filer
) == 1 )
1259 block_t
*p_block
= block_Alloc( block
.i_buffer
);
1262 p_block
->i_dts
= block
.i_dts
;
1263 p_block
->i_pts
= block
.i_pts
;
1264 p_block
->i_flags
= block
.i_flags
;
1265 p_block
->i_length
= block
.i_length
;
1266 p_block
->i_nb_samples
= block
.i_nb_samples
;
1267 p_block
->i_buffer
= fread( p_block
->p_buffer
, 1, block
.i_buffer
, p_storage
->p_filer
);
1269 p_cmd
->u
.send
.p_block
= p_block
;
1273 //perror( "TsStoragePopCmd" );
1274 p_cmd
->u
.send
.p_block
= block_Alloc( 1 );
1279 /*****************************************************************************
1281 *****************************************************************************/
1282 static void CmdClean( ts_cmd_t
*p_cmd
)
1284 switch( p_cmd
->i_type
)
1287 CmdCleanAdd( p_cmd
);
1290 CmdCleanSend( p_cmd
);
1293 CmdCleanControl( p_cmd
);
1298 vlc_assert_unreachable();
1303 static int CmdInitAdd( ts_cmd_t
*p_cmd
, es_out_id_t
*p_es
, const es_format_t
*p_fmt
, bool b_copy
)
1305 p_cmd
->i_type
= C_ADD
;
1306 p_cmd
->i_date
= mdate();
1307 p_cmd
->u
.add
.p_es
= p_es
;
1310 p_cmd
->u
.add
.p_fmt
= malloc( sizeof(*p_fmt
) );
1311 if( !p_cmd
->u
.add
.p_fmt
)
1312 return VLC_EGENERIC
;
1313 es_format_Copy( p_cmd
->u
.add
.p_fmt
, p_fmt
);
1317 p_cmd
->u
.add
.p_fmt
= (es_format_t
*)p_fmt
;
1321 static void CmdExecuteAdd( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1323 p_cmd
->u
.add
.p_es
->p_es
= es_out_Add( p_out
, p_cmd
->u
.add
.p_fmt
);
1325 static void CmdCleanAdd( ts_cmd_t
*p_cmd
)
1327 es_format_Clean( p_cmd
->u
.add
.p_fmt
);
1328 free( p_cmd
->u
.add
.p_fmt
);
1331 static void CmdInitSend( ts_cmd_t
*p_cmd
, es_out_id_t
*p_es
, block_t
*p_block
)
1333 p_cmd
->i_type
= C_SEND
;
1334 p_cmd
->i_date
= mdate();
1335 p_cmd
->u
.send
.p_es
= p_es
;
1336 p_cmd
->u
.send
.p_block
= p_block
;
1338 static int CmdExecuteSend( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1340 block_t
*p_block
= p_cmd
->u
.send
.p_block
;
1342 p_cmd
->u
.send
.p_block
= NULL
;
1346 if( p_cmd
->u
.send
.p_es
->p_es
)
1347 return es_out_Send( p_out
, p_cmd
->u
.send
.p_es
->p_es
, p_block
);
1348 block_Release( p_block
);
1350 return VLC_EGENERIC
;
1352 static void CmdCleanSend( ts_cmd_t
*p_cmd
)
1354 if( p_cmd
->u
.send
.p_block
)
1355 block_Release( p_cmd
->u
.send
.p_block
);
1358 static int CmdInitDel( ts_cmd_t
*p_cmd
, es_out_id_t
*p_es
)
1360 p_cmd
->i_type
= C_DEL
;
1361 p_cmd
->i_date
= mdate();
1362 p_cmd
->u
.del
.p_es
= p_es
;
1365 static void CmdExecuteDel( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1367 if( p_cmd
->u
.del
.p_es
->p_es
)
1368 es_out_Del( p_out
, p_cmd
->u
.del
.p_es
->p_es
);
1369 free( p_cmd
->u
.del
.p_es
);
1372 static int CmdInitControl( ts_cmd_t
*p_cmd
, int i_query
, va_list args
, bool b_copy
)
1374 p_cmd
->i_type
= C_CONTROL
;
1375 p_cmd
->i_date
= mdate();
1376 p_cmd
->u
.control
.i_query
= i_query
;
1380 /* Pass-through control */
1381 case ES_OUT_SET_MODE
: /* arg1= int */
1382 case ES_OUT_SET_GROUP
: /* arg1= int */
1383 case ES_OUT_DEL_GROUP
: /* arg1=int i_group */
1384 p_cmd
->u
.control
.u
.i_int
= (int)va_arg( args
, int );
1387 case ES_OUT_SET_PCR
: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1388 case ES_OUT_SET_NEXT_DISPLAY_TIME
: /* arg1=int64_t i_pts(microsecond) */
1389 p_cmd
->u
.control
.u
.i_i64
= (int64_t)va_arg( args
, int64_t );
1392 case ES_OUT_SET_GROUP_PCR
: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1393 p_cmd
->u
.control
.u
.int_i64
.i_int
= (int)va_arg( args
, int );
1394 p_cmd
->u
.control
.u
.int_i64
.i_i64
= (int64_t)va_arg( args
, int64_t );
1397 case ES_OUT_SET_ES_SCRAMBLED_STATE
:
1398 p_cmd
->u
.control
.u
.es_bool
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1399 p_cmd
->u
.control
.u
.es_bool
.b_bool
= (bool)va_arg( args
, int );
1402 case ES_OUT_RESET_PCR
: /* no arg */
1403 case ES_OUT_SET_EOS
:
1406 case ES_OUT_SET_META
: /* arg1=const vlc_meta_t* */
1407 case ES_OUT_SET_GROUP_META
: /* arg1=int i_group arg2=const vlc_meta_t* */
1409 if( i_query
== ES_OUT_SET_GROUP_META
)
1410 p_cmd
->u
.control
.u
.int_meta
.i_int
= (int)va_arg( args
, int );
1411 const vlc_meta_t
*p_meta
= va_arg( args
, const vlc_meta_t
* );
1415 p_cmd
->u
.control
.u
.int_meta
.p_meta
= vlc_meta_New();
1416 if( !p_cmd
->u
.control
.u
.int_meta
.p_meta
)
1417 return VLC_EGENERIC
;
1418 vlc_meta_Merge( p_cmd
->u
.control
.u
.int_meta
.p_meta
, p_meta
);
1422 /* The cast is only needed to avoid warning */
1423 p_cmd
->u
.control
.u
.int_meta
.p_meta
= (vlc_meta_t
*)p_meta
;
1428 case ES_OUT_SET_GROUP_EPG
: /* arg1=int i_group arg2=const vlc_epg_t* */
1430 p_cmd
->u
.control
.u
.int_epg
.i_int
= (int)va_arg( args
, int );
1431 const vlc_epg_t
*p_epg
= va_arg( args
, const vlc_epg_t
* );
1435 p_cmd
->u
.control
.u
.int_epg
.p_epg
= vlc_epg_Duplicate( p_epg
);
1436 if( !p_cmd
->u
.control
.u
.int_epg
.p_epg
)
1437 return VLC_EGENERIC
;
1441 /* The cast is only needed to avoid warning */
1442 p_cmd
->u
.control
.u
.int_epg
.p_epg
= (vlc_epg_t
*)p_epg
;
1446 case ES_OUT_SET_GROUP_EPG_EVENT
: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1448 p_cmd
->u
.control
.u
.int_epg_evt
.i_int
= (int)va_arg( args
, int );
1449 const vlc_epg_event_t
*p_evt
= va_arg( args
, const vlc_epg_event_t
* );
1453 p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
= vlc_epg_event_Duplicate( p_evt
);
1454 if( !p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
)
1455 return VLC_EGENERIC
;
1459 /* The cast is only needed to avoid warning */
1460 p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
= (vlc_epg_event_t
*)p_evt
;
1464 case ES_OUT_SET_EPG_TIME
: /* arg1=int64_t (seconds) */
1465 p_cmd
->u
.control
.u
.i_i64
= (int64_t)va_arg( args
, int64_t );
1468 /* Modified control */
1469 case ES_OUT_SET_ES
: /* arg1= es_out_id_t* */
1470 case ES_OUT_RESTART_ES
: /* arg1= es_out_id_t* */
1471 case ES_OUT_SET_ES_DEFAULT
: /* arg1= es_out_id_t* */
1472 p_cmd
->u
.control
.u
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1475 case ES_OUT_SET_ES_CAT_POLICY
:
1476 p_cmd
->u
.control
.u
.es_policy
.i_cat
= (int) va_arg( args
, int );
1477 p_cmd
->u
.control
.u
.es_policy
.i_policy
= (int) va_arg( args
, int );
1480 case ES_OUT_SET_ES_STATE
:/* arg1= es_out_id_t* arg2=bool */
1481 p_cmd
->u
.control
.u
.es_bool
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1482 p_cmd
->u
.control
.u
.es_bool
.b_bool
= (bool)va_arg( args
, int );
1485 case ES_OUT_SET_ES_FMT
: /* arg1= es_out_id_t* arg2=es_format_t* */
1487 p_cmd
->u
.control
.u
.es_fmt
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1488 es_format_t
*p_fmt
= (es_format_t
*)va_arg( args
, es_format_t
* );
1492 p_cmd
->u
.control
.u
.es_fmt
.p_fmt
= malloc( sizeof(*p_fmt
) );
1493 if( !p_cmd
->u
.control
.u
.es_fmt
.p_fmt
)
1494 return VLC_EGENERIC
;
1495 es_format_Copy( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
, p_fmt
);
1499 p_cmd
->u
.control
.u
.es_fmt
.p_fmt
= p_fmt
;
1503 case ES_OUT_SET_TIMES
:
1505 double f_position
= (double)va_arg( args
, double );
1506 mtime_t i_time
= (mtime_t
)va_arg( args
, mtime_t
);
1507 mtime_t i_length
= (mtime_t
)va_arg( args
, mtime_t
);
1509 p_cmd
->u
.control
.u
.times
.f_position
= f_position
;
1510 p_cmd
->u
.control
.u
.times
.i_time
= i_time
;
1511 p_cmd
->u
.control
.u
.times
.i_length
= i_length
;
1514 case ES_OUT_SET_JITTER
:
1516 mtime_t i_pts_delay
= (mtime_t
)va_arg( args
, mtime_t
);
1517 mtime_t i_pts_jitter
= (mtime_t
)va_arg( args
, mtime_t
);
1518 int i_cr_average
= (int)va_arg( args
, int );
1520 p_cmd
->u
.control
.u
.jitter
.i_pts_delay
= i_pts_delay
;
1521 p_cmd
->u
.control
.u
.jitter
.i_pts_jitter
= i_pts_jitter
;
1522 p_cmd
->u
.control
.u
.jitter
.i_cr_average
= i_cr_average
;
1527 vlc_assert_unreachable();
1528 return VLC_EGENERIC
;
1533 static int CmdExecuteControl( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1535 const int i_query
= p_cmd
->u
.control
.i_query
;
1539 /* Pass-through control */
1540 case ES_OUT_SET_MODE
: /* arg1= int */
1541 case ES_OUT_SET_GROUP
: /* arg1= int */
1542 case ES_OUT_DEL_GROUP
: /* arg1=int i_group */
1543 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.i_int
);
1545 case ES_OUT_SET_PCR
: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1546 case ES_OUT_SET_NEXT_DISPLAY_TIME
: /* arg1=int64_t i_pts(microsecond) */
1547 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.i_i64
);
1549 case ES_OUT_SET_GROUP_PCR
: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1550 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_i64
.i_int
,
1551 p_cmd
->u
.control
.u
.int_i64
.i_i64
);
1553 case ES_OUT_RESET_PCR
: /* no arg */
1554 case ES_OUT_SET_EOS
:
1555 return es_out_Control( p_out
, i_query
);
1557 case ES_OUT_SET_GROUP_META
: /* arg1=int i_group arg2=const vlc_meta_t* */
1558 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_meta
.i_int
,
1559 p_cmd
->u
.control
.u
.int_meta
.p_meta
);
1561 case ES_OUT_SET_GROUP_EPG
: /* arg1=int i_group arg2=const vlc_epg_t* */
1562 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_epg
.i_int
,
1563 p_cmd
->u
.control
.u
.int_epg
.p_epg
);
1565 case ES_OUT_SET_GROUP_EPG_EVENT
: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1566 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_epg_evt
.i_int
,
1567 p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
);
1569 case ES_OUT_SET_EPG_TIME
: /* arg1=int64_t */
1570 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.i_i64
);
1572 case ES_OUT_SET_ES_SCRAMBLED_STATE
: /* arg1=int es_out_id_t* arg2=bool */
1573 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_bool
.p_es
->p_es
,
1574 p_cmd
->u
.control
.u
.es_bool
.b_bool
);
1576 case ES_OUT_SET_META
: /* arg1=const vlc_meta_t* */
1577 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_meta
.p_meta
);
1579 /* Modified control */
1580 case ES_OUT_SET_ES
: /* arg1= es_out_id_t* */
1581 case ES_OUT_RESTART_ES
: /* arg1= es_out_id_t* */
1582 case ES_OUT_SET_ES_DEFAULT
: /* arg1= es_out_id_t* */
1583 return es_out_Control( p_out
, i_query
, !p_cmd
->u
.control
.u
.p_es
? NULL
:
1584 p_cmd
->u
.control
.u
.p_es
->p_es
);
1586 case ES_OUT_SET_ES_STATE
:/* arg1= es_out_id_t* arg2=bool */
1587 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_bool
.p_es
->p_es
,
1588 p_cmd
->u
.control
.u
.es_bool
.b_bool
);
1590 case ES_OUT_SET_ES_CAT_POLICY
:
1591 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_policy
.i_cat
,
1592 p_cmd
->u
.control
.u
.es_policy
.i_policy
);
1594 case ES_OUT_SET_ES_FMT
: /* arg1= es_out_id_t* arg2=es_format_t* */
1595 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_fmt
.p_es
->p_es
,
1596 p_cmd
->u
.control
.u
.es_fmt
.p_fmt
);
1598 case ES_OUT_SET_TIMES
:
1599 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.times
.f_position
,
1600 p_cmd
->u
.control
.u
.times
.i_time
,
1601 p_cmd
->u
.control
.u
.times
.i_length
);
1602 case ES_OUT_SET_JITTER
:
1603 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.jitter
.i_pts_delay
,
1604 p_cmd
->u
.control
.u
.jitter
.i_pts_jitter
,
1605 p_cmd
->u
.control
.u
.jitter
.i_cr_average
);
1608 vlc_assert_unreachable();
1609 return VLC_EGENERIC
;
1612 static void CmdCleanControl( ts_cmd_t
*p_cmd
)
1614 switch( p_cmd
->u
.control
.i_query
)
1616 case ES_OUT_SET_GROUP_META
:
1617 case ES_OUT_SET_META
:
1618 if( p_cmd
->u
.control
.u
.int_meta
.p_meta
)
1619 vlc_meta_Delete( p_cmd
->u
.control
.u
.int_meta
.p_meta
);
1621 case ES_OUT_SET_GROUP_EPG
:
1622 if( p_cmd
->u
.control
.u
.int_epg
.p_epg
)
1623 vlc_epg_Delete( p_cmd
->u
.control
.u
.int_epg
.p_epg
);
1625 case ES_OUT_SET_GROUP_EPG_EVENT
:
1626 if( p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
)
1627 vlc_epg_event_Delete( p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
);
1629 case ES_OUT_SET_ES_FMT
:
1630 if( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
)
1632 es_format_Clean( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
);
1633 free( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
);
1639 static int GetTmpFile( char **filename
, const char *dirname
)
1642 && asprintf( filename
, "%s"DIR_SEP PACKAGE_NAME
"-timeshift.XXXXXX",
1645 vlc_mkdir( dirname
, 0700 );
1647 int fd
= vlc_mkstemp( *filename
);
1654 *filename
= strdup( DIR_SEP
"tmp"DIR_SEP PACKAGE_NAME
"-timeshift.XXXXXX" );
1655 if( unlikely(*filename
== NULL
) )
1658 int fd
= vlc_mkstemp( *filename
);