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