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