]> git.sesse.net Git - vlc/blob - src/input/es_out_timeshift.c
15809c1ed9e19befe44bbe45d3f5670b114bdafd
[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->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->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->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     /* TODO ? or to remove ? */
586     case ES_OUT_GET_TS:
587         return VLC_EGENERIC;
588
589     /* Pass-through control */
590     case ES_OUT_SET_ACTIVE:
591     case ES_OUT_SET_MODE:
592     case ES_OUT_SET_GROUP:
593     case ES_OUT_SET_PCR:
594     case ES_OUT_SET_GROUP_PCR:
595     case ES_OUT_RESET_PCR:
596     case ES_OUT_SET_NEXT_DISPLAY_TIME:
597     case ES_OUT_SET_GROUP_META:
598     case ES_OUT_SET_GROUP_EPG:
599     case ES_OUT_DEL_GROUP:
600     case ES_OUT_SET_ES:
601     case ES_OUT_RESTART_ES:
602     case ES_OUT_SET_ES_DEFAULT:
603     case ES_OUT_SET_ES_STATE:
604     case ES_OUT_SET_ES_FMT:
605     case ES_OUT_SET_TIMES:
606     {
607         ts_cmd_t cmd;
608         if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
609             return VLC_EGENERIC;
610         if( p_sys->b_delayed )
611         {
612             TsPushCmd( p_sys->p_thread, &cmd );
613             return VLC_SUCCESS;
614         }
615         return CmdExecuteControl( p_sys->p_out, &cmd );
616     }
617
618     /* Special control when delayed */
619     case ES_OUT_GET_ES_STATE:
620     {
621         es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
622         bool *pb_enabled = (bool*)va_arg( args, bool* );
623
624         if( p_sys->b_delayed )
625         {
626             *pb_enabled = true;
627             return VLC_SUCCESS;
628         }
629         return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
630     }
631
632     /* Special internal input control */
633     case ES_OUT_GET_EMPTY:
634     {
635         bool *pb_empty = (bool*)va_arg( args, bool* );
636         return ControlLockedGetEmpty( p_out, pb_empty );
637     }
638     case ES_OUT_GET_WAKE_UP: /* TODO ? */
639     {
640         mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
641         return ControlLockedGetWakeup( p_out, pi_wakeup );
642     }
643     case ES_OUT_GET_BUFFERING:
644     {
645         bool *pb_buffering = (bool *)va_arg( args, bool* );
646         return ControlLockedGetBuffering( p_out, pb_buffering );
647     }
648     case ES_OUT_SET_PAUSE_STATE:
649     {
650         const bool b_source_paused = (bool)va_arg( args, int );
651         const bool b_paused = (bool)va_arg( args, int );
652         const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
653
654         return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
655     }
656     case ES_OUT_SET_RATE:
657     {
658         const int i_src_rate = (int)va_arg( args, int );
659         const int i_rate = (int)va_arg( args, int );
660
661         return ControlLockedSetRate( p_out, i_src_rate, i_rate );
662     }
663     case ES_OUT_SET_TIME:
664     {
665         const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
666
667         return ControlLockedSetTime( p_out, i_date );
668     }
669     case ES_OUT_SET_FRAME_NEXT:
670     {
671         return ControlLockedSetFrameNext( p_out );
672     }
673
674     default:
675         msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
676         assert(0);
677         return VLC_EGENERIC;
678     }
679 }
680 static int Control( es_out_t *p_out, int i_query, va_list args )
681 {
682     es_out_sys_t *p_sys = p_out->p_sys;
683     int i_ret;
684
685     vlc_mutex_lock( &p_sys->lock );
686
687     TsAutoStop( p_out );
688
689     i_ret = ControlLocked( p_out, i_query, args );
690
691     vlc_mutex_unlock( &p_sys->lock );
692
693     return i_ret;
694 }
695
696 /*****************************************************************************
697  *
698  *****************************************************************************/
699 static void TsDestructor( vlc_object_t *p_this )
700 {
701     ts_thread_t *p_ts = (ts_thread_t*)p_this;
702
703     vlc_cond_destroy( &p_ts->wait );
704     vlc_mutex_destroy( &p_ts->lock );
705 }
706 static int TsStart( es_out_t *p_out )
707 {
708     es_out_sys_t *p_sys = p_out->p_sys;
709     ts_thread_t *p_ts;
710
711     assert( !p_sys->b_delayed );
712
713     p_sys->p_thread = p_ts = vlc_custom_create( p_sys->p_input, sizeof(ts_thread_t),
714                                                 VLC_OBJECT_GENERIC, "es out timeshift" );
715     if( !p_ts )
716         return VLC_EGENERIC;
717
718     p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
719     p_ts->psz_tmp_path = p_sys->psz_tmp_path;
720     p_ts->p_input = p_sys->p_input;
721     p_ts->p_out = p_sys->p_out;
722     vlc_mutex_init( &p_ts->lock );
723     vlc_cond_init( &p_ts->wait );
724     p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
725     p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
726     p_ts->i_rate_source = p_sys->i_input_rate_source;
727     p_ts->i_rate        = p_sys->i_input_rate;
728     p_ts->i_rate_date = -1;
729     p_ts->i_rate_delay = 0;
730     p_ts->i_buffering_delay = 0;
731     p_ts->i_cmd_delay = 0;
732     p_ts->p_storage_r = NULL;
733     p_ts->p_storage_w = NULL;
734
735     vlc_object_set_destructor( p_ts, TsDestructor );
736
737     p_sys->b_delayed = true;
738     if( vlc_thread_create( p_ts, "es out timeshift",
739                            TsRun, VLC_THREAD_PRIORITY_INPUT, false ) )
740     {
741         msg_Err( p_sys->p_input, "cannot create input thread" );
742
743         vlc_object_release( p_ts );
744
745         p_sys->b_delayed = false;
746         return VLC_EGENERIC;
747     }
748
749     return VLC_SUCCESS;
750 }
751 static void TsAutoStop( es_out_t *p_out )
752 {
753     es_out_sys_t *p_sys = p_out->p_sys;
754
755     if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_thread ) )
756         return;
757
758     msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
759     TsStop( p_sys->p_thread );
760
761     p_sys->b_delayed = false;
762 }
763 static void TsStop( ts_thread_t *p_ts )
764 {
765     vlc_object_kill( p_ts );
766     vlc_thread_join( p_ts );
767
768     vlc_mutex_lock( &p_ts->lock );
769     for( ;; )
770     {
771         ts_cmd_t cmd;
772
773         if( TsPopCmdLocked( p_ts, &cmd, true ) )
774             break;
775
776         CmdClean( &cmd );
777     }
778     assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
779     if( p_ts->p_storage_r )
780         TsStorageDelete( p_ts->p_storage_r );
781     vlc_mutex_unlock( &p_ts->lock );
782
783     vlc_object_release( p_ts );
784 }
785 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
786 {
787     vlc_mutex_lock( &p_ts->lock );
788
789     if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
790     {
791         ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
792
793         if( !p_storage )
794         {
795             CmdClean( p_cmd );
796             vlc_mutex_unlock( &p_ts->lock );
797             /* TODO warn the user (but only once) */
798             return;
799         }
800
801         if( !p_ts->p_storage_w )
802         {
803             p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
804         }
805         else
806         {
807             TsStoragePack( p_ts->p_storage_w );
808             p_ts->p_storage_w->p_next = p_storage;
809             p_ts->p_storage_w = p_storage;
810         }
811     }
812
813     /* TODO return error and warn the user (but only once) */
814     TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
815
816     vlc_cond_signal( &p_ts->wait );
817
818     vlc_mutex_unlock( &p_ts->lock );
819 }
820 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
821 {
822     vlc_assert_locked( &p_ts->lock );
823
824     if( TsStorageIsEmpty( p_ts->p_storage_r ) )
825         return VLC_EGENERIC;
826
827     TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
828
829     while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
830     {
831         ts_storage_t *p_next = p_ts->p_storage_r->p_next;
832         if( !p_next )
833             break;
834
835         TsStorageDelete( p_ts->p_storage_r );
836         p_ts->p_storage_r = p_next;
837     }
838
839     return VLC_SUCCESS;
840 }
841 static bool TsHasCmd( ts_thread_t *p_ts )
842 {
843     bool b_cmd;
844
845     vlc_mutex_lock( &p_ts->lock );
846     b_cmd =  TsStorageIsEmpty( p_ts->p_storage_r );
847     vlc_mutex_unlock( &p_ts->lock );
848
849     return b_cmd;
850 }
851 static bool TsIsUnused( ts_thread_t *p_ts )
852 {
853     bool b_unused;
854
855     vlc_mutex_lock( &p_ts->lock );
856     b_unused = !p_ts->b_paused &&
857                p_ts->i_rate == p_ts->i_rate_source &&
858                TsStorageIsEmpty( p_ts->p_storage_r );
859     vlc_mutex_unlock( &p_ts->lock );
860
861     return b_unused;
862 }
863 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
864 {
865     vlc_mutex_lock( &p_ts->lock );
866
867     int i_ret;
868     if( b_paused )
869     {
870         assert( !b_source_paused );
871         i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
872     }
873     else
874     {
875         i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
876     }
877
878     if( !i_ret )
879     {
880         if( !b_paused )
881         {
882             assert( p_ts->i_pause_date > 0 );
883
884             p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
885         }
886
887         p_ts->b_paused = b_paused;
888         p_ts->i_pause_date = i_date;
889
890         vlc_cond_signal( &p_ts->wait );
891     }
892     vlc_mutex_unlock( &p_ts->lock );
893     return i_ret;
894 }
895 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
896 {
897     int i_ret;
898
899     vlc_mutex_lock( &p_ts->lock );
900     p_ts->i_cmd_delay += p_ts->i_rate_delay;
901
902     p_ts->i_rate_date = -1;
903     p_ts->i_rate_delay = 0;
904     p_ts->i_rate = i_rate;
905     p_ts->i_rate_source = i_src_rate;
906
907     i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
908     vlc_mutex_unlock( &p_ts->lock );
909
910     return i_ret;
911 }
912
913 static void *TsRun( vlc_object_t *p_thread )
914 {
915     ts_thread_t *p_ts = (ts_thread_t*)p_thread;
916     mtime_t i_buffering_date = -1;
917
918     for( ;; )
919     {
920         ts_cmd_t cmd;
921         mtime_t  i_deadline;
922         bool b_buffering;
923
924         /* Pop a command to execute */
925         vlc_mutex_lock( &p_ts->lock );
926         mutex_cleanup_push( &p_ts->lock );
927
928         for( ;; )
929         {
930             const int canc = vlc_savecancel();
931             b_buffering = es_out_GetBuffering( p_ts->p_out );
932
933             if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
934             {
935                 vlc_restorecancel( canc );
936                 break;
937             }
938             vlc_restorecancel( canc );
939
940             vlc_cond_wait( &p_ts->wait, &p_ts->lock );
941         }
942
943         if( b_buffering && i_buffering_date < 0 )
944         {
945             i_buffering_date = cmd.i_date;
946         }
947         else if( i_buffering_date > 0 )
948         {
949             p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
950             if( b_buffering )
951                 i_buffering_date = cmd.i_date;
952             else
953                 i_buffering_date = -1;
954         }
955
956         if( p_ts->i_rate_date < 0 )
957             p_ts->i_rate_date = cmd.i_date;
958
959         p_ts->i_rate_delay = 0;
960         if( p_ts->i_rate_source != p_ts->i_rate )
961         {
962             const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
963             p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
964         }
965         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 )
966         {
967             const int canc = vlc_savecancel();
968
969             /* Auto reset to rate 1.0 */
970             msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
971
972             p_ts->i_cmd_delay = 0;
973             p_ts->i_buffering_delay = 0;
974
975             p_ts->i_rate_delay = 0;
976             p_ts->i_rate_date = -1;
977             p_ts->i_rate = p_ts->i_rate_source;
978
979             if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
980             {
981                 vlc_value_t val = { .i_int = p_ts->i_rate };
982                 /* Warn back input
983                  * FIXME it is perfectly safe BUT it is ugly as it may hide a
984                  * rate change requested by user */
985                 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
986             }
987
988             vlc_restorecancel( canc );
989         }
990         i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
991
992         vlc_cleanup_run();
993
994         /* Regulate the speed of command processing to the same one than
995          * reading  */
996         vlc_cleanup_push( cmd_cleanup_routine, &cmd );
997
998         mwait( i_deadline );
999
1000         vlc_cleanup_pop();
1001
1002         /* Execute the command  */
1003         const int canc = vlc_savecancel();
1004         switch( cmd.i_type )
1005         {
1006         case C_ADD:
1007             CmdExecuteAdd( p_ts->p_out, &cmd );
1008             CmdCleanAdd( &cmd );
1009             break;
1010         case C_SEND:
1011             CmdExecuteSend( p_ts->p_out, &cmd );
1012             CmdCleanSend( &cmd );
1013             break;
1014         case C_CONTROL:
1015             CmdExecuteControl( p_ts->p_out, &cmd );
1016             CmdCleanControl( &cmd );
1017             break;
1018         case C_DEL:
1019             CmdExecuteDel( p_ts->p_out, &cmd );
1020             break;
1021         default:
1022             assert(0);
1023             break;
1024         }
1025         vlc_restorecancel( canc );
1026     }
1027
1028     return NULL;
1029 }
1030
1031 /*****************************************************************************
1032  *
1033  *****************************************************************************/
1034 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1035 {
1036     ts_storage_t *p_storage = calloc( 1, sizeof(ts_storage_t) );
1037     if( !p_storage )
1038         return NULL;
1039
1040     /* */
1041     p_storage->p_next = NULL;
1042
1043     /* */
1044     p_storage->i_file_max = i_tmp_size_max;
1045     p_storage->i_file_size = 0;
1046     p_storage->p_filew = GetTmpFile( &p_storage->psz_file, psz_tmp_path );
1047     if( p_storage->psz_file )
1048         p_storage->p_filer = utf8_fopen( p_storage->psz_file, "rb" );
1049
1050     /* */
1051     p_storage->i_cmd_w = 0;
1052     p_storage->i_cmd_r = 0;
1053     p_storage->i_cmd_max = 30000;
1054     p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1055     //fprintf( stderr, "\nSTORAGE name=%s size=%d kbytes\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1056
1057     if( !p_storage->p_cmd || !p_storage->p_filew || !p_storage->p_filer )
1058     {
1059         TsStorageDelete( p_storage );
1060         return NULL;
1061     }
1062     return p_storage;
1063 }
1064 static void TsStorageDelete( ts_storage_t *p_storage )
1065 {
1066     while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1067     {
1068         ts_cmd_t cmd;
1069
1070         TsStoragePopCmd( p_storage, &cmd, true );
1071
1072         CmdClean( &cmd );
1073     }
1074     free( p_storage->p_cmd );
1075
1076     if( p_storage->p_filer )
1077         fclose( p_storage->p_filer );
1078     if( p_storage->p_filew )
1079         fclose( p_storage->p_filew );
1080
1081     if( p_storage->psz_file )
1082     {
1083         utf8_unlink( p_storage->psz_file );
1084         free( p_storage->psz_file );
1085     }
1086
1087     free( p_storage );
1088 }
1089 static void TsStoragePack( ts_storage_t *p_storage )
1090 {
1091     /* Try to release a bit of memory */
1092     if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1093         return;
1094
1095     p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1096
1097     ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1098     if( p_new )
1099         p_storage->p_cmd = p_new;
1100 }
1101 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1102 {
1103     if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1104     {
1105         size_t i_size = sizeof(*p_cmd->send.p_block) + p_cmd->send.p_block->i_buffer;
1106
1107         if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1108             return true;
1109     }
1110     return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1111 }
1112 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1113 {
1114     return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1115 }
1116 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1117 {
1118     ts_cmd_t cmd = *p_cmd;
1119
1120     assert( !TsStorageIsFull( p_storage, p_cmd ) );
1121
1122     if( cmd.i_type == C_SEND )
1123     {
1124         block_t *p_block = cmd.send.p_block;
1125
1126         cmd.send.p_block = NULL;
1127         cmd.send.i_offset = ftell( p_storage->p_filew );
1128
1129         if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1130         {
1131             block_Release( p_block );
1132             return;
1133         }
1134         p_storage->i_file_size += sizeof(*p_block);
1135         if( p_block->i_buffer > 0 )
1136         {
1137             if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1138             {
1139                 block_Release( p_block );
1140                 return;
1141             }
1142         }
1143         p_storage->i_file_size += p_block->i_buffer;
1144         block_Release( p_block );
1145
1146         if( b_flush )
1147             fflush( p_storage->p_filew );
1148     }
1149     p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1150 }
1151 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1152 {
1153     assert( !TsStorageIsEmpty( p_storage ) );
1154
1155     *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1156     if( p_cmd->i_type == C_SEND )
1157     {
1158         block_t block;
1159
1160         if( !b_flush &&
1161             !fseek( p_storage->p_filer, p_cmd->send.i_offset, SEEK_SET ) &&
1162             fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1163         {
1164             block_t *p_block = block_Alloc( block.i_buffer );
1165             if( p_block )
1166             {
1167                 p_block->i_dts      = block.i_dts;
1168                 p_block->i_pts      = block.i_pts;
1169                 p_block->i_flags    = block.i_flags;
1170                 p_block->i_length   = block.i_length;
1171                 p_block->i_rate     = block.i_rate;
1172                 p_block->i_samples  = block.i_samples;
1173                 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1174             }
1175             p_cmd->send.p_block = p_block;
1176         }
1177         else
1178         {
1179             //fprintf( stderr, "TsStoragePopCmd: %m\n" );
1180             p_cmd->send.p_block = block_Alloc( 1 );
1181         }
1182     }
1183 }
1184
1185 /*****************************************************************************
1186  *
1187  *****************************************************************************/
1188 static void CmdClean( ts_cmd_t *p_cmd )
1189 {
1190     switch( p_cmd->i_type )
1191     {
1192     case C_ADD:
1193         CmdCleanAdd( p_cmd );
1194         break;
1195     case C_SEND:
1196         CmdCleanSend( p_cmd );
1197         break;
1198     case C_CONTROL:
1199         CmdCleanControl( p_cmd );
1200         break;
1201     case C_DEL:
1202         break;
1203     default:
1204         assert(0);
1205         break;
1206     }
1207 }
1208
1209 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1210 {
1211     p_cmd->i_type = C_ADD;
1212     p_cmd->i_date = mdate();
1213     p_cmd->add.p_es = p_es;
1214     if( b_copy )
1215     {
1216         p_cmd->add.p_fmt = malloc( sizeof(*p_fmt) );
1217         if( !p_cmd->add.p_fmt )
1218             return VLC_EGENERIC;
1219         es_format_Copy( p_cmd->add.p_fmt, p_fmt );
1220     }
1221     else
1222     {
1223         p_cmd->add.p_fmt = (es_format_t*)p_fmt;
1224     }
1225     return VLC_SUCCESS;
1226 }
1227 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1228 {
1229     p_cmd->add.p_es->p_es = es_out_Add( p_out, p_cmd->add.p_fmt );
1230 }
1231 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1232 {
1233     es_format_Clean( p_cmd->add.p_fmt );
1234     free( p_cmd->add.p_fmt );
1235 }
1236
1237 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1238 {
1239     p_cmd->i_type = C_SEND;
1240     p_cmd->i_date = mdate();
1241     p_cmd->send.p_es = p_es;
1242     p_cmd->send.p_block = p_block;
1243 }
1244 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1245 {
1246     block_t *p_block = p_cmd->send.p_block;
1247
1248     p_cmd->send.p_block = NULL;
1249
1250     if( p_block )
1251     {
1252         if( p_cmd->send.p_es->p_es )
1253             return es_out_Send( p_out, p_cmd->send.p_es->p_es, p_block );
1254         block_Release( p_block );
1255     }
1256     return VLC_EGENERIC;
1257 }
1258 static void CmdCleanSend( ts_cmd_t *p_cmd )
1259 {
1260     if( p_cmd->send.p_block )
1261         block_Release( p_cmd->send.p_block );
1262 }
1263
1264 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1265 {
1266     p_cmd->i_type = C_DEL;
1267     p_cmd->i_date = mdate();
1268     p_cmd->del.p_es = p_es;
1269     return VLC_SUCCESS;
1270 }
1271 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1272 {
1273     if( p_cmd->del.p_es->p_es )
1274         es_out_Del( p_out, p_cmd->del.p_es->p_es );
1275     free( p_cmd->del.p_es );
1276 }
1277
1278 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1279 {
1280     p_cmd->i_type = C_CONTROL;
1281     p_cmd->i_date = mdate();
1282     p_cmd->control.i_query = i_query;
1283
1284     switch( i_query )
1285     {
1286     /* Pass-through control */
1287     case ES_OUT_SET_ACTIVE:  /* arg1= bool                     */
1288         p_cmd->control.b_bool = (bool)va_arg( args, int );
1289         break;
1290
1291     case ES_OUT_SET_MODE:    /* arg1= int                            */
1292     case ES_OUT_SET_GROUP:   /* arg1= int                            */
1293     case ES_OUT_DEL_GROUP:   /* arg1=int i_group */
1294         p_cmd->control.i_int = (int)va_arg( args, int );
1295         break;
1296
1297     case ES_OUT_SET_PCR:                /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1298     case ES_OUT_SET_NEXT_DISPLAY_TIME:  /* arg1=int64_t i_pts(microsecond) */
1299         p_cmd->control.i_i64 = (int64_t)va_arg( args, int64_t );
1300         break;
1301
1302     case ES_OUT_SET_GROUP_PCR:          /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1303         p_cmd->control.int_i64.i_int = (int)va_arg( args, int );
1304         p_cmd->control.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1305         break;
1306
1307     case ES_OUT_RESET_PCR:           /* no arg */
1308         break;
1309
1310     case ES_OUT_SET_GROUP_META:  /* arg1=int i_group arg2=vlc_meta_t* */
1311     {
1312         p_cmd->control.int_meta.i_int = (int)va_arg( args, int );
1313         vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t * );
1314
1315         if( b_copy )
1316         {
1317             p_cmd->control.int_meta.p_meta = vlc_meta_New();
1318             if( !p_cmd->control.int_meta.p_meta )
1319                 return VLC_EGENERIC;
1320             vlc_meta_Merge( p_cmd->control.int_meta.p_meta, p_meta );
1321         }
1322         else
1323         {
1324             p_cmd->control.int_meta.p_meta = p_meta;
1325         }
1326         break;
1327     }
1328
1329     case ES_OUT_SET_GROUP_EPG:   /* arg1=int i_group arg2=vlc_epg_t* */
1330     {
1331         p_cmd->control.int_epg.i_int = (int)va_arg( args, int );
1332         vlc_epg_t *p_epg = (vlc_epg_t*)va_arg( args, vlc_epg_t * );
1333
1334         if( b_copy )
1335         {
1336             p_cmd->control.int_epg.p_epg = vlc_epg_New( p_epg->psz_name );
1337             if( !p_cmd->control.int_epg.p_epg )
1338                 return VLC_EGENERIC;
1339             for( int i = 0; i < p_epg->i_event; i++ )
1340             {
1341                 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1342
1343                 vlc_epg_AddEvent( p_cmd->control.int_epg.p_epg,
1344                                   p_evt->i_start, p_evt->i_duration,
1345                                   p_evt->psz_name,
1346                                   p_evt->psz_short_description, p_evt->psz_description );
1347             }
1348             vlc_epg_SetCurrent( p_cmd->control.int_epg.p_epg,
1349                                 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1350         }
1351         else
1352         {
1353             p_cmd->control.int_epg.p_epg = p_epg;
1354         }
1355         break;
1356     }
1357
1358     /* Modified control */
1359     case ES_OUT_SET_ES:      /* arg1= es_out_id_t*                   */
1360     case ES_OUT_RESTART_ES:  /* arg1= es_out_id_t*                   */
1361     case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t*                */
1362         p_cmd->control.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1363         break;
1364
1365     case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool   */
1366         p_cmd->control.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1367         p_cmd->control.es_bool.b_bool = (bool)va_arg( args, int );
1368         break;
1369
1370     case ES_OUT_SET_ES_FMT:     /* arg1= es_out_id_t* arg2=es_format_t* */
1371     {
1372         p_cmd->control.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1373         es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1374
1375         if( b_copy )
1376         {
1377             p_cmd->control.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1378             if( !p_cmd->control.es_fmt.p_fmt )
1379                 return VLC_EGENERIC;
1380             es_format_Copy( p_cmd->control.es_fmt.p_fmt, p_fmt );
1381         }
1382         else
1383         {
1384             p_cmd->control.es_fmt.p_fmt = p_fmt;
1385         }
1386         break;
1387     }
1388     case ES_OUT_SET_TIMES:
1389     {
1390         double f_position = (double)va_arg( args, double );
1391         mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
1392         mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
1393
1394         p_cmd->control.times.f_position = f_position;
1395         p_cmd->control.times.i_time = i_time;
1396         p_cmd->control.times.i_length = i_length;
1397         break;
1398     }
1399
1400     default:
1401         assert(0);
1402         return VLC_EGENERIC;
1403     }
1404
1405     return VLC_SUCCESS;
1406 }
1407 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1408 {
1409     const int i_query = p_cmd->control.i_query;
1410
1411     switch( i_query )
1412     {
1413     /* Pass-through control */
1414     case ES_OUT_SET_ACTIVE:  /* arg1= bool                     */
1415         return es_out_Control( p_out, i_query, p_cmd->control.b_bool );
1416
1417     case ES_OUT_SET_MODE:    /* arg1= int                            */
1418     case ES_OUT_SET_GROUP:   /* arg1= int                            */
1419     case ES_OUT_DEL_GROUP:   /* arg1=int i_group */
1420         return es_out_Control( p_out, i_query, p_cmd->control.i_int );
1421
1422     case ES_OUT_SET_PCR:                /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1423     case ES_OUT_SET_NEXT_DISPLAY_TIME:  /* arg1=int64_t i_pts(microsecond) */
1424         return es_out_Control( p_out, i_query, p_cmd->control.i_i64 );
1425
1426     case ES_OUT_SET_GROUP_PCR:          /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1427         return es_out_Control( p_out, i_query, p_cmd->control.int_i64.i_int,
1428                                                p_cmd->control.int_i64.i_i64 );
1429
1430     case ES_OUT_RESET_PCR:           /* no arg */
1431         return es_out_Control( p_out, i_query );
1432
1433     case ES_OUT_SET_GROUP_META:  /* arg1=int i_group arg2=vlc_meta_t* */
1434         return es_out_Control( p_out, i_query, p_cmd->control.int_meta.i_int,
1435                                                p_cmd->control.int_meta.p_meta );
1436
1437     case ES_OUT_SET_GROUP_EPG:   /* arg1=int i_group arg2=vlc_epg_t* */
1438         return es_out_Control( p_out, i_query, p_cmd->control.int_epg.i_int,
1439                                                p_cmd->control.int_epg.p_epg );
1440
1441     /* Modified control */
1442     case ES_OUT_SET_ES:      /* arg1= es_out_id_t*                   */
1443     case ES_OUT_RESTART_ES:  /* arg1= es_out_id_t*                   */
1444     case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t*                */
1445         return es_out_Control( p_out, i_query, p_cmd->control.p_es->p_es );
1446
1447     case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool   */
1448         return es_out_Control( p_out, i_query, p_cmd->control.es_bool.p_es->p_es,
1449                                                p_cmd->control.es_bool.b_bool );
1450
1451     case ES_OUT_SET_ES_FMT:     /* arg1= es_out_id_t* arg2=es_format_t* */
1452         return es_out_Control( p_out, i_query, p_cmd->control.es_fmt.p_es->p_es,
1453                                                p_cmd->control.es_fmt.p_fmt );
1454
1455     case ES_OUT_SET_TIMES:
1456         return es_out_Control( p_out, i_query, p_cmd->control.times.f_position,
1457                                                p_cmd->control.times.i_time,
1458                                                p_cmd->control.times.i_length );
1459
1460     default:
1461         assert(0);
1462         return VLC_EGENERIC;
1463     }
1464 }
1465 static void CmdCleanControl( ts_cmd_t *p_cmd )
1466 {
1467     if( p_cmd->control.i_query == ES_OUT_SET_GROUP_META &&
1468         p_cmd->control.int_meta.p_meta )
1469     {
1470         vlc_meta_Delete( p_cmd->control.int_meta.p_meta );
1471     }
1472     else if( p_cmd->control.i_query == ES_OUT_SET_GROUP_EPG &&
1473              p_cmd->control.int_epg.p_epg )
1474     {
1475         vlc_epg_Delete( p_cmd->control.int_epg.p_epg );
1476     }
1477     else if( p_cmd->control.i_query == ES_OUT_SET_ES_FMT &&
1478              p_cmd->control.es_fmt.p_fmt )
1479     {
1480         es_format_Clean( p_cmd->control.es_fmt.p_fmt );
1481         free( p_cmd->control.es_fmt.p_fmt );
1482     }
1483 }
1484
1485
1486 /*****************************************************************************
1487  * GetTmpFile/Path:
1488  *****************************************************************************/
1489 static char *GetTmpPath( char *psz_path )
1490 {
1491     if( psz_path && *psz_path )
1492     {
1493         /* Make sure that the path exists and is a directory */
1494         struct stat s;
1495         const int i_ret = utf8_stat( psz_path, &s );
1496
1497         if( i_ret < 0 && !utf8_mkdir( psz_path, 0600 ) )
1498             return psz_path;
1499         else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1500             return psz_path;
1501     }
1502     free( psz_path );
1503
1504     /* Create a suitable path */
1505 #if defined (WIN32) && !defined (UNDER_CE)
1506     const DWORD dwCount = GetTempPathW( 0, NULL );
1507     wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1508     if( psw_path )
1509     {
1510         if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1511         {
1512             free( psw_path );
1513
1514             psw_path = _wgetcwd( NULL, 0 );
1515         }
1516     }
1517
1518     psz_path = NULL;
1519     if( psw_path )
1520     {
1521         psz_path = FromWide( psw_path );
1522         while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1523             psz_path[strlen( psz_path ) - 1] = '\0';
1524
1525         free( psw_path );
1526     }
1527
1528     if( !psz_path || *psz_path == '\0' )
1529     {
1530         free( psz_path );
1531         return strdup( "C:" );
1532     }
1533 #else
1534     psz_path = strdup( "/tmp" );
1535 #endif
1536
1537     return psz_path;
1538 }
1539
1540 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path )
1541 {
1542     char *psz_name;
1543     int fd;
1544     FILE *f;
1545
1546     /* */
1547     *ppsz_file = NULL;
1548     if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1549         return NULL;
1550
1551     /* */
1552     fd = utf8_mkstemp( psz_name );
1553     *ppsz_file = psz_name;
1554
1555     if( fd < 0 )
1556         return NULL;
1557
1558     /* */
1559     f = fdopen( fd, "rw+" );
1560     if( !f )
1561         close( fd );
1562
1563     return f;
1564 }
1565