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) */
144 vlc_tick_t i_pts_delay
;
145 vlc_tick_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 vlc_tick_t i_pause_date
;
204 vlc_tick_t i_rate_date
;
205 vlc_tick_t i_rate_delay
;
208 vlc_tick_t i_buffering_delay
;
211 ts_storage_t
*p_storage_r
;
212 ts_storage_t
*p_storage_w
;
214 vlc_tick_t i_cmd_delay
;
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
;
252 static void Del ( es_out_t
*, es_out_id_t
* );
254 static int TsStart( es_out_t
* );
255 static void TsAutoStop( es_out_t
* );
257 static void TsStop( ts_thread_t
* );
258 static void TsPushCmd( ts_thread_t
*, ts_cmd_t
* );
259 static int TsPopCmdLocked( ts_thread_t
*, ts_cmd_t
*, bool b_flush
);
260 static bool TsHasCmd( ts_thread_t
* );
261 static bool TsIsUnused( ts_thread_t
* );
262 static int TsChangePause( ts_thread_t
*, bool b_source_paused
, bool b_paused
, vlc_tick_t i_date
);
263 static int TsChangeRate( ts_thread_t
*, int i_src_rate
, int i_rate
);
265 static void *TsRun( void * );
267 static ts_storage_t
*TsStorageNew( const char *psz_path
, int64_t i_tmp_size_max
);
268 static void TsStorageDelete( ts_storage_t
* );
269 static void TsStoragePack( ts_storage_t
*p_storage
);
270 static bool TsStorageIsFull( ts_storage_t
*, const ts_cmd_t
*p_cmd
);
271 static bool TsStorageIsEmpty( ts_storage_t
* );
272 static void TsStoragePushCmd( ts_storage_t
*, const ts_cmd_t
*p_cmd
, bool b_flush
);
273 static void TsStoragePopCmd( ts_storage_t
*p_storage
, ts_cmd_t
*p_cmd
, bool b_flush
);
275 static void CmdClean( ts_cmd_t
* );
276 static void cmd_cleanup_routine( void *p
) { CmdClean( p
); }
278 static int CmdInitAdd ( ts_cmd_t
*, es_out_id_t
*, const es_format_t
*, bool b_copy
);
279 static void CmdInitSend ( ts_cmd_t
*, es_out_id_t
*, block_t
* );
280 static int CmdInitDel ( ts_cmd_t
*, es_out_id_t
* );
281 static int CmdInitControl( ts_cmd_t
*, int i_query
, va_list, bool b_copy
);
284 static void CmdCleanAdd ( ts_cmd_t
* );
285 static void CmdCleanSend ( ts_cmd_t
* );
286 static void CmdCleanControl( ts_cmd_t
*p_cmd
);
288 /* XXX these functions will take the destination es_out_t */
289 static void CmdExecuteAdd ( es_out_t
*, ts_cmd_t
* );
290 static int CmdExecuteSend ( es_out_t
*, ts_cmd_t
* );
291 static void CmdExecuteDel ( es_out_t
*, ts_cmd_t
* );
292 static int CmdExecuteControl( es_out_t
*, ts_cmd_t
* );
295 static int GetTmpFile( char **ppsz_file
, const char *psz_path
);
297 static const struct es_out_callbacks es_out_timeshift_cbs
;
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_sys_t
*p_sys
= malloc( sizeof(*p_sys
) );
308 p_sys
->out
.cbs
= &es_out_timeshift_cbs
;
311 p_sys
->b_input_paused
= false;
312 p_sys
->b_input_paused_source
= false;
313 p_sys
->p_input
= p_input
;
314 p_sys
->i_input_rate
= i_rate
;
315 p_sys
->i_input_rate_source
= i_rate
;
317 p_sys
->p_out
= p_next_out
;
318 vlc_mutex_init_recursive( &p_sys
->lock
);
320 p_sys
->b_delayed
= false;
323 TAB_INIT( p_sys
->i_es
, p_sys
->pp_es
);
326 const int i_tmp_size_max
= var_CreateGetInteger( p_input
, "input-timeshift-granularity" );
327 if( i_tmp_size_max
< 0 )
328 p_sys
->i_tmp_size_max
= 50*1024*1024;
330 p_sys
->i_tmp_size_max
= __MAX( i_tmp_size_max
, 1*1024*1024 );
331 msg_Dbg( p_input
, "using timeshift granularity of %d MiB",
332 (int)p_sys
->i_tmp_size_max
/(1024*1024) );
334 p_sys
->psz_tmp_path
= var_InheritString( p_input
, "input-timeshift-path" );
335 #if defined (_WIN32) && !VLC_WINSTORE_APP
336 if( p_sys
->psz_tmp_path
== NULL
)
338 const DWORD count
= GetTempPath( 0, NULL
);
341 TCHAR
*path
= vlc_alloc( count
+ 1, sizeof(TCHAR
) );
344 DWORD ret
= GetTempPath( count
+ 1, path
);
345 if( ret
!= 0 && ret
<= count
)
346 p_sys
->psz_tmp_path
= FromT( path
);
351 if( p_sys
->psz_tmp_path
== NULL
)
353 wchar_t *wpath
= _wgetcwd( NULL
, 0 );
356 p_sys
->psz_tmp_path
= FromWide( wpath
);
360 if( p_sys
->psz_tmp_path
== NULL
)
361 p_sys
->psz_tmp_path
= strdup( "C:" );
363 if( p_sys
->psz_tmp_path
!= NULL
)
365 size_t len
= strlen( p_sys
->psz_tmp_path
);
367 while( len
> 0 && p_sys
->psz_tmp_path
[len
- 1] == DIR_SEP_CHAR
)
370 p_sys
->psz_tmp_path
[len
] = '\0';
373 if( p_sys
->psz_tmp_path
!= NULL
)
374 msg_Dbg( p_input
, "using timeshift path: %s", p_sys
->psz_tmp_path
);
376 msg_Dbg( p_input
, "using default timeshift path" );
379 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
391 /*****************************************************************************
393 *****************************************************************************/
394 static void Destroy( es_out_t
*p_out
)
396 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
398 if( p_sys
->b_delayed
)
400 TsStop( p_sys
->p_ts
);
401 p_sys
->b_delayed
= false;
404 while( p_sys
->i_es
> 0 )
405 Del( p_out
, p_sys
->pp_es
[0] );
406 TAB_CLEAN( p_sys
->i_es
, p_sys
->pp_es
);
408 free( p_sys
->psz_tmp_path
);
409 vlc_mutex_destroy( &p_sys
->lock
);
413 static es_out_id_t
*Add( es_out_t
*p_out
, const es_format_t
*p_fmt
)
415 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
418 es_out_id_t
*p_es
= malloc( sizeof( *p_es
) );
422 vlc_mutex_lock( &p_sys
->lock
);
426 if( CmdInitAdd( &cmd
, p_es
, p_fmt
, p_sys
->b_delayed
) )
428 vlc_mutex_unlock( &p_sys
->lock
);
433 TAB_APPEND( p_sys
->i_es
, p_sys
->pp_es
, p_es
);
435 if( p_sys
->b_delayed
)
436 TsPushCmd( p_sys
->p_ts
, &cmd
);
438 CmdExecuteAdd( p_sys
->p_out
, &cmd
);
440 vlc_mutex_unlock( &p_sys
->lock
);
444 static int Send( es_out_t
*p_out
, es_out_id_t
*p_es
, block_t
*p_block
)
446 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
448 int i_ret
= VLC_SUCCESS
;
450 vlc_mutex_lock( &p_sys
->lock
);
454 CmdInitSend( &cmd
, p_es
, p_block
);
455 if( p_sys
->b_delayed
)
456 TsPushCmd( p_sys
->p_ts
, &cmd
);
458 i_ret
= CmdExecuteSend( p_sys
->p_out
, &cmd
) ;
460 vlc_mutex_unlock( &p_sys
->lock
);
464 static void Del( es_out_t
*p_out
, es_out_id_t
*p_es
)
466 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
469 vlc_mutex_lock( &p_sys
->lock
);
473 CmdInitDel( &cmd
, p_es
);
474 if( p_sys
->b_delayed
)
475 TsPushCmd( p_sys
->p_ts
, &cmd
);
477 CmdExecuteDel( p_sys
->p_out
, &cmd
);
479 TAB_REMOVE( p_sys
->i_es
, p_sys
->pp_es
, p_es
);
481 vlc_mutex_unlock( &p_sys
->lock
);
484 static int ControlLockedGetEmpty( es_out_t
*p_out
, bool *pb_empty
)
486 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
488 if( p_sys
->b_delayed
&& TsHasCmd( p_sys
->p_ts
) )
491 *pb_empty
= es_out_GetEmpty( p_sys
->p_out
);
495 static int ControlLockedGetWakeup( es_out_t
*p_out
, vlc_tick_t
*pi_wakeup
)
497 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
499 if( p_sys
->b_delayed
)
501 assert( !input_priv(p_sys
->p_input
)->b_can_pace_control
);
506 *pi_wakeup
= es_out_GetWakeup( p_sys
->p_out
);
511 static int ControlLockedGetBuffering( es_out_t
*p_out
, bool *pb_buffering
)
513 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
515 if( p_sys
->b_delayed
)
516 *pb_buffering
= true;
518 *pb_buffering
= es_out_GetBuffering( p_sys
->p_out
);
522 static int ControlLockedSetPauseState( es_out_t
*p_out
, bool b_source_paused
, bool b_paused
, vlc_tick_t i_date
)
524 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
527 if( !p_sys
->b_delayed
&& !b_source_paused
== !b_paused
)
529 i_ret
= es_out_SetPauseState( p_sys
->p_out
, b_source_paused
, b_paused
, i_date
);
533 i_ret
= VLC_EGENERIC
;
534 if( !input_priv(p_sys
->p_input
)->b_can_pace_control
)
536 if( !p_sys
->b_delayed
)
538 if( p_sys
->b_delayed
)
539 i_ret
= TsChangePause( p_sys
->p_ts
, b_source_paused
, b_paused
, i_date
);
543 /* XXX we may do it BUT it would be better to finish the clock clean up+improvements
544 * and so be able to advertize correctly pace control property in access
546 msg_Err( p_sys
->p_input
, "EsOutTimeshift does not work with streams that have pace control" );
552 p_sys
->b_input_paused_source
= b_source_paused
;
553 p_sys
->b_input_paused
= b_paused
;
557 static int ControlLockedSetRate( es_out_t
*p_out
, int i_src_rate
, int i_rate
)
559 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
562 if( !p_sys
->b_delayed
&& i_src_rate
== i_rate
)
564 i_ret
= es_out_SetRate( p_sys
->p_out
, i_src_rate
, i_rate
);
568 i_ret
= VLC_EGENERIC
;
569 if( !input_priv(p_sys
->p_input
)->b_can_pace_control
)
571 if( !p_sys
->b_delayed
)
573 if( p_sys
->b_delayed
)
574 i_ret
= TsChangeRate( p_sys
->p_ts
, i_src_rate
, i_rate
);
578 /* XXX we may do it BUT it would be better to finish the clock clean up+improvements
579 * and so be able to advertize correctly pace control property in access
581 msg_Err( p_sys
->p_input
, "EsOutTimeshift does not work with streams that have pace control" );
588 p_sys
->i_input_rate_source
= i_src_rate
;
589 p_sys
->i_input_rate
= i_rate
;
593 static int ControlLockedSetFrameNext( es_out_t
*p_out
)
595 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
597 return es_out_SetFrameNext( p_sys
->p_out
);
600 static int ControlLocked( es_out_t
*p_out
, int i_query
, va_list args
)
602 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
606 /* Pass-through control */
607 case ES_OUT_SET_MODE
:
608 case ES_OUT_SET_GROUP
:
610 case ES_OUT_SET_GROUP_PCR
:
611 case ES_OUT_RESET_PCR
:
612 case ES_OUT_SET_NEXT_DISPLAY_TIME
:
613 case ES_OUT_SET_GROUP_META
:
614 case ES_OUT_SET_GROUP_EPG
:
615 case ES_OUT_SET_GROUP_EPG_EVENT
:
616 case ES_OUT_SET_EPG_TIME
:
617 case ES_OUT_SET_ES_SCRAMBLED_STATE
:
618 case ES_OUT_DEL_GROUP
:
619 case ES_OUT_SET_META
:
621 case ES_OUT_UNSET_ES
:
622 case ES_OUT_RESTART_ES
:
623 case ES_OUT_SET_ES_DEFAULT
:
624 case ES_OUT_SET_ES_STATE
:
625 case ES_OUT_SET_ES_CAT_POLICY
:
626 case ES_OUT_SET_ES_FMT
:
627 case ES_OUT_SET_TIMES
:
628 case ES_OUT_SET_JITTER
:
632 if( CmdInitControl( &cmd
, i_query
, args
, p_sys
->b_delayed
) )
634 if( p_sys
->b_delayed
)
636 TsPushCmd( p_sys
->p_ts
, &cmd
);
639 return CmdExecuteControl( p_sys
->p_out
, &cmd
);
642 /* Special control when delayed */
643 case ES_OUT_GET_ES_STATE
:
645 es_out_id_t
*p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
646 bool *pb_enabled
= (bool*)va_arg( args
, bool* );
648 if( p_sys
->b_delayed
)
653 return es_out_Control( p_sys
->p_out
, ES_OUT_GET_ES_STATE
, p_es
->p_es
, pb_enabled
);
655 /* Special internal input control */
656 case ES_OUT_GET_EMPTY
:
658 bool *pb_empty
= (bool*)va_arg( args
, bool* );
659 return ControlLockedGetEmpty( p_out
, pb_empty
);
661 case ES_OUT_GET_WAKE_UP
: /* TODO ? */
663 vlc_tick_t
*pi_wakeup
= (vlc_tick_t
*)va_arg( args
, vlc_tick_t
* );
664 return ControlLockedGetWakeup( p_out
, pi_wakeup
);
666 case ES_OUT_GET_BUFFERING
:
668 bool *pb_buffering
= (bool *)va_arg( args
, bool* );
669 return ControlLockedGetBuffering( p_out
, pb_buffering
);
671 case ES_OUT_SET_PAUSE_STATE
:
673 const bool b_source_paused
= (bool)va_arg( args
, int );
674 const bool b_paused
= (bool)va_arg( args
, int );
675 const vlc_tick_t i_date
= (vlc_tick_t
) va_arg( args
, vlc_tick_t
);
677 return ControlLockedSetPauseState( p_out
, b_source_paused
, b_paused
, i_date
);
679 case ES_OUT_SET_RATE
:
681 const int i_src_rate
= (int)va_arg( args
, int );
682 const int i_rate
= (int)va_arg( args
, int );
684 return ControlLockedSetRate( p_out
, i_src_rate
, i_rate
);
686 case ES_OUT_SET_FRAME_NEXT
:
688 return ControlLockedSetFrameNext( p_out
);
691 case ES_OUT_GET_PCR_SYSTEM
:
692 if( p_sys
->b_delayed
)
695 case ES_OUT_GET_GROUP_FORCED
:
696 case ES_OUT_POST_SUBNODE
:
697 return es_out_vaControl( p_sys
->p_out
, i_query
, args
);
699 case ES_OUT_MODIFY_PCR_SYSTEM
:
701 const bool b_absolute
= va_arg( args
, int );
702 const vlc_tick_t i_system
= va_arg( args
, vlc_tick_t
);
704 if( b_absolute
&& p_sys
->b_delayed
)
707 return es_out_ControlModifyPcrSystem( p_sys
->p_out
, b_absolute
, i_system
);
710 /* Invalid queries for this es_out level */
711 case ES_OUT_SET_ES_BY_ID
:
712 case ES_OUT_RESTART_ES_BY_ID
:
713 case ES_OUT_SET_ES_DEFAULT_BY_ID
:
714 case ES_OUT_GET_ES_OBJECTS_BY_ID
:
715 case ES_OUT_STOP_ALL_ES
:
716 case ES_OUT_START_ALL_ES
:
717 case ES_OUT_SET_DELAY
:
718 case ES_OUT_SET_RECORD_STATE
:
720 vlc_assert_unreachable();
724 static int Control( es_out_t
*p_out
, int i_query
, va_list args
)
726 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
729 vlc_mutex_lock( &p_sys
->lock
);
733 i_ret
= ControlLocked( p_out
, i_query
, args
);
735 vlc_mutex_unlock( &p_sys
->lock
);
740 static const struct es_out_callbacks es_out_timeshift_cbs
=
749 /*****************************************************************************
751 *****************************************************************************/
752 static void TsDestroy( ts_thread_t
*p_ts
)
754 vlc_cond_destroy( &p_ts
->wait
);
755 vlc_mutex_destroy( &p_ts
->lock
);
758 static int TsStart( es_out_t
*p_out
)
760 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
763 assert( !p_sys
->b_delayed
);
765 p_sys
->p_ts
= p_ts
= calloc(1, sizeof(*p_ts
));
769 p_ts
->i_tmp_size_max
= p_sys
->i_tmp_size_max
;
770 p_ts
->psz_tmp_path
= p_sys
->psz_tmp_path
;
771 p_ts
->p_input
= p_sys
->p_input
;
772 p_ts
->p_out
= p_sys
->p_out
;
773 vlc_mutex_init( &p_ts
->lock
);
774 vlc_cond_init( &p_ts
->wait
);
775 p_ts
->b_paused
= p_sys
->b_input_paused
&& !p_sys
->b_input_paused_source
;
776 p_ts
->i_pause_date
= p_ts
->b_paused
? vlc_tick_now() : -1;
777 p_ts
->i_rate_source
= p_sys
->i_input_rate_source
;
778 p_ts
->i_rate
= p_sys
->i_input_rate
;
779 p_ts
->i_rate_date
= -1;
780 p_ts
->i_rate_delay
= 0;
781 p_ts
->i_buffering_delay
= 0;
782 p_ts
->i_cmd_delay
= 0;
783 p_ts
->p_storage_r
= NULL
;
784 p_ts
->p_storage_w
= NULL
;
786 p_sys
->b_delayed
= true;
787 if( vlc_clone( &p_ts
->thread
, TsRun
, p_ts
, VLC_THREAD_PRIORITY_INPUT
) )
789 msg_Err( p_sys
->p_input
, "cannot create timeshift thread" );
793 p_sys
->b_delayed
= false;
799 static void TsAutoStop( es_out_t
*p_out
)
801 es_out_sys_t
*p_sys
= container_of(p_out
, es_out_sys_t
, out
);
803 if( !p_sys
->b_delayed
|| !TsIsUnused( p_sys
->p_ts
) )
806 msg_Warn( p_sys
->p_input
, "es out timeshift: auto stop" );
807 TsStop( p_sys
->p_ts
);
809 p_sys
->b_delayed
= false;
811 static void TsStop( ts_thread_t
*p_ts
)
813 vlc_cancel( p_ts
->thread
);
814 vlc_join( p_ts
->thread
, NULL
);
816 vlc_mutex_lock( &p_ts
->lock
);
821 if( TsPopCmdLocked( p_ts
, &cmd
, true ) )
826 assert( !p_ts
->p_storage_r
|| !p_ts
->p_storage_r
->p_next
);
827 if( p_ts
->p_storage_r
)
828 TsStorageDelete( p_ts
->p_storage_r
);
829 vlc_mutex_unlock( &p_ts
->lock
);
833 static void TsPushCmd( ts_thread_t
*p_ts
, ts_cmd_t
*p_cmd
)
835 vlc_mutex_lock( &p_ts
->lock
);
837 if( !p_ts
->p_storage_w
|| TsStorageIsFull( p_ts
->p_storage_w
, p_cmd
) )
839 ts_storage_t
*p_storage
= TsStorageNew( p_ts
->psz_tmp_path
, p_ts
->i_tmp_size_max
);
844 vlc_mutex_unlock( &p_ts
->lock
);
845 /* TODO warn the user (but only once) */
849 if( !p_ts
->p_storage_w
)
851 p_ts
->p_storage_r
= p_ts
->p_storage_w
= p_storage
;
855 TsStoragePack( p_ts
->p_storage_w
);
856 p_ts
->p_storage_w
->p_next
= p_storage
;
857 p_ts
->p_storage_w
= p_storage
;
861 /* TODO return error and warn the user (but only once) */
862 TsStoragePushCmd( p_ts
->p_storage_w
, p_cmd
, p_ts
->p_storage_r
== p_ts
->p_storage_w
);
864 vlc_cond_signal( &p_ts
->wait
);
866 vlc_mutex_unlock( &p_ts
->lock
);
868 static int TsPopCmdLocked( ts_thread_t
*p_ts
, ts_cmd_t
*p_cmd
, bool b_flush
)
870 vlc_assert_locked( &p_ts
->lock
);
872 if( TsStorageIsEmpty( p_ts
->p_storage_r
) )
875 TsStoragePopCmd( p_ts
->p_storage_r
, p_cmd
, b_flush
);
877 while( TsStorageIsEmpty( p_ts
->p_storage_r
) )
879 ts_storage_t
*p_next
= p_ts
->p_storage_r
->p_next
;
883 TsStorageDelete( p_ts
->p_storage_r
);
884 p_ts
->p_storage_r
= p_next
;
889 static bool TsHasCmd( ts_thread_t
*p_ts
)
893 vlc_mutex_lock( &p_ts
->lock
);
894 b_cmd
= TsStorageIsEmpty( p_ts
->p_storage_r
);
895 vlc_mutex_unlock( &p_ts
->lock
);
899 static bool TsIsUnused( ts_thread_t
*p_ts
)
903 vlc_mutex_lock( &p_ts
->lock
);
904 b_unused
= !p_ts
->b_paused
&&
905 p_ts
->i_rate
== p_ts
->i_rate_source
&&
906 TsStorageIsEmpty( p_ts
->p_storage_r
);
907 vlc_mutex_unlock( &p_ts
->lock
);
911 static int TsChangePause( ts_thread_t
*p_ts
, bool b_source_paused
, bool b_paused
, vlc_tick_t i_date
)
913 vlc_mutex_lock( &p_ts
->lock
);
918 assert( !b_source_paused
);
919 i_ret
= es_out_SetPauseState( p_ts
->p_out
, true, true, i_date
);
923 i_ret
= es_out_SetPauseState( p_ts
->p_out
, false, false, i_date
);
930 assert( p_ts
->i_pause_date
> 0 );
932 p_ts
->i_cmd_delay
+= i_date
- p_ts
->i_pause_date
;
935 p_ts
->b_paused
= b_paused
;
936 p_ts
->i_pause_date
= i_date
;
938 vlc_cond_signal( &p_ts
->wait
);
940 vlc_mutex_unlock( &p_ts
->lock
);
943 static int TsChangeRate( ts_thread_t
*p_ts
, int i_src_rate
, int i_rate
)
947 vlc_mutex_lock( &p_ts
->lock
);
948 p_ts
->i_cmd_delay
+= p_ts
->i_rate_delay
;
950 p_ts
->i_rate_date
= -1;
951 p_ts
->i_rate_delay
= 0;
952 p_ts
->i_rate
= i_rate
;
953 p_ts
->i_rate_source
= i_src_rate
;
955 i_ret
= es_out_SetRate( p_ts
->p_out
, i_rate
, i_rate
);
956 vlc_mutex_unlock( &p_ts
->lock
);
961 static void *TsRun( void *p_data
)
963 ts_thread_t
*p_ts
= p_data
;
964 vlc_tick_t i_buffering_date
= -1;
969 vlc_tick_t i_deadline
;
972 /* Pop a command to execute */
973 vlc_mutex_lock( &p_ts
->lock
);
974 mutex_cleanup_push( &p_ts
->lock
);
978 const int canc
= vlc_savecancel();
979 b_buffering
= es_out_GetBuffering( p_ts
->p_out
);
981 if( ( !p_ts
->b_paused
|| b_buffering
) && !TsPopCmdLocked( p_ts
, &cmd
, false ) )
983 vlc_restorecancel( canc
);
986 vlc_restorecancel( canc
);
988 vlc_cond_wait( &p_ts
->wait
, &p_ts
->lock
);
991 if( b_buffering
&& i_buffering_date
< 0 )
993 i_buffering_date
= cmd
.i_date
;
995 else if( i_buffering_date
> 0 )
997 p_ts
->i_buffering_delay
+= i_buffering_date
- cmd
.i_date
; /* It is < 0 */
999 i_buffering_date
= cmd
.i_date
;
1001 i_buffering_date
= -1;
1004 if( p_ts
->i_rate_date
< 0 )
1005 p_ts
->i_rate_date
= cmd
.i_date
;
1007 p_ts
->i_rate_delay
= 0;
1008 if( p_ts
->i_rate_source
!= p_ts
->i_rate
)
1010 const vlc_tick_t i_duration
= cmd
.i_date
- p_ts
->i_rate_date
;
1011 p_ts
->i_rate_delay
= i_duration
* p_ts
->i_rate
/ p_ts
->i_rate_source
- i_duration
;
1013 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
)
1015 const int canc
= vlc_savecancel();
1017 /* Auto reset to rate 1.0 */
1018 msg_Warn( p_ts
->p_input
, "es out timeshift: auto reset rate to %d", p_ts
->i_rate_source
);
1020 p_ts
->i_cmd_delay
= 0;
1021 p_ts
->i_buffering_delay
= 0;
1023 p_ts
->i_rate_delay
= 0;
1024 p_ts
->i_rate_date
= -1;
1025 p_ts
->i_rate
= p_ts
->i_rate_source
;
1027 if( !es_out_SetRate( p_ts
->p_out
, p_ts
->i_rate_source
, p_ts
->i_rate
) )
1029 vlc_value_t val
= { .i_int
= p_ts
->i_rate
};
1031 * FIXME it is perfectly safe BUT it is ugly as it may hide a
1032 * rate change requested by user */
1033 input_ControlPushHelper( p_ts
->p_input
, INPUT_CONTROL_SET_RATE
, &val
);
1036 vlc_restorecancel( canc
);
1038 i_deadline
= cmd
.i_date
+ p_ts
->i_cmd_delay
+ p_ts
->i_rate_delay
+ p_ts
->i_buffering_delay
;
1041 vlc_mutex_unlock( &p_ts
->lock
);
1043 /* Regulate the speed of command processing to the same one than
1045 vlc_cleanup_push( cmd_cleanup_routine
, &cmd
);
1047 vlc_tick_wait( i_deadline
);
1051 /* Execute the command */
1052 const int canc
= vlc_savecancel();
1053 switch( cmd
.i_type
)
1056 CmdExecuteAdd( p_ts
->p_out
, &cmd
);
1057 CmdCleanAdd( &cmd
);
1060 CmdExecuteSend( p_ts
->p_out
, &cmd
);
1061 CmdCleanSend( &cmd
);
1064 CmdExecuteControl( p_ts
->p_out
, &cmd
);
1065 CmdCleanControl( &cmd
);
1068 CmdExecuteDel( p_ts
->p_out
, &cmd
);
1071 vlc_assert_unreachable();
1074 vlc_restorecancel( canc
);
1080 /*****************************************************************************
1082 *****************************************************************************/
1083 static ts_storage_t
*TsStorageNew( const char *psz_tmp_path
, int64_t i_tmp_size_max
)
1085 ts_storage_t
*p_storage
= malloc( sizeof (*p_storage
) );
1086 if( unlikely(p_storage
== NULL
) )
1090 int fd
= GetTmpFile( &psz_file
, psz_tmp_path
);
1097 p_storage
->p_filew
= fdopen( fd
, "w+b" );
1098 if( p_storage
->p_filew
== NULL
)
1101 vlc_unlink( psz_file
);
1105 p_storage
->p_filer
= vlc_fopen( psz_file
, "rb" );
1106 if( p_storage
->p_filer
== NULL
)
1108 fclose( p_storage
->p_filew
);
1109 vlc_unlink( psz_file
);
1114 vlc_unlink( psz_file
);
1117 p_storage
->psz_file
= psz_file
;
1119 p_storage
->p_next
= NULL
;
1122 p_storage
->i_file_max
= i_tmp_size_max
;
1123 p_storage
->i_file_size
= 0;
1126 p_storage
->i_cmd_w
= 0;
1127 p_storage
->i_cmd_r
= 0;
1128 p_storage
->i_cmd_max
= 30000;
1129 p_storage
->p_cmd
= vlc_alloc( p_storage
->i_cmd_max
, sizeof(*p_storage
->p_cmd
) );
1130 //fprintf( stderr, "\nSTORAGE name=%s size=%d KiB\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1132 if( !p_storage
->p_cmd
)
1134 TsStorageDelete( p_storage
);
1144 static void TsStorageDelete( ts_storage_t
*p_storage
)
1146 while( p_storage
->i_cmd_r
< p_storage
->i_cmd_w
)
1150 TsStoragePopCmd( p_storage
, &cmd
, true );
1154 free( p_storage
->p_cmd
);
1156 fclose( p_storage
->p_filer
);
1157 fclose( p_storage
->p_filew
);
1159 vlc_unlink( p_storage
->psz_file
);
1160 free( p_storage
->psz_file
);
1165 static void TsStoragePack( ts_storage_t
*p_storage
)
1167 /* Try to release a bit of memory */
1168 if( p_storage
->i_cmd_w
>= p_storage
->i_cmd_max
)
1171 p_storage
->i_cmd_max
= __MAX( p_storage
->i_cmd_w
, 1 );
1173 ts_cmd_t
*p_new
= realloc( p_storage
->p_cmd
, p_storage
->i_cmd_max
* sizeof(*p_storage
->p_cmd
) );
1175 p_storage
->p_cmd
= p_new
;
1177 static bool TsStorageIsFull( ts_storage_t
*p_storage
, const ts_cmd_t
*p_cmd
)
1179 if( p_cmd
&& p_cmd
->i_type
== C_SEND
&& p_storage
->i_cmd_w
> 0 )
1181 size_t i_size
= sizeof(*p_cmd
->u
.send
.p_block
) + p_cmd
->u
.send
.p_block
->i_buffer
;
1183 if( p_storage
->i_file_size
+ i_size
>= p_storage
->i_file_max
)
1186 return p_storage
->i_cmd_w
>= p_storage
->i_cmd_max
;
1188 static bool TsStorageIsEmpty( ts_storage_t
*p_storage
)
1190 return !p_storage
|| p_storage
->i_cmd_r
>= p_storage
->i_cmd_w
;
1192 static void TsStoragePushCmd( ts_storage_t
*p_storage
, const ts_cmd_t
*p_cmd
, bool b_flush
)
1194 ts_cmd_t cmd
= *p_cmd
;
1196 assert( !TsStorageIsFull( p_storage
, p_cmd
) );
1198 if( cmd
.i_type
== C_SEND
)
1200 block_t
*p_block
= cmd
.u
.send
.p_block
;
1202 cmd
.u
.send
.p_block
= NULL
;
1203 cmd
.u
.send
.i_offset
= ftell( p_storage
->p_filew
);
1205 if( fwrite( p_block
, sizeof(*p_block
), 1, p_storage
->p_filew
) != 1 )
1207 block_Release( p_block
);
1210 p_storage
->i_file_size
+= sizeof(*p_block
);
1211 if( p_block
->i_buffer
> 0 )
1213 if( fwrite( p_block
->p_buffer
, p_block
->i_buffer
, 1, p_storage
->p_filew
) != 1 )
1215 block_Release( p_block
);
1219 p_storage
->i_file_size
+= p_block
->i_buffer
;
1220 block_Release( p_block
);
1223 fflush( p_storage
->p_filew
);
1225 p_storage
->p_cmd
[p_storage
->i_cmd_w
++] = cmd
;
1227 static void TsStoragePopCmd( ts_storage_t
*p_storage
, ts_cmd_t
*p_cmd
, bool b_flush
)
1229 assert( !TsStorageIsEmpty( p_storage
) );
1231 *p_cmd
= p_storage
->p_cmd
[p_storage
->i_cmd_r
++];
1232 if( p_cmd
->i_type
== C_SEND
)
1237 !fseek( p_storage
->p_filer
, p_cmd
->u
.send
.i_offset
, SEEK_SET
) &&
1238 fread( &block
, sizeof(block
), 1, p_storage
->p_filer
) == 1 )
1240 block_t
*p_block
= block_Alloc( block
.i_buffer
);
1243 p_block
->i_dts
= block
.i_dts
;
1244 p_block
->i_pts
= block
.i_pts
;
1245 p_block
->i_flags
= block
.i_flags
;
1246 p_block
->i_length
= block
.i_length
;
1247 p_block
->i_nb_samples
= block
.i_nb_samples
;
1248 p_block
->i_buffer
= fread( p_block
->p_buffer
, 1, block
.i_buffer
, p_storage
->p_filer
);
1250 p_cmd
->u
.send
.p_block
= p_block
;
1254 //perror( "TsStoragePopCmd" );
1255 p_cmd
->u
.send
.p_block
= block_Alloc( 1 );
1260 /*****************************************************************************
1262 *****************************************************************************/
1263 static void CmdClean( ts_cmd_t
*p_cmd
)
1265 switch( p_cmd
->i_type
)
1268 CmdCleanAdd( p_cmd
);
1271 CmdCleanSend( p_cmd
);
1274 CmdCleanControl( p_cmd
);
1279 vlc_assert_unreachable();
1284 static int CmdInitAdd( ts_cmd_t
*p_cmd
, es_out_id_t
*p_es
, const es_format_t
*p_fmt
, bool b_copy
)
1286 p_cmd
->i_type
= C_ADD
;
1287 p_cmd
->i_date
= vlc_tick_now();
1288 p_cmd
->u
.add
.p_es
= p_es
;
1291 p_cmd
->u
.add
.p_fmt
= malloc( sizeof(*p_fmt
) );
1292 if( !p_cmd
->u
.add
.p_fmt
)
1293 return VLC_EGENERIC
;
1294 es_format_Copy( p_cmd
->u
.add
.p_fmt
, p_fmt
);
1298 p_cmd
->u
.add
.p_fmt
= (es_format_t
*)p_fmt
;
1302 static void CmdExecuteAdd( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1304 p_cmd
->u
.add
.p_es
->p_es
= es_out_Add( p_out
, p_cmd
->u
.add
.p_fmt
);
1306 static void CmdCleanAdd( ts_cmd_t
*p_cmd
)
1308 es_format_Clean( p_cmd
->u
.add
.p_fmt
);
1309 free( p_cmd
->u
.add
.p_fmt
);
1312 static void CmdInitSend( ts_cmd_t
*p_cmd
, es_out_id_t
*p_es
, block_t
*p_block
)
1314 p_cmd
->i_type
= C_SEND
;
1315 p_cmd
->i_date
= vlc_tick_now();
1316 p_cmd
->u
.send
.p_es
= p_es
;
1317 p_cmd
->u
.send
.p_block
= p_block
;
1319 static int CmdExecuteSend( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1321 block_t
*p_block
= p_cmd
->u
.send
.p_block
;
1323 p_cmd
->u
.send
.p_block
= NULL
;
1327 if( p_cmd
->u
.send
.p_es
->p_es
)
1328 return es_out_Send( p_out
, p_cmd
->u
.send
.p_es
->p_es
, p_block
);
1329 block_Release( p_block
);
1331 return VLC_EGENERIC
;
1333 static void CmdCleanSend( ts_cmd_t
*p_cmd
)
1335 if( p_cmd
->u
.send
.p_block
)
1336 block_Release( p_cmd
->u
.send
.p_block
);
1339 static int CmdInitDel( ts_cmd_t
*p_cmd
, es_out_id_t
*p_es
)
1341 p_cmd
->i_type
= C_DEL
;
1342 p_cmd
->i_date
= vlc_tick_now();
1343 p_cmd
->u
.del
.p_es
= p_es
;
1346 static void CmdExecuteDel( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1348 if( p_cmd
->u
.del
.p_es
->p_es
)
1349 es_out_Del( p_out
, p_cmd
->u
.del
.p_es
->p_es
);
1350 free( p_cmd
->u
.del
.p_es
);
1353 static int CmdInitControl( ts_cmd_t
*p_cmd
, int i_query
, va_list args
, bool b_copy
)
1355 p_cmd
->i_type
= C_CONTROL
;
1356 p_cmd
->i_date
= vlc_tick_now();
1357 p_cmd
->u
.control
.i_query
= i_query
;
1361 /* Pass-through control */
1362 case ES_OUT_SET_MODE
: /* arg1= int */
1363 case ES_OUT_SET_GROUP
: /* arg1= int */
1364 case ES_OUT_DEL_GROUP
: /* arg1=int i_group */
1365 p_cmd
->u
.control
.u
.i_int
= (int)va_arg( args
, int );
1368 case ES_OUT_SET_PCR
: /* arg1=vlc_tick_t i_pcr(microsecond!) (using default group 0)*/
1369 case ES_OUT_SET_NEXT_DISPLAY_TIME
: /* arg1=int64_t i_pts(microsecond) */
1370 p_cmd
->u
.control
.u
.i_i64
= (int64_t)va_arg( args
, int64_t );
1373 case ES_OUT_SET_GROUP_PCR
: /* arg1= int i_group, arg2=vlc_tick_t i_pcr(microsecond!)*/
1374 p_cmd
->u
.control
.u
.int_i64
.i_int
= (int)va_arg( args
, int );
1375 p_cmd
->u
.control
.u
.int_i64
.i_i64
= (int64_t)va_arg( args
, vlc_tick_t
);
1378 case ES_OUT_SET_ES_SCRAMBLED_STATE
:
1379 p_cmd
->u
.control
.u
.es_bool
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1380 p_cmd
->u
.control
.u
.es_bool
.b_bool
= (bool)va_arg( args
, int );
1383 case ES_OUT_RESET_PCR
: /* no arg */
1384 case ES_OUT_SET_EOS
:
1387 case ES_OUT_SET_META
: /* arg1=const vlc_meta_t* */
1388 case ES_OUT_SET_GROUP_META
: /* arg1=int i_group arg2=const vlc_meta_t* */
1390 if( i_query
== ES_OUT_SET_GROUP_META
)
1391 p_cmd
->u
.control
.u
.int_meta
.i_int
= (int)va_arg( args
, int );
1392 const vlc_meta_t
*p_meta
= va_arg( args
, const vlc_meta_t
* );
1396 p_cmd
->u
.control
.u
.int_meta
.p_meta
= vlc_meta_New();
1397 if( !p_cmd
->u
.control
.u
.int_meta
.p_meta
)
1398 return VLC_EGENERIC
;
1399 vlc_meta_Merge( p_cmd
->u
.control
.u
.int_meta
.p_meta
, p_meta
);
1403 /* The cast is only needed to avoid warning */
1404 p_cmd
->u
.control
.u
.int_meta
.p_meta
= (vlc_meta_t
*)p_meta
;
1409 case ES_OUT_SET_GROUP_EPG
: /* arg1=int i_group arg2=const vlc_epg_t* */
1411 p_cmd
->u
.control
.u
.int_epg
.i_int
= (int)va_arg( args
, int );
1412 const vlc_epg_t
*p_epg
= va_arg( args
, const vlc_epg_t
* );
1416 p_cmd
->u
.control
.u
.int_epg
.p_epg
= vlc_epg_Duplicate( p_epg
);
1417 if( !p_cmd
->u
.control
.u
.int_epg
.p_epg
)
1418 return VLC_EGENERIC
;
1422 /* The cast is only needed to avoid warning */
1423 p_cmd
->u
.control
.u
.int_epg
.p_epg
= (vlc_epg_t
*)p_epg
;
1427 case ES_OUT_SET_GROUP_EPG_EVENT
: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1429 p_cmd
->u
.control
.u
.int_epg_evt
.i_int
= (int)va_arg( args
, int );
1430 const vlc_epg_event_t
*p_evt
= va_arg( args
, const vlc_epg_event_t
* );
1434 p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
= vlc_epg_event_Duplicate( p_evt
);
1435 if( !p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
)
1436 return VLC_EGENERIC
;
1440 /* The cast is only needed to avoid warning */
1441 p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
= (vlc_epg_event_t
*)p_evt
;
1445 case ES_OUT_SET_EPG_TIME
: /* arg1=int64_t (seconds) */
1446 p_cmd
->u
.control
.u
.i_i64
= (int64_t)va_arg( args
, int64_t );
1449 /* Modified control */
1450 case ES_OUT_SET_ES
: /* arg1= es_out_id_t* */
1451 case ES_OUT_UNSET_ES
: /* arg1= es_out_id_t* */
1452 case ES_OUT_RESTART_ES
: /* arg1= es_out_id_t* */
1453 case ES_OUT_SET_ES_DEFAULT
: /* arg1= es_out_id_t* */
1454 p_cmd
->u
.control
.u
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1457 case ES_OUT_SET_ES_CAT_POLICY
:
1458 p_cmd
->u
.control
.u
.es_policy
.i_cat
= (int) va_arg( args
, int );
1459 p_cmd
->u
.control
.u
.es_policy
.i_policy
= (int) va_arg( args
, int );
1462 case ES_OUT_SET_ES_STATE
:/* arg1= es_out_id_t* arg2=bool */
1463 p_cmd
->u
.control
.u
.es_bool
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1464 p_cmd
->u
.control
.u
.es_bool
.b_bool
= (bool)va_arg( args
, int );
1467 case ES_OUT_SET_ES_FMT
: /* arg1= es_out_id_t* arg2=es_format_t* */
1469 p_cmd
->u
.control
.u
.es_fmt
.p_es
= (es_out_id_t
*)va_arg( args
, es_out_id_t
* );
1470 es_format_t
*p_fmt
= (es_format_t
*)va_arg( args
, es_format_t
* );
1474 p_cmd
->u
.control
.u
.es_fmt
.p_fmt
= malloc( sizeof(*p_fmt
) );
1475 if( !p_cmd
->u
.control
.u
.es_fmt
.p_fmt
)
1476 return VLC_EGENERIC
;
1477 es_format_Copy( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
, p_fmt
);
1481 p_cmd
->u
.control
.u
.es_fmt
.p_fmt
= p_fmt
;
1485 case ES_OUT_SET_TIMES
:
1487 double f_position
= (double)va_arg( args
, double );
1488 vlc_tick_t i_time
= (vlc_tick_t
)va_arg( args
, vlc_tick_t
);
1489 vlc_tick_t i_length
= (vlc_tick_t
)va_arg( args
, vlc_tick_t
);
1491 p_cmd
->u
.control
.u
.times
.f_position
= f_position
;
1492 p_cmd
->u
.control
.u
.times
.i_time
= i_time
;
1493 p_cmd
->u
.control
.u
.times
.i_length
= i_length
;
1496 case ES_OUT_SET_JITTER
:
1498 vlc_tick_t i_pts_delay
= (vlc_tick_t
)va_arg( args
, vlc_tick_t
);
1499 vlc_tick_t i_pts_jitter
= (vlc_tick_t
)va_arg( args
, vlc_tick_t
);
1500 int i_cr_average
= (int)va_arg( args
, int );
1502 p_cmd
->u
.control
.u
.jitter
.i_pts_delay
= i_pts_delay
;
1503 p_cmd
->u
.control
.u
.jitter
.i_pts_jitter
= i_pts_jitter
;
1504 p_cmd
->u
.control
.u
.jitter
.i_cr_average
= i_cr_average
;
1509 vlc_assert_unreachable();
1510 return VLC_EGENERIC
;
1515 static int CmdExecuteControl( es_out_t
*p_out
, ts_cmd_t
*p_cmd
)
1517 const int i_query
= p_cmd
->u
.control
.i_query
;
1521 /* Pass-through control */
1522 case ES_OUT_SET_MODE
: /* arg1= int */
1523 case ES_OUT_SET_GROUP
: /* arg1= int */
1524 case ES_OUT_DEL_GROUP
: /* arg1=int i_group */
1525 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.i_int
);
1527 case ES_OUT_SET_PCR
: /* arg1=vlc_tick_t i_pcr(microsecond!) (using default group 0)*/
1528 case ES_OUT_SET_NEXT_DISPLAY_TIME
: /* arg1=int64_t i_pts(microsecond) */
1529 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.i_i64
);
1531 case ES_OUT_SET_GROUP_PCR
: /* arg1= int i_group, arg2=vlc_tick_t i_pcr(microsecond!)*/
1532 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_i64
.i_int
,
1533 p_cmd
->u
.control
.u
.int_i64
.i_i64
);
1535 case ES_OUT_RESET_PCR
: /* no arg */
1536 case ES_OUT_SET_EOS
:
1537 return es_out_Control( p_out
, i_query
);
1539 case ES_OUT_SET_GROUP_META
: /* arg1=int i_group arg2=const vlc_meta_t* */
1540 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_meta
.i_int
,
1541 p_cmd
->u
.control
.u
.int_meta
.p_meta
);
1543 case ES_OUT_SET_GROUP_EPG
: /* arg1=int i_group arg2=const vlc_epg_t* */
1544 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_epg
.i_int
,
1545 p_cmd
->u
.control
.u
.int_epg
.p_epg
);
1547 case ES_OUT_SET_GROUP_EPG_EVENT
: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1548 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_epg_evt
.i_int
,
1549 p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
);
1551 case ES_OUT_SET_EPG_TIME
: /* arg1=int64_t */
1552 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.i_i64
);
1554 case ES_OUT_SET_ES_SCRAMBLED_STATE
: /* arg1=int es_out_id_t* arg2=bool */
1555 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_bool
.p_es
->p_es
,
1556 p_cmd
->u
.control
.u
.es_bool
.b_bool
);
1558 case ES_OUT_SET_META
: /* arg1=const vlc_meta_t* */
1559 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.int_meta
.p_meta
);
1561 /* Modified control */
1562 case ES_OUT_SET_ES
: /* arg1= es_out_id_t* */
1563 case ES_OUT_UNSET_ES
: /* arg1= es_out_id_t* */
1564 case ES_OUT_RESTART_ES
: /* arg1= es_out_id_t* */
1565 case ES_OUT_SET_ES_DEFAULT
: /* arg1= es_out_id_t* */
1566 return es_out_Control( p_out
, i_query
, !p_cmd
->u
.control
.u
.p_es
? NULL
:
1567 p_cmd
->u
.control
.u
.p_es
->p_es
);
1569 case ES_OUT_SET_ES_STATE
:/* arg1= es_out_id_t* arg2=bool */
1570 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_bool
.p_es
->p_es
,
1571 p_cmd
->u
.control
.u
.es_bool
.b_bool
);
1573 case ES_OUT_SET_ES_CAT_POLICY
:
1574 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_policy
.i_cat
,
1575 p_cmd
->u
.control
.u
.es_policy
.i_policy
);
1577 case ES_OUT_SET_ES_FMT
: /* arg1= es_out_id_t* arg2=es_format_t* */
1578 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.es_fmt
.p_es
->p_es
,
1579 p_cmd
->u
.control
.u
.es_fmt
.p_fmt
);
1581 case ES_OUT_SET_TIMES
:
1582 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.times
.f_position
,
1583 p_cmd
->u
.control
.u
.times
.i_time
,
1584 p_cmd
->u
.control
.u
.times
.i_length
);
1585 case ES_OUT_SET_JITTER
:
1586 return es_out_Control( p_out
, i_query
, p_cmd
->u
.control
.u
.jitter
.i_pts_delay
,
1587 p_cmd
->u
.control
.u
.jitter
.i_pts_jitter
,
1588 p_cmd
->u
.control
.u
.jitter
.i_cr_average
);
1591 vlc_assert_unreachable();
1592 return VLC_EGENERIC
;
1595 static void CmdCleanControl( ts_cmd_t
*p_cmd
)
1597 switch( p_cmd
->u
.control
.i_query
)
1599 case ES_OUT_SET_GROUP_META
:
1600 case ES_OUT_SET_META
:
1601 if( p_cmd
->u
.control
.u
.int_meta
.p_meta
)
1602 vlc_meta_Delete( p_cmd
->u
.control
.u
.int_meta
.p_meta
);
1604 case ES_OUT_SET_GROUP_EPG
:
1605 if( p_cmd
->u
.control
.u
.int_epg
.p_epg
)
1606 vlc_epg_Delete( p_cmd
->u
.control
.u
.int_epg
.p_epg
);
1608 case ES_OUT_SET_GROUP_EPG_EVENT
:
1609 if( p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
)
1610 vlc_epg_event_Delete( p_cmd
->u
.control
.u
.int_epg_evt
.p_evt
);
1612 case ES_OUT_SET_ES_FMT
:
1613 if( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
)
1615 es_format_Clean( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
);
1616 free( p_cmd
->u
.control
.u
.es_fmt
.p_fmt
);
1622 static int GetTmpFile( char **filename
, const char *dirname
)
1625 && asprintf( filename
, "%s"DIR_SEP PACKAGE_NAME
"-timeshift.XXXXXX",
1628 vlc_mkdir( dirname
, 0700 );
1630 int fd
= vlc_mkstemp( *filename
);
1637 *filename
= strdup( DIR_SEP
"tmp"DIR_SEP PACKAGE_NAME
"-timeshift.XXXXXX" );
1638 if( unlikely(*filename
== NULL
) )
1641 int fd
= vlc_mkstemp( *filename
);