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 *****************************************************************************/
108 input_thread_t *p_input;
112 int64_t i_tmp_size_max; /* Maximal temporary file size in byte */
113 char *psz_tmp_path; /* Path for temporary files */
115 /* Lock for all following fields */
121 vlc_object_t *p_thread;
124 mtime_t i_pause_date;
129 mtime_t i_rate_delay;
141 static es_out_id_t *Add ( es_out_t *, const es_format_t * );
142 static int Send ( es_out_t *, es_out_id_t *, block_t * );
143 static void Del ( es_out_t *, es_out_id_t * );
144 static int Control( es_out_t *, int i_query, va_list );
145 static void Destroy( es_out_t * );
147 static int TsStart( es_out_t * );
148 static void TsStop( es_out_t * );
149 static void *TsRun( vlc_object_t * );
151 static void CmdPush( es_out_t *, const ts_cmd_t * );
152 static int CmdPop( es_out_t *, ts_cmd_t * );
153 static void CmdClean( ts_cmd_t * );
154 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
156 static int CmdInitAdd ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
157 static void CmdInitSend ( ts_cmd_t *, es_out_id_t *, block_t * );
158 static int CmdInitDel ( ts_cmd_t *, es_out_id_t * );
159 static int CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
161 static void CmdExecuteAdd ( es_out_t *, ts_cmd_t * );
162 static int CmdExecuteSend ( es_out_t *, ts_cmd_t * );
163 static void CmdExecuteDel ( es_out_t *, ts_cmd_t * );
164 static int CmdExecuteControl( es_out_t *, ts_cmd_t * );
166 static void CmdCleanAdd ( ts_cmd_t * );
167 static void CmdCleanSend ( ts_cmd_t * );
168 static void CmdCleanControl( ts_cmd_t *p_cmd );
170 static char *GetTmpPath( char *psz_path );
171 static FILE *GetTmpFile( const char *psz_path );
173 /*****************************************************************************
174 * input_EsOutTimeshiftNew:
175 *****************************************************************************/
176 es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
178 es_out_t *p_out = malloc( sizeof(*p_out) );
182 es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
191 p_out->pf_send = Send;
193 p_out->pf_control = Control;
194 p_out->pf_destroy = Destroy;
195 p_out->p_sys = p_sys;
196 p_out->b_sout = p_input->p->p_sout != NULL;
199 p_sys->p_input = p_input;
200 p_sys->p_out = p_next_out;
201 vlc_mutex_init_recursive( &p_sys->lock );
202 vlc_cond_init( &p_sys->wait );
204 p_sys->b_delayed = false;
205 p_sys->p_thread = NULL;
206 p_sys->b_paused = false;
207 p_sys->i_pause_date = -1;
208 p_sys->i_rate_source =
209 p_sys->i_rate = i_rate;
210 p_sys->i_rate_date = -1;
211 p_sys->i_rate_delay = 0;
212 p_sys->i_cmd_delay = 0;
214 TAB_INIT( p_sys->i_es, p_sys->pp_es );
215 TAB_INIT( p_sys->i_cmd, p_sys->pp_cmd );
218 * timeshift-granularity
221 p_sys->i_tmp_size_max = 50 * 1024*1024;
222 p_sys->psz_tmp_path = GetTmpPath( NULL );
227 /*****************************************************************************
229 *****************************************************************************/
230 static void Destroy( es_out_t *p_out )
232 es_out_sys_t *p_sys = p_out->p_sys;
234 if( p_sys->b_delayed )
237 TAB_CLEAN( p_sys->i_cmd, p_sys->pp_cmd );
239 while( p_sys->i_es > 0 )
240 Del( p_out, p_sys->pp_es[0] );
241 TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
243 free( p_sys->psz_tmp_path );
244 vlc_cond_destroy( &p_sys->wait );
245 vlc_mutex_destroy( &p_sys->lock );
250 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
252 es_out_sys_t *p_sys = p_out->p_sys;
255 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
259 vlc_mutex_lock( &p_sys->lock );
261 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
263 vlc_mutex_unlock( &p_sys->lock );
268 TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
270 if( p_sys->b_delayed )
271 CmdPush( p_out, &cmd );
273 CmdExecuteAdd( p_out, &cmd );
275 vlc_mutex_unlock( &p_sys->lock );
279 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
281 es_out_sys_t *p_sys = p_out->p_sys;
283 int i_ret = VLC_SUCCESS;
285 vlc_mutex_lock( &p_sys->lock );
287 CmdInitSend( &cmd, p_es, p_block );
288 if( p_sys->b_delayed )
289 CmdPush( p_out, &cmd );
291 i_ret = CmdExecuteSend( p_out, &cmd) ;
293 vlc_mutex_unlock( &p_sys->lock );
297 static void Del( es_out_t *p_out, es_out_id_t *p_es )
299 es_out_sys_t *p_sys = p_out->p_sys;
302 vlc_mutex_lock( &p_sys->lock );
304 CmdInitDel( &cmd, p_es );
305 if( p_sys->b_delayed )
306 CmdPush( p_out, &cmd );
308 CmdExecuteDel( p_out, &cmd );
310 TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
312 vlc_mutex_unlock( &p_sys->lock );
315 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
317 es_out_sys_t *p_sys = p_out->p_sys;
319 if( p_sys->b_delayed && p_sys->i_cmd > 0 )
322 *pb_empty = es_out_GetEmpty( p_sys->p_out );
326 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
328 es_out_sys_t *p_sys = p_out->p_sys;
330 if( p_sys->b_delayed )
332 assert( !p_sys->p_input->b_can_pace_control );
337 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
342 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
344 es_out_sys_t *p_sys = p_out->p_sys;
346 if( p_sys->b_delayed )
347 *pb_buffering = true;
349 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
353 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
355 es_out_sys_t *p_sys = p_out->p_sys;
357 if( !p_sys->b_delayed &&
358 !b_source_paused == !b_paused )
360 return es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
362 if( p_sys->p_input->b_can_pace_control )
364 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
365 * and so be able to advertize correctly pace control property in access
367 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
374 assert( !b_source_paused );
376 if( !p_sys->b_delayed )
379 i_ret = es_out_SetPauseState( p_sys->p_out, true, true, i_date );
383 i_ret = es_out_SetPauseState( p_sys->p_out, false, false, i_date );
390 assert( p_sys->i_pause_date > 0 );
392 p_sys->i_cmd_delay += i_date - p_sys->i_pause_date;
395 p_sys->b_paused = b_paused;
396 p_sys->i_pause_date = i_date;
398 vlc_cond_signal( &p_sys->wait );
402 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
404 es_out_sys_t *p_sys = p_out->p_sys;
406 if( !p_sys->b_delayed &&
407 i_src_rate == i_rate )
409 return es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
411 if( p_sys->p_input->b_can_pace_control )
413 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
414 * and so be able to advertize correctly pace control property in access
416 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
420 p_sys->i_cmd_delay += p_sys->i_rate_delay;
422 p_sys->i_rate_date = -1;
423 p_sys->i_rate_delay = 0;
424 p_sys->i_rate = i_rate;
425 p_sys->i_rate_source = i_src_rate;
427 if( !p_sys->b_delayed )
430 return es_out_SetRate( p_sys->p_out, i_rate, i_rate );
432 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
434 es_out_sys_t *p_sys = p_out->p_sys;
436 if( !p_sys->b_delayed )
437 return es_out_SetTime( p_sys->p_out, i_date );
440 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
443 static int ControlLockedSetFrameNext( es_out_t *p_out )
445 es_out_sys_t *p_sys = p_out->p_sys;
447 if( !p_sys->b_delayed )
448 return es_out_SetFrameNext( p_sys->p_out );
451 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support frame next" );
455 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
457 es_out_sys_t *p_sys = p_out->p_sys;
461 /* Invalid query for this es_out level */
462 case ES_OUT_SET_ES_BY_ID:
463 case ES_OUT_RESTART_ES_BY_ID:
464 case ES_OUT_SET_ES_DEFAULT_BY_ID:
465 case ES_OUT_SET_DELAY:
466 case ES_OUT_SET_RECORD_STATE:
470 /* TODO ? or to remove ? */
474 /* Pass-through control */
475 case ES_OUT_SET_ACTIVE:
476 case ES_OUT_GET_ACTIVE:
477 case ES_OUT_SET_MODE:
478 case ES_OUT_GET_MODE:
479 case ES_OUT_SET_GROUP:
480 case ES_OUT_GET_GROUP:
482 case ES_OUT_SET_GROUP_PCR:
483 case ES_OUT_RESET_PCR:
484 case ES_OUT_SET_NEXT_DISPLAY_TIME:
485 case ES_OUT_SET_GROUP_META:
486 case ES_OUT_SET_GROUP_EPG:
487 case ES_OUT_DEL_GROUP:
489 case ES_OUT_RESTART_ES:
490 case ES_OUT_SET_ES_DEFAULT:
491 case ES_OUT_SET_ES_STATE:
492 case ES_OUT_GET_ES_STATE:
493 case ES_OUT_SET_ES_FMT:
496 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
498 if( p_sys->b_delayed )
500 CmdPush( p_out, &cmd );
503 return CmdExecuteControl( p_out, &cmd );
506 /* Special control */
507 case ES_OUT_GET_EMPTY:
509 bool *pb_empty = (bool*)va_arg( args, bool* );
510 return ControlLockedGetEmpty( p_out, pb_empty );
512 case ES_OUT_GET_WAKE_UP: /* TODO ? */
514 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
515 return ControlLockedGetWakeup( p_out, pi_wakeup );
517 case ES_OUT_GET_BUFFERING:
519 bool *pb_buffering = (bool *)va_arg( args, bool* );
520 return ControlLockedGetBuffering( p_out, pb_buffering );
522 case ES_OUT_SET_PAUSE_STATE:
524 const bool b_source_paused = (bool)va_arg( args, int );
525 const bool b_paused = (bool)va_arg( args, int );
526 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
528 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
530 case ES_OUT_SET_RATE:
532 const int i_src_rate = (int)va_arg( args, int );
533 const int i_rate = (int)va_arg( args, int );
535 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
537 case ES_OUT_SET_TIME:
539 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
541 return ControlLockedSetTime( p_out, i_date );
543 case ES_OUT_SET_FRAME_NEXT:
545 return ControlLockedSetFrameNext( p_out );
549 msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
554 static int Control( es_out_t *p_out, int i_query, va_list args )
556 es_out_sys_t *p_sys = p_out->p_sys;
559 vlc_mutex_lock( &p_sys->lock );
560 i_ret = ControlLocked( p_out, i_query, args );
561 vlc_mutex_unlock( &p_sys->lock );
566 /*****************************************************************************
568 *****************************************************************************/
569 static int TsStart( es_out_t *p_out )
571 es_out_sys_t *p_sys = p_out->p_sys;
573 assert( !p_sys->b_delayed );
575 p_sys->p_thread = vlc_custom_create( p_sys->p_input, sizeof(vlc_object_t),
576 VLC_OBJECT_GENERIC, "es out timeshift" );
577 if( !p_sys->p_thread )
580 p_sys->b_delayed = true;
581 p_sys->p_thread->p_private = p_out;
582 if( vlc_thread_create( p_sys->p_thread, "es out timeshift",
583 TsRun, VLC_THREAD_PRIORITY_INPUT, false ) )
585 msg_Err( p_sys->p_input, "cannot create input thread" );
586 vlc_object_release( p_sys->p_thread );
588 p_sys->b_delayed = false;
594 static void TsStop( es_out_t *p_out )
596 es_out_sys_t *p_sys = p_out->p_sys;
598 assert( p_sys->b_delayed );
600 vlc_object_kill( p_sys->p_thread );
601 vlc_thread_join( p_sys->p_thread );
607 if( CmdPop( p_out, &cmd ) )
613 p_sys->b_delayed = false;
615 static void *TsRun( vlc_object_t *p_thread )
617 es_out_t *p_out = p_thread->p_private;
618 es_out_sys_t *p_sys = p_out->p_sys;
625 /* Pop a command to execute */
626 vlc_mutex_lock( &p_sys->lock );
627 mutex_cleanup_push( &p_sys->lock );
629 while( p_sys->b_paused || CmdPop( p_out, &cmd ) )
630 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
632 if( p_sys->i_rate_date < 0 )
633 p_sys->i_rate_date = cmd.i_date;
635 p_sys->i_rate_delay = 0;
636 if( p_sys->i_rate_source != p_sys->i_rate )
638 const mtime_t i_duration = cmd.i_date - p_sys->i_rate_date;
639 p_sys->i_rate_delay = i_duration * p_sys->i_rate / p_sys->i_rate_source - i_duration;
642 i_deadline = cmd.i_date + p_sys->i_cmd_delay + p_sys->i_rate_delay;
644 if( p_sys->i_cmd_delay + p_sys->i_rate_delay < 0 )
646 /* TODO handle when we cannot go faster anymore */
647 msg_Err( p_sys->p_input, "FIXME rate underflow" );
652 /* Regulate the speed of command processing to the same one than
654 //fprintf( stderr, "d=%d - %d\n", (int)(p_sys->i_cmd_delay/1000), (int)( (mdate() - cmd.i_date - p_sys->i_cmd_delay)/1000) );
655 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
661 /* Execute the command */
662 const int canc = vlc_savecancel();
664 vlc_mutex_lock( &p_sys->lock );
668 CmdExecuteAdd( p_out, &cmd );
672 CmdExecuteSend( p_out, &cmd );
673 CmdCleanSend( &cmd );
676 CmdExecuteControl( p_out, &cmd );
677 CmdCleanControl( &cmd );
680 CmdExecuteDel( p_out, &cmd );
686 vlc_mutex_unlock( &p_sys->lock );
688 vlc_restorecancel( canc );
694 /*****************************************************************************
696 *****************************************************************************/
697 static void CmdPush( es_out_t *p_out, const ts_cmd_t *p_cmd )
699 es_out_sys_t *p_sys = p_out->p_sys;
700 ts_cmd_t *p_dup = malloc( sizeof(*p_dup) );
705 TAB_APPEND( p_sys->i_cmd, p_sys->pp_cmd, p_dup );
707 vlc_cond_signal( &p_sys->wait );
710 static int CmdPop( es_out_t *p_out, ts_cmd_t *p_cmd )
712 es_out_sys_t *p_sys = p_out->p_sys;
714 if( p_sys->i_cmd <= 0 )
717 *p_cmd = *p_sys->pp_cmd[0];
719 free( p_sys->pp_cmd[0] );
720 TAB_REMOVE( p_sys->i_cmd, p_sys->pp_cmd, p_sys->pp_cmd[0] );
724 static void CmdClean( ts_cmd_t *p_cmd )
726 switch( p_cmd->i_type )
729 CmdCleanAdd( p_cmd );
732 CmdCleanSend( p_cmd );
735 CmdCleanControl( p_cmd );
745 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
747 p_cmd->i_type = C_ADD;
748 p_cmd->i_date = mdate();
749 p_cmd->add.p_es = p_es;
752 p_cmd->add.p_fmt = malloc( sizeof(*p_fmt) );
753 if( !p_cmd->add.p_fmt )
755 es_format_Copy( p_cmd->add.p_fmt, p_fmt );
759 p_cmd->add.p_fmt = (es_format_t*)p_fmt;
763 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
765 es_out_sys_t *p_sys = p_out->p_sys;
767 p_cmd->add.p_es->p_es = es_out_Add( p_sys->p_out, p_cmd->add.p_fmt );
769 static void CmdCleanAdd( ts_cmd_t *p_cmd )
771 es_format_Clean( p_cmd->add.p_fmt );
772 free( p_cmd->add.p_fmt );
775 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
777 p_cmd->i_type = C_SEND;
778 p_cmd->i_date = mdate();
779 p_cmd->send.p_es = p_es;
780 p_cmd->send.p_block = p_block;
782 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
784 es_out_sys_t *p_sys = p_out->p_sys;
785 block_t *p_block = p_cmd->send.p_block;
787 p_cmd->send.p_block = NULL;
791 if( p_cmd->send.p_es->p_es )
792 return es_out_Send( p_sys->p_out, p_cmd->send.p_es->p_es, p_block );
793 block_Release( p_block );
797 static void CmdCleanSend( ts_cmd_t *p_cmd )
799 if( p_cmd->send.p_block )
800 block_Release( p_cmd->send.p_block );
803 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
805 p_cmd->i_type = C_DEL;
806 p_cmd->i_date = mdate();
807 p_cmd->del.p_es = p_es;
810 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
812 es_out_sys_t *p_sys = p_out->p_sys;
814 if( p_cmd->del.p_es->p_es )
815 es_out_Del( p_sys->p_out, p_cmd->del.p_es->p_es );
816 free( p_cmd->del.p_es );
819 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
821 p_cmd->i_type = C_CONTROL;
822 p_cmd->i_date = mdate();
823 p_cmd->control.i_query = i_query;
824 p_cmd->control.p_meta = NULL;
825 p_cmd->control.p_epg = NULL;
826 p_cmd->control.p_fmt = NULL;
830 /* Pass-through control */
831 case ES_OUT_SET_ACTIVE: /* arg1= bool */
832 p_cmd->control.b_bool = (bool)va_arg( args, int );
835 case ES_OUT_GET_ACTIVE: /* arg1= bool* */
836 p_cmd->control.pb_bool = (bool*)va_arg( args, bool * );
839 case ES_OUT_SET_MODE: /* arg1= int */
840 case ES_OUT_SET_GROUP: /* arg1= int */
841 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
842 p_cmd->control.i_int = (int)va_arg( args, int );
845 case ES_OUT_GET_MODE: /* arg2= int* */
846 case ES_OUT_GET_GROUP: /* arg1= int* */
847 p_cmd->control.pi_int = (int*)va_arg( args, int * );
850 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
851 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
852 p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
855 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
856 p_cmd->control.i_int = (int)va_arg( args, int );
857 p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
860 case ES_OUT_RESET_PCR: /* no arg */
863 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
865 p_cmd->control.i_int = (int)va_arg( args, int );
866 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
870 p_cmd->control.p_meta = vlc_meta_New();
871 if( !p_cmd->control.p_meta )
873 vlc_meta_Merge( p_cmd->control.p_meta, p_meta );
877 p_cmd->control.p_meta = p_meta;
882 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
884 p_cmd->control.i_int = (int)va_arg( args, int );
885 vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
889 p_cmd->control.p_epg = vlc_epg_New( p_epg->psz_name );
890 if( !p_cmd->control.p_epg )
892 for( int i = 0; i < p_epg->i_event; i++ )
894 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
896 vlc_epg_AddEvent( p_cmd->control.p_epg,
897 p_evt->i_start, p_evt->i_duration,
899 p_evt->psz_short_description, p_evt->psz_description );
901 vlc_epg_SetCurrent( p_cmd->control.p_epg,
902 p_epg->p_current ? p_epg->p_current->i_start : -1 );
906 p_cmd->control.p_epg = p_epg;
911 /* Modified control */
912 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
913 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
914 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
915 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
918 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
919 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
920 p_cmd->control.b_bool = (bool)va_arg( args, int );
923 case ES_OUT_GET_ES_STATE:/* arg1= es_out_id_t* arg2=bool* */
924 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
925 p_cmd->control.pb_bool = (bool*)va_arg( args, bool * );
928 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
930 p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
931 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
935 p_cmd->control.p_fmt = malloc( sizeof(*p_fmt) );
936 if( !p_cmd->control.p_fmt )
938 es_format_Copy( p_cmd->control.p_fmt, p_fmt );
942 p_cmd->control.p_fmt = p_fmt;
954 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
956 es_out_sys_t *p_sys = p_out->p_sys;
957 const int i_query = p_cmd->control.i_query;
961 /* Pass-through control */
962 case ES_OUT_SET_ACTIVE: /* arg1= bool */
963 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.b_bool );
965 case ES_OUT_GET_ACTIVE: /* arg1= bool* */
966 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.pb_bool );
968 case ES_OUT_SET_MODE: /* arg1= int */
969 case ES_OUT_SET_GROUP: /* arg1= int */
970 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
971 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.i_int );
973 case ES_OUT_GET_MODE: /* arg2= int* */
974 case ES_OUT_GET_GROUP: /* arg1= int* */
975 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.pi_int );
977 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
978 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
979 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.i_i64 );
981 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
982 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.i_int, p_cmd->control.i_i64 );
984 case ES_OUT_RESET_PCR: /* no arg */
985 return es_out_Control( p_sys->p_out, i_query );
987 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=vlc_meta_t* */
988 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.i_int, p_cmd->control.p_meta );
990 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=vlc_epg_t* */
991 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.i_int, p_cmd->control.p_epg );
993 /* Modified control */
994 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
995 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
996 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
997 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.p_es->p_es );
999 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1000 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.p_es->p_es, p_cmd->control.b_bool );
1002 case ES_OUT_GET_ES_STATE:/* arg1= es_out_id_t* arg2=bool* */
1003 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.p_es->p_es, p_cmd->control.pb_bool );
1005 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1006 return es_out_Control( p_sys->p_out, i_query, p_cmd->control.p_es->p_es, p_cmd->control.p_fmt );
1010 msg_Err( p_sys->p_input, "Unknown es_out_Control query in CmdExecuteControl!" );
1011 return VLC_EGENERIC;
1014 static void CmdCleanControl( ts_cmd_t *p_cmd )
1016 if( p_cmd->control.p_meta )
1017 vlc_meta_Delete( p_cmd->control.p_meta );
1018 if( p_cmd->control.p_epg )
1019 vlc_epg_Delete( p_cmd->control.p_epg );
1020 if( p_cmd->control.p_fmt )
1022 es_format_Clean( p_cmd->control.p_fmt );
1023 free( p_cmd->control.p_fmt );
1028 /*****************************************************************************
1030 *****************************************************************************/
1031 static char *GetTmpPath( char *psz_path )
1033 if( psz_path && *psz_path )
1035 /* Make sure that the path exists and is a directory */
1037 const int i_ret = utf8_stat( psz_path, &s );
1039 if( i_ret < 0 && !utf8_mkdir( psz_path, 0600 ) )
1041 else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1046 /* Create a suitable path */
1047 #if defined (WIN32) && !defined (UNDER_CE)
1048 const DWORD dwCount = GetTempPathW( 0, NULL );
1049 wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1052 if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1056 psw_path = _wgetcwd( NULL, 0 );
1063 psz_path = FromWide( psw_path );
1064 while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1065 psz_path[strlen( psz_path ) - 1] = '\0';
1070 if( !psz_path || *psz_path == '\0' )
1073 return strdup( "C:" );
1076 psz_path = strdup( "/tmp" );
1082 static FILE *GetTmpFile( const char *psz_path )
1089 if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1093 fd = mkstemp( psz_name );
1100 f = fdopen( fd, "rw+" );