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