es_out: add ES_OUT_UNSET_ES
[vlc.git] / src / input / es_out_timeshift.c
blob552b89613213a130c8763c13ecc29de0517e3134
1 /*****************************************************************************
2 * es_out_timeshift.c: Es Out timeshift.
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <assert.h>
34 #include <errno.h>
35 #if defined (_WIN32)
36 # include <direct.h>
37 #endif
38 #include <sys/stat.h>
39 #include <unistd.h>
41 #include <vlc_common.h>
42 #include <vlc_fs.h>
43 #ifdef _WIN32
44 # include <vlc_charset.h>
45 #endif
46 #include <vlc_input.h>
47 #include <vlc_es_out.h>
48 #include <vlc_block.h>
49 #include "input_internal.h"
50 #include "es_out.h"
51 #include "es_out_timeshift.h"
53 /*****************************************************************************
54 * Local prototypes
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__))
60 #else
61 # define attribute_packed
62 #endif
64 enum
66 C_ADD,
67 C_SEND,
68 C_DEL,
69 C_CONTROL,
72 typedef struct attribute_packed
74 es_out_id_t *p_es;
75 es_format_t *p_fmt;
76 } ts_cmd_add_t;
78 typedef struct attribute_packed
80 es_out_id_t *p_es;
81 } ts_cmd_del_t;
83 typedef struct attribute_packed
85 es_out_id_t *p_es;
86 block_t *p_block;
87 int i_offset; /* We do not use file > INT_MAX */
88 } ts_cmd_send_t;
90 typedef struct attribute_packed
92 int i_query;
94 union
96 bool b_bool;
97 int i_int;
98 int64_t i_i64;
99 es_out_id_t *p_es;
100 struct
102 int i_int;
103 int64_t i_i64;
104 } int_i64;
105 struct
107 int i_int;
108 vlc_meta_t *p_meta;
109 } int_meta;
110 struct
112 int i_int;
113 vlc_epg_t *p_epg;
114 } int_epg;
115 struct
117 int i_int;
118 vlc_epg_event_t *p_evt;
119 } int_epg_evt;
120 struct
122 es_out_id_t *p_es;
123 bool b_bool;
124 } es_bool;
125 struct
127 es_out_id_t *p_es;
128 es_format_t *p_fmt;
129 } es_fmt;
130 struct
132 int i_cat;
133 int i_policy;
134 } es_policy;
135 struct
137 /* FIXME Really too big (double make the whole thing too big) */
138 double f_position;
139 vlc_tick_t i_time;
140 vlc_tick_t i_length;
141 } times;
142 struct
144 vlc_tick_t i_pts_delay;
145 vlc_tick_t i_pts_jitter;
146 int i_cr_average;
147 } jitter;
148 } u;
149 } ts_cmd_control_t;
151 typedef struct attribute_packed
153 int8_t i_type;
154 vlc_tick_t i_date;
155 union
157 ts_cmd_add_t add;
158 ts_cmd_del_t del;
159 ts_cmd_send_t send;
160 ts_cmd_control_t control;
161 } u;
162 } ts_cmd_t;
164 typedef struct ts_storage_t ts_storage_t;
165 struct ts_storage_t
167 ts_storage_t *p_next;
169 /* */
170 #ifdef _WIN32
171 char *psz_file; /* Filename */
172 #endif
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 */
178 /* */
179 int i_cmd_r;
180 int i_cmd_w;
181 int i_cmd_max;
182 ts_cmd_t *p_cmd;
185 typedef struct
187 vlc_thread_t thread;
188 input_thread_t *p_input;
189 es_out_t *p_out;
190 int64_t i_tmp_size_max;
191 const char *psz_tmp_path;
193 /* Lock for all following fields */
194 vlc_mutex_t lock;
195 vlc_cond_t wait;
197 /* */
198 bool b_paused;
199 vlc_tick_t i_pause_date;
201 /* */
202 int i_rate;
203 int i_rate_source;
204 vlc_tick_t i_rate_date;
205 vlc_tick_t i_rate_delay;
207 /* */
208 vlc_tick_t i_buffering_delay;
210 /* */
211 ts_storage_t *p_storage_r;
212 ts_storage_t *p_storage_w;
214 vlc_tick_t i_cmd_delay;
216 } ts_thread_t;
218 struct es_out_id_t
220 es_out_id_t *p_es;
223 typedef struct
225 input_thread_t *p_input;
226 es_out_t *p_out;
228 /* Configuration */
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 */
233 vlc_mutex_t lock;
235 /* */
236 bool b_delayed;
237 ts_thread_t *p_ts;
239 /* */
240 bool b_input_paused;
241 bool b_input_paused_source;
242 int i_input_rate;
243 int i_input_rate_source;
245 /* */
246 int i_es;
247 es_out_id_t **pp_es;
249 es_out_t out;
250 } es_out_sys_t;
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 );
283 /* */
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 * );
294 /* File helpers */
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) );
305 if( !p_sys )
306 return NULL;
308 p_sys->out.cbs = &es_out_timeshift_cbs;
310 /* */
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;
321 p_sys->p_ts = NULL;
323 TAB_INIT( p_sys->i_es, p_sys->pp_es );
325 /* */
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;
329 else
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 );
339 if( count > 0 )
341 TCHAR *path = vlc_alloc( count + 1, sizeof(TCHAR) );
342 if( path != NULL )
344 DWORD ret = GetTempPath( count + 1, path );
345 if( ret != 0 && ret <= count )
346 p_sys->psz_tmp_path = FromT( path );
347 free( path );
351 if( p_sys->psz_tmp_path == NULL )
353 wchar_t *wpath = _wgetcwd( NULL, 0 );
354 if( wpath != NULL )
356 p_sys->psz_tmp_path = FromWide( wpath );
357 free( 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 )
368 len--;
370 p_sys->psz_tmp_path[len] = '\0';
372 #endif
373 if( p_sys->psz_tmp_path != NULL )
374 msg_Dbg( p_input, "using timeshift path: %s", p_sys->psz_tmp_path );
375 else
376 msg_Dbg( p_input, "using default timeshift path" );
378 #if 0
379 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
380 S(ts_cmd_t);
381 S(ts_cmd_control_t);
382 S(ts_cmd_send_t);
383 S(ts_cmd_del_t);
384 S(ts_cmd_add_t);
385 #undef S
386 #endif
388 return &p_sys->out;
391 /*****************************************************************************
392 * Internal functions
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 );
410 free( p_sys );
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);
416 ts_cmd_t cmd;
418 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
419 if( !p_es )
420 return NULL;
422 vlc_mutex_lock( &p_sys->lock );
424 TsAutoStop( p_out );
426 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
428 vlc_mutex_unlock( &p_sys->lock );
429 free( p_es );
430 return NULL;
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 );
437 else
438 CmdExecuteAdd( p_sys->p_out, &cmd );
440 vlc_mutex_unlock( &p_sys->lock );
442 return p_es;
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);
447 ts_cmd_t cmd;
448 int i_ret = VLC_SUCCESS;
450 vlc_mutex_lock( &p_sys->lock );
452 TsAutoStop( p_out );
454 CmdInitSend( &cmd, p_es, p_block );
455 if( p_sys->b_delayed )
456 TsPushCmd( p_sys->p_ts, &cmd );
457 else
458 i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
460 vlc_mutex_unlock( &p_sys->lock );
462 return i_ret;
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);
467 ts_cmd_t cmd;
469 vlc_mutex_lock( &p_sys->lock );
471 TsAutoStop( p_out );
473 CmdInitDel( &cmd, p_es );
474 if( p_sys->b_delayed )
475 TsPushCmd( p_sys->p_ts, &cmd );
476 else
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 ) )
489 *pb_empty = false;
490 else
491 *pb_empty = es_out_GetEmpty( p_sys->p_out );
493 return VLC_SUCCESS;
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 );
502 *pi_wakeup = 0;
504 else
506 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
509 return VLC_SUCCESS;
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;
517 else
518 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
520 return VLC_SUCCESS;
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);
525 int i_ret;
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 );
531 else
533 i_ret = VLC_EGENERIC;
534 if( !input_priv(p_sys->p_input)->b_can_pace_control )
536 if( !p_sys->b_delayed )
537 TsStart( p_out );
538 if( p_sys->b_delayed )
539 i_ret = TsChangePause( p_sys->p_ts, b_source_paused, b_paused, i_date );
541 else
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
545 * module */
546 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
550 if( !i_ret )
552 p_sys->b_input_paused_source = b_source_paused;
553 p_sys->b_input_paused = b_paused;
555 return i_ret;
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);
560 int i_ret;
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 );
566 else
568 i_ret = VLC_EGENERIC;
569 if( !input_priv(p_sys->p_input)->b_can_pace_control )
571 if( !p_sys->b_delayed )
572 TsStart( p_out );
573 if( p_sys->b_delayed )
574 i_ret = TsChangeRate( p_sys->p_ts, i_src_rate, i_rate );
576 else
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
580 * module */
581 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
586 if( !i_ret )
588 p_sys->i_input_rate_source = i_src_rate;
589 p_sys->i_input_rate = i_rate;
591 return i_ret;
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);
604 switch( i_query )
606 /* Pass-through control */
607 case ES_OUT_SET_MODE:
608 case ES_OUT_SET_GROUP:
609 case ES_OUT_SET_PCR:
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:
620 case ES_OUT_SET_ES:
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:
629 case ES_OUT_SET_EOS:
631 ts_cmd_t cmd;
632 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
633 return VLC_EGENERIC;
634 if( p_sys->b_delayed )
636 TsPushCmd( p_sys->p_ts, &cmd );
637 return VLC_SUCCESS;
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 )
650 *pb_enabled = true;
651 return VLC_SUCCESS;
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 )
693 return VLC_EGENERIC;
694 /* fall through */
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 )
705 return VLC_EGENERIC;
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:
719 default:
720 vlc_assert_unreachable();
721 return VLC_EGENERIC;
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);
727 int i_ret;
729 vlc_mutex_lock( &p_sys->lock );
731 TsAutoStop( p_out );
733 i_ret = ControlLocked( p_out, i_query, args );
735 vlc_mutex_unlock( &p_sys->lock );
737 return i_ret;
740 static const struct es_out_callbacks es_out_timeshift_cbs =
742 .add = Add,
743 .send = Send,
744 .del = Del,
745 .control = Control,
746 .destroy = Destroy,
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 );
756 free( p_ts );
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);
761 ts_thread_t *p_ts;
763 assert( !p_sys->b_delayed );
765 p_sys->p_ts = p_ts = calloc(1, sizeof(*p_ts));
766 if( !p_ts )
767 return VLC_EGENERIC;
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" );
791 TsDestroy( p_ts );
793 p_sys->b_delayed = false;
794 return VLC_EGENERIC;
797 return VLC_SUCCESS;
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 ) )
804 return;
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 );
817 for( ;; )
819 ts_cmd_t cmd;
821 if( TsPopCmdLocked( p_ts, &cmd, true ) )
822 break;
824 CmdClean( &cmd );
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 );
831 TsDestroy( p_ts );
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 );
841 if( !p_storage )
843 CmdClean( p_cmd );
844 vlc_mutex_unlock( &p_ts->lock );
845 /* TODO warn the user (but only once) */
846 return;
849 if( !p_ts->p_storage_w )
851 p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
853 else
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 ) )
873 return VLC_EGENERIC;
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;
880 if( !p_next )
881 break;
883 TsStorageDelete( p_ts->p_storage_r );
884 p_ts->p_storage_r = p_next;
887 return VLC_SUCCESS;
889 static bool TsHasCmd( ts_thread_t *p_ts )
891 bool b_cmd;
893 vlc_mutex_lock( &p_ts->lock );
894 b_cmd = TsStorageIsEmpty( p_ts->p_storage_r );
895 vlc_mutex_unlock( &p_ts->lock );
897 return b_cmd;
899 static bool TsIsUnused( ts_thread_t *p_ts )
901 bool b_unused;
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 );
909 return b_unused;
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 );
915 int i_ret;
916 if( b_paused )
918 assert( !b_source_paused );
919 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
921 else
923 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
926 if( !i_ret )
928 if( !b_paused )
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 );
941 return i_ret;
943 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
945 int i_ret;
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 );
958 return i_ret;
961 static void *TsRun( void *p_data )
963 ts_thread_t *p_ts = p_data;
964 vlc_tick_t i_buffering_date = -1;
966 for( ;; )
968 ts_cmd_t cmd;
969 vlc_tick_t i_deadline;
970 bool b_buffering;
972 /* Pop a command to execute */
973 vlc_mutex_lock( &p_ts->lock );
974 mutex_cleanup_push( &p_ts->lock );
976 for( ;; )
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 );
984 break;
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 */
998 if( b_buffering )
999 i_buffering_date = cmd.i_date;
1000 else
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 };
1030 /* Warn back input
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;
1040 vlc_cleanup_pop();
1041 vlc_mutex_unlock( &p_ts->lock );
1043 /* Regulate the speed of command processing to the same one than
1044 * reading */
1045 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
1047 vlc_tick_wait( i_deadline );
1049 vlc_cleanup_pop();
1051 /* Execute the command */
1052 const int canc = vlc_savecancel();
1053 switch( cmd.i_type )
1055 case C_ADD:
1056 CmdExecuteAdd( p_ts->p_out, &cmd );
1057 CmdCleanAdd( &cmd );
1058 break;
1059 case C_SEND:
1060 CmdExecuteSend( p_ts->p_out, &cmd );
1061 CmdCleanSend( &cmd );
1062 break;
1063 case C_CONTROL:
1064 CmdExecuteControl( p_ts->p_out, &cmd );
1065 CmdCleanControl( &cmd );
1066 break;
1067 case C_DEL:
1068 CmdExecuteDel( p_ts->p_out, &cmd );
1069 break;
1070 default:
1071 vlc_assert_unreachable();
1072 break;
1074 vlc_restorecancel( canc );
1077 return NULL;
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) )
1087 return NULL;
1089 char *psz_file;
1090 int fd = GetTmpFile( &psz_file, psz_tmp_path );
1091 if( fd == -1 )
1093 free( p_storage );
1094 return NULL;
1097 p_storage->p_filew = fdopen( fd, "w+b" );
1098 if( p_storage->p_filew == NULL )
1100 vlc_close( fd );
1101 vlc_unlink( psz_file );
1102 goto error;
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 );
1110 goto error;
1113 #ifndef _WIN32
1114 vlc_unlink( psz_file );
1115 free( psz_file );
1116 #else
1117 p_storage->psz_file = psz_file;
1118 #endif
1119 p_storage->p_next = NULL;
1121 /* */
1122 p_storage->i_file_max = i_tmp_size_max;
1123 p_storage->i_file_size = 0;
1125 /* */
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 );
1135 return NULL;
1137 return p_storage;
1138 error:
1139 free( psz_file );
1140 free( p_storage );
1141 return NULL;
1144 static void TsStorageDelete( ts_storage_t *p_storage )
1146 while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1148 ts_cmd_t cmd;
1150 TsStoragePopCmd( p_storage, &cmd, true );
1152 CmdClean( &cmd );
1154 free( p_storage->p_cmd );
1156 fclose( p_storage->p_filer );
1157 fclose( p_storage->p_filew );
1158 #ifdef _WIN32
1159 vlc_unlink( p_storage->psz_file );
1160 free( p_storage->psz_file );
1161 #endif
1162 free( p_storage );
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 )
1169 return;
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) );
1174 if( p_new )
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 )
1184 return true;
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 );
1208 return;
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 );
1216 return;
1219 p_storage->i_file_size += p_block->i_buffer;
1220 block_Release( p_block );
1222 if( b_flush )
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 )
1234 block_t block;
1236 if( !b_flush &&
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 );
1241 if( p_block )
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;
1252 else
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 )
1267 case C_ADD:
1268 CmdCleanAdd( p_cmd );
1269 break;
1270 case C_SEND:
1271 CmdCleanSend( p_cmd );
1272 break;
1273 case C_CONTROL:
1274 CmdCleanControl( p_cmd );
1275 break;
1276 case C_DEL:
1277 break;
1278 default:
1279 vlc_assert_unreachable();
1280 break;
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;
1289 if( b_copy )
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 );
1296 else
1298 p_cmd->u.add.p_fmt = (es_format_t*)p_fmt;
1300 return VLC_SUCCESS;
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;
1325 if( p_block )
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;
1344 return VLC_SUCCESS;
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;
1359 switch( 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 );
1366 break;
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 );
1371 break;
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 );
1376 break;
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 );
1381 break;
1383 case ES_OUT_RESET_PCR: /* no arg */
1384 case ES_OUT_SET_EOS:
1385 break;
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 * );
1394 if( b_copy )
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 );
1401 else
1403 /* The cast is only needed to avoid warning */
1404 p_cmd->u.control.u.int_meta.p_meta = (vlc_meta_t*)p_meta;
1406 break;
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 * );
1414 if( b_copy )
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;
1420 else
1422 /* The cast is only needed to avoid warning */
1423 p_cmd->u.control.u.int_epg.p_epg = (vlc_epg_t*)p_epg;
1425 break;
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 * );
1432 if( b_copy )
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;
1438 else
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;
1443 break;
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 );
1447 break;
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 * );
1455 break;
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 );
1460 break;
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 );
1465 break;
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 * );
1472 if( b_copy )
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 );
1479 else
1481 p_cmd->u.control.u.es_fmt.p_fmt = p_fmt;
1483 break;
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;
1494 break;
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;
1505 break;
1508 default:
1509 vlc_assert_unreachable();
1510 return VLC_EGENERIC;
1513 return VLC_SUCCESS;
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;
1519 switch( 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 );
1590 default:
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 );
1603 break;
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 );
1607 break;
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 );
1611 break;
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 );
1618 break;
1622 static int GetTmpFile( char **filename, const char *dirname )
1624 if( dirname != NULL
1625 && asprintf( filename, "%s"DIR_SEP PACKAGE_NAME"-timeshift.XXXXXX",
1626 dirname ) >= 0 )
1628 vlc_mkdir( dirname, 0700 );
1630 int fd = vlc_mkstemp( *filename );
1631 if( fd != -1 )
1632 return fd;
1634 free( *filename );
1637 *filename = strdup( DIR_SEP"tmp"DIR_SEP PACKAGE_NAME"-timeshift.XXXXXX" );
1638 if( unlikely(*filename == NULL) )
1639 return -1;
1641 int fd = vlc_mkstemp( *filename );
1642 if( fd != -1 )
1643 return fd;
1645 free( *filename );
1646 return -1;