]> git.sesse.net Git - vlc/blob - src/input/es_out_timeshift.c
c96ed1a355b6ae02e224330339601deec4c10a42
[vlc] / src / input / es_out_timeshift.c
1 /*****************************************************************************
2  * es_out_timeshift.c: Es Out timeshift.
3  *****************************************************************************
4  * Copyright (C) 2008 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar < fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <assert.h>
35 #if defined (WIN32) && !defined (UNDER_CE)
36 #  include <direct.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #   include <sys/stat.h>
40 #endif
41
42 #include <vlc_common.h>
43 #include <vlc_charset.h>
44
45 #include <vlc_input.h>
46 #include <vlc_es_out.h>
47 #include <vlc_block.h>
48 #include "input_internal.h"
49 #include "es_out.h"
50 #include "es_out_timeshift.h"
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55
56 /* XXX attribute_packed is (and MUST be) used ONLY to reduce memory usage */
57 #ifdef HAVE_ATTRIBUTE_PACKED
58 #   define attribute_packed __attribute__((__packed__))
59 #else
60 #   define attribute_packed
61 #endif
62
63 enum
64 {
65     C_ADD,
66     C_SEND,
67     C_DEL,
68     C_CONTROL,
69 };
70
71 typedef struct attribute_packed
72 {
73     es_out_id_t *p_es;
74     es_format_t *p_fmt;
75 } ts_cmd_add_t;
76
77 typedef struct attribute_packed
78 {
79     es_out_id_t *p_es;
80 } ts_cmd_del_t;
81
82 typedef struct attribute_packed
83 {
84     es_out_id_t *p_es;
85     block_t *p_block;
86     int     i_offset;  /* We do not use file > INT_MAX */
87 } ts_cmd_send_t;
88
89 typedef struct attribute_packed
90 {
91     int  i_query;
92
93     union
94     {
95         bool b_bool;
96         int  i_int;
97         int64_t i_i64;
98         es_out_id_t *p_es;
99         struct
100         {
101             int     i_int;
102             int64_t i_i64;
103         } int_i64;
104         struct
105         {
106             int        i_int;
107             vlc_meta_t *p_meta;
108         } int_meta;
109         struct
110         {
111             int       i_int;
112             vlc_epg_t *p_epg;
113         } int_epg;
114         struct
115         {
116             es_out_id_t *p_es;
117             bool        b_bool;
118         } es_bool;
119         struct
120         {
121             es_out_id_t *p_es;
122             es_format_t *p_fmt;
123         } es_fmt;
124         struct
125         {
126             /* FIXME Really too big (double make the whole thing too big) */
127             double  f_position;
128             mtime_t i_time;
129             mtime_t i_length;
130         } times;
131         struct
132         {
133             mtime_t i_pts_delay;
134             int     i_cr_average;
135         } jitter;
136     };
137 } ts_cmd_control_t;
138
139 typedef struct attribute_packed
140 {
141     int8_t  i_type;
142     mtime_t i_date;
143     union
144     {
145         ts_cmd_add_t     add;
146         ts_cmd_del_t     del;
147         ts_cmd_send_t    send;
148         ts_cmd_control_t control;
149     };
150 } ts_cmd_t;
151
152 typedef struct ts_storage_t ts_storage_t;
153 struct ts_storage_t
154 {
155     ts_storage_t *p_next;
156
157     /* */
158     char    *psz_file;  /* Filename */
159     size_t  i_file_max; /* Max size in bytes */
160     int64_t i_file_size;/* Current size in bytes */
161     FILE    *p_filew;   /* FILE handle for data writing */
162     FILE    *p_filer;   /* FILE handle for data reading */
163
164     /* */
165     int      i_cmd_r;
166     int      i_cmd_w;
167     int      i_cmd_max;
168     ts_cmd_t *p_cmd;
169 };
170
171 typedef struct
172 {
173     VLC_COMMON_MEMBERS
174
175     /* */
176     input_thread_t *p_input;
177     es_out_t       *p_out;
178     int64_t        i_tmp_size_max;
179     const char     *psz_tmp_path;
180
181     /* Lock for all following fields */
182     vlc_mutex_t    lock;
183     vlc_cond_t     wait;
184
185     /* */
186     bool           b_paused;
187     mtime_t        i_pause_date;
188
189     /* */
190     int            i_rate;
191     int            i_rate_source;
192     mtime_t        i_rate_date;
193     mtime_t        i_rate_delay;
194
195     /* */
196     mtime_t        i_buffering_delay;
197
198     /* */
199     ts_storage_t   *p_storage_r;
200     ts_storage_t   *p_storage_w;
201
202     mtime_t        i_cmd_delay;
203
204 } ts_thread_t;
205
206 struct es_out_id_t
207 {
208     es_out_id_t *p_es;
209 };
210
211 struct es_out_sys_t
212 {
213     input_thread_t *p_input;
214         es_out_t       *p_out;
215
216     /* Configuration */
217     int64_t        i_tmp_size_max;    /* Maximal temporary file size in byte */
218     char           *psz_tmp_path;     /* Path for temporary files */
219
220     /* Lock for all following fields */
221     vlc_mutex_t    lock;
222
223     /* */
224     bool           b_delayed;
225     ts_thread_t   *p_thread;
226
227     /* */
228     bool           b_input_paused;
229     bool           b_input_paused_source;
230     int            i_input_rate;
231     int            i_input_rate_source;
232
233     /* */
234     int            i_es;
235     es_out_id_t    **pp_es;
236 };
237
238 static es_out_id_t *Add    ( es_out_t *, const es_format_t * );
239 static int          Send   ( es_out_t *, es_out_id_t *, block_t * );
240 static void         Del    ( es_out_t *, es_out_id_t * );
241 static int          Control( es_out_t *, int i_query, va_list );
242 static void         Destroy( es_out_t * );
243
244 static int          TsStart( es_out_t * );
245 static void         TsAutoStop( es_out_t * );
246
247 static void         TsStop( ts_thread_t * );
248 static void         TsPushCmd( ts_thread_t *, ts_cmd_t * );
249 static int          TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
250 static bool         TsHasCmd( ts_thread_t * );
251 static bool         TsIsUnused( ts_thread_t * );
252 static int          TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
253 static int          TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
254
255 static void         *TsRun( vlc_object_t * );
256
257 static ts_storage_t *TsStorageNew( const char *psz_path, int64_t i_tmp_size_max );
258 static void         TsStorageDelete( ts_storage_t * );
259 static void         TsStoragePack( ts_storage_t *p_storage );
260 static bool         TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
261 static bool         TsStorageIsEmpty( ts_storage_t * );
262 static void         TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
263 static void         TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
264
265 static void CmdClean( ts_cmd_t * );
266 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
267
268 static int  CmdInitAdd    ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
269 static void CmdInitSend   ( ts_cmd_t *, es_out_id_t *, block_t * );
270 static int  CmdInitDel    ( ts_cmd_t *, es_out_id_t * );
271 static int  CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
272
273 /* */
274 static void CmdCleanAdd    ( ts_cmd_t * );
275 static void CmdCleanSend   ( ts_cmd_t * );
276 static void CmdCleanControl( ts_cmd_t *p_cmd );
277
278 /* XXX these functions will take the destination es_out_t */
279 static void CmdExecuteAdd    ( es_out_t *, ts_cmd_t * );
280 static int  CmdExecuteSend   ( es_out_t *, ts_cmd_t * );
281 static void CmdExecuteDel    ( es_out_t *, ts_cmd_t * );
282 static int  CmdExecuteControl( es_out_t *, ts_cmd_t * );
283
284 /* File helpers */
285 static char *GetTmpPath( char *psz_path );
286 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path );
287
288 /*****************************************************************************
289  * input_EsOutTimeshiftNew:
290  *****************************************************************************/
291 es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
292 {
293     es_out_t *p_out = malloc( sizeof(*p_out) );
294     if( !p_out )
295         return NULL;
296
297     es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
298     if( !p_sys )
299     {
300         free( p_out );
301         return NULL;
302     }
303
304     /* */
305     p_out->pf_add     = Add;
306     p_out->pf_send    = Send;
307     p_out->pf_del     = Del;
308     p_out->pf_control = Control;
309     p_out->pf_destroy = Destroy;
310     p_out->p_sys      = p_sys;
311     p_out->b_sout     = p_input->p->p_sout != NULL;
312
313     /* */
314     p_sys->b_input_paused = false;
315     p_sys->b_input_paused_source = false;
316     p_sys->p_input = p_input;
317     p_sys->i_input_rate = i_rate;
318     p_sys->i_input_rate_source = i_rate;
319
320     p_sys->p_out = p_next_out;
321     vlc_mutex_init_recursive( &p_sys->lock );
322
323     p_sys->b_delayed = false;
324     p_sys->p_thread = NULL;
325
326     TAB_INIT( p_sys->i_es, p_sys->pp_es );
327
328     /* */
329     const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
330     if( i_tmp_size_max < 0 )
331         p_sys->i_tmp_size_max = 50*1024*1024;
332     else
333         p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
334     msg_Dbg( p_input, "using timeshift granularity of %d bytes",
335              (int)p_sys->i_tmp_size_max );
336
337     char *psz_tmp_path = var_CreateGetNonEmptyString( p_input, "input-timeshift-path" );
338     p_sys->psz_tmp_path = GetTmpPath( psz_tmp_path );
339     msg_Dbg( p_input, "using timeshift  path '%s'", p_sys->psz_tmp_path );
340
341 #if 0
342 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
343     S(ts_cmd_t);
344     S(ts_cmd_control_t);
345     S(ts_cmd_send_t);
346     S(ts_cmd_del_t);
347     S(ts_cmd_add_t);
348 #undef S
349 #endif
350
351     return p_out;
352 }
353
354 /*****************************************************************************
355  * Internal functions
356  *****************************************************************************/
357 static void Destroy( es_out_t *p_out )
358 {
359     es_out_sys_t *p_sys = p_out->p_sys;
360
361     if( p_sys->b_delayed )
362     {
363         TsStop( p_sys->p_thread );
364         p_sys->b_delayed = false;
365     }
366
367     while( p_sys->i_es > 0 )
368         Del( p_out, p_sys->pp_es[0] );
369     TAB_CLEAN( p_sys->i_es, p_sys->pp_es  );
370
371     free( p_sys->psz_tmp_path );
372     vlc_mutex_destroy( &p_sys->lock );
373     free( p_sys );
374     free( p_out );
375 }
376
377 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
378 {
379     es_out_sys_t *p_sys = p_out->p_sys;
380     ts_cmd_t cmd;
381
382     es_out_id_t *p_es = malloc( sizeof( *p_es ) );
383     if( !p_es )
384         return NULL;
385
386     vlc_mutex_lock( &p_sys->lock );
387
388     TsAutoStop( p_out );
389
390     if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
391     {
392         vlc_mutex_unlock( &p_sys->lock );
393         free( p_es );
394         return NULL;
395     }
396
397     TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
398
399     if( p_sys->b_delayed )
400         TsPushCmd( p_sys->p_thread, &cmd );
401     else
402         CmdExecuteAdd( p_sys->p_out, &cmd );
403
404     vlc_mutex_unlock( &p_sys->lock );
405
406     return p_es;
407 }
408 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
409 {
410     es_out_sys_t *p_sys = p_out->p_sys;
411     ts_cmd_t cmd;
412     int i_ret = VLC_SUCCESS;
413
414     vlc_mutex_lock( &p_sys->lock );
415
416     TsAutoStop( p_out );
417
418     CmdInitSend( &cmd, p_es, p_block );
419     if( p_sys->b_delayed )
420         TsPushCmd( p_sys->p_thread, &cmd );
421     else
422         i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
423
424     vlc_mutex_unlock( &p_sys->lock );
425
426     return i_ret;
427 }
428 static void Del( es_out_t *p_out, es_out_id_t *p_es )
429 {
430     es_out_sys_t *p_sys = p_out->p_sys;
431     ts_cmd_t cmd;
432
433     vlc_mutex_lock( &p_sys->lock );
434
435     TsAutoStop( p_out );
436
437     CmdInitDel( &cmd, p_es );
438     if( p_sys->b_delayed )
439         TsPushCmd( p_sys->p_thread, &cmd );
440     else
441         CmdExecuteDel( p_sys->p_out, &cmd );
442
443     TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
444
445     vlc_mutex_unlock( &p_sys->lock );
446 }
447
448 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
449 {
450     es_out_sys_t *p_sys = p_out->p_sys;
451
452     if( p_sys->b_delayed && TsHasCmd( p_sys->p_thread ) )
453         *pb_empty = false;
454     else
455         *pb_empty = es_out_GetEmpty( p_sys->p_out );
456
457     return VLC_SUCCESS;
458 }
459 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
460 {
461     es_out_sys_t *p_sys = p_out->p_sys;
462
463     if( p_sys->b_delayed )
464     {
465         assert( !p_sys->p_input->p->b_can_pace_control );
466         *pi_wakeup = 0;
467     }
468     else
469     {
470         *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
471     }
472
473     return VLC_SUCCESS;
474 }
475 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
476 {
477     es_out_sys_t *p_sys = p_out->p_sys;
478
479     if( p_sys->b_delayed )
480         *pb_buffering = true;
481     else
482         *pb_buffering = es_out_GetBuffering( p_sys->p_out );
483
484     return VLC_SUCCESS;
485 }
486 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
487 {
488     es_out_sys_t *p_sys = p_out->p_sys;
489     int i_ret;
490
491     if( !p_sys->b_delayed && !b_source_paused == !b_paused )
492     {
493         i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
494     }
495     else
496     {
497         i_ret = VLC_EGENERIC;
498         if( !p_sys->p_input->p->b_can_pace_control )
499         {
500             if( !p_sys->b_delayed )
501                 TsStart( p_out );
502             if( p_sys->b_delayed )
503                 i_ret = TsChangePause( p_sys->p_thread, b_source_paused, b_paused, i_date );
504         }
505         else
506         {
507             /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
508              * and so be able to advertize correctly pace control property in access
509              * module */
510             msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
511         }
512     }
513
514     if( !i_ret )
515     {
516         p_sys->b_input_paused_source = b_source_paused;
517         p_sys->b_input_paused = b_paused;
518     }
519     return i_ret;
520 }
521 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
522 {
523     es_out_sys_t *p_sys = p_out->p_sys;
524     int i_ret;
525
526     if( !p_sys->b_delayed && i_src_rate == i_rate )
527     {
528         i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
529     }
530     else
531     {
532         i_ret = VLC_EGENERIC;
533         if( !p_sys->p_input->p->b_can_pace_control )
534         {
535             if( !p_sys->b_delayed )
536                 TsStart( p_out );
537             if( p_sys->b_delayed )
538                 i_ret = TsChangeRate( p_sys->p_thread, i_src_rate, i_rate );
539         }
540         else
541         {
542             /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
543              * and so be able to advertize correctly pace control property in access
544              * module */
545             msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have space control" );
546         }
547
548     }
549
550     if( !i_ret )
551     {
552         p_sys->i_input_rate_source = i_src_rate;
553         p_sys->i_input_rate = i_rate;
554     }
555     return i_ret;
556 }
557 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
558 {
559     es_out_sys_t *p_sys = p_out->p_sys;
560
561     if( !p_sys->b_delayed )
562         return es_out_SetTime( p_sys->p_out, i_date );
563
564     /* TODO */
565     msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
566     return VLC_EGENERIC;
567 }
568 static int ControlLockedSetFrameNext( es_out_t *p_out )
569 {
570     es_out_sys_t *p_sys = p_out->p_sys;
571
572     return es_out_SetFrameNext( p_sys->p_out );
573 }
574
575 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
576 {
577     es_out_sys_t *p_sys = p_out->p_sys;
578
579     switch( i_query )
580     {
581     /* Invalid query for this es_out level */
582     case ES_OUT_SET_ES_BY_ID:
583     case ES_OUT_RESTART_ES_BY_ID:
584     case ES_OUT_SET_ES_DEFAULT_BY_ID:
585     case ES_OUT_SET_DELAY:
586     case ES_OUT_SET_RECORD_STATE:
587         assert(0);
588         return VLC_EGENERIC;
589
590     /* Pass-through control */
591     case ES_OUT_SET_ACTIVE:
592     case ES_OUT_SET_MODE:
593     case ES_OUT_SET_GROUP:
594     case ES_OUT_SET_PCR:
595     case ES_OUT_SET_GROUP_PCR:
596     case ES_OUT_RESET_PCR:
597     case ES_OUT_SET_NEXT_DISPLAY_TIME:
598     case ES_OUT_SET_GROUP_META:
599     case ES_OUT_SET_GROUP_EPG:
600     case ES_OUT_DEL_GROUP:
601     case ES_OUT_SET_ES:
602     case ES_OUT_RESTART_ES:
603     case ES_OUT_SET_ES_DEFAULT:
604     case ES_OUT_SET_ES_STATE:
605     case ES_OUT_SET_ES_FMT:
606     case ES_OUT_SET_TIMES:
607     case ES_OUT_SET_JITTER:
608     {
609         ts_cmd_t cmd;
610         if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
611             return VLC_EGENERIC;
612         if( p_sys->b_delayed )
613         {
614             TsPushCmd( p_sys->p_thread, &cmd );
615             return VLC_SUCCESS;
616         }
617         return CmdExecuteControl( p_sys->p_out, &cmd );
618     }
619
620     /* Special control when delayed */
621     case ES_OUT_GET_ES_STATE:
622     {
623         es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
624         bool *pb_enabled = (bool*)va_arg( args, bool* );
625
626         if( p_sys->b_delayed )
627         {
628             *pb_enabled = true;
629             return VLC_SUCCESS;
630         }
631         return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
632     }
633
634     /* Special internal input control */
635     case ES_OUT_GET_EMPTY:
636     {
637         bool *pb_empty = (bool*)va_arg( args, bool* );
638         return ControlLockedGetEmpty( p_out, pb_empty );
639     }
640     case ES_OUT_GET_WAKE_UP: /* TODO ? */
641     {
642         mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
643         return ControlLockedGetWakeup( p_out, pi_wakeup );
644     }
645     case ES_OUT_GET_BUFFERING:
646     {
647         bool *pb_buffering = (bool *)va_arg( args, bool* );
648         return ControlLockedGetBuffering( p_out, pb_buffering );
649     }
650     case ES_OUT_SET_PAUSE_STATE:
651     {
652         const bool b_source_paused = (bool)va_arg( args, int );
653         const bool b_paused = (bool)va_arg( args, int );
654         const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
655
656         return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
657     }
658     case ES_OUT_SET_RATE:
659     {
660         const int i_src_rate = (int)va_arg( args, int );
661         const int i_rate = (int)va_arg( args, int );
662
663         return ControlLockedSetRate( p_out, i_src_rate, i_rate );
664     }
665     case ES_OUT_SET_TIME:
666     {
667         const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
668
669         return ControlLockedSetTime( p_out, i_date );
670     }
671     case ES_OUT_SET_FRAME_NEXT:
672     {
673         return ControlLockedSetFrameNext( p_out );
674     }
675
676     default:
677         msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
678         assert(0);
679         return VLC_EGENERIC;
680     }
681 }
682 static int Control( es_out_t *p_out, int i_query, va_list args )
683 {
684     es_out_sys_t *p_sys = p_out->p_sys;
685     int i_ret;
686
687     vlc_mutex_lock( &p_sys->lock );
688
689     TsAutoStop( p_out );
690
691     i_ret = ControlLocked( p_out, i_query, args );
692
693     vlc_mutex_unlock( &p_sys->lock );
694
695     return i_ret;
696 }
697
698 /*****************************************************************************
699  *
700  *****************************************************************************/
701 static void TsDestructor( vlc_object_t *p_this )
702 {
703     ts_thread_t *p_ts = (ts_thread_t*)p_this;
704
705     vlc_cond_destroy( &p_ts->wait );
706     vlc_mutex_destroy( &p_ts->lock );
707 }
708 static int TsStart( es_out_t *p_out )
709 {
710     es_out_sys_t *p_sys = p_out->p_sys;
711     ts_thread_t *p_ts;
712
713     assert( !p_sys->b_delayed );
714
715     p_sys->p_thread = p_ts = vlc_custom_create( p_sys->p_input, sizeof(ts_thread_t),
716                                                 VLC_OBJECT_GENERIC, "es out timeshift" );
717     if( !p_ts )
718         return VLC_EGENERIC;
719
720     p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
721     p_ts->psz_tmp_path = p_sys->psz_tmp_path;
722     p_ts->p_input = p_sys->p_input;
723     p_ts->p_out = p_sys->p_out;
724     vlc_mutex_init( &p_ts->lock );
725     vlc_cond_init( &p_ts->wait );
726     p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
727     p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
728     p_ts->i_rate_source = p_sys->i_input_rate_source;
729     p_ts->i_rate        = p_sys->i_input_rate;
730     p_ts->i_rate_date = -1;
731     p_ts->i_rate_delay = 0;
732     p_ts->i_buffering_delay = 0;
733     p_ts->i_cmd_delay = 0;
734     p_ts->p_storage_r = NULL;
735     p_ts->p_storage_w = NULL;
736
737     vlc_object_set_destructor( p_ts, TsDestructor );
738
739     p_sys->b_delayed = true;
740     if( vlc_thread_create( p_ts, "es out timeshift",
741                            TsRun, VLC_THREAD_PRIORITY_INPUT ) )
742     {
743         msg_Err( p_sys->p_input, "cannot create input thread" );
744
745         vlc_object_release( p_ts );
746
747         p_sys->b_delayed = false;
748         return VLC_EGENERIC;
749     }
750
751     return VLC_SUCCESS;
752 }
753 static void TsAutoStop( es_out_t *p_out )
754 {
755     es_out_sys_t *p_sys = p_out->p_sys;
756
757     if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_thread ) )
758         return;
759
760     msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
761     TsStop( p_sys->p_thread );
762
763     p_sys->b_delayed = false;
764 }
765 static void TsStop( ts_thread_t *p_ts )
766 {
767     vlc_object_kill( p_ts );
768     vlc_thread_join( p_ts );
769
770     vlc_mutex_lock( &p_ts->lock );
771     for( ;; )
772     {
773         ts_cmd_t cmd;
774
775         if( TsPopCmdLocked( p_ts, &cmd, true ) )
776             break;
777
778         CmdClean( &cmd );
779     }
780     assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
781     if( p_ts->p_storage_r )
782         TsStorageDelete( p_ts->p_storage_r );
783     vlc_mutex_unlock( &p_ts->lock );
784
785     vlc_object_release( p_ts );
786 }
787 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
788 {
789     vlc_mutex_lock( &p_ts->lock );
790
791     if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
792     {
793         ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
794
795         if( !p_storage )
796         {
797             CmdClean( p_cmd );
798             vlc_mutex_unlock( &p_ts->lock );
799             /* TODO warn the user (but only once) */
800             return;
801         }
802
803         if( !p_ts->p_storage_w )
804         {
805             p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
806         }
807         else
808         {
809             TsStoragePack( p_ts->p_storage_w );
810             p_ts->p_storage_w->p_next = p_storage;
811             p_ts->p_storage_w = p_storage;
812         }
813     }
814
815     /* TODO return error and warn the user (but only once) */
816     TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
817
818     vlc_cond_signal( &p_ts->wait );
819
820     vlc_mutex_unlock( &p_ts->lock );
821 }
822 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
823 {
824     vlc_assert_locked( &p_ts->lock );
825
826     if( TsStorageIsEmpty( p_ts->p_storage_r ) )
827         return VLC_EGENERIC;
828
829     TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
830
831     while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
832     {
833         ts_storage_t *p_next = p_ts->p_storage_r->p_next;
834         if( !p_next )
835             break;
836
837         TsStorageDelete( p_ts->p_storage_r );
838         p_ts->p_storage_r = p_next;
839     }
840
841     return VLC_SUCCESS;
842 }
843 static bool TsHasCmd( ts_thread_t *p_ts )
844 {
845     bool b_cmd;
846
847     vlc_mutex_lock( &p_ts->lock );
848     b_cmd =  TsStorageIsEmpty( p_ts->p_storage_r );
849     vlc_mutex_unlock( &p_ts->lock );
850
851     return b_cmd;
852 }
853 static bool TsIsUnused( ts_thread_t *p_ts )
854 {
855     bool b_unused;
856
857     vlc_mutex_lock( &p_ts->lock );
858     b_unused = !p_ts->b_paused &&
859                p_ts->i_rate == p_ts->i_rate_source &&
860                TsStorageIsEmpty( p_ts->p_storage_r );
861     vlc_mutex_unlock( &p_ts->lock );
862
863     return b_unused;
864 }
865 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
866 {
867     vlc_mutex_lock( &p_ts->lock );
868
869     int i_ret;
870     if( b_paused )
871     {
872         assert( !b_source_paused );
873         i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
874     }
875     else
876     {
877         i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
878     }
879
880     if( !i_ret )
881     {
882         if( !b_paused )
883         {
884             assert( p_ts->i_pause_date > 0 );
885
886             p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
887         }
888
889         p_ts->b_paused = b_paused;
890         p_ts->i_pause_date = i_date;
891
892         vlc_cond_signal( &p_ts->wait );
893     }
894     vlc_mutex_unlock( &p_ts->lock );
895     return i_ret;
896 }
897 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
898 {
899     int i_ret;
900
901     vlc_mutex_lock( &p_ts->lock );
902     p_ts->i_cmd_delay += p_ts->i_rate_delay;
903
904     p_ts->i_rate_date = -1;
905     p_ts->i_rate_delay = 0;
906     p_ts->i_rate = i_rate;
907     p_ts->i_rate_source = i_src_rate;
908
909     i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
910     vlc_mutex_unlock( &p_ts->lock );
911
912     return i_ret;
913 }
914
915 static void *TsRun( vlc_object_t *p_thread )
916 {
917     ts_thread_t *p_ts = (ts_thread_t*)p_thread;
918     mtime_t i_buffering_date = -1;
919
920     for( ;; )
921     {
922         ts_cmd_t cmd;
923         mtime_t  i_deadline;
924         bool b_buffering;
925
926         /* Pop a command to execute */
927         vlc_mutex_lock( &p_ts->lock );
928         mutex_cleanup_push( &p_ts->lock );
929
930         for( ;; )
931         {
932             const int canc = vlc_savecancel();
933             b_buffering = es_out_GetBuffering( p_ts->p_out );
934
935             if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
936             {
937                 vlc_restorecancel( canc );
938                 break;
939             }
940             vlc_restorecancel( canc );
941
942             vlc_cond_wait( &p_ts->wait, &p_ts->lock );
943         }
944
945         if( b_buffering && i_buffering_date < 0 )
946         {
947             i_buffering_date = cmd.i_date;
948         }
949         else if( i_buffering_date > 0 )
950         {
951             p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
952             if( b_buffering )
953                 i_buffering_date = cmd.i_date;
954             else
955                 i_buffering_date = -1;
956         }
957
958         if( p_ts->i_rate_date < 0 )
959             p_ts->i_rate_date = cmd.i_date;
960
961         p_ts->i_rate_delay = 0;
962         if( p_ts->i_rate_source != p_ts->i_rate )
963         {
964             const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
965             p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
966         }
967         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 )
968         {
969             const int canc = vlc_savecancel();
970
971             /* Auto reset to rate 1.0 */
972             msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
973
974             p_ts->i_cmd_delay = 0;
975             p_ts->i_buffering_delay = 0;
976
977             p_ts->i_rate_delay = 0;
978             p_ts->i_rate_date = -1;
979             p_ts->i_rate = p_ts->i_rate_source;
980
981             if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
982             {
983                 vlc_value_t val = { .i_int = p_ts->i_rate };
984                 /* Warn back input
985                  * FIXME it is perfectly safe BUT it is ugly as it may hide a
986                  * rate change requested by user */
987                 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
988             }
989
990             vlc_restorecancel( canc );
991         }
992         i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
993
994         vlc_cleanup_run();
995
996         /* Regulate the speed of command processing to the same one than
997          * reading  */
998         vlc_cleanup_push( cmd_cleanup_routine, &cmd );
999
1000         mwait( i_deadline );
1001
1002         vlc_cleanup_pop();
1003
1004         /* Execute the command  */
1005         const int canc = vlc_savecancel();
1006         switch( cmd.i_type )
1007         {
1008         case C_ADD:
1009             CmdExecuteAdd( p_ts->p_out, &cmd );
1010             CmdCleanAdd( &cmd );
1011             break;
1012         case C_SEND:
1013             CmdExecuteSend( p_ts->p_out, &cmd );
1014             CmdCleanSend( &cmd );
1015             break;
1016         case C_CONTROL:
1017             CmdExecuteControl( p_ts->p_out, &cmd );
1018             CmdCleanControl( &cmd );
1019             break;
1020         case C_DEL:
1021             CmdExecuteDel( p_ts->p_out, &cmd );
1022             break;
1023         default:
1024             assert(0);
1025             break;
1026         }
1027         vlc_restorecancel( canc );
1028     }
1029
1030     return NULL;
1031 }
1032
1033 /*****************************************************************************
1034  *
1035  *****************************************************************************/
1036 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1037 {
1038     ts_storage_t *p_storage = calloc( 1, sizeof(ts_storage_t) );
1039     if( !p_storage )
1040         return NULL;
1041
1042     /* */
1043     p_storage->p_next = NULL;
1044
1045     /* */
1046     p_storage->i_file_max = i_tmp_size_max;
1047     p_storage->i_file_size = 0;
1048     p_storage->p_filew = GetTmpFile( &p_storage->psz_file, psz_tmp_path );
1049     if( p_storage->psz_file )
1050         p_storage->p_filer = utf8_fopen( p_storage->psz_file, "rb" );
1051
1052     /* */
1053     p_storage->i_cmd_w = 0;
1054     p_storage->i_cmd_r = 0;
1055     p_storage->i_cmd_max = 30000;
1056     p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1057     //fprintf( stderr, "\nSTORAGE name=%s size=%d kbytes\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1058
1059     if( !p_storage->p_cmd || !p_storage->p_filew || !p_storage->p_filer )
1060     {
1061         TsStorageDelete( p_storage );
1062         return NULL;
1063     }
1064     return p_storage;
1065 }
1066 static void TsStorageDelete( ts_storage_t *p_storage )
1067 {
1068     while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1069     {
1070         ts_cmd_t cmd;
1071
1072         TsStoragePopCmd( p_storage, &cmd, true );
1073
1074         CmdClean( &cmd );
1075     }
1076     free( p_storage->p_cmd );
1077
1078     if( p_storage->p_filer )
1079         fclose( p_storage->p_filer );
1080     if( p_storage->p_filew )
1081         fclose( p_storage->p_filew );
1082
1083     if( p_storage->psz_file )
1084     {
1085         utf8_unlink( p_storage->psz_file );
1086         free( p_storage->psz_file );
1087     }
1088
1089     free( p_storage );
1090 }
1091 static void TsStoragePack( ts_storage_t *p_storage )
1092 {
1093     /* Try to release a bit of memory */
1094     if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1095         return;
1096
1097     p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1098
1099     ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1100     if( p_new )
1101         p_storage->p_cmd = p_new;
1102 }
1103 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1104 {
1105     if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1106     {
1107         size_t i_size = sizeof(*p_cmd->send.p_block) + p_cmd->send.p_block->i_buffer;
1108
1109         if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1110             return true;
1111     }
1112     return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1113 }
1114 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1115 {
1116     return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1117 }
1118 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1119 {
1120     ts_cmd_t cmd = *p_cmd;
1121
1122     assert( !TsStorageIsFull( p_storage, p_cmd ) );
1123
1124     if( cmd.i_type == C_SEND )
1125     {
1126         block_t *p_block = cmd.send.p_block;
1127
1128         cmd.send.p_block = NULL;
1129         cmd.send.i_offset = ftell( p_storage->p_filew );
1130
1131         if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1132         {
1133             block_Release( p_block );
1134             return;
1135         }
1136         p_storage->i_file_size += sizeof(*p_block);
1137         if( p_block->i_buffer > 0 )
1138         {
1139             if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1140             {
1141                 block_Release( p_block );
1142                 return;
1143             }
1144         }
1145         p_storage->i_file_size += p_block->i_buffer;
1146         block_Release( p_block );
1147
1148         if( b_flush )
1149             fflush( p_storage->p_filew );
1150     }
1151     p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1152 }
1153 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1154 {
1155     assert( !TsStorageIsEmpty( p_storage ) );
1156
1157     *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1158     if( p_cmd->i_type == C_SEND )
1159     {
1160         block_t block;
1161
1162         if( !b_flush &&
1163             !fseek( p_storage->p_filer, p_cmd->send.i_offset, SEEK_SET ) &&
1164             fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1165         {
1166             block_t *p_block = block_Alloc( block.i_buffer );
1167             if( p_block )
1168             {
1169                 p_block->i_dts      = block.i_dts;
1170                 p_block->i_pts      = block.i_pts;
1171                 p_block->i_flags    = block.i_flags;
1172                 p_block->i_length   = block.i_length;
1173                 p_block->i_rate     = block.i_rate;
1174                 p_block->i_samples  = block.i_samples;
1175                 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1176             }
1177             p_cmd->send.p_block = p_block;
1178         }
1179         else
1180         {
1181             //fprintf( stderr, "TsStoragePopCmd: %m\n" );
1182             p_cmd->send.p_block = block_Alloc( 1 );
1183         }
1184     }
1185 }
1186
1187 /*****************************************************************************
1188  *
1189  *****************************************************************************/
1190 static void CmdClean( ts_cmd_t *p_cmd )
1191 {
1192     switch( p_cmd->i_type )
1193     {
1194     case C_ADD:
1195         CmdCleanAdd( p_cmd );
1196         break;
1197     case C_SEND:
1198         CmdCleanSend( p_cmd );
1199         break;
1200     case C_CONTROL:
1201         CmdCleanControl( p_cmd );
1202         break;
1203     case C_DEL:
1204         break;
1205     default:
1206         assert(0);
1207         break;
1208     }
1209 }
1210
1211 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1212 {
1213     p_cmd->i_type = C_ADD;
1214     p_cmd->i_date = mdate();
1215     p_cmd->add.p_es = p_es;
1216     if( b_copy )
1217     {
1218         p_cmd->add.p_fmt = malloc( sizeof(*p_fmt) );
1219         if( !p_cmd->add.p_fmt )
1220             return VLC_EGENERIC;
1221         es_format_Copy( p_cmd->add.p_fmt, p_fmt );
1222     }
1223     else
1224     {
1225         p_cmd->add.p_fmt = (es_format_t*)p_fmt;
1226     }
1227     return VLC_SUCCESS;
1228 }
1229 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1230 {
1231     p_cmd->add.p_es->p_es = es_out_Add( p_out, p_cmd->add.p_fmt );
1232 }
1233 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1234 {
1235     es_format_Clean( p_cmd->add.p_fmt );
1236     free( p_cmd->add.p_fmt );
1237 }
1238
1239 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1240 {
1241     p_cmd->i_type = C_SEND;
1242     p_cmd->i_date = mdate();
1243     p_cmd->send.p_es = p_es;
1244     p_cmd->send.p_block = p_block;
1245 }
1246 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1247 {
1248     block_t *p_block = p_cmd->send.p_block;
1249
1250     p_cmd->send.p_block = NULL;
1251
1252     if( p_block )
1253     {
1254         if( p_cmd->send.p_es->p_es )
1255             return es_out_Send( p_out, p_cmd->send.p_es->p_es, p_block );
1256         block_Release( p_block );
1257     }
1258     return VLC_EGENERIC;
1259 }
1260 static void CmdCleanSend( ts_cmd_t *p_cmd )
1261 {
1262     if( p_cmd->send.p_block )
1263         block_Release( p_cmd->send.p_block );
1264 }
1265
1266 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1267 {
1268     p_cmd->i_type = C_DEL;
1269     p_cmd->i_date = mdate();
1270     p_cmd->del.p_es = p_es;
1271     return VLC_SUCCESS;
1272 }
1273 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1274 {
1275     if( p_cmd->del.p_es->p_es )
1276         es_out_Del( p_out, p_cmd->del.p_es->p_es );
1277     free( p_cmd->del.p_es );
1278 }
1279
1280 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1281 {
1282     p_cmd->i_type = C_CONTROL;
1283     p_cmd->i_date = mdate();
1284     p_cmd->control.i_query = i_query;
1285
1286     switch( i_query )
1287     {
1288     /* Pass-through control */
1289     case ES_OUT_SET_ACTIVE:  /* arg1= bool                     */
1290         p_cmd->control.b_bool = (bool)va_arg( args, int );
1291         break;
1292
1293     case ES_OUT_SET_MODE:    /* arg1= int                            */
1294     case ES_OUT_SET_GROUP:   /* arg1= int                            */
1295     case ES_OUT_DEL_GROUP:   /* arg1=int i_group */
1296         p_cmd->control.i_int = (int)va_arg( args, int );
1297         break;
1298
1299     case ES_OUT_SET_PCR:                /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1300     case ES_OUT_SET_NEXT_DISPLAY_TIME:  /* arg1=int64_t i_pts(microsecond) */
1301         p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
1302         break;
1303
1304     case ES_OUT_SET_GROUP_PCR:          /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1305         p_cmd->control.int_i64.i_int = (int)va_arg( args, int );
1306         p_cmd->control.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1307         break;
1308
1309     case ES_OUT_RESET_PCR:           /* no arg */
1310         break;
1311
1312     case ES_OUT_SET_GROUP_META:  /* arg1=int i_group arg2=vlc_meta_t* */
1313     {
1314         p_cmd->control.int_meta.i_int = (int)va_arg( args, int );
1315         vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1316
1317         if( b_copy )
1318         {
1319             p_cmd->control.int_meta.p_meta = vlc_meta_New();
1320             if( !p_cmd->control.int_meta.p_meta )
1321                 return VLC_EGENERIC;
1322             vlc_meta_Merge( p_cmd->control.int_meta.p_meta, p_meta );
1323         }
1324         else
1325         {
1326             p_cmd->control.int_meta.p_meta = p_meta;
1327         }
1328         break;
1329     }
1330
1331     case ES_OUT_SET_GROUP_EPG:   /* arg1=int i_group arg2=vlc_epg_t* */
1332     {
1333         p_cmd->control.int_epg.i_int = (int)va_arg( args, int );
1334         vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1335
1336         if( b_copy )
1337         {
1338             p_cmd->control.int_epg.p_epg = vlc_epg_New( p_epg->psz_name );
1339             if( !p_cmd->control.int_epg.p_epg )
1340                 return VLC_EGENERIC;
1341             for( int i = 0; i < p_epg->i_event; i++ )
1342             {
1343                 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1344
1345                 vlc_epg_AddEvent( p_cmd->control.int_epg.p_epg,
1346                                   p_evt->i_start, p_evt->i_duration,
1347                                   p_evt->psz_name,
1348                                   p_evt->psz_short_description, p_evt->psz_description );
1349             }
1350             vlc_epg_SetCurrent( p_cmd->control.int_epg.p_epg,
1351                                 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1352         }
1353         else
1354         {
1355             p_cmd->control.int_epg.p_epg = p_epg;
1356         }
1357         break;
1358     }
1359
1360     /* Modified control */
1361     case ES_OUT_SET_ES:      /* arg1= es_out_id_t*                   */
1362     case ES_OUT_RESTART_ES:  /* arg1= es_out_id_t*                   */
1363     case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t*                */
1364         p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1365         break;
1366
1367     case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool   */
1368         p_cmd->control.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1369         p_cmd->control.es_bool.b_bool = (bool)va_arg( args, int );
1370         break;
1371
1372     case ES_OUT_SET_ES_FMT:     /* arg1= es_out_id_t* arg2=es_format_t* */
1373     {
1374         p_cmd->control.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1375         es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1376
1377         if( b_copy )
1378         {
1379             p_cmd->control.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1380             if( !p_cmd->control.es_fmt.p_fmt )
1381                 return VLC_EGENERIC;
1382             es_format_Copy( p_cmd->control.es_fmt.p_fmt, p_fmt );
1383         }
1384         else
1385         {
1386             p_cmd->control.es_fmt.p_fmt = p_fmt;
1387         }
1388         break;
1389     }
1390     case ES_OUT_SET_TIMES:
1391     {
1392         double f_position = (double)va_arg( args, double );
1393         mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
1394         mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
1395
1396         p_cmd->control.times.f_position = f_position;
1397         p_cmd->control.times.i_time = i_time;
1398         p_cmd->control.times.i_length = i_length;
1399         break;
1400     }
1401     case ES_OUT_SET_JITTER:
1402     {
1403         mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
1404         int     i_cr_average = (int)va_arg( args, int );
1405
1406         p_cmd->control.jitter.i_pts_delay = i_pts_delay;
1407         p_cmd->control.jitter.i_cr_average = i_cr_average;
1408         break;
1409     }
1410
1411     default:
1412         assert(0);
1413         return VLC_EGENERIC;
1414     }
1415
1416     return VLC_SUCCESS;
1417 }
1418 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1419 {
1420     const int i_query = p_cmd->control.i_query;
1421
1422     switch( i_query )
1423     {
1424     /* Pass-through control */
1425     case ES_OUT_SET_ACTIVE:  /* arg1= bool                     */
1426         return es_out_Control( p_out, i_query, p_cmd->control.b_bool );
1427
1428     case ES_OUT_SET_MODE:    /* arg1= int                            */
1429     case ES_OUT_SET_GROUP:   /* arg1= int                            */
1430     case ES_OUT_DEL_GROUP:   /* arg1=int i_group */
1431         return es_out_Control( p_out, i_query, p_cmd->control.i_int );
1432
1433     case ES_OUT_SET_PCR:                /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1434     case ES_OUT_SET_NEXT_DISPLAY_TIME:  /* arg1=int64_t i_pts(microsecond) */
1435         return es_out_Control( p_out, i_query, p_cmd->control.i_i64 );
1436
1437     case ES_OUT_SET_GROUP_PCR:          /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1438         return es_out_Control( p_out, i_query, p_cmd->control.int_i64.i_int,
1439                                                p_cmd->control.int_i64.i_i64 );
1440
1441     case ES_OUT_RESET_PCR:           /* no arg */
1442         return es_out_Control( p_out, i_query );
1443
1444     case ES_OUT_SET_GROUP_META:  /* arg1=int i_group arg2=vlc_meta_t* */
1445         return es_out_Control( p_out, i_query, p_cmd->control.int_meta.i_int,
1446                                                p_cmd->control.int_meta.p_meta );
1447
1448     case ES_OUT_SET_GROUP_EPG:   /* arg1=int i_group arg2=vlc_epg_t* */
1449         return es_out_Control( p_out, i_query, p_cmd->control.int_epg.i_int,
1450                                                p_cmd->control.int_epg.p_epg );
1451
1452     /* Modified control */
1453     case ES_OUT_SET_ES:      /* arg1= es_out_id_t*                   */
1454     case ES_OUT_RESTART_ES:  /* arg1= es_out_id_t*                   */
1455     case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t*                */
1456         return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es );
1457
1458     case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool   */
1459         return es_out_Control( p_out, i_query, p_cmd->control.es_bool.p_es->p_es,
1460                                                p_cmd->control.es_bool.b_bool );
1461
1462     case ES_OUT_SET_ES_FMT:     /* arg1= es_out_id_t* arg2=es_format_t* */
1463         return es_out_Control( p_out, i_query, p_cmd->control.es_fmt.p_es->p_es,
1464                                                p_cmd->control.es_fmt.p_fmt );
1465
1466     case ES_OUT_SET_TIMES:
1467         return es_out_Control( p_out, i_query, p_cmd->control.times.f_position,
1468                                                p_cmd->control.times.i_time,
1469                                                p_cmd->control.times.i_length );
1470     case ES_OUT_SET_JITTER:
1471         return es_out_Control( p_out, i_query, p_cmd->control.jitter.i_pts_delay,
1472                                                p_cmd->control.jitter.i_cr_average );
1473
1474     default:
1475         assert(0);
1476         return VLC_EGENERIC;
1477     }
1478 }
1479 static void CmdCleanControl( ts_cmd_t *p_cmd )
1480 {
1481     if( p_cmd->control.i_query == ES_OUT_SET_GROUP_META &&
1482         p_cmd->control.int_meta.p_meta )
1483     {
1484         vlc_meta_Delete( p_cmd->control.int_meta.p_meta );
1485     }
1486     else if( p_cmd->control.i_query == ES_OUT_SET_GROUP_EPG &&
1487              p_cmd->control.int_epg.p_epg )
1488     {
1489         vlc_epg_Delete( p_cmd->control.int_epg.p_epg );
1490     }
1491     else if( p_cmd->control.i_query == ES_OUT_SET_ES_FMT &&
1492              p_cmd->control.es_fmt.p_fmt )
1493     {
1494         es_format_Clean( p_cmd->control.es_fmt.p_fmt );
1495         free( p_cmd->control.es_fmt.p_fmt );
1496     }
1497 }
1498
1499
1500 /*****************************************************************************
1501  * GetTmpFile/Path:
1502  *****************************************************************************/
1503 static char *GetTmpPath( char *psz_path )
1504 {
1505     if( psz_path && *psz_path )
1506     {
1507         /* Make sure that the path exists and is a directory */
1508         struct stat s;
1509         const int i_ret = utf8_stat( psz_path, &s );
1510
1511         if( i_ret < 0 && !utf8_mkdir( psz_path, 0600 ) )
1512             return psz_path;
1513         else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1514             return psz_path;
1515     }
1516     free( psz_path );
1517
1518     /* Create a suitable path */
1519 #if defined (WIN32) && !defined (UNDER_CE)
1520     const DWORD dwCount = GetTempPathW( 0, NULL );
1521     wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1522     if( psw_path )
1523     {
1524         if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1525         {
1526             free( psw_path );
1527
1528             psw_path = _wgetcwd( NULL, 0 );
1529         }
1530     }
1531
1532     psz_path = NULL;
1533     if( psw_path )
1534     {
1535         psz_path = FromWide( psw_path );
1536         while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1537             psz_path[strlen( psz_path ) - 1] = '\0';
1538
1539         free( psw_path );
1540     }
1541
1542     if( !psz_path || *psz_path == '\0' )
1543     {
1544         free( psz_path );
1545         return strdup( "C:" );
1546     }
1547 #else
1548     psz_path = strdup( "/tmp" );
1549 #endif
1550
1551     return psz_path;
1552 }
1553
1554 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path )
1555 {
1556     char *psz_name;
1557     int fd;
1558     FILE *f;
1559
1560     /* */
1561     *ppsz_file = NULL;
1562     if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1563         return NULL;
1564
1565     /* */
1566     fd = utf8_mkstemp( psz_name );
1567     *ppsz_file = psz_name;
1568
1569     if( fd < 0 )
1570         return NULL;
1571
1572     /* */
1573     f = fdopen( fd, "w+b" );
1574     if( !f )
1575         close( fd );
1576
1577     return f;
1578 }
1579