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