macosx: remove unused variable and empty tabulated line
[vlc.git] / src / input / es_out_timeshift.c
blobff5bd6bdbe1de2030108027d33230eba9e5af742
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_SET_ES_DEFAULT:
648 case ES_OUT_SET_ES_STATE:
649 case ES_OUT_SET_ES_CAT_POLICY:
650 case ES_OUT_SET_ES_FMT:
651 case ES_OUT_SET_TIMES:
652 case ES_OUT_SET_JITTER:
653 case ES_OUT_SET_EOS:
655 ts_cmd_t cmd;
656 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
657 return VLC_EGENERIC;
658 if( p_sys->b_delayed )
660 TsPushCmd( p_sys->p_ts, &cmd );
661 return VLC_SUCCESS;
663 return CmdExecuteControl( p_sys->p_out, &cmd );
666 /* Special control when delayed */
667 case ES_OUT_GET_ES_STATE:
669 es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
670 bool *pb_enabled = (bool*)va_arg( args, bool* );
672 if( p_sys->b_delayed )
674 *pb_enabled = true;
675 return VLC_SUCCESS;
677 return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
679 /* Special internal input control */
680 case ES_OUT_GET_EMPTY:
682 bool *pb_empty = (bool*)va_arg( args, bool* );
683 return ControlLockedGetEmpty( p_out, pb_empty );
685 case ES_OUT_GET_WAKE_UP: /* TODO ? */
687 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
688 return ControlLockedGetWakeup( p_out, pi_wakeup );
690 case ES_OUT_GET_BUFFERING:
692 bool *pb_buffering = (bool *)va_arg( args, bool* );
693 return ControlLockedGetBuffering( p_out, pb_buffering );
695 case ES_OUT_SET_PAUSE_STATE:
697 const bool b_source_paused = (bool)va_arg( args, int );
698 const bool b_paused = (bool)va_arg( args, int );
699 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
701 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
703 case ES_OUT_SET_RATE:
705 const int i_src_rate = (int)va_arg( args, int );
706 const int i_rate = (int)va_arg( args, int );
708 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
710 case ES_OUT_SET_TIME:
712 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
714 return ControlLockedSetTime( p_out, i_date );
716 case ES_OUT_SET_FRAME_NEXT:
718 return ControlLockedSetFrameNext( p_out );
721 case ES_OUT_GET_PCR_SYSTEM:
722 if( p_sys->b_delayed )
723 return VLC_EGENERIC;
724 /* fall through */
725 case ES_OUT_GET_GROUP_FORCED:
726 case ES_OUT_POST_SUBNODE:
727 return es_out_vaControl( p_sys->p_out, i_query, args );
729 case ES_OUT_MODIFY_PCR_SYSTEM:
731 const bool b_absolute = va_arg( args, int );
732 const mtime_t i_system = va_arg( args, mtime_t );
734 if( b_absolute && p_sys->b_delayed )
735 return VLC_EGENERIC;
737 return es_out_ControlModifyPcrSystem( p_sys->p_out, b_absolute, i_system );
740 /* Invalid queries for this es_out level */
741 case ES_OUT_SET_ES_BY_ID:
742 case ES_OUT_RESTART_ES_BY_ID:
743 case ES_OUT_SET_ES_DEFAULT_BY_ID:
744 case ES_OUT_GET_ES_OBJECTS_BY_ID:
745 case ES_OUT_SET_DELAY:
746 case ES_OUT_SET_RECORD_STATE:
747 default:
748 vlc_assert_unreachable();
749 return VLC_EGENERIC;
752 static int Control( es_out_t *p_out, int i_query, va_list args )
754 es_out_sys_t *p_sys = p_out->p_sys;
755 int i_ret;
757 vlc_mutex_lock( &p_sys->lock );
759 TsAutoStop( p_out );
761 i_ret = ControlLocked( p_out, i_query, args );
763 vlc_mutex_unlock( &p_sys->lock );
765 return i_ret;
768 /*****************************************************************************
770 *****************************************************************************/
771 static void TsDestroy( ts_thread_t *p_ts )
773 vlc_cond_destroy( &p_ts->wait );
774 vlc_mutex_destroy( &p_ts->lock );
775 free( p_ts );
777 static int TsStart( es_out_t *p_out )
779 es_out_sys_t *p_sys = p_out->p_sys;
780 ts_thread_t *p_ts;
782 assert( !p_sys->b_delayed );
784 p_sys->p_ts = p_ts = calloc(1, sizeof(*p_ts));
785 if( !p_ts )
786 return VLC_EGENERIC;
788 p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
789 p_ts->psz_tmp_path = p_sys->psz_tmp_path;
790 p_ts->p_input = p_sys->p_input;
791 p_ts->p_out = p_sys->p_out;
792 vlc_mutex_init( &p_ts->lock );
793 vlc_cond_init( &p_ts->wait );
794 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
795 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
796 p_ts->i_rate_source = p_sys->i_input_rate_source;
797 p_ts->i_rate = p_sys->i_input_rate;
798 p_ts->i_rate_date = -1;
799 p_ts->i_rate_delay = 0;
800 p_ts->i_buffering_delay = 0;
801 p_ts->i_cmd_delay = 0;
802 p_ts->p_storage_r = NULL;
803 p_ts->p_storage_w = NULL;
805 p_sys->b_delayed = true;
806 if( vlc_clone( &p_ts->thread, TsRun, p_ts, VLC_THREAD_PRIORITY_INPUT ) )
808 msg_Err( p_sys->p_input, "cannot create timeshift thread" );
810 TsDestroy( p_ts );
812 p_sys->b_delayed = false;
813 return VLC_EGENERIC;
816 return VLC_SUCCESS;
818 static void TsAutoStop( es_out_t *p_out )
820 es_out_sys_t *p_sys = p_out->p_sys;
822 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_ts ) )
823 return;
825 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
826 TsStop( p_sys->p_ts );
828 p_sys->b_delayed = false;
830 static void TsStop( ts_thread_t *p_ts )
832 vlc_cancel( p_ts->thread );
833 vlc_join( p_ts->thread, NULL );
835 vlc_mutex_lock( &p_ts->lock );
836 for( ;; )
838 ts_cmd_t cmd;
840 if( TsPopCmdLocked( p_ts, &cmd, true ) )
841 break;
843 CmdClean( &cmd );
845 assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
846 if( p_ts->p_storage_r )
847 TsStorageDelete( p_ts->p_storage_r );
848 vlc_mutex_unlock( &p_ts->lock );
850 TsDestroy( p_ts );
852 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
854 vlc_mutex_lock( &p_ts->lock );
856 if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
858 ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
860 if( !p_storage )
862 CmdClean( p_cmd );
863 vlc_mutex_unlock( &p_ts->lock );
864 /* TODO warn the user (but only once) */
865 return;
868 if( !p_ts->p_storage_w )
870 p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
872 else
874 TsStoragePack( p_ts->p_storage_w );
875 p_ts->p_storage_w->p_next = p_storage;
876 p_ts->p_storage_w = p_storage;
880 /* TODO return error and warn the user (but only once) */
881 TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
883 vlc_cond_signal( &p_ts->wait );
885 vlc_mutex_unlock( &p_ts->lock );
887 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
889 vlc_assert_locked( &p_ts->lock );
891 if( TsStorageIsEmpty( p_ts->p_storage_r ) )
892 return VLC_EGENERIC;
894 TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
896 while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
898 ts_storage_t *p_next = p_ts->p_storage_r->p_next;
899 if( !p_next )
900 break;
902 TsStorageDelete( p_ts->p_storage_r );
903 p_ts->p_storage_r = p_next;
906 return VLC_SUCCESS;
908 static bool TsHasCmd( ts_thread_t *p_ts )
910 bool b_cmd;
912 vlc_mutex_lock( &p_ts->lock );
913 b_cmd = TsStorageIsEmpty( p_ts->p_storage_r );
914 vlc_mutex_unlock( &p_ts->lock );
916 return b_cmd;
918 static bool TsIsUnused( ts_thread_t *p_ts )
920 bool b_unused;
922 vlc_mutex_lock( &p_ts->lock );
923 b_unused = !p_ts->b_paused &&
924 p_ts->i_rate == p_ts->i_rate_source &&
925 TsStorageIsEmpty( p_ts->p_storage_r );
926 vlc_mutex_unlock( &p_ts->lock );
928 return b_unused;
930 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
932 vlc_mutex_lock( &p_ts->lock );
934 int i_ret;
935 if( b_paused )
937 assert( !b_source_paused );
938 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
940 else
942 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
945 if( !i_ret )
947 if( !b_paused )
949 assert( p_ts->i_pause_date > 0 );
951 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
954 p_ts->b_paused = b_paused;
955 p_ts->i_pause_date = i_date;
957 vlc_cond_signal( &p_ts->wait );
959 vlc_mutex_unlock( &p_ts->lock );
960 return i_ret;
962 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
964 int i_ret;
966 vlc_mutex_lock( &p_ts->lock );
967 p_ts->i_cmd_delay += p_ts->i_rate_delay;
969 p_ts->i_rate_date = -1;
970 p_ts->i_rate_delay = 0;
971 p_ts->i_rate = i_rate;
972 p_ts->i_rate_source = i_src_rate;
974 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
975 vlc_mutex_unlock( &p_ts->lock );
977 return i_ret;
980 static void *TsRun( void *p_data )
982 ts_thread_t *p_ts = p_data;
983 mtime_t i_buffering_date = -1;
985 for( ;; )
987 ts_cmd_t cmd;
988 mtime_t i_deadline;
989 bool b_buffering;
991 /* Pop a command to execute */
992 vlc_mutex_lock( &p_ts->lock );
993 mutex_cleanup_push( &p_ts->lock );
995 for( ;; )
997 const int canc = vlc_savecancel();
998 b_buffering = es_out_GetBuffering( p_ts->p_out );
1000 if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
1002 vlc_restorecancel( canc );
1003 break;
1005 vlc_restorecancel( canc );
1007 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
1010 if( b_buffering && i_buffering_date < 0 )
1012 i_buffering_date = cmd.i_date;
1014 else if( i_buffering_date > 0 )
1016 p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
1017 if( b_buffering )
1018 i_buffering_date = cmd.i_date;
1019 else
1020 i_buffering_date = -1;
1023 if( p_ts->i_rate_date < 0 )
1024 p_ts->i_rate_date = cmd.i_date;
1026 p_ts->i_rate_delay = 0;
1027 if( p_ts->i_rate_source != p_ts->i_rate )
1029 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
1030 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
1032 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 )
1034 const int canc = vlc_savecancel();
1036 /* Auto reset to rate 1.0 */
1037 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
1039 p_ts->i_cmd_delay = 0;
1040 p_ts->i_buffering_delay = 0;
1042 p_ts->i_rate_delay = 0;
1043 p_ts->i_rate_date = -1;
1044 p_ts->i_rate = p_ts->i_rate_source;
1046 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
1048 vlc_value_t val = { .i_int = p_ts->i_rate };
1049 /* Warn back input
1050 * FIXME it is perfectly safe BUT it is ugly as it may hide a
1051 * rate change requested by user */
1052 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
1055 vlc_restorecancel( canc );
1057 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
1059 vlc_cleanup_pop();
1060 vlc_mutex_unlock( &p_ts->lock );
1062 /* Regulate the speed of command processing to the same one than
1063 * reading */
1064 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
1066 mwait( i_deadline );
1068 vlc_cleanup_pop();
1070 /* Execute the command */
1071 const int canc = vlc_savecancel();
1072 switch( cmd.i_type )
1074 case C_ADD:
1075 CmdExecuteAdd( p_ts->p_out, &cmd );
1076 CmdCleanAdd( &cmd );
1077 break;
1078 case C_SEND:
1079 CmdExecuteSend( p_ts->p_out, &cmd );
1080 CmdCleanSend( &cmd );
1081 break;
1082 case C_CONTROL:
1083 CmdExecuteControl( p_ts->p_out, &cmd );
1084 CmdCleanControl( &cmd );
1085 break;
1086 case C_DEL:
1087 CmdExecuteDel( p_ts->p_out, &cmd );
1088 break;
1089 default:
1090 vlc_assert_unreachable();
1091 break;
1093 vlc_restorecancel( canc );
1096 return NULL;
1099 /*****************************************************************************
1101 *****************************************************************************/
1102 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1104 ts_storage_t *p_storage = malloc( sizeof (*p_storage) );
1105 if( unlikely(p_storage == NULL) )
1106 return NULL;
1108 char *psz_file;
1109 int fd = GetTmpFile( &psz_file, psz_tmp_path );
1110 if( fd == -1 )
1112 free( p_storage );
1113 return NULL;
1116 p_storage->p_filew = fdopen( fd, "w+b" );
1117 if( p_storage->p_filew == NULL )
1119 vlc_close( fd );
1120 vlc_unlink( psz_file );
1121 goto error;
1124 p_storage->p_filer = vlc_fopen( psz_file, "rb" );
1125 if( p_storage->p_filer == NULL )
1127 fclose( p_storage->p_filew );
1128 vlc_unlink( psz_file );
1129 goto error;
1132 #ifndef _WIN32
1133 vlc_unlink( psz_file );
1134 free( psz_file );
1135 #else
1136 p_storage->psz_file = psz_file;
1137 #endif
1138 p_storage->p_next = NULL;
1140 /* */
1141 p_storage->i_file_max = i_tmp_size_max;
1142 p_storage->i_file_size = 0;
1144 /* */
1145 p_storage->i_cmd_w = 0;
1146 p_storage->i_cmd_r = 0;
1147 p_storage->i_cmd_max = 30000;
1148 p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1149 //fprintf( stderr, "\nSTORAGE name=%s size=%d KiB\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1151 if( !p_storage->p_cmd )
1153 TsStorageDelete( p_storage );
1154 return NULL;
1156 return p_storage;
1157 error:
1158 free( psz_file );
1159 free( p_storage );
1160 return NULL;
1163 static void TsStorageDelete( ts_storage_t *p_storage )
1165 while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1167 ts_cmd_t cmd;
1169 TsStoragePopCmd( p_storage, &cmd, true );
1171 CmdClean( &cmd );
1173 free( p_storage->p_cmd );
1175 fclose( p_storage->p_filer );
1176 fclose( p_storage->p_filew );
1177 #ifdef _WIN32
1178 vlc_unlink( p_storage->psz_file );
1179 free( p_storage->psz_file );
1180 #endif
1181 free( p_storage );
1184 static void TsStoragePack( ts_storage_t *p_storage )
1186 /* Try to release a bit of memory */
1187 if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1188 return;
1190 p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1192 ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1193 if( p_new )
1194 p_storage->p_cmd = p_new;
1196 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1198 if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1200 size_t i_size = sizeof(*p_cmd->u.send.p_block) + p_cmd->u.send.p_block->i_buffer;
1202 if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1203 return true;
1205 return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1207 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1209 return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1211 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1213 ts_cmd_t cmd = *p_cmd;
1215 assert( !TsStorageIsFull( p_storage, p_cmd ) );
1217 if( cmd.i_type == C_SEND )
1219 block_t *p_block = cmd.u.send.p_block;
1221 cmd.u.send.p_block = NULL;
1222 cmd.u.send.i_offset = ftell( p_storage->p_filew );
1224 if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1226 block_Release( p_block );
1227 return;
1229 p_storage->i_file_size += sizeof(*p_block);
1230 if( p_block->i_buffer > 0 )
1232 if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1234 block_Release( p_block );
1235 return;
1238 p_storage->i_file_size += p_block->i_buffer;
1239 block_Release( p_block );
1241 if( b_flush )
1242 fflush( p_storage->p_filew );
1244 p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1246 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1248 assert( !TsStorageIsEmpty( p_storage ) );
1250 *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1251 if( p_cmd->i_type == C_SEND )
1253 block_t block;
1255 if( !b_flush &&
1256 !fseek( p_storage->p_filer, p_cmd->u.send.i_offset, SEEK_SET ) &&
1257 fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1259 block_t *p_block = block_Alloc( block.i_buffer );
1260 if( p_block )
1262 p_block->i_dts = block.i_dts;
1263 p_block->i_pts = block.i_pts;
1264 p_block->i_flags = block.i_flags;
1265 p_block->i_length = block.i_length;
1266 p_block->i_nb_samples = block.i_nb_samples;
1267 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1269 p_cmd->u.send.p_block = p_block;
1271 else
1273 //perror( "TsStoragePopCmd" );
1274 p_cmd->u.send.p_block = block_Alloc( 1 );
1279 /*****************************************************************************
1281 *****************************************************************************/
1282 static void CmdClean( ts_cmd_t *p_cmd )
1284 switch( p_cmd->i_type )
1286 case C_ADD:
1287 CmdCleanAdd( p_cmd );
1288 break;
1289 case C_SEND:
1290 CmdCleanSend( p_cmd );
1291 break;
1292 case C_CONTROL:
1293 CmdCleanControl( p_cmd );
1294 break;
1295 case C_DEL:
1296 break;
1297 default:
1298 vlc_assert_unreachable();
1299 break;
1303 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1305 p_cmd->i_type = C_ADD;
1306 p_cmd->i_date = mdate();
1307 p_cmd->u.add.p_es = p_es;
1308 if( b_copy )
1310 p_cmd->u.add.p_fmt = malloc( sizeof(*p_fmt) );
1311 if( !p_cmd->u.add.p_fmt )
1312 return VLC_EGENERIC;
1313 es_format_Copy( p_cmd->u.add.p_fmt, p_fmt );
1315 else
1317 p_cmd->u.add.p_fmt = (es_format_t*)p_fmt;
1319 return VLC_SUCCESS;
1321 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1323 p_cmd->u.add.p_es->p_es = es_out_Add( p_out, p_cmd->u.add.p_fmt );
1325 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1327 es_format_Clean( p_cmd->u.add.p_fmt );
1328 free( p_cmd->u.add.p_fmt );
1331 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1333 p_cmd->i_type = C_SEND;
1334 p_cmd->i_date = mdate();
1335 p_cmd->u.send.p_es = p_es;
1336 p_cmd->u.send.p_block = p_block;
1338 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1340 block_t *p_block = p_cmd->u.send.p_block;
1342 p_cmd->u.send.p_block = NULL;
1344 if( p_block )
1346 if( p_cmd->u.send.p_es->p_es )
1347 return es_out_Send( p_out, p_cmd->u.send.p_es->p_es, p_block );
1348 block_Release( p_block );
1350 return VLC_EGENERIC;
1352 static void CmdCleanSend( ts_cmd_t *p_cmd )
1354 if( p_cmd->u.send.p_block )
1355 block_Release( p_cmd->u.send.p_block );
1358 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1360 p_cmd->i_type = C_DEL;
1361 p_cmd->i_date = mdate();
1362 p_cmd->u.del.p_es = p_es;
1363 return VLC_SUCCESS;
1365 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1367 if( p_cmd->u.del.p_es->p_es )
1368 es_out_Del( p_out, p_cmd->u.del.p_es->p_es );
1369 free( p_cmd->u.del.p_es );
1372 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1374 p_cmd->i_type = C_CONTROL;
1375 p_cmd->i_date = mdate();
1376 p_cmd->u.control.i_query = i_query;
1378 switch( i_query )
1380 /* Pass-through control */
1381 case ES_OUT_SET_MODE: /* arg1= int */
1382 case ES_OUT_SET_GROUP: /* arg1= int */
1383 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1384 p_cmd->u.control.u.i_int = (int)va_arg( args, int );
1385 break;
1387 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1388 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1389 p_cmd->u.control.u.i_i64 = (int64_t)va_arg( args, int64_t );
1390 break;
1392 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1393 p_cmd->u.control.u.int_i64.i_int = (int)va_arg( args, int );
1394 p_cmd->u.control.u.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1395 break;
1397 case ES_OUT_SET_ES_SCRAMBLED_STATE:
1398 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1399 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1400 break;
1402 case ES_OUT_RESET_PCR: /* no arg */
1403 case ES_OUT_SET_EOS:
1404 break;
1406 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1407 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1409 if( i_query == ES_OUT_SET_GROUP_META )
1410 p_cmd->u.control.u.int_meta.i_int = (int)va_arg( args, int );
1411 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
1413 if( b_copy )
1415 p_cmd->u.control.u.int_meta.p_meta = vlc_meta_New();
1416 if( !p_cmd->u.control.u.int_meta.p_meta )
1417 return VLC_EGENERIC;
1418 vlc_meta_Merge( p_cmd->u.control.u.int_meta.p_meta, p_meta );
1420 else
1422 /* The cast is only needed to avoid warning */
1423 p_cmd->u.control.u.int_meta.p_meta = (vlc_meta_t*)p_meta;
1425 break;
1428 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1430 p_cmd->u.control.u.int_epg.i_int = (int)va_arg( args, int );
1431 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
1433 if( b_copy )
1435 p_cmd->u.control.u.int_epg.p_epg = vlc_epg_Duplicate( p_epg );
1436 if( !p_cmd->u.control.u.int_epg.p_epg )
1437 return VLC_EGENERIC;
1439 else
1441 /* The cast is only needed to avoid warning */
1442 p_cmd->u.control.u.int_epg.p_epg = (vlc_epg_t*)p_epg;
1444 break;
1446 case ES_OUT_SET_GROUP_EPG_EVENT: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1448 p_cmd->u.control.u.int_epg_evt.i_int = (int)va_arg( args, int );
1449 const vlc_epg_event_t *p_evt = va_arg( args, const vlc_epg_event_t * );
1451 if( b_copy )
1453 p_cmd->u.control.u.int_epg_evt.p_evt = vlc_epg_event_Duplicate( p_evt );
1454 if( !p_cmd->u.control.u.int_epg_evt.p_evt )
1455 return VLC_EGENERIC;
1457 else
1459 /* The cast is only needed to avoid warning */
1460 p_cmd->u.control.u.int_epg_evt.p_evt = (vlc_epg_event_t*)p_evt;
1462 break;
1464 case ES_OUT_SET_EPG_TIME: /* arg1=int64_t (seconds) */
1465 p_cmd->u.control.u.i_i64 = (int64_t)va_arg( args, int64_t );
1466 break;
1468 /* Modified control */
1469 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1470 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1471 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1472 p_cmd->u.control.u.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1473 break;
1475 case ES_OUT_SET_ES_CAT_POLICY:
1476 p_cmd->u.control.u.es_policy.i_cat = (int) va_arg( args, int );
1477 p_cmd->u.control.u.es_policy.i_policy = (int) va_arg( args, int );
1478 break;
1480 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1481 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1482 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1483 break;
1485 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1487 p_cmd->u.control.u.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1488 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1490 if( b_copy )
1492 p_cmd->u.control.u.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1493 if( !p_cmd->u.control.u.es_fmt.p_fmt )
1494 return VLC_EGENERIC;
1495 es_format_Copy( p_cmd->u.control.u.es_fmt.p_fmt, p_fmt );
1497 else
1499 p_cmd->u.control.u.es_fmt.p_fmt = p_fmt;
1501 break;
1503 case ES_OUT_SET_TIMES:
1505 double f_position = (double)va_arg( args, double );
1506 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
1507 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
1509 p_cmd->u.control.u.times.f_position = f_position;
1510 p_cmd->u.control.u.times.i_time = i_time;
1511 p_cmd->u.control.u.times.i_length = i_length;
1512 break;
1514 case ES_OUT_SET_JITTER:
1516 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
1517 mtime_t i_pts_jitter = (mtime_t)va_arg( args, mtime_t );
1518 int i_cr_average = (int)va_arg( args, int );
1520 p_cmd->u.control.u.jitter.i_pts_delay = i_pts_delay;
1521 p_cmd->u.control.u.jitter.i_pts_jitter = i_pts_jitter;
1522 p_cmd->u.control.u.jitter.i_cr_average = i_cr_average;
1523 break;
1526 default:
1527 vlc_assert_unreachable();
1528 return VLC_EGENERIC;
1531 return VLC_SUCCESS;
1533 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1535 const int i_query = p_cmd->u.control.i_query;
1537 switch( i_query )
1539 /* Pass-through control */
1540 case ES_OUT_SET_MODE: /* arg1= int */
1541 case ES_OUT_SET_GROUP: /* arg1= int */
1542 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1543 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_int );
1545 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1546 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1547 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_i64 );
1549 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1550 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_i64.i_int,
1551 p_cmd->u.control.u.int_i64.i_i64 );
1553 case ES_OUT_RESET_PCR: /* no arg */
1554 case ES_OUT_SET_EOS:
1555 return es_out_Control( p_out, i_query );
1557 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1558 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.i_int,
1559 p_cmd->u.control.u.int_meta.p_meta );
1561 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1562 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_epg.i_int,
1563 p_cmd->u.control.u.int_epg.p_epg );
1565 case ES_OUT_SET_GROUP_EPG_EVENT: /* arg1=int i_group arg2=const vlc_epg_event_t* */
1566 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_epg_evt.i_int,
1567 p_cmd->u.control.u.int_epg_evt.p_evt );
1569 case ES_OUT_SET_EPG_TIME: /* arg1=int64_t */
1570 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_i64 );
1572 case ES_OUT_SET_ES_SCRAMBLED_STATE: /* arg1=int es_out_id_t* arg2=bool */
1573 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1574 p_cmd->u.control.u.es_bool.b_bool );
1576 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1577 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.p_meta );
1579 /* Modified control */
1580 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1581 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1582 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1583 return es_out_Control( p_out, i_query, !p_cmd->u.control.u.p_es ? NULL :
1584 p_cmd->u.control.u.p_es->p_es );
1586 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1587 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1588 p_cmd->u.control.u.es_bool.b_bool );
1590 case ES_OUT_SET_ES_CAT_POLICY:
1591 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_policy.i_cat,
1592 p_cmd->u.control.u.es_policy.i_policy );
1594 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1595 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_fmt.p_es->p_es,
1596 p_cmd->u.control.u.es_fmt.p_fmt );
1598 case ES_OUT_SET_TIMES:
1599 return es_out_Control( p_out, i_query, p_cmd->u.control.u.times.f_position,
1600 p_cmd->u.control.u.times.i_time,
1601 p_cmd->u.control.u.times.i_length );
1602 case ES_OUT_SET_JITTER:
1603 return es_out_Control( p_out, i_query, p_cmd->u.control.u.jitter.i_pts_delay,
1604 p_cmd->u.control.u.jitter.i_pts_jitter,
1605 p_cmd->u.control.u.jitter.i_cr_average );
1607 default:
1608 vlc_assert_unreachable();
1609 return VLC_EGENERIC;
1612 static void CmdCleanControl( ts_cmd_t *p_cmd )
1614 switch( p_cmd->u.control.i_query )
1616 case ES_OUT_SET_GROUP_META:
1617 case ES_OUT_SET_META:
1618 if( p_cmd->u.control.u.int_meta.p_meta )
1619 vlc_meta_Delete( p_cmd->u.control.u.int_meta.p_meta );
1620 break;
1621 case ES_OUT_SET_GROUP_EPG:
1622 if( p_cmd->u.control.u.int_epg.p_epg )
1623 vlc_epg_Delete( p_cmd->u.control.u.int_epg.p_epg );
1624 break;
1625 case ES_OUT_SET_GROUP_EPG_EVENT:
1626 if( p_cmd->u.control.u.int_epg_evt.p_evt )
1627 vlc_epg_event_Delete( p_cmd->u.control.u.int_epg_evt.p_evt );
1628 break;
1629 case ES_OUT_SET_ES_FMT:
1630 if( p_cmd->u.control.u.es_fmt.p_fmt )
1632 es_format_Clean( p_cmd->u.control.u.es_fmt.p_fmt );
1633 free( p_cmd->u.control.u.es_fmt.p_fmt );
1635 break;
1639 static int GetTmpFile( char **filename, const char *dirname )
1641 if( dirname != NULL
1642 && asprintf( filename, "%s"DIR_SEP PACKAGE_NAME"-timeshift.XXXXXX",
1643 dirname ) >= 0 )
1645 vlc_mkdir( dirname, 0700 );
1647 int fd = vlc_mkstemp( *filename );
1648 if( fd != -1 )
1649 return fd;
1651 free( *filename );
1654 *filename = strdup( DIR_SEP"tmp"DIR_SEP PACKAGE_NAME"-timeshift.XXXXXX" );
1655 if( unlikely(*filename == NULL) )
1656 return -1;
1658 int fd = vlc_mkstemp( *filename );
1659 if( fd != -1 )
1660 return fd;
1662 free( *filename );
1663 return -1;