codec/esout: add support for CEA708
[vlc.git] / src / input / es_out_timeshift.c
blob48f42f5f45f7afceec7d7cb4e8af35618abe3a11
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 mtime_t i_time;
140 mtime_t i_length;
141 } times;
142 struct
144 mtime_t i_pts_delay;
145 mtime_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 mtime_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 mtime_t i_pause_date;
201 /* */
202 int i_rate;
203 int i_rate_source;
204 mtime_t i_rate_date;
205 mtime_t i_rate_delay;
207 /* */
208 mtime_t i_buffering_delay;
210 /* */
211 ts_storage_t *p_storage_r;
212 ts_storage_t *p_storage_w;
214 mtime_t i_cmd_delay;
216 } ts_thread_t;
218 struct es_out_id_t
220 es_out_id_t *p_es;
223 struct es_out_sys_t
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;
250 static es_out_id_t *Add ( es_out_t *, const es_format_t * );
251 static int Send ( es_out_t *, es_out_id_t *, block_t * );
252 static void Del ( es_out_t *, es_out_id_t * );
253 static int Control( es_out_t *, int i_query, va_list );
254 static void Destroy( es_out_t * );
256 static int TsStart( es_out_t * );
257 static void TsAutoStop( es_out_t * );
259 static void TsStop( ts_thread_t * );
260 static void TsPushCmd( ts_thread_t *, ts_cmd_t * );
261 static int TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
262 static bool TsHasCmd( ts_thread_t * );
263 static bool TsIsUnused( ts_thread_t * );
264 static int TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
265 static int TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
267 static void *TsRun( void * );
269 static ts_storage_t *TsStorageNew( const char *psz_path, int64_t i_tmp_size_max );
270 static void TsStorageDelete( ts_storage_t * );
271 static void TsStoragePack( ts_storage_t *p_storage );
272 static bool TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
273 static bool TsStorageIsEmpty( ts_storage_t * );
274 static void TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
275 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
277 static void CmdClean( ts_cmd_t * );
278 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
280 static int CmdInitAdd ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
281 static void CmdInitSend ( ts_cmd_t *, es_out_id_t *, block_t * );
282 static int CmdInitDel ( ts_cmd_t *, es_out_id_t * );
283 static int CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
285 /* */
286 static void CmdCleanAdd ( ts_cmd_t * );
287 static void CmdCleanSend ( ts_cmd_t * );
288 static void CmdCleanControl( ts_cmd_t *p_cmd );
290 /* XXX these functions will take the destination es_out_t */
291 static void CmdExecuteAdd ( es_out_t *, ts_cmd_t * );
292 static int CmdExecuteSend ( es_out_t *, ts_cmd_t * );
293 static void CmdExecuteDel ( es_out_t *, ts_cmd_t * );
294 static int CmdExecuteControl( es_out_t *, ts_cmd_t * );
296 /* File helpers */
297 static int GetTmpFile( char **ppsz_file, const char *psz_path );
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_t *p_out = malloc( sizeof(*p_out) );
305 if( !p_out )
306 return NULL;
308 es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
309 if( !p_sys )
311 free( p_out );
312 return NULL;
315 /* */
316 p_out->pf_add = Add;
317 p_out->pf_send = Send;
318 p_out->pf_del = Del;
319 p_out->pf_control = Control;
320 p_out->pf_destroy = Destroy;
321 p_out->p_sys = p_sys;
323 /* */
324 p_sys->b_input_paused = false;
325 p_sys->b_input_paused_source = false;
326 p_sys->p_input = p_input;
327 p_sys->i_input_rate = i_rate;
328 p_sys->i_input_rate_source = i_rate;
330 p_sys->p_out = p_next_out;
331 vlc_mutex_init_recursive( &p_sys->lock );
333 p_sys->b_delayed = false;
334 p_sys->p_ts = NULL;
336 TAB_INIT( p_sys->i_es, p_sys->pp_es );
338 /* */
339 const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
340 if( i_tmp_size_max < 0 )
341 p_sys->i_tmp_size_max = 50*1024*1024;
342 else
343 p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
344 msg_Dbg( p_input, "using timeshift granularity of %d MiB",
345 (int)p_sys->i_tmp_size_max/(1024*1024) );
347 p_sys->psz_tmp_path = var_InheritString( p_input, "input-timeshift-path" );
348 #if defined (_WIN32) && !VLC_WINSTORE_APP
349 if( p_sys->psz_tmp_path == NULL )
351 const DWORD count = GetTempPath( 0, NULL );
352 if( count > 0 )
354 TCHAR *path = malloc( (count + 1) * sizeof(TCHAR) );
355 if( path != NULL )
357 DWORD ret = GetTempPath( count + 1, path );
358 if( ret != 0 && ret <= count )
359 p_sys->psz_tmp_path = FromT( path );
360 free( path );
364 if( p_sys->psz_tmp_path == NULL )
366 wchar_t *wpath = _wgetcwd( NULL, 0 );
367 if( wpath != NULL )
369 p_sys->psz_tmp_path = FromWide( wpath );
370 free( wpath );
373 if( p_sys->psz_tmp_path == NULL )
374 p_sys->psz_tmp_path = strdup( "C:" );
376 if( p_sys->psz_tmp_path != NULL )
378 size_t len = strlen( p_sys->psz_tmp_path );
380 while( len > 0 && p_sys->psz_tmp_path[len - 1] == DIR_SEP_CHAR )
381 len--;
383 p_sys->psz_tmp_path[len] = '\0';
385 #endif
386 if( p_sys->psz_tmp_path != NULL )
387 msg_Dbg( p_input, "using timeshift path: %s", p_sys->psz_tmp_path );
388 else
389 msg_Dbg( p_input, "using default timeshift path" );
391 #if 0
392 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
393 S(ts_cmd_t);
394 S(ts_cmd_control_t);
395 S(ts_cmd_send_t);
396 S(ts_cmd_del_t);
397 S(ts_cmd_add_t);
398 #undef S
399 #endif
401 return p_out;
404 /*****************************************************************************
405 * Internal functions
406 *****************************************************************************/
407 static void Destroy( es_out_t *p_out )
409 es_out_sys_t *p_sys = p_out->p_sys;
411 if( p_sys->b_delayed )
413 TsStop( p_sys->p_ts );
414 p_sys->b_delayed = false;
417 while( p_sys->i_es > 0 )
418 Del( p_out, p_sys->pp_es[0] );
419 TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
421 free( p_sys->psz_tmp_path );
422 vlc_mutex_destroy( &p_sys->lock );
423 free( p_sys );
424 free( p_out );
427 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
429 es_out_sys_t *p_sys = p_out->p_sys;
430 ts_cmd_t cmd;
432 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
433 if( !p_es )
434 return NULL;
436 vlc_mutex_lock( &p_sys->lock );
438 TsAutoStop( p_out );
440 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
442 vlc_mutex_unlock( &p_sys->lock );
443 free( p_es );
444 return NULL;
447 TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
449 if( p_sys->b_delayed )
450 TsPushCmd( p_sys->p_ts, &cmd );
451 else
452 CmdExecuteAdd( p_sys->p_out, &cmd );
454 vlc_mutex_unlock( &p_sys->lock );
456 return p_es;
458 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
460 es_out_sys_t *p_sys = p_out->p_sys;
461 ts_cmd_t cmd;
462 int i_ret = VLC_SUCCESS;
464 vlc_mutex_lock( &p_sys->lock );
466 TsAutoStop( p_out );
468 CmdInitSend( &cmd, p_es, p_block );
469 if( p_sys->b_delayed )
470 TsPushCmd( p_sys->p_ts, &cmd );
471 else
472 i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
474 vlc_mutex_unlock( &p_sys->lock );
476 return i_ret;
478 static void Del( es_out_t *p_out, es_out_id_t *p_es )
480 es_out_sys_t *p_sys = p_out->p_sys;
481 ts_cmd_t cmd;
483 vlc_mutex_lock( &p_sys->lock );
485 TsAutoStop( p_out );
487 CmdInitDel( &cmd, p_es );
488 if( p_sys->b_delayed )
489 TsPushCmd( p_sys->p_ts, &cmd );
490 else
491 CmdExecuteDel( p_sys->p_out, &cmd );
493 TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
495 vlc_mutex_unlock( &p_sys->lock );
498 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
500 es_out_sys_t *p_sys = p_out->p_sys;
502 if( p_sys->b_delayed && TsHasCmd( p_sys->p_ts ) )
503 *pb_empty = false;
504 else
505 *pb_empty = es_out_GetEmpty( p_sys->p_out );
507 return VLC_SUCCESS;
509 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
511 es_out_sys_t *p_sys = p_out->p_sys;
513 if( p_sys->b_delayed )
515 assert( !input_priv(p_sys->p_input)->b_can_pace_control );
516 *pi_wakeup = 0;
518 else
520 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
523 return VLC_SUCCESS;
525 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
527 es_out_sys_t *p_sys = p_out->p_sys;
529 if( p_sys->b_delayed )
530 *pb_buffering = true;
531 else
532 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
534 return VLC_SUCCESS;
536 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
538 es_out_sys_t *p_sys = p_out->p_sys;
539 int i_ret;
541 if( !p_sys->b_delayed && !b_source_paused == !b_paused )
543 i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
545 else
547 i_ret = VLC_EGENERIC;
548 if( !input_priv(p_sys->p_input)->b_can_pace_control )
550 if( !p_sys->b_delayed )
551 TsStart( p_out );
552 if( p_sys->b_delayed )
553 i_ret = TsChangePause( p_sys->p_ts, b_source_paused, b_paused, i_date );
555 else
557 /* XXX we may do it BUT it would be better to finish the clock clean up+improvements
558 * and so be able to advertize correctly pace control property in access
559 * module */
560 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
564 if( !i_ret )
566 p_sys->b_input_paused_source = b_source_paused;
567 p_sys->b_input_paused = b_paused;
569 return i_ret;
571 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
573 es_out_sys_t *p_sys = p_out->p_sys;
574 int i_ret;
576 if( !p_sys->b_delayed && i_src_rate == i_rate )
578 i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
580 else
582 i_ret = VLC_EGENERIC;
583 if( !input_priv(p_sys->p_input)->b_can_pace_control )
585 if( !p_sys->b_delayed )
586 TsStart( p_out );
587 if( p_sys->b_delayed )
588 i_ret = TsChangeRate( p_sys->p_ts, i_src_rate, i_rate );
590 else
592 /* XXX we may do it BUT it would be better to finish the clock clean up+improvements
593 * and so be able to advertize correctly pace control property in access
594 * module */
595 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
600 if( !i_ret )
602 p_sys->i_input_rate_source = i_src_rate;
603 p_sys->i_input_rate = i_rate;
605 return i_ret;
607 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
609 es_out_sys_t *p_sys = p_out->p_sys;
611 if( !p_sys->b_delayed )
612 return es_out_SetTime( p_sys->p_out, i_date );
614 /* TODO */
615 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
616 return VLC_EGENERIC;
618 static int ControlLockedSetFrameNext( es_out_t *p_out )
620 es_out_sys_t *p_sys = p_out->p_sys;
622 return es_out_SetFrameNext( p_sys->p_out );
625 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
627 es_out_sys_t *p_sys = p_out->p_sys;
629 switch( i_query )
631 /* Pass-through control */
632 case ES_OUT_SET_MODE:
633 case ES_OUT_SET_GROUP:
634 case ES_OUT_SET_PCR:
635 case ES_OUT_SET_GROUP_PCR:
636 case ES_OUT_RESET_PCR:
637 case ES_OUT_SET_NEXT_DISPLAY_TIME:
638 case ES_OUT_SET_GROUP_META:
639 case ES_OUT_SET_GROUP_EPG:
640 case ES_OUT_SET_GROUP_EPG_EVENT:
641 case ES_OUT_SET_EPG_TIME:
642 case ES_OUT_SET_ES_SCRAMBLED_STATE:
643 case ES_OUT_DEL_GROUP:
644 case ES_OUT_SET_META:
645 case ES_OUT_SET_ES:
646 case ES_OUT_RESTART_ES:
647 case ES_OUT_RESTART_ALL_ES:
648 case ES_OUT_SET_ES_DEFAULT:
649 case ES_OUT_SET_ES_STATE:
650 case ES_OUT_SET_ES_CAT_POLICY:
651 case ES_OUT_SET_ES_FMT:
652 case ES_OUT_SET_TIMES:
653 case ES_OUT_SET_JITTER:
654 case ES_OUT_SET_EOS:
656 ts_cmd_t cmd;
657 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
658 return VLC_EGENERIC;
659 if( p_sys->b_delayed )
661 TsPushCmd( p_sys->p_ts, &cmd );
662 return VLC_SUCCESS;
664 return CmdExecuteControl( p_sys->p_out, &cmd );
667 /* Special control when delayed */
668 case ES_OUT_GET_ES_STATE:
670 es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
671 bool *pb_enabled = (bool*)va_arg( args, bool* );
673 if( p_sys->b_delayed )
675 *pb_enabled = true;
676 return VLC_SUCCESS;
678 return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
680 /* Special internal input control */
681 case ES_OUT_GET_EMPTY:
683 bool *pb_empty = (bool*)va_arg( args, bool* );
684 return ControlLockedGetEmpty( p_out, pb_empty );
686 case ES_OUT_GET_WAKE_UP: /* TODO ? */
688 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
689 return ControlLockedGetWakeup( p_out, pi_wakeup );
691 case ES_OUT_GET_BUFFERING:
693 bool *pb_buffering = (bool *)va_arg( args, bool* );
694 return ControlLockedGetBuffering( p_out, pb_buffering );
696 case ES_OUT_SET_PAUSE_STATE:
698 const bool b_source_paused = (bool)va_arg( args, int );
699 const bool b_paused = (bool)va_arg( args, int );
700 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
702 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
704 case ES_OUT_SET_RATE:
706 const int i_src_rate = (int)va_arg( args, int );
707 const int i_rate = (int)va_arg( args, int );
709 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
711 case ES_OUT_SET_TIME:
713 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
715 return ControlLockedSetTime( p_out, i_date );
717 case ES_OUT_SET_FRAME_NEXT:
719 return ControlLockedSetFrameNext( p_out );
722 case ES_OUT_GET_PCR_SYSTEM:
723 if( p_sys->b_delayed )
724 return VLC_EGENERIC;
725 /* fall through */
726 case ES_OUT_GET_GROUP_FORCED:
727 case ES_OUT_POST_SUBNODE:
728 return es_out_vaControl( p_sys->p_out, i_query, args );
730 case ES_OUT_MODIFY_PCR_SYSTEM:
732 const bool b_absolute = va_arg( args, int );
733 const mtime_t i_system = va_arg( args, mtime_t );
735 if( b_absolute && p_sys->b_delayed )
736 return VLC_EGENERIC;
738 return es_out_ControlModifyPcrSystem( p_sys->p_out, b_absolute, i_system );
741 /* Invalid queries for this es_out level */
742 case ES_OUT_SET_ES_BY_ID:
743 case ES_OUT_RESTART_ES_BY_ID:
744 case ES_OUT_SET_ES_DEFAULT_BY_ID:
745 case ES_OUT_GET_ES_OBJECTS_BY_ID:
746 case ES_OUT_SET_DELAY:
747 case ES_OUT_SET_RECORD_STATE:
748 default:
749 vlc_assert_unreachable();
750 return VLC_EGENERIC;
753 static int Control( es_out_t *p_out, int i_query, va_list args )
755 es_out_sys_t *p_sys = p_out->p_sys;
756 int i_ret;
758 vlc_mutex_lock( &p_sys->lock );
760 TsAutoStop( p_out );
762 i_ret = ControlLocked( p_out, i_query, args );
764 vlc_mutex_unlock( &p_sys->lock );
766 return i_ret;
769 /*****************************************************************************
771 *****************************************************************************/
772 static void TsDestroy( ts_thread_t *p_ts )
774 vlc_cond_destroy( &p_ts->wait );
775 vlc_mutex_destroy( &p_ts->lock );
776 free( p_ts );
778 static int TsStart( es_out_t *p_out )
780 es_out_sys_t *p_sys = p_out->p_sys;
781 ts_thread_t *p_ts;
783 assert( !p_sys->b_delayed );
785 p_sys->p_ts = p_ts = calloc(1, sizeof(*p_ts));
786 if( !p_ts )
787 return VLC_EGENERIC;
789 p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
790 p_ts->psz_tmp_path = p_sys->psz_tmp_path;
791 p_ts->p_input = p_sys->p_input;
792 p_ts->p_out = p_sys->p_out;
793 vlc_mutex_init( &p_ts->lock );
794 vlc_cond_init( &p_ts->wait );
795 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
796 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
797 p_ts->i_rate_source = p_sys->i_input_rate_source;
798 p_ts->i_rate = p_sys->i_input_rate;
799 p_ts->i_rate_date = -1;
800 p_ts->i_rate_delay = 0;
801 p_ts->i_buffering_delay = 0;
802 p_ts->i_cmd_delay = 0;
803 p_ts->p_storage_r = NULL;
804 p_ts->p_storage_w = NULL;
806 p_sys->b_delayed = true;
807 if( vlc_clone( &p_ts->thread, TsRun, p_ts, VLC_THREAD_PRIORITY_INPUT ) )
809 msg_Err( p_sys->p_input, "cannot create timeshift thread" );
811 TsDestroy( p_ts );
813 p_sys->b_delayed = false;
814 return VLC_EGENERIC;
817 return VLC_SUCCESS;
819 static void TsAutoStop( es_out_t *p_out )
821 es_out_sys_t *p_sys = p_out->p_sys;
823 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_ts ) )
824 return;
826 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
827 TsStop( p_sys->p_ts );
829 p_sys->b_delayed = false;
831 static void TsStop( ts_thread_t *p_ts )
833 vlc_cancel( p_ts->thread );
834 vlc_join( p_ts->thread, NULL );
836 vlc_mutex_lock( &p_ts->lock );
837 for( ;; )
839 ts_cmd_t cmd;
841 if( TsPopCmdLocked( p_ts, &cmd, true ) )
842 break;
844 CmdClean( &cmd );
846 assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
847 if( p_ts->p_storage_r )
848 TsStorageDelete( p_ts->p_storage_r );
849 vlc_mutex_unlock( &p_ts->lock );
851 TsDestroy( p_ts );
853 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
855 vlc_mutex_lock( &p_ts->lock );
857 if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
859 ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
861 if( !p_storage )
863 CmdClean( p_cmd );
864 vlc_mutex_unlock( &p_ts->lock );
865 /* TODO warn the user (but only once) */
866 return;
869 if( !p_ts->p_storage_w )
871 p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
873 else
875 TsStoragePack( p_ts->p_storage_w );
876 p_ts->p_storage_w->p_next = p_storage;
877 p_ts->p_storage_w = p_storage;
881 /* TODO return error and warn the user (but only once) */
882 TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
884 vlc_cond_signal( &p_ts->wait );
886 vlc_mutex_unlock( &p_ts->lock );
888 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
890 vlc_assert_locked( &p_ts->lock );
892 if( TsStorageIsEmpty( p_ts->p_storage_r ) )
893 return VLC_EGENERIC;
895 TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
897 while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
899 ts_storage_t *p_next = p_ts->p_storage_r->p_next;
900 if( !p_next )
901 break;
903 TsStorageDelete( p_ts->p_storage_r );
904 p_ts->p_storage_r = p_next;
907 return VLC_SUCCESS;
909 static bool TsHasCmd( ts_thread_t *p_ts )
911 bool b_cmd;
913 vlc_mutex_lock( &p_ts->lock );
914 b_cmd = TsStorageIsEmpty( p_ts->p_storage_r );
915 vlc_mutex_unlock( &p_ts->lock );
917 return b_cmd;
919 static bool TsIsUnused( ts_thread_t *p_ts )
921 bool b_unused;
923 vlc_mutex_lock( &p_ts->lock );
924 b_unused = !p_ts->b_paused &&
925 p_ts->i_rate == p_ts->i_rate_source &&
926 TsStorageIsEmpty( p_ts->p_storage_r );
927 vlc_mutex_unlock( &p_ts->lock );
929 return b_unused;
931 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
933 vlc_mutex_lock( &p_ts->lock );
935 int i_ret;
936 if( b_paused )
938 assert( !b_source_paused );
939 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
941 else
943 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
946 if( !i_ret )
948 if( !b_paused )
950 assert( p_ts->i_pause_date > 0 );
952 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
955 p_ts->b_paused = b_paused;
956 p_ts->i_pause_date = i_date;
958 vlc_cond_signal( &p_ts->wait );
960 vlc_mutex_unlock( &p_ts->lock );
961 return i_ret;
963 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
965 int i_ret;
967 vlc_mutex_lock( &p_ts->lock );
968 p_ts->i_cmd_delay += p_ts->i_rate_delay;
970 p_ts->i_rate_date = -1;
971 p_ts->i_rate_delay = 0;
972 p_ts->i_rate = i_rate;
973 p_ts->i_rate_source = i_src_rate;
975 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
976 vlc_mutex_unlock( &p_ts->lock );
978 return i_ret;
981 static void *TsRun( void *p_data )
983 ts_thread_t *p_ts = p_data;
984 mtime_t i_buffering_date = -1;
986 for( ;; )
988 ts_cmd_t cmd;
989 mtime_t i_deadline;
990 bool b_buffering;
992 /* Pop a command to execute */
993 vlc_mutex_lock( &p_ts->lock );
994 mutex_cleanup_push( &p_ts->lock );
996 for( ;; )
998 const int canc = vlc_savecancel();
999 b_buffering = es_out_GetBuffering( p_ts->p_out );
1001 if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
1003 vlc_restorecancel( canc );
1004 break;
1006 vlc_restorecancel( canc );
1008 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
1011 if( b_buffering && i_buffering_date < 0 )
1013 i_buffering_date = cmd.i_date;
1015 else if( i_buffering_date > 0 )
1017 p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
1018 if( b_buffering )
1019 i_buffering_date = cmd.i_date;
1020 else
1021 i_buffering_date = -1;
1024 if( p_ts->i_rate_date < 0 )
1025 p_ts->i_rate_date = cmd.i_date;
1027 p_ts->i_rate_delay = 0;
1028 if( p_ts->i_rate_source != p_ts->i_rate )
1030 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
1031 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
1033 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 )
1035 const int canc = vlc_savecancel();
1037 /* Auto reset to rate 1.0 */
1038 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
1040 p_ts->i_cmd_delay = 0;
1041 p_ts->i_buffering_delay = 0;
1043 p_ts->i_rate_delay = 0;
1044 p_ts->i_rate_date = -1;
1045 p_ts->i_rate = p_ts->i_rate_source;
1047 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
1049 vlc_value_t val = { .i_int = p_ts->i_rate };
1050 /* Warn back input
1051 * FIXME it is perfectly safe BUT it is ugly as it may hide a
1052 * rate change requested by user */
1053 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
1056 vlc_restorecancel( canc );
1058 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
1060 vlc_cleanup_pop();
1061 vlc_mutex_unlock( &p_ts->lock );
1063 /* Regulate the speed of command processing to the same one than
1064 * reading */
1065 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
1067 mwait( i_deadline );
1069 vlc_cleanup_pop();
1071 /* Execute the command */
1072 const int canc = vlc_savecancel();
1073 switch( cmd.i_type )
1075 case C_ADD:
1076 CmdExecuteAdd( p_ts->p_out, &cmd );
1077 CmdCleanAdd( &cmd );
1078 break;
1079 case C_SEND:
1080 CmdExecuteSend( p_ts->p_out, &cmd );
1081 CmdCleanSend( &cmd );
1082 break;
1083 case C_CONTROL:
1084 CmdExecuteControl( p_ts->p_out, &cmd );
1085 CmdCleanControl( &cmd );
1086 break;
1087 case C_DEL:
1088 CmdExecuteDel( p_ts->p_out, &cmd );
1089 break;
1090 default:
1091 vlc_assert_unreachable();
1092 break;
1094 vlc_restorecancel( canc );
1097 return NULL;
1100 /*****************************************************************************
1102 *****************************************************************************/
1103 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1105 ts_storage_t *p_storage = malloc( sizeof (*p_storage) );
1106 if( unlikely(p_storage == NULL) )
1107 return NULL;
1109 char *psz_file;
1110 int fd = GetTmpFile( &psz_file, psz_tmp_path );
1111 if( fd == -1 )
1113 free( p_storage );
1114 return NULL;
1117 p_storage->p_filew = fdopen( fd, "w+b" );
1118 if( p_storage->p_filew == NULL )
1120 vlc_close( fd );
1121 vlc_unlink( psz_file );
1122 goto error;
1125 p_storage->p_filer = vlc_fopen( psz_file, "rb" );
1126 if( p_storage->p_filer == NULL )
1128 fclose( p_storage->p_filew );
1129 vlc_unlink( psz_file );
1130 goto error;
1133 #ifndef _WIN32
1134 vlc_unlink( psz_file );
1135 free( psz_file );
1136 #else
1137 p_storage->psz_file = psz_file;
1138 #endif
1139 p_storage->p_next = NULL;
1141 /* */
1142 p_storage->i_file_max = i_tmp_size_max;
1143 p_storage->i_file_size = 0;
1145 /* */
1146 p_storage->i_cmd_w = 0;
1147 p_storage->i_cmd_r = 0;
1148 p_storage->i_cmd_max = 30000;
1149 p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1150 //fprintf( stderr, "\nSTORAGE name=%s size=%d KiB\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1152 if( !p_storage->p_cmd )
1154 TsStorageDelete( p_storage );
1155 return NULL;
1157 return p_storage;
1158 error:
1159 free( psz_file );
1160 free( p_storage );
1161 return NULL;
1164 static void TsStorageDelete( ts_storage_t *p_storage )
1166 while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1168 ts_cmd_t cmd;
1170 TsStoragePopCmd( p_storage, &cmd, true );
1172 CmdClean( &cmd );
1174 free( p_storage->p_cmd );
1176 fclose( p_storage->p_filer );
1177 fclose( p_storage->p_filew );
1178 #ifdef _WIN32
1179 vlc_unlink( p_storage->psz_file );
1180 free( p_storage->psz_file );
1181 #endif
1182 free( p_storage );
1185 static void TsStoragePack( ts_storage_t *p_storage )
1187 /* Try to release a bit of memory */
1188 if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1189 return;
1191 p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1193 ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1194 if( p_new )
1195 p_storage->p_cmd = p_new;
1197 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1199 if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1201 size_t i_size = sizeof(*p_cmd->u.send.p_block) + p_cmd->u.send.p_block->i_buffer;
1203 if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1204 return true;
1206 return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1208 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1210 return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1212 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1214 ts_cmd_t cmd = *p_cmd;
1216 assert( !TsStorageIsFull( p_storage, p_cmd ) );
1218 if( cmd.i_type == C_SEND )
1220 block_t *p_block = cmd.u.send.p_block;
1222 cmd.u.send.p_block = NULL;
1223 cmd.u.send.i_offset = ftell( p_storage->p_filew );
1225 if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1227 block_Release( p_block );
1228 return;
1230 p_storage->i_file_size += sizeof(*p_block);
1231 if( p_block->i_buffer > 0 )
1233 if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1235 block_Release( p_block );
1236 return;
1239 p_storage->i_file_size += p_block->i_buffer;
1240 block_Release( p_block );
1242 if( b_flush )
1243 fflush( p_storage->p_filew );
1245 p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1247 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1249 assert( !TsStorageIsEmpty( p_storage ) );
1251 *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1252 if( p_cmd->i_type == C_SEND )
1254 block_t block;
1256 if( !b_flush &&
1257 !fseek( p_storage->p_filer, p_cmd->u.send.i_offset, SEEK_SET ) &&
1258 fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1260 block_t *p_block = block_Alloc( block.i_buffer );
1261 if( p_block )
1263 p_block->i_dts = block.i_dts;
1264 p_block->i_pts = block.i_pts;
1265 p_block->i_flags = block.i_flags;
1266 p_block->i_length = block.i_length;
1267 p_block->i_nb_samples = block.i_nb_samples;
1268 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1270 p_cmd->u.send.p_block = p_block;
1272 else
1274 //perror( "TsStoragePopCmd" );
1275 p_cmd->u.send.p_block = block_Alloc( 1 );
1280 /*****************************************************************************
1282 *****************************************************************************/
1283 static void CmdClean( ts_cmd_t *p_cmd )
1285 switch( p_cmd->i_type )
1287 case C_ADD:
1288 CmdCleanAdd( p_cmd );
1289 break;
1290 case C_SEND:
1291 CmdCleanSend( p_cmd );
1292 break;
1293 case C_CONTROL:
1294 CmdCleanControl( p_cmd );
1295 break;
1296 case C_DEL:
1297 break;
1298 default:
1299 vlc_assert_unreachable();
1300 break;
1304 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1306 p_cmd->i_type = C_ADD;
1307 p_cmd->i_date = mdate();
1308 p_cmd->u.add.p_es = p_es;
1309 if( b_copy )
1311 p_cmd->u.add.p_fmt = malloc( sizeof(*p_fmt) );
1312 if( !p_cmd->u.add.p_fmt )
1313 return VLC_EGENERIC;
1314 es_format_Copy( p_cmd->u.add.p_fmt, p_fmt );
1316 else
1318 p_cmd->u.add.p_fmt = (es_format_t*)p_fmt;
1320 return VLC_SUCCESS;
1322 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1324 p_cmd->u.add.p_es->p_es = es_out_Add( p_out, p_cmd->u.add.p_fmt );
1326 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1328 es_format_Clean( p_cmd->u.add.p_fmt );
1329 free( p_cmd->u.add.p_fmt );
1332 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1334 p_cmd->i_type = C_SEND;
1335 p_cmd->i_date = mdate();
1336 p_cmd->u.send.p_es = p_es;
1337 p_cmd->u.send.p_block = p_block;
1339 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1341 block_t *p_block = p_cmd->u.send.p_block;
1343 p_cmd->u.send.p_block = NULL;
1345 if( p_block )
1347 if( p_cmd->u.send.p_es->p_es )
1348 return es_out_Send( p_out, p_cmd->u.send.p_es->p_es, p_block );
1349 block_Release( p_block );
1351 return VLC_EGENERIC;
1353 static void CmdCleanSend( ts_cmd_t *p_cmd )
1355 if( p_cmd->u.send.p_block )
1356 block_Release( p_cmd->u.send.p_block );
1359 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1361 p_cmd->i_type = C_DEL;
1362 p_cmd->i_date = mdate();
1363 p_cmd->u.del.p_es = p_es;
1364 return VLC_SUCCESS;
1366 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1368 if( p_cmd->u.del.p_es->p_es )
1369 es_out_Del( p_out, p_cmd->u.del.p_es->p_es );
1370 free( p_cmd->u.del.p_es );
1373 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1375 p_cmd->i_type = C_CONTROL;
1376 p_cmd->i_date = mdate();
1377 p_cmd->u.control.i_query = i_query;
1379 switch( i_query )
1381 /* Pass-through control */
1382 case ES_OUT_SET_MODE: /* arg1= int */
1383 case ES_OUT_SET_GROUP: /* arg1= int */
1384 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1385 p_cmd->u.control.u.i_int = (int)va_arg( args, int );
1386 break;
1388 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1389 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1390 p_cmd->u.control.u.i_i64 = (int64_t)va_arg( args, int64_t );
1391 break;
1393 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1394 p_cmd->u.control.u.int_i64.i_int = (int)va_arg( args, int );
1395 p_cmd->u.control.u.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1396 break;
1398 case ES_OUT_SET_ES_SCRAMBLED_STATE:
1399 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1400 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1401 break;
1403 case ES_OUT_RESET_PCR: /* no arg */
1404 case ES_OUT_SET_EOS:
1405 case ES_OUT_RESTART_ALL_ES:
1406 break;
1408 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1409 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1411 if( i_query == ES_OUT_SET_GROUP_META )
1412 p_cmd->u.control.u.int_meta.i_int = (int)va_arg( args, int );
1413 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
1415 if( b_copy )
1417 p_cmd->u.control.u.int_meta.p_meta = vlc_meta_New();
1418 if( !p_cmd->u.control.u.int_meta.p_meta )
1419 return VLC_EGENERIC;
1420 vlc_meta_Merge( p_cmd->u.control.u.int_meta.p_meta, p_meta );
1422 else
1424 /* The cast is only needed to avoid warning */
1425 p_cmd->u.control.u.int_meta.p_meta = (vlc_meta_t*)p_meta;
1427 break;
1430 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1432 p_cmd->u.control.u.int_epg.i_int = (int)va_arg( args, int );
1433 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
1435 if( b_copy )
1437 p_cmd->u.control.u.int_epg.p_epg = vlc_epg_Duplicate( p_epg );
1438 if( !p_cmd->u.control.u.int_epg.p_epg )
1439 return VLC_EGENERIC;
1441 else
1443 /* The cast is only needed to avoid warning */
1444 p_cmd->u.control.u.int_epg.p_epg = (vlc_epg_t*)p_epg;
1446 break;
1448 case ES_OUT_SET_GROUP_EPG_EVENT: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1450 p_cmd->u.control.u.int_epg_evt.i_int = (int)va_arg( args, int );
1451 const vlc_epg_event_t *p_evt = va_arg( args, const vlc_epg_event_t * );
1453 if( b_copy )
1455 p_cmd->u.control.u.int_epg_evt.p_evt = vlc_epg_event_Duplicate( p_evt );
1456 if( !p_cmd->u.control.u.int_epg_evt.p_evt )
1457 return VLC_EGENERIC;
1459 else
1461 /* The cast is only needed to avoid warning */
1462 p_cmd->u.control.u.int_epg_evt.p_evt = (vlc_epg_event_t*)p_evt;
1464 break;
1466 case ES_OUT_SET_EPG_TIME: /* arg1=int64_t (seconds) */
1467 p_cmd->u.control.u.i_i64 = (int64_t)va_arg( args, int64_t );
1468 break;
1470 /* Modified control */
1471 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1472 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1473 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1474 p_cmd->u.control.u.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1475 break;
1477 case ES_OUT_SET_ES_CAT_POLICY:
1478 p_cmd->u.control.u.es_policy.i_cat = (int) va_arg( args, int );
1479 p_cmd->u.control.u.es_policy.i_policy = (int) va_arg( args, int );
1480 break;
1482 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1483 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1484 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1485 break;
1487 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1489 p_cmd->u.control.u.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1490 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1492 if( b_copy )
1494 p_cmd->u.control.u.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1495 if( !p_cmd->u.control.u.es_fmt.p_fmt )
1496 return VLC_EGENERIC;
1497 es_format_Copy( p_cmd->u.control.u.es_fmt.p_fmt, p_fmt );
1499 else
1501 p_cmd->u.control.u.es_fmt.p_fmt = p_fmt;
1503 break;
1505 case ES_OUT_SET_TIMES:
1507 double f_position = (double)va_arg( args, double );
1508 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
1509 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
1511 p_cmd->u.control.u.times.f_position = f_position;
1512 p_cmd->u.control.u.times.i_time = i_time;
1513 p_cmd->u.control.u.times.i_length = i_length;
1514 break;
1516 case ES_OUT_SET_JITTER:
1518 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
1519 mtime_t i_pts_jitter = (mtime_t)va_arg( args, mtime_t );
1520 int i_cr_average = (int)va_arg( args, int );
1522 p_cmd->u.control.u.jitter.i_pts_delay = i_pts_delay;
1523 p_cmd->u.control.u.jitter.i_pts_jitter = i_pts_jitter;
1524 p_cmd->u.control.u.jitter.i_cr_average = i_cr_average;
1525 break;
1528 default:
1529 vlc_assert_unreachable();
1530 return VLC_EGENERIC;
1533 return VLC_SUCCESS;
1535 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1537 const int i_query = p_cmd->u.control.i_query;
1539 switch( i_query )
1541 /* Pass-through control */
1542 case ES_OUT_SET_MODE: /* arg1= int */
1543 case ES_OUT_SET_GROUP: /* arg1= int */
1544 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1545 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_int );
1547 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1548 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1549 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_i64 );
1551 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1552 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_i64.i_int,
1553 p_cmd->u.control.u.int_i64.i_i64 );
1555 case ES_OUT_RESET_PCR: /* no arg */
1556 case ES_OUT_SET_EOS:
1557 case ES_OUT_RESTART_ALL_ES:
1558 return es_out_Control( p_out, i_query );
1560 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1561 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.i_int,
1562 p_cmd->u.control.u.int_meta.p_meta );
1564 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1565 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_epg.i_int,
1566 p_cmd->u.control.u.int_epg.p_epg );
1568 case ES_OUT_SET_GROUP_EPG_EVENT: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1569 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_epg_evt.i_int,
1570 p_cmd->u.control.u.int_epg_evt.p_evt );
1572 case ES_OUT_SET_EPG_TIME: /* arg1=int64_t */
1573 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_i64 );
1575 case ES_OUT_SET_ES_SCRAMBLED_STATE: /* arg1=int es_out_id_t* arg2=bool */
1576 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1577 p_cmd->u.control.u.es_bool.b_bool );
1579 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1580 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.p_meta );
1582 /* Modified control */
1583 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1584 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1585 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1586 return es_out_Control( p_out, i_query, !p_cmd->u.control.u.p_es ? NULL :
1587 p_cmd->u.control.u.p_es->p_es );
1589 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1590 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1591 p_cmd->u.control.u.es_bool.b_bool );
1593 case ES_OUT_SET_ES_CAT_POLICY:
1594 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_policy.i_cat,
1595 p_cmd->u.control.u.es_policy.i_policy );
1597 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1598 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_fmt.p_es->p_es,
1599 p_cmd->u.control.u.es_fmt.p_fmt );
1601 case ES_OUT_SET_TIMES:
1602 return es_out_Control( p_out, i_query, p_cmd->u.control.u.times.f_position,
1603 p_cmd->u.control.u.times.i_time,
1604 p_cmd->u.control.u.times.i_length );
1605 case ES_OUT_SET_JITTER:
1606 return es_out_Control( p_out, i_query, p_cmd->u.control.u.jitter.i_pts_delay,
1607 p_cmd->u.control.u.jitter.i_pts_jitter,
1608 p_cmd->u.control.u.jitter.i_cr_average );
1610 default:
1611 vlc_assert_unreachable();
1612 return VLC_EGENERIC;
1615 static void CmdCleanControl( ts_cmd_t *p_cmd )
1617 switch( p_cmd->u.control.i_query )
1619 case ES_OUT_SET_GROUP_META:
1620 case ES_OUT_SET_META:
1621 if( p_cmd->u.control.u.int_meta.p_meta )
1622 vlc_meta_Delete( p_cmd->u.control.u.int_meta.p_meta );
1623 break;
1624 case ES_OUT_SET_GROUP_EPG:
1625 if( p_cmd->u.control.u.int_epg.p_epg )
1626 vlc_epg_Delete( p_cmd->u.control.u.int_epg.p_epg );
1627 break;
1628 case ES_OUT_SET_GROUP_EPG_EVENT:
1629 if( p_cmd->u.control.u.int_epg_evt.p_evt )
1630 vlc_epg_event_Delete( p_cmd->u.control.u.int_epg_evt.p_evt );
1631 break;
1632 case ES_OUT_SET_ES_FMT:
1633 if( p_cmd->u.control.u.es_fmt.p_fmt )
1635 es_format_Clean( p_cmd->u.control.u.es_fmt.p_fmt );
1636 free( p_cmd->u.control.u.es_fmt.p_fmt );
1638 break;
1642 static int GetTmpFile( char **filename, const char *dirname )
1644 if( dirname != NULL
1645 && asprintf( filename, "%s"DIR_SEP PACKAGE_NAME"-timeshift.XXXXXX",
1646 dirname ) >= 0 )
1648 vlc_mkdir( dirname, 0700 );
1650 int fd = vlc_mkstemp( *filename );
1651 if( fd != -1 )
1652 return fd;
1654 free( *filename );
1657 *filename = strdup( DIR_SEP"tmp"DIR_SEP PACKAGE_NAME"-timeshift.XXXXXX" );
1658 if( unlikely(*filename == NULL) )
1659 return -1;
1661 int fd = vlc_mkstemp( *filename );
1662 if( fd != -1 )
1663 return fd;
1665 free( *filename );
1666 return -1;