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