1 /*****************************************************************************
2 * es_out_timeshift.c: Es Out timeshift.
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
7 * Authors: Laurent Aimar < fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #if defined (WIN32) && !defined (UNDER_CE)
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
42 #include <vlc_common.h>
43 #include <vlc_charset.h>
45 #include <vlc_input.h>
46 #include <vlc_es_out.h>
47 #include <vlc_block.h>
48 #include "input_internal.h"
50 #include "es_out_timeshift.h"
52 /*****************************************************************************
54 *****************************************************************************/
103 ts_cmd_control_t control;
112 input_thread_t *p_input;
115 /* Lock for all following fields */
121 mtime_t i_pause_date;
127 mtime_t i_rate_delay;
130 mtime_t i_buffering_delay;
146 input_thread_t *p_input;
150 int64_t i_tmp_size_max; /* Maximal temporary file size in byte */
151 char *psz_tmp_path; /* Path for temporary files */
153 /* Lock for all following fields */
158 ts_thread_t *p_thread;
162 bool b_input_paused_source;
164 int i_input_rate_source;
171 static es_out_id_t *Add ( es_out_t *, const es_format_t * );
172 static int Send ( es_out_t *, es_out_id_t *, block_t * );
173 static void Del ( es_out_t *, es_out_id_t * );
174 static int Control( es_out_t *, int i_query, va_list );
175 static void Destroy( es_out_t * );
177 static int TsStart( es_out_t * );
178 static void TsAutoStop( es_out_t * );
180 static void TsStop( ts_thread_t * );
181 static void TsPushCmd( ts_thread_t *, const ts_cmd_t * );
182 static int TsPopCmdLocked( ts_thread_t *, ts_cmd_t * );
183 static bool TsHasCmd( ts_thread_t * );
184 static bool TsIsUnused( ts_thread_t * );
185 static int TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
186 static int TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
188 static void *TsRun( vlc_object_t * );
190 static void CmdClean( ts_cmd_t * );
191 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
193 static int CmdInitAdd ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
194 static void CmdInitSend ( ts_cmd_t *, es_out_id_t *, block_t * );
195 static int CmdInitDel ( ts_cmd_t *, es_out_id_t * );
196 static int CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
199 static void CmdCleanAdd ( ts_cmd_t * );
200 static void CmdCleanSend ( ts_cmd_t * );
201 static void CmdCleanControl( ts_cmd_t *p_cmd );
203 /* XXX these functions will take the destination es_out_t */
204 static void CmdExecuteAdd ( es_out_t *, ts_cmd_t * );
205 static int CmdExecuteSend ( es_out_t *, ts_cmd_t * );
206 static void CmdExecuteDel ( es_out_t *, ts_cmd_t * );
207 static int CmdExecuteControl( es_out_t *, ts_cmd_t * );
210 static char *GetTmpPath( char *psz_path );
211 static FILE *GetTmpFile( const char *psz_path );
213 /*****************************************************************************
214 * input_EsOutTimeshiftNew:
215 *****************************************************************************/
216 es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
218 es_out_t *p_out = malloc( sizeof(*p_out) );
222 es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
231 p_out->pf_send = Send;
233 p_out->pf_control = Control;
234 p_out->pf_destroy = Destroy;
235 p_out->p_sys = p_sys;
236 p_out->b_sout = p_input->p->p_sout != NULL;
239 p_sys->b_input_paused = false;
240 p_sys->b_input_paused_source = false;
241 p_sys->p_input = p_input;
242 p_sys->i_input_rate = i_rate;
243 p_sys->i_input_rate_source = i_rate;
245 p_sys->p_out = p_next_out;
246 vlc_mutex_init_recursive( &p_sys->lock );
248 p_sys->b_delayed = false;
249 p_sys->p_thread = NULL;
251 TAB_INIT( p_sys->i_es, p_sys->pp_es );
254 * timeshift-granularity
257 p_sys->i_tmp_size_max = 50 * 1024*1024;
258 p_sys->psz_tmp_path = GetTmpPath( NULL );
263 /*****************************************************************************
265 *****************************************************************************/
266 static void Destroy( es_out_t *p_out )
268 es_out_sys_t *p_sys = p_out->p_sys;
270 if( p_sys->b_delayed )
272 TsStop( p_sys->p_thread );
273 p_sys->b_delayed = false;
276 while( p_sys->i_es > 0 )
277 Del( p_out, p_sys->pp_es[0] );
278 TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
280 free( p_sys->psz_tmp_path );
281 vlc_mutex_destroy( &p_sys->lock );
286 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
288 es_out_sys_t *p_sys = p_out->p_sys;
291 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
295 vlc_mutex_lock( &p_sys->lock );
299 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
301 vlc_mutex_unlock( &p_sys->lock );
306 TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
308 if( p_sys->b_delayed )
309 TsPushCmd( p_sys->p_thread, &cmd );
311 CmdExecuteAdd( p_sys->p_out, &cmd );
313 vlc_mutex_unlock( &p_sys->lock );
317 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
319 es_out_sys_t *p_sys = p_out->p_sys;
321 int i_ret = VLC_SUCCESS;
323 vlc_mutex_lock( &p_sys->lock );
327 CmdInitSend( &cmd, p_es, p_block );
328 if( p_sys->b_delayed )
329 TsPushCmd( p_sys->p_thread, &cmd );
331 i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
333 vlc_mutex_unlock( &p_sys->lock );
337 static void Del( es_out_t *p_out, es_out_id_t *p_es )
339 es_out_sys_t *p_sys = p_out->p_sys;
342 vlc_mutex_lock( &p_sys->lock );
346 CmdInitDel( &cmd, p_es );
347 if( p_sys->b_delayed )
348 TsPushCmd( p_sys->p_thread, &cmd );
350 CmdExecuteDel( p_sys->p_out, &cmd );
352 TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
354 vlc_mutex_unlock( &p_sys->lock );
357 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
359 es_out_sys_t *p_sys = p_out->p_sys;
361 if( p_sys->b_delayed && TsHasCmd( p_sys->p_thread ) )
364 *pb_empty = es_out_GetEmpty( p_sys->p_out );
368 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
370 es_out_sys_t *p_sys = p_out->p_sys;
372 if( p_sys->b_delayed )
374 assert( !p_sys->p_input->b_can_pace_control );
379 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
384 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
386 es_out_sys_t *p_sys = p_out->p_sys;
388 if( p_sys->b_delayed )
389 *pb_buffering = true;
391 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
395 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
397 es_out_sys_t *p_sys = p_out->p_sys;
400 if( !p_sys->b_delayed && !b_source_paused == !b_paused )
402 i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
406 i_ret = VLC_EGENERIC;
407 if( !p_sys->p_input->b_can_pace_control )
409 if( !p_sys->b_delayed )
411 if( p_sys->b_delayed )
412 i_ret = TsChangePause( p_sys->p_thread, b_source_paused, b_paused, i_date );
416 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
417 * and so be able to advertize correctly pace control property in access
419 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
425 p_sys->b_input_paused_source = b_source_paused;
426 p_sys->b_input_paused = b_paused;
430 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
432 es_out_sys_t *p_sys = p_out->p_sys;
435 if( !p_sys->b_delayed && i_src_rate == i_rate )
437 i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
441 i_ret = VLC_EGENERIC;
442 if( !p_sys->p_input->b_can_pace_control )
444 if( !p_sys->b_delayed )
446 if( p_sys->b_delayed )
447 i_ret = TsChangeRate( p_sys->p_thread, i_src_rate, i_rate );
451 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
452 * and so be able to advertize correctly pace control property in access
454 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
461 p_sys->i_input_rate_source = i_src_rate;
462 p_sys->i_input_rate = i_rate;
466 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
468 es_out_sys_t *p_sys = p_out->p_sys;
470 if( !p_sys->b_delayed )
471 return es_out_SetTime( p_sys->p_out, i_date );
474 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
477 static int ControlLockedSetFrameNext( es_out_t *p_out )
479 es_out_sys_t *p_sys = p_out->p_sys;
481 return es_out_SetFrameNext( p_sys->p_out );
484 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
486 es_out_sys_t *p_sys = p_out->p_sys;
490 /* Invalid query for this es_out level */
491 case ES_OUT_SET_ES_BY_ID:
492 case ES_OUT_RESTART_ES_BY_ID:
493 case ES_OUT_SET_ES_DEFAULT_BY_ID:
494 case ES_OUT_SET_DELAY:
495 case ES_OUT_SET_RECORD_STATE:
499 /* TODO ? or to remove ? */
503 /* Pass-through control */
504 case ES_OUT_SET_ACTIVE:
505 case ES_OUT_SET_MODE:
506 case ES_OUT_SET_GROUP:
508 case ES_OUT_SET_GROUP_PCR:
509 case ES_OUT_RESET_PCR:
510 case ES_OUT_SET_NEXT_DISPLAY_TIME:
511 case ES_OUT_SET_GROUP_META:
512 case ES_OUT_SET_GROUP_EPG:
513 case ES_OUT_DEL_GROUP:
515 case ES_OUT_RESTART_ES:
516 case ES_OUT_SET_ES_DEFAULT:
517 case ES_OUT_SET_ES_STATE:
518 case ES_OUT_SET_ES_FMT:
521 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
523 if( p_sys->b_delayed )
525 TsPushCmd( p_sys->p_thread, &cmd );
528 return CmdExecuteControl( p_sys->p_out, &cmd );
531 /* Special control when delayed */
532 case ES_OUT_GET_ES_STATE:
534 es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
535 bool *pb_enabled = (bool*)va_arg( args, bool* );
537 if( p_sys->b_delayed )
542 return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es, pb_enabled );
545 /* Special internal input control */
546 case ES_OUT_GET_EMPTY:
548 bool *pb_empty = (bool*)va_arg( args, bool* );
549 return ControlLockedGetEmpty( p_out, pb_empty );
551 case ES_OUT_GET_WAKE_UP: /* TODO ? */
553 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
554 return ControlLockedGetWakeup( p_out, pi_wakeup );
556 case ES_OUT_GET_BUFFERING:
558 bool *pb_buffering = (bool *)va_arg( args, bool* );
559 return ControlLockedGetBuffering( p_out, pb_buffering );
561 case ES_OUT_SET_PAUSE_STATE:
563 const bool b_source_paused = (bool)va_arg( args, int );
564 const bool b_paused = (bool)va_arg( args, int );
565 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
567 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
569 case ES_OUT_SET_RATE:
571 const int i_src_rate = (int)va_arg( args, int );
572 const int i_rate = (int)va_arg( args, int );
574 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
576 case ES_OUT_SET_TIME:
578 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
580 return ControlLockedSetTime( p_out, i_date );
582 case ES_OUT_SET_FRAME_NEXT:
584 return ControlLockedSetFrameNext( p_out );
588 msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
593 static int Control( es_out_t *p_out, int i_query, va_list args )
595 es_out_sys_t *p_sys = p_out->p_sys;
598 vlc_mutex_lock( &p_sys->lock );
602 i_ret = ControlLocked( p_out, i_query, args );
604 vlc_mutex_unlock( &p_sys->lock );
609 /*****************************************************************************
611 *****************************************************************************/
612 static void TsDestructor( vlc_object_t *p_this )
614 ts_thread_t *p_ts = (ts_thread_t*)p_this;
616 vlc_cond_destroy( &p_ts->wait );
617 vlc_mutex_destroy( &p_ts->lock );
619 static int TsStart( es_out_t *p_out )
621 es_out_sys_t *p_sys = p_out->p_sys;
624 assert( !p_sys->b_delayed );
626 p_sys->p_thread = p_ts = vlc_custom_create( p_sys->p_input, sizeof(ts_thread_t),
627 VLC_OBJECT_GENERIC, "es out timeshift" );
631 p_ts->p_input = p_sys->p_input;
632 p_ts->p_out = p_sys->p_out;
633 vlc_mutex_init( &p_ts->lock );
634 vlc_cond_init( &p_ts->wait );
635 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
636 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
637 p_ts->i_rate_source = p_sys->i_input_rate_source;
638 p_ts->i_rate = p_sys->i_input_rate;
639 p_ts->i_rate_date = -1;
640 p_ts->i_rate_delay = 0;
641 p_ts->i_buffering_delay = 0;
642 p_ts->i_cmd_delay = 0;
643 TAB_INIT( p_ts->i_cmd, p_ts->pp_cmd );
645 vlc_object_set_destructor( p_ts, TsDestructor );
647 p_sys->b_delayed = true;
648 if( vlc_thread_create( p_ts, "es out timeshift",
649 TsRun, VLC_THREAD_PRIORITY_INPUT, false ) )
651 msg_Err( p_sys->p_input, "cannot create input thread" );
653 vlc_object_release( p_ts );
655 p_sys->b_delayed = false;
661 static void TsAutoStop( es_out_t *p_out )
663 es_out_sys_t *p_sys = p_out->p_sys;
665 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_thread ) )
668 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
669 TsStop( p_sys->p_thread );
671 p_sys->b_delayed = false;
673 static void TsStop( ts_thread_t *p_ts )
675 vlc_object_kill( p_ts );
676 vlc_thread_join( p_ts );
678 vlc_mutex_lock( &p_ts->lock );
683 if( TsPopCmdLocked( p_ts, &cmd ) )
688 vlc_mutex_unlock( &p_ts->lock );
689 TAB_CLEAN( p_ts->i_cmd, p_ts->pp_cmd );
691 vlc_object_release( p_ts );
693 static void TsPushCmd( ts_thread_t *p_ts, const ts_cmd_t *p_cmd )
695 ts_cmd_t *p_dup = malloc( sizeof(*p_dup) );
701 vlc_mutex_lock( &p_ts->lock );
703 TAB_APPEND( p_ts->i_cmd, p_ts->pp_cmd, p_dup );
704 vlc_cond_signal( &p_ts->wait );
706 vlc_mutex_unlock( &p_ts->lock );
709 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
711 vlc_assert_locked( &p_ts->lock );
713 if( p_ts->i_cmd <= 0 )
716 *p_cmd = *p_ts->pp_cmd[0];
718 free( p_ts->pp_cmd[0] );
719 TAB_REMOVE( p_ts->i_cmd, p_ts->pp_cmd, p_ts->pp_cmd[0] );
722 static bool TsHasCmd( ts_thread_t *p_ts )
726 vlc_mutex_lock( &p_ts->lock );
727 b_cmd = p_ts->i_cmd > 0;
728 vlc_mutex_unlock( &p_ts->lock );
732 static bool TsIsUnused( ts_thread_t *p_ts )
736 vlc_mutex_lock( &p_ts->lock );
737 b_unused = !p_ts->b_paused &&
738 p_ts->i_rate == p_ts->i_rate_source &&
740 vlc_mutex_unlock( &p_ts->lock );
744 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
746 vlc_mutex_lock( &p_ts->lock );
751 assert( !b_source_paused );
752 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
756 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
763 assert( p_ts->i_pause_date > 0 );
765 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
768 p_ts->b_paused = b_paused;
769 p_ts->i_pause_date = i_date;
771 vlc_cond_signal( &p_ts->wait );
773 vlc_mutex_unlock( &p_ts->lock );
776 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
780 vlc_mutex_lock( &p_ts->lock );
781 p_ts->i_cmd_delay += p_ts->i_rate_delay;
783 p_ts->i_rate_date = -1;
784 p_ts->i_rate_delay = 0;
785 p_ts->i_rate = i_rate;
786 p_ts->i_rate_source = i_src_rate;
788 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
789 vlc_mutex_unlock( &p_ts->lock );
794 static void *TsRun( vlc_object_t *p_thread )
796 ts_thread_t *p_ts = (ts_thread_t*)p_thread;
797 mtime_t i_buffering_date = -1;
805 /* Pop a command to execute */
806 vlc_mutex_lock( &p_ts->lock );
807 mutex_cleanup_push( &p_ts->lock );
811 const int canc = vlc_savecancel();
812 b_buffering = es_out_GetBuffering( p_ts->p_out );
813 vlc_restorecancel( canc );
815 if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd ) )
818 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
821 if( b_buffering && i_buffering_date < 0 )
823 i_buffering_date = cmd.i_date;
825 else if( i_buffering_date > 0 )
827 p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
829 i_buffering_date = cmd.i_date;
831 i_buffering_date = -1;
834 if( p_ts->i_rate_date < 0 )
835 p_ts->i_rate_date = cmd.i_date;
837 p_ts->i_rate_delay = 0;
838 if( p_ts->i_rate_source != p_ts->i_rate )
840 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
841 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
843 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 )
845 const int canc = vlc_savecancel();
847 /* Auto reset to rate 1.0 */
848 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
850 p_ts->i_cmd_delay = 0;
851 p_ts->i_buffering_delay = 0;
853 p_ts->i_rate_delay = 0;
854 p_ts->i_rate_date = -1;
855 p_ts->i_rate = p_ts->i_rate_source;
857 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
859 vlc_value_t val = { .i_int = p_ts->i_rate };
861 * FIXME it is perfectly safe BUT it is ugly as it may hide a
862 * rate change requested by user */
863 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
866 vlc_restorecancel( canc );
868 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
872 /* Regulate the speed of command processing to the same one than
874 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
880 /* Execute the command */
881 const int canc = vlc_savecancel();
885 CmdExecuteAdd( p_ts->p_out, &cmd );
889 CmdExecuteSend( p_ts->p_out, &cmd );
890 CmdCleanSend( &cmd );
893 CmdExecuteControl( p_ts->p_out, &cmd );
894 CmdCleanControl( &cmd );
897 CmdExecuteDel( p_ts->p_out, &cmd );
903 vlc_restorecancel( canc );
909 /*****************************************************************************
911 *****************************************************************************/
912 static void CmdClean( ts_cmd_t *p_cmd )
914 switch( p_cmd->i_type )
917 CmdCleanAdd( p_cmd );
920 CmdCleanSend( p_cmd );
923 CmdCleanControl( p_cmd );
933 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
935 p_cmd->i_type = C_ADD;
936 p_cmd->i_date = mdate();
937 p_cmd->add.p_es = p_es;
940 p_cmd->add.p_fmt = malloc( sizeof(*p_fmt) );
941 if( !p_cmd->add.p_fmt )
943 es_format_Copy( p_cmd->add.p_fmt, p_fmt );
947 p_cmd->add.p_fmt = (es_format_t*)p_fmt;
951 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
953 p_cmd->add.p_es->p_es = es_out_Add( p_out, p_cmd->add.p_fmt );
955 static void CmdCleanAdd( ts_cmd_t *p_cmd )
957 es_format_Clean( p_cmd->add.p_fmt );
958 free( p_cmd->add.p_fmt );
961 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
963 p_cmd->i_type = C_SEND;
964 p_cmd->i_date = mdate();
965 p_cmd->send.p_es = p_es;
966 p_cmd->send.p_block = p_block;
968 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
970 block_t *p_block = p_cmd->send.p_block;
972 p_cmd->send.p_block = NULL;
976 if( p_cmd->send.p_es->p_es )
977 return es_out_Send( p_out, p_cmd->send.p_es->p_es, p_block );
978 block_Release( p_block );
982 static void CmdCleanSend( ts_cmd_t *p_cmd )
984 if( p_cmd->send.p_block )
985 block_Release( p_cmd->send.p_block );
988 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
990 p_cmd->i_type = C_DEL;
991 p_cmd->i_date = mdate();
992 p_cmd->del.p_es = p_es;
995 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
997 if( p_cmd->del.p_es->p_es )
998 es_out_Del( p_out, p_cmd->del.p_es->p_es );
999 free( p_cmd->del.p_es );
1002 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1004 p_cmd->i_type = C_CONTROL;
1005 p_cmd->i_date = mdate();
1006 p_cmd->control.i_query = i_query;
1007 p_cmd->control.p_meta = NULL;
1008 p_cmd->control.p_epg = NULL;
1009 p_cmd->control.p_fmt = NULL;
1013 /* Pass-through control */
1014 case ES_OUT_SET_ACTIVE: /* arg1= bool */
1015 p_cmd->control.b_bool = (bool)va_arg( args, int );
1018 case ES_OUT_SET_MODE: /* arg1= int */
1019 case ES_OUT_SET_GROUP: /* arg1= int */
1020 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1021 p_cmd->control.i_int = (int)va_arg( args, int );
1024 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1025 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1026 p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
1029 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1030 p_cmd->control.i_int = (int)va_arg( args, int );
1031 p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
1034 case ES_OUT_RESET_PCR: /* no arg */
1037 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1039 p_cmd->control.i_int = (int)va_arg( args, int );
1040 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1044 p_cmd->control.p_meta = vlc_meta_New();
1045 if( !p_cmd->control.p_meta )
1046 return VLC_EGENERIC;
1047 vlc_meta_Merge( p_cmd->control.p_meta, p_meta );
1051 p_cmd->control.p_meta = p_meta;
1056 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1058 p_cmd->control.i_int = (int)va_arg( args, int );
1059 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1063 p_cmd->control.p_epg = vlc_epg_New( p_epg->psz_name );
1064 if( !p_cmd->control.p_epg )
1065 return VLC_EGENERIC;
1066 for( int i = 0; i < p_epg->i_event; i++ )
1068 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1070 vlc_epg_AddEvent( p_cmd->control.p_epg,
1071 p_evt->i_start, p_evt->i_duration,
1073 p_evt->psz_short_description, p_evt->psz_description );
1075 vlc_epg_SetCurrent( p_cmd->control.p_epg,
1076 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1080 p_cmd->control.p_epg = p_epg;
1085 /* Modified control */
1086 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1087 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1088 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1089 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1092 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1093 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1094 p_cmd->control.b_bool = (bool)va_arg( args, int );
1097 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1099 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1100 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1104 p_cmd->control.p_fmt = malloc( sizeof(*p_fmt) );
1105 if( !p_cmd->control.p_fmt )
1106 return VLC_EGENERIC;
1107 es_format_Copy( p_cmd->control.p_fmt, p_fmt );
1111 p_cmd->control.p_fmt = p_fmt;
1118 return VLC_EGENERIC;
1123 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1125 const int i_query = p_cmd->control.i_query;
1129 /* Pass-through control */
1130 case ES_OUT_SET_ACTIVE: /* arg1= bool */
1131 return es_out_Control( p_out, i_query, p_cmd->control.b_bool );
1133 case ES_OUT_SET_MODE: /* arg1= int */
1134 case ES_OUT_SET_GROUP: /* arg1= int */
1135 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1136 return es_out_Control( p_out, i_query, p_cmd->control.i_int );
1138 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1139 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1140 return es_out_Control( p_out, i_query, p_cmd->control.i_i64 );
1142 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1143 return es_out_Control( p_out, i_query, p_cmd->control.i_int, p_cmd->control.i_i64 );
1145 case ES_OUT_RESET_PCR: /* no arg */
1146 return es_out_Control( p_out, i_query );
1148 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1149 return es_out_Control( p_out, i_query, p_cmd->control.i_int, p_cmd->control.p_meta );
1151 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1152 return es_out_Control( p_out, i_query, p_cmd->control.i_int, p_cmd->control.p_epg );
1154 /* Modified control */
1155 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1156 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1157 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1158 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es );
1160 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1161 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es, p_cmd->control.b_bool );
1163 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1164 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es, p_cmd->control.p_fmt );
1168 return VLC_EGENERIC;
1171 static void CmdCleanControl( ts_cmd_t *p_cmd )
1173 if( p_cmd->control.p_meta )
1174 vlc_meta_Delete( p_cmd->control.p_meta );
1175 if( p_cmd->control.p_epg )
1176 vlc_epg_Delete( p_cmd->control.p_epg );
1177 if( p_cmd->control.p_fmt )
1179 es_format_Clean( p_cmd->control.p_fmt );
1180 free( p_cmd->control.p_fmt );
1185 /*****************************************************************************
1187 *****************************************************************************/
1188 static char *GetTmpPath( char *psz_path )
1190 if( psz_path && *psz_path )
1192 /* Make sure that the path exists and is a directory */
1194 const int i_ret = utf8_stat( psz_path, &s );
1196 if( i_ret < 0 && !utf8_mkdir( psz_path, 0600 ) )
1198 else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1203 /* Create a suitable path */
1204 #if defined (WIN32) && !defined (UNDER_CE)
1205 const DWORD dwCount = GetTempPathW( 0, NULL );
1206 wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1209 if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1213 psw_path = _wgetcwd( NULL, 0 );
1220 psz_path = FromWide( psw_path );
1221 while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1222 psz_path[strlen( psz_path ) - 1] = '\0';
1227 if( !psz_path || *psz_path == '\0' )
1230 return strdup( "C:" );
1233 psz_path = strdup( "/tmp" );
1239 static FILE *GetTmpFile( const char *psz_path )
1246 if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1250 fd = mkstemp( psz_name );
1257 f = fdopen( fd, "rw+" );