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