1 /*****************************************************************************
2 * es_out_timeshift.c: Es Out timeshift.
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
7 * Authors: Laurent Aimar < fenrir _AT_ via _DOT_ ecp _DOT_ fr>
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 *****************************************************************************/
105 ts_cmd_control_t control;
114 input_thread_t *p_input;
117 /* Lock for all following fields */
123 mtime_t i_pause_date;
129 mtime_t i_rate_delay;
145 input_thread_t *p_input;
149 int64_t i_tmp_size_max; /* Maximal temporary file size in byte */
150 char *psz_tmp_path; /* Path for temporary files */
152 /* Lock for all following fields */
157 ts_thread_t *p_thread;
161 bool b_input_paused_source;
163 int i_input_rate_source;
170 static es_out_id_t *Add ( es_out_t *, const es_format_t * );
171 static int Send ( es_out_t *, es_out_id_t *, block_t * );
172 static void Del ( es_out_t *, es_out_id_t * );
173 static int Control( es_out_t *, int i_query, va_list );
174 static void Destroy( es_out_t * );
176 static int TsStart( es_out_t * );
177 static void TsAutoStop( es_out_t * );
179 static void TsStop( ts_thread_t * );
180 static void TsPushCmd( ts_thread_t *, const ts_cmd_t * );
181 static int TsPopCmdLocked( ts_thread_t *, ts_cmd_t * );
182 static bool TsHasCmd( ts_thread_t * );
183 static bool TsIsUnused( ts_thread_t * );
184 static int TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
185 static int TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
187 static void *TsRun( vlc_object_t * );
189 static void CmdClean( ts_cmd_t * );
190 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
192 static int CmdInitAdd ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
193 static void CmdInitSend ( ts_cmd_t *, es_out_id_t *, block_t * );
194 static int CmdInitDel ( ts_cmd_t *, es_out_id_t * );
195 static int CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
198 static void CmdCleanAdd ( ts_cmd_t * );
199 static void CmdCleanSend ( ts_cmd_t * );
200 static void CmdCleanControl( ts_cmd_t *p_cmd );
202 /* XXX these functions will take the destination es_out_t */
203 static void CmdExecuteAdd ( es_out_t *, ts_cmd_t * );
204 static int CmdExecuteSend ( es_out_t *, ts_cmd_t * );
205 static void CmdExecuteDel ( es_out_t *, ts_cmd_t * );
206 static int CmdExecuteControl( es_out_t *, ts_cmd_t * );
209 static char *GetTmpPath( char *psz_path );
210 static FILE *GetTmpFile( const char *psz_path );
212 /*****************************************************************************
213 * input_EsOutTimeshiftNew:
214 *****************************************************************************/
215 es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
217 es_out_t *p_out = malloc( sizeof(*p_out) );
221 es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
230 p_out->pf_send = Send;
232 p_out->pf_control = Control;
233 p_out->pf_destroy = Destroy;
234 p_out->p_sys = p_sys;
235 p_out->b_sout = p_input->p->p_sout != NULL;
238 p_sys->b_input_paused = false;
239 p_sys->b_input_paused_source = false;
240 p_sys->p_input = p_input;
241 p_sys->i_input_rate = i_rate;
242 p_sys->i_input_rate_source = i_rate;
244 p_sys->p_out = p_next_out;
245 vlc_mutex_init_recursive( &p_sys->lock );
247 p_sys->b_delayed = false;
248 p_sys->p_thread = NULL;
250 TAB_INIT( p_sys->i_es, p_sys->pp_es );
253 * timeshift-granularity
256 p_sys->i_tmp_size_max = 50 * 1024*1024;
257 p_sys->psz_tmp_path = GetTmpPath( NULL );
262 /*****************************************************************************
264 *****************************************************************************/
265 static void Destroy( es_out_t *p_out )
267 es_out_sys_t *p_sys = p_out->p_sys;
269 if( p_sys->b_delayed )
271 TsStop( p_sys->p_thread );
272 p_sys->b_delayed = false;
275 while( p_sys->i_es > 0 )
276 Del( p_out, p_sys->pp_es[0] );
277 TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
279 free( p_sys->psz_tmp_path );
280 vlc_mutex_destroy( &p_sys->lock );
285 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
287 es_out_sys_t *p_sys = p_out->p_sys;
290 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
294 vlc_mutex_lock( &p_sys->lock );
298 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
300 vlc_mutex_unlock( &p_sys->lock );
305 TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
307 if( p_sys->b_delayed )
308 TsPushCmd( p_sys->p_thread, &cmd );
310 CmdExecuteAdd( p_sys->p_out, &cmd );
312 vlc_mutex_unlock( &p_sys->lock );
316 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
318 es_out_sys_t *p_sys = p_out->p_sys;
320 int i_ret = VLC_SUCCESS;
322 vlc_mutex_lock( &p_sys->lock );
326 CmdInitSend( &cmd, p_es, p_block );
327 if( p_sys->b_delayed )
328 TsPushCmd( p_sys->p_thread, &cmd );
330 i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
332 vlc_mutex_unlock( &p_sys->lock );
336 static void Del( es_out_t *p_out, es_out_id_t *p_es )
338 es_out_sys_t *p_sys = p_out->p_sys;
341 vlc_mutex_lock( &p_sys->lock );
345 CmdInitDel( &cmd, p_es );
346 if( p_sys->b_delayed )
347 TsPushCmd( p_sys->p_thread, &cmd );
349 CmdExecuteDel( p_sys->p_out, &cmd );
351 TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
353 vlc_mutex_unlock( &p_sys->lock );
356 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
358 es_out_sys_t *p_sys = p_out->p_sys;
360 if( p_sys->b_delayed && TsHasCmd( p_sys->p_thread ) )
363 *pb_empty = es_out_GetEmpty( p_sys->p_out );
367 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
369 es_out_sys_t *p_sys = p_out->p_sys;
371 if( p_sys->b_delayed )
373 assert( !p_sys->p_input->b_can_pace_control );
378 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
383 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
385 es_out_sys_t *p_sys = p_out->p_sys;
387 if( p_sys->b_delayed )
388 *pb_buffering = true;
390 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
394 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
396 es_out_sys_t *p_sys = p_out->p_sys;
399 if( !p_sys->b_delayed && !b_source_paused == !b_paused )
401 i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
405 i_ret = VLC_EGENERIC;
406 if( !p_sys->p_input->b_can_pace_control )
408 if( !p_sys->b_delayed )
410 if( p_sys->b_delayed )
411 i_ret = TsChangePause( p_sys->p_thread, b_source_paused, b_paused, i_date );
415 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
416 * and so be able to advertize correctly pace control property in access
418 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
424 p_sys->b_input_paused_source = b_source_paused;
425 p_sys->b_input_paused = b_paused;
429 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
431 es_out_sys_t *p_sys = p_out->p_sys;
434 if( !p_sys->b_delayed && i_src_rate == i_rate )
436 i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
440 i_ret = VLC_EGENERIC;
441 if( !p_sys->p_input->b_can_pace_control )
443 if( !p_sys->b_delayed )
445 if( p_sys->b_delayed )
446 i_ret = TsChangeRate( p_sys->p_thread, i_src_rate, i_rate );
450 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
451 * and so be able to advertize correctly pace control property in access
453 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
460 p_sys->i_input_rate_source = i_src_rate;
461 p_sys->i_input_rate = i_rate;
465 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
467 es_out_sys_t *p_sys = p_out->p_sys;
469 if( !p_sys->b_delayed )
470 return es_out_SetTime( p_sys->p_out, i_date );
473 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
476 static int ControlLockedSetFrameNext( es_out_t *p_out )
478 es_out_sys_t *p_sys = p_out->p_sys;
480 if( !p_sys->b_delayed )
481 return es_out_SetFrameNext( p_sys->p_out );
484 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support frame next" );
488 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
490 es_out_sys_t *p_sys = p_out->p_sys;
494 /* Invalid query for this es_out level */
495 case ES_OUT_SET_ES_BY_ID:
496 case ES_OUT_RESTART_ES_BY_ID:
497 case ES_OUT_SET_ES_DEFAULT_BY_ID:
498 case ES_OUT_SET_DELAY:
499 case ES_OUT_SET_RECORD_STATE:
503 /* TODO ? or to remove ? */
507 /* Pass-through control */
508 case ES_OUT_SET_ACTIVE:
509 case ES_OUT_GET_ACTIVE:
510 case ES_OUT_SET_MODE:
511 case ES_OUT_GET_MODE:
512 case ES_OUT_SET_GROUP:
513 case ES_OUT_GET_GROUP:
515 case ES_OUT_SET_GROUP_PCR:
516 case ES_OUT_RESET_PCR:
517 case ES_OUT_SET_NEXT_DISPLAY_TIME:
518 case ES_OUT_SET_GROUP_META:
519 case ES_OUT_SET_GROUP_EPG:
520 case ES_OUT_DEL_GROUP:
522 case ES_OUT_RESTART_ES:
523 case ES_OUT_SET_ES_DEFAULT:
524 case ES_OUT_SET_ES_STATE:
525 case ES_OUT_GET_ES_STATE:
526 case ES_OUT_SET_ES_FMT:
529 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
531 if( p_sys->b_delayed )
533 TsPushCmd( p_sys->p_thread, &cmd );
536 return CmdExecuteControl( p_sys->p_out, &cmd );
539 /* Special control */
540 case ES_OUT_GET_EMPTY:
542 bool *pb_empty = (bool*)va_arg( args, bool* );
543 return ControlLockedGetEmpty( p_out, pb_empty );
545 case ES_OUT_GET_WAKE_UP: /* TODO ? */
547 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
548 return ControlLockedGetWakeup( p_out, pi_wakeup );
550 case ES_OUT_GET_BUFFERING:
552 bool *pb_buffering = (bool *)va_arg( args, bool* );
553 return ControlLockedGetBuffering( p_out, pb_buffering );
555 case ES_OUT_SET_PAUSE_STATE:
557 const bool b_source_paused = (bool)va_arg( args, int );
558 const bool b_paused = (bool)va_arg( args, int );
559 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
561 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
563 case ES_OUT_SET_RATE:
565 const int i_src_rate = (int)va_arg( args, int );
566 const int i_rate = (int)va_arg( args, int );
568 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
570 case ES_OUT_SET_TIME:
572 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
574 return ControlLockedSetTime( p_out, i_date );
576 case ES_OUT_SET_FRAME_NEXT:
578 return ControlLockedSetFrameNext( p_out );
582 msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
587 static int Control( es_out_t *p_out, int i_query, va_list args )
589 es_out_sys_t *p_sys = p_out->p_sys;
592 vlc_mutex_lock( &p_sys->lock );
596 i_ret = ControlLocked( p_out, i_query, args );
598 vlc_mutex_unlock( &p_sys->lock );
603 /*****************************************************************************
605 *****************************************************************************/
606 static void TsDestructor( vlc_object_t *p_this )
608 ts_thread_t *p_ts = (ts_thread_t*)p_this;
610 vlc_cond_destroy( &p_ts->wait );
611 vlc_mutex_destroy( &p_ts->lock );
613 static int TsStart( es_out_t *p_out )
615 es_out_sys_t *p_sys = p_out->p_sys;
618 assert( !p_sys->b_delayed );
620 p_sys->p_thread = p_ts = vlc_custom_create( p_sys->p_input, sizeof(ts_thread_t),
621 VLC_OBJECT_GENERIC, "es out timeshift" );
625 p_ts->p_input = p_sys->p_input;
626 p_ts->p_out = p_sys->p_out;
627 vlc_mutex_init( &p_ts->lock );
628 vlc_cond_init( &p_ts->wait );
629 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
630 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
631 p_ts->i_rate_source = p_sys->i_input_rate_source;
632 p_ts->i_rate = p_sys->i_input_rate;
633 p_ts->i_rate_date = -1;
634 p_ts->i_rate_delay = 0;
635 p_ts->i_cmd_delay = 0;
636 TAB_INIT( p_ts->i_cmd, p_ts->pp_cmd );
638 vlc_object_set_destructor( p_ts, TsDestructor );
640 p_sys->b_delayed = true;
641 if( vlc_thread_create( p_ts, "es out timeshift",
642 TsRun, VLC_THREAD_PRIORITY_INPUT, false ) )
644 msg_Err( p_sys->p_input, "cannot create input thread" );
646 vlc_object_release( p_ts );
648 p_sys->b_delayed = false;
654 static void TsAutoStop( es_out_t *p_out )
656 es_out_sys_t *p_sys = p_out->p_sys;
658 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_thread ) )
661 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
662 TsStop( p_sys->p_thread );
664 p_sys->b_delayed = false;
666 static void TsStop( ts_thread_t *p_ts )
668 vlc_object_kill( p_ts );
669 vlc_thread_join( p_ts );
671 vlc_mutex_lock( &p_ts->lock );
676 if( TsPopCmdLocked( p_ts, &cmd ) )
681 vlc_mutex_unlock( &p_ts->lock );
682 TAB_CLEAN( p_ts->i_cmd, p_ts->pp_cmd );
684 vlc_object_release( p_ts );
686 static void TsPushCmd( ts_thread_t *p_ts, const ts_cmd_t *p_cmd )
688 ts_cmd_t *p_dup = malloc( sizeof(*p_dup) );
694 vlc_mutex_lock( &p_ts->lock );
696 TAB_APPEND( p_ts->i_cmd, p_ts->pp_cmd, p_dup );
697 vlc_cond_signal( &p_ts->wait );
699 vlc_mutex_unlock( &p_ts->lock );
702 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
704 vlc_assert_locked( &p_ts->lock );
706 if( p_ts->i_cmd <= 0 )
709 *p_cmd = *p_ts->pp_cmd[0];
711 free( p_ts->pp_cmd[0] );
712 TAB_REMOVE( p_ts->i_cmd, p_ts->pp_cmd, p_ts->pp_cmd[0] );
715 static bool TsHasCmd( ts_thread_t *p_ts )
719 vlc_mutex_lock( &p_ts->lock );
720 b_cmd = p_ts->i_cmd > 0;
721 vlc_mutex_unlock( &p_ts->lock );
725 static bool TsIsUnused( ts_thread_t *p_ts )
729 vlc_mutex_lock( &p_ts->lock );
730 b_unused = !p_ts->b_paused &&
731 p_ts->i_rate == p_ts->i_rate_source &&
733 vlc_mutex_unlock( &p_ts->lock );
737 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
739 vlc_mutex_lock( &p_ts->lock );
744 assert( !b_source_paused );
745 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
749 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
756 assert( p_ts->i_pause_date > 0 );
758 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
761 p_ts->b_paused = b_paused;
762 p_ts->i_pause_date = i_date;
764 vlc_cond_signal( &p_ts->wait );
766 vlc_mutex_unlock( &p_ts->lock );
769 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
773 vlc_mutex_lock( &p_ts->lock );
774 p_ts->i_cmd_delay += p_ts->i_rate_delay;
776 p_ts->i_rate_date = -1;
777 p_ts->i_rate_delay = 0;
778 p_ts->i_rate = i_rate;
779 p_ts->i_rate_source = i_src_rate;
781 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
782 vlc_mutex_unlock( &p_ts->lock );
787 static void *TsRun( vlc_object_t *p_thread )
789 ts_thread_t *p_ts = (ts_thread_t*)p_thread;
796 /* Pop a command to execute */
797 vlc_mutex_lock( &p_ts->lock );
798 mutex_cleanup_push( &p_ts->lock );
800 while( p_ts->b_paused || TsPopCmdLocked( p_ts, &cmd ) )
801 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
803 if( p_ts->i_rate_date < 0 )
804 p_ts->i_rate_date = cmd.i_date;
806 p_ts->i_rate_delay = 0;
807 if( p_ts->i_rate_source != p_ts->i_rate )
809 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
810 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
812 if( p_ts->i_cmd_delay + p_ts->i_rate_delay < 0 )
814 const int canc = vlc_savecancel();
816 /* Auto reset to rate 1.0 */
817 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
819 p_ts->i_cmd_delay = 0;
821 p_ts->i_rate_date = -1;
822 p_ts->i_rate_delay = 0;
823 p_ts->i_rate = p_ts->i_rate_source;
825 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
827 vlc_value_t val = { .i_int = p_ts->i_rate };
829 * FIXME it is perfectly safe BUT it is ugly as it may hide a
830 * rate change requested by user */
831 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
834 vlc_restorecancel( canc );
836 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay;
840 /* Regulate the speed of command processing to the same one than
842 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
848 /* Execute the command */
849 const int canc = vlc_savecancel();
853 CmdExecuteAdd( p_ts->p_out, &cmd );
857 CmdExecuteSend( p_ts->p_out, &cmd );
858 CmdCleanSend( &cmd );
861 CmdExecuteControl( p_ts->p_out, &cmd );
862 CmdCleanControl( &cmd );
865 CmdExecuteDel( p_ts->p_out, &cmd );
871 vlc_restorecancel( canc );
877 /*****************************************************************************
879 *****************************************************************************/
880 static void CmdClean( ts_cmd_t *p_cmd )
882 switch( p_cmd->i_type )
885 CmdCleanAdd( p_cmd );
888 CmdCleanSend( p_cmd );
891 CmdCleanControl( p_cmd );
901 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
903 p_cmd->i_type = C_ADD;
904 p_cmd->i_date = mdate();
905 p_cmd->add.p_es = p_es;
908 p_cmd->add.p_fmt = malloc( sizeof(*p_fmt) );
909 if( !p_cmd->add.p_fmt )
911 es_format_Copy( p_cmd->add.p_fmt, p_fmt );
915 p_cmd->add.p_fmt = (es_format_t*)p_fmt;
919 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
921 p_cmd->add.p_es->p_es = es_out_Add( p_out, p_cmd->add.p_fmt );
923 static void CmdCleanAdd( ts_cmd_t *p_cmd )
925 es_format_Clean( p_cmd->add.p_fmt );
926 free( p_cmd->add.p_fmt );
929 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
931 p_cmd->i_type = C_SEND;
932 p_cmd->i_date = mdate();
933 p_cmd->send.p_es = p_es;
934 p_cmd->send.p_block = p_block;
936 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
938 block_t *p_block = p_cmd->send.p_block;
940 p_cmd->send.p_block = NULL;
944 if( p_cmd->send.p_es->p_es )
945 return es_out_Send( p_out, p_cmd->send.p_es->p_es, p_block );
946 block_Release( p_block );
950 static void CmdCleanSend( ts_cmd_t *p_cmd )
952 if( p_cmd->send.p_block )
953 block_Release( p_cmd->send.p_block );
956 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
958 p_cmd->i_type = C_DEL;
959 p_cmd->i_date = mdate();
960 p_cmd->del.p_es = p_es;
963 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
965 if( p_cmd->del.p_es->p_es )
966 es_out_Del( p_out, p_cmd->del.p_es->p_es );
967 free( p_cmd->del.p_es );
970 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
972 p_cmd->i_type = C_CONTROL;
973 p_cmd->i_date = mdate();
974 p_cmd->control.i_query = i_query;
975 p_cmd->control.p_meta = NULL;
976 p_cmd->control.p_epg = NULL;
977 p_cmd->control.p_fmt = NULL;
981 /* Pass-through control */
982 case ES_OUT_SET_ACTIVE: /* arg1= bool */
983 p_cmd->control.b_bool = (bool)va_arg( args, int );
986 case ES_OUT_GET_ACTIVE: /* arg1= bool* */
987 p_cmd->control.pb_bool = (bool*)va_arg( args, bool * );
990 case ES_OUT_SET_MODE: /* arg1= int */
991 case ES_OUT_SET_GROUP: /* arg1= int */
992 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
993 p_cmd->control.i_int = (int)va_arg( args, int );
996 case ES_OUT_GET_MODE: /* arg2= int* */
997 case ES_OUT_GET_GROUP: /* arg1= int* */
998 p_cmd->control.pi_int = (int*)va_arg( args, int * );
1001 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1002 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1003 p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
1006 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1007 p_cmd->control.i_int = (int)va_arg( args, int );
1008 p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
1011 case ES_OUT_RESET_PCR: /* no arg */
1014 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1016 p_cmd->control.i_int = (int)va_arg( args, int );
1017 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1021 p_cmd->control.p_meta = vlc_meta_New();
1022 if( !p_cmd->control.p_meta )
1023 return VLC_EGENERIC;
1024 vlc_meta_Merge( p_cmd->control.p_meta, p_meta );
1028 p_cmd->control.p_meta = p_meta;
1033 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1035 p_cmd->control.i_int = (int)va_arg( args, int );
1036 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1040 p_cmd->control.p_epg = vlc_epg_New( p_epg->psz_name );
1041 if( !p_cmd->control.p_epg )
1042 return VLC_EGENERIC;
1043 for( int i = 0; i < p_epg->i_event; i++ )
1045 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1047 vlc_epg_AddEvent( p_cmd->control.p_epg,
1048 p_evt->i_start, p_evt->i_duration,
1050 p_evt->psz_short_description, p_evt->psz_description );
1052 vlc_epg_SetCurrent( p_cmd->control.p_epg,
1053 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1057 p_cmd->control.p_epg = p_epg;
1062 /* Modified control */
1063 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1064 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1065 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1066 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1069 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1070 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1071 p_cmd->control.b_bool = (bool)va_arg( args, int );
1074 case ES_OUT_GET_ES_STATE:/* arg1= es_out_id_t* arg2=bool* */
1075 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1076 p_cmd->control.pb_bool = (bool*)va_arg( args, bool * );
1079 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1081 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1082 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1086 p_cmd->control.p_fmt = malloc( sizeof(*p_fmt) );
1087 if( !p_cmd->control.p_fmt )
1088 return VLC_EGENERIC;
1089 es_format_Copy( p_cmd->control.p_fmt, p_fmt );
1093 p_cmd->control.p_fmt = p_fmt;
1100 return VLC_EGENERIC;
1105 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1107 const int i_query = p_cmd->control.i_query;
1111 /* Pass-through control */
1112 case ES_OUT_SET_ACTIVE: /* arg1= bool */
1113 return es_out_Control( p_out, i_query, p_cmd->control.b_bool );
1115 case ES_OUT_GET_ACTIVE: /* arg1= bool* */
1116 return es_out_Control( p_out, i_query, p_cmd->control.pb_bool );
1118 case ES_OUT_SET_MODE: /* arg1= int */
1119 case ES_OUT_SET_GROUP: /* arg1= int */
1120 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1121 return es_out_Control( p_out, i_query, p_cmd->control.i_int );
1123 case ES_OUT_GET_MODE: /* arg2= int* */
1124 case ES_OUT_GET_GROUP: /* arg1= int* */
1125 return es_out_Control( p_out, i_query, p_cmd->control.pi_int );
1127 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1128 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1129 return es_out_Control( p_out, i_query, p_cmd->control.i_i64 );
1131 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1132 return es_out_Control( p_out, i_query, p_cmd->control.i_int, p_cmd->control.i_i64 );
1134 case ES_OUT_RESET_PCR: /* no arg */
1135 return es_out_Control( p_out, i_query );
1137 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
1138 return es_out_Control( p_out, i_query, p_cmd->control.i_int, p_cmd->control.p_meta );
1140 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
1141 return es_out_Control( p_out, i_query, p_cmd->control.i_int, p_cmd->control.p_epg );
1143 /* Modified control */
1144 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1145 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1146 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1147 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es );
1149 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1150 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es, p_cmd->control.b_bool );
1152 case ES_OUT_GET_ES_STATE:/* arg1= es_out_id_t* arg2=bool* */
1153 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es, p_cmd->control.pb_bool );
1155 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1156 return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es, p_cmd->control.p_fmt );
1160 return VLC_EGENERIC;
1163 static void CmdCleanControl( ts_cmd_t *p_cmd )
1165 if( p_cmd->control.p_meta )
1166 vlc_meta_Delete( p_cmd->control.p_meta );
1167 if( p_cmd->control.p_epg )
1168 vlc_epg_Delete( p_cmd->control.p_epg );
1169 if( p_cmd->control.p_fmt )
1171 es_format_Clean( p_cmd->control.p_fmt );
1172 free( p_cmd->control.p_fmt );
1177 /*****************************************************************************
1179 *****************************************************************************/
1180 static char *GetTmpPath( char *psz_path )
1182 if( psz_path && *psz_path )
1184 /* Make sure that the path exists and is a directory */
1186 const int i_ret = utf8_stat( psz_path, &s );
1188 if( i_ret < 0 && !utf8_mkdir( psz_path, 0600 ) )
1190 else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1195 /* Create a suitable path */
1196 #if defined (WIN32) && !defined (UNDER_CE)
1197 const DWORD dwCount = GetTempPathW( 0, NULL );
1198 wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1201 if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1205 psw_path = _wgetcwd( NULL, 0 );
1212 psz_path = FromWide( psw_path );
1213 while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1214 psz_path[strlen( psz_path ) - 1] = '\0';
1219 if( !psz_path || *psz_path == '\0' )
1222 return strdup( "C:" );
1225 psz_path = strdup( "/tmp" );
1231 static FILE *GetTmpFile( const char *psz_path )
1238 if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1242 fd = mkstemp( psz_name );
1249 f = fdopen( fd, "rw+" );