qt4: improve code readability.
[vlc/asuraparaju-public.git] / src / input / es_out_timeshift.c
blob3ac19d5abd660db7efdf1b2728c0c25205670ebe
1 /*****************************************************************************
2 * es_out_timeshift.c: Es Out timeshift.
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
5 * $Id$
7 * Authors: Laurent Aimar < fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
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
41 #include <vlc_common.h>
42 #include <vlc_fs.h>
43 #ifdef WIN32
44 # include <vlc_charset.h>
45 #endif
46 #include <vlc_input.h>
47 #include <vlc_es_out.h>
48 #include <vlc_block.h>
49 #include "input_internal.h"
50 #include "es_out.h"
51 #include "es_out_timeshift.h"
53 /*****************************************************************************
54 * Local prototypes
55 *****************************************************************************/
57 /* XXX attribute_packed is (and MUST be) used ONLY to reduce memory usage */
58 #ifdef HAVE_ATTRIBUTE_PACKED
59 # define attribute_packed __attribute__((__packed__))
60 #else
61 # define attribute_packed
62 #endif
64 enum
66 C_ADD,
67 C_SEND,
68 C_DEL,
69 C_CONTROL,
72 typedef struct attribute_packed
74 es_out_id_t *p_es;
75 es_format_t *p_fmt;
76 } ts_cmd_add_t;
78 typedef struct attribute_packed
80 es_out_id_t *p_es;
81 } ts_cmd_del_t;
83 typedef struct attribute_packed
85 es_out_id_t *p_es;
86 block_t *p_block;
87 int i_offset; /* We do not use file > INT_MAX */
88 } ts_cmd_send_t;
90 typedef struct attribute_packed
92 int i_query;
94 union
96 bool b_bool;
97 int i_int;
98 int64_t i_i64;
99 es_out_id_t *p_es;
100 struct
102 int i_int;
103 int64_t i_i64;
104 } int_i64;
105 struct
107 int i_int;
108 vlc_meta_t *p_meta;
109 } int_meta;
110 struct
112 int i_int;
113 vlc_epg_t *p_epg;
114 } int_epg;
115 struct
117 es_out_id_t *p_es;
118 bool b_bool;
119 } es_bool;
120 struct
122 es_out_id_t *p_es;
123 es_format_t *p_fmt;
124 } es_fmt;
125 struct
127 /* FIXME Really too big (double make the whole thing too big) */
128 double f_position;
129 mtime_t i_time;
130 mtime_t i_length;
131 } times;
132 struct
134 mtime_t i_pts_delay;
135 mtime_t i_pts_jitter;
136 int i_cr_average;
137 } jitter;
138 } u;
139 } ts_cmd_control_t;
141 typedef struct attribute_packed
143 int8_t i_type;
144 mtime_t i_date;
145 union
147 ts_cmd_add_t add;
148 ts_cmd_del_t del;
149 ts_cmd_send_t send;
150 ts_cmd_control_t control;
151 } u;
152 } ts_cmd_t;
154 typedef struct ts_storage_t ts_storage_t;
155 struct ts_storage_t
157 ts_storage_t *p_next;
159 /* */
160 char *psz_file; /* Filename */
161 size_t i_file_max; /* Max size in bytes */
162 int64_t i_file_size;/* Current size in bytes */
163 FILE *p_filew; /* FILE handle for data writing */
164 FILE *p_filer; /* FILE handle for data reading */
166 /* */
167 int i_cmd_r;
168 int i_cmd_w;
169 int i_cmd_max;
170 ts_cmd_t *p_cmd;
173 typedef struct
175 VLC_COMMON_MEMBERS
177 /* */
178 input_thread_t *p_input;
179 es_out_t *p_out;
180 int64_t i_tmp_size_max;
181 const char *psz_tmp_path;
183 /* Lock for all following fields */
184 vlc_mutex_t lock;
185 vlc_cond_t wait;
187 /* */
188 bool b_paused;
189 mtime_t i_pause_date;
191 /* */
192 int i_rate;
193 int i_rate_source;
194 mtime_t i_rate_date;
195 mtime_t i_rate_delay;
197 /* */
198 mtime_t i_buffering_delay;
200 /* */
201 ts_storage_t *p_storage_r;
202 ts_storage_t *p_storage_w;
204 mtime_t i_cmd_delay;
206 } ts_thread_t;
208 struct es_out_id_t
210 es_out_id_t *p_es;
213 struct es_out_sys_t
215 input_thread_t *p_input;
216 es_out_t *p_out;
218 /* Configuration */
219 int64_t i_tmp_size_max; /* Maximal temporary file size in byte */
220 char *psz_tmp_path; /* Path for temporary files */
222 /* Lock for all following fields */
223 vlc_mutex_t lock;
225 /* */
226 bool b_delayed;
227 ts_thread_t *p_thread;
229 /* */
230 bool b_input_paused;
231 bool b_input_paused_source;
232 int i_input_rate;
233 int i_input_rate_source;
235 /* */
236 int i_es;
237 es_out_id_t **pp_es;
240 static es_out_id_t *Add ( es_out_t *, const es_format_t * );
241 static int Send ( es_out_t *, es_out_id_t *, block_t * );
242 static void Del ( es_out_t *, es_out_id_t * );
243 static int Control( es_out_t *, int i_query, va_list );
244 static void Destroy( es_out_t * );
246 static int TsStart( es_out_t * );
247 static void TsAutoStop( es_out_t * );
249 static void TsStop( ts_thread_t * );
250 static void TsPushCmd( ts_thread_t *, ts_cmd_t * );
251 static int TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
252 static bool TsHasCmd( ts_thread_t * );
253 static bool TsIsUnused( ts_thread_t * );
254 static int TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, mtime_t i_date );
255 static int TsChangeRate( ts_thread_t *, int i_src_rate, int i_rate );
257 static void *TsRun( vlc_object_t * );
259 static ts_storage_t *TsStorageNew( const char *psz_path, int64_t i_tmp_size_max );
260 static void TsStorageDelete( ts_storage_t * );
261 static void TsStoragePack( ts_storage_t *p_storage );
262 static bool TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
263 static bool TsStorageIsEmpty( ts_storage_t * );
264 static void TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
265 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
267 static void CmdClean( ts_cmd_t * );
268 static void cmd_cleanup_routine( void *p ) { CmdClean( p ); }
270 static int CmdInitAdd ( ts_cmd_t *, es_out_id_t *, const es_format_t *, bool b_copy );
271 static void CmdInitSend ( ts_cmd_t *, es_out_id_t *, block_t * );
272 static int CmdInitDel ( ts_cmd_t *, es_out_id_t * );
273 static int CmdInitControl( ts_cmd_t *, int i_query, va_list, bool b_copy );
275 /* */
276 static void CmdCleanAdd ( ts_cmd_t * );
277 static void CmdCleanSend ( ts_cmd_t * );
278 static void CmdCleanControl( ts_cmd_t *p_cmd );
280 /* XXX these functions will take the destination es_out_t */
281 static void CmdExecuteAdd ( es_out_t *, ts_cmd_t * );
282 static int CmdExecuteSend ( es_out_t *, ts_cmd_t * );
283 static void CmdExecuteDel ( es_out_t *, ts_cmd_t * );
284 static int CmdExecuteControl( es_out_t *, ts_cmd_t * );
286 /* File helpers */
287 static char *GetTmpPath( char *psz_path );
288 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path );
290 /*****************************************************************************
291 * input_EsOutTimeshiftNew:
292 *****************************************************************************/
293 es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, int i_rate )
295 es_out_t *p_out = malloc( sizeof(*p_out) );
296 if( !p_out )
297 return NULL;
299 es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
300 if( !p_sys )
302 free( p_out );
303 return NULL;
306 /* */
307 p_out->pf_add = Add;
308 p_out->pf_send = Send;
309 p_out->pf_del = Del;
310 p_out->pf_control = Control;
311 p_out->pf_destroy = Destroy;
312 p_out->p_sys = p_sys;
314 /* */
315 p_sys->b_input_paused = false;
316 p_sys->b_input_paused_source = false;
317 p_sys->p_input = p_input;
318 p_sys->i_input_rate = i_rate;
319 p_sys->i_input_rate_source = i_rate;
321 p_sys->p_out = p_next_out;
322 vlc_mutex_init_recursive( &p_sys->lock );
324 p_sys->b_delayed = false;
325 p_sys->p_thread = NULL;
327 TAB_INIT( p_sys->i_es, p_sys->pp_es );
329 /* */
330 const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
331 if( i_tmp_size_max < 0 )
332 p_sys->i_tmp_size_max = 50*1024*1024;
333 else
334 p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
335 msg_Dbg( p_input, "using timeshift granularity of %d MiB",
336 (int)p_sys->i_tmp_size_max/(1024*1024) );
338 char *psz_tmp_path = var_CreateGetNonEmptyString( p_input, "input-timeshift-path" );
339 p_sys->psz_tmp_path = GetTmpPath( psz_tmp_path );
340 msg_Dbg( p_input, "using timeshift path '%s'", p_sys->psz_tmp_path );
342 #if 0
343 #define S(t) msg_Err( p_input, "SIZEOF("#t")=%d", sizeof(t) )
344 S(ts_cmd_t);
345 S(ts_cmd_control_t);
346 S(ts_cmd_send_t);
347 S(ts_cmd_del_t);
348 S(ts_cmd_add_t);
349 #undef S
350 #endif
352 return p_out;
355 /*****************************************************************************
356 * Internal functions
357 *****************************************************************************/
358 static void Destroy( es_out_t *p_out )
360 es_out_sys_t *p_sys = p_out->p_sys;
362 if( p_sys->b_delayed )
364 TsStop( p_sys->p_thread );
365 p_sys->b_delayed = false;
368 while( p_sys->i_es > 0 )
369 Del( p_out, p_sys->pp_es[0] );
370 TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
372 free( p_sys->psz_tmp_path );
373 vlc_mutex_destroy( &p_sys->lock );
374 free( p_sys );
375 free( p_out );
378 static es_out_id_t *Add( es_out_t *p_out, const es_format_t *p_fmt )
380 es_out_sys_t *p_sys = p_out->p_sys;
381 ts_cmd_t cmd;
383 es_out_id_t *p_es = malloc( sizeof( *p_es ) );
384 if( !p_es )
385 return NULL;
387 vlc_mutex_lock( &p_sys->lock );
389 TsAutoStop( p_out );
391 if( CmdInitAdd( &cmd, p_es, p_fmt, p_sys->b_delayed ) )
393 vlc_mutex_unlock( &p_sys->lock );
394 free( p_es );
395 return NULL;
398 TAB_APPEND( p_sys->i_es, p_sys->pp_es, p_es );
400 if( p_sys->b_delayed )
401 TsPushCmd( p_sys->p_thread, &cmd );
402 else
403 CmdExecuteAdd( p_sys->p_out, &cmd );
405 vlc_mutex_unlock( &p_sys->lock );
407 return p_es;
409 static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
411 es_out_sys_t *p_sys = p_out->p_sys;
412 ts_cmd_t cmd;
413 int i_ret = VLC_SUCCESS;
415 vlc_mutex_lock( &p_sys->lock );
417 TsAutoStop( p_out );
419 CmdInitSend( &cmd, p_es, p_block );
420 if( p_sys->b_delayed )
421 TsPushCmd( p_sys->p_thread, &cmd );
422 else
423 i_ret = CmdExecuteSend( p_sys->p_out, &cmd) ;
425 vlc_mutex_unlock( &p_sys->lock );
427 return i_ret;
429 static void Del( es_out_t *p_out, es_out_id_t *p_es )
431 es_out_sys_t *p_sys = p_out->p_sys;
432 ts_cmd_t cmd;
434 vlc_mutex_lock( &p_sys->lock );
436 TsAutoStop( p_out );
438 CmdInitDel( &cmd, p_es );
439 if( p_sys->b_delayed )
440 TsPushCmd( p_sys->p_thread, &cmd );
441 else
442 CmdExecuteDel( p_sys->p_out, &cmd );
444 TAB_REMOVE( p_sys->i_es, p_sys->pp_es, p_es );
446 vlc_mutex_unlock( &p_sys->lock );
449 static int ControlLockedGetEmpty( es_out_t *p_out, bool *pb_empty )
451 es_out_sys_t *p_sys = p_out->p_sys;
453 if( p_sys->b_delayed && TsHasCmd( p_sys->p_thread ) )
454 *pb_empty = false;
455 else
456 *pb_empty = es_out_GetEmpty( p_sys->p_out );
458 return VLC_SUCCESS;
460 static int ControlLockedGetWakeup( es_out_t *p_out, mtime_t *pi_wakeup )
462 es_out_sys_t *p_sys = p_out->p_sys;
464 if( p_sys->b_delayed )
466 assert( !p_sys->p_input->p->b_can_pace_control );
467 *pi_wakeup = 0;
469 else
471 *pi_wakeup = es_out_GetWakeup( p_sys->p_out );
474 return VLC_SUCCESS;
476 static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
478 es_out_sys_t *p_sys = p_out->p_sys;
480 if( p_sys->b_delayed )
481 *pb_buffering = true;
482 else
483 *pb_buffering = es_out_GetBuffering( p_sys->p_out );
485 return VLC_SUCCESS;
487 static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, mtime_t i_date )
489 es_out_sys_t *p_sys = p_out->p_sys;
490 int i_ret;
492 if( !p_sys->b_delayed && !b_source_paused == !b_paused )
494 i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
496 else
498 i_ret = VLC_EGENERIC;
499 if( !p_sys->p_input->p->b_can_pace_control )
501 if( !p_sys->b_delayed )
502 TsStart( p_out );
503 if( p_sys->b_delayed )
504 i_ret = TsChangePause( p_sys->p_thread, b_source_paused, b_paused, i_date );
506 else
508 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
509 * and so be able to advertize correctly pace control property in access
510 * module */
511 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
515 if( !i_ret )
517 p_sys->b_input_paused_source = b_source_paused;
518 p_sys->b_input_paused = b_paused;
520 return i_ret;
522 static int ControlLockedSetRate( es_out_t *p_out, int i_src_rate, int i_rate )
524 es_out_sys_t *p_sys = p_out->p_sys;
525 int i_ret;
527 if( !p_sys->b_delayed && i_src_rate == i_rate )
529 i_ret = es_out_SetRate( p_sys->p_out, i_src_rate, i_rate );
531 else
533 i_ret = VLC_EGENERIC;
534 if( !p_sys->p_input->p->b_can_pace_control )
536 if( !p_sys->b_delayed )
537 TsStart( p_out );
538 if( p_sys->b_delayed )
539 i_ret = TsChangeRate( p_sys->p_thread, i_src_rate, i_rate );
541 else
543 /* XXX we may do it BUT it would be better to finish the clock clean up+improvments
544 * and so be able to advertize correctly pace control property in access
545 * module */
546 msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
551 if( !i_ret )
553 p_sys->i_input_rate_source = i_src_rate;
554 p_sys->i_input_rate = i_rate;
556 return i_ret;
558 static int ControlLockedSetTime( es_out_t *p_out, mtime_t i_date )
560 es_out_sys_t *p_sys = p_out->p_sys;
562 if( !p_sys->b_delayed )
563 return es_out_SetTime( p_sys->p_out, i_date );
565 /* TODO */
566 msg_Err( p_sys->p_input, "EsOutTimeshift does not yet support time change" );
567 return VLC_EGENERIC;
569 static int ControlLockedSetFrameNext( es_out_t *p_out )
571 es_out_sys_t *p_sys = p_out->p_sys;
573 return es_out_SetFrameNext( p_sys->p_out );
576 static int ControlLocked( es_out_t *p_out, int i_query, va_list args )
578 es_out_sys_t *p_sys = p_out->p_sys;
580 switch( i_query )
582 /* Invalid query for this es_out level */
583 case ES_OUT_SET_ES_BY_ID:
584 case ES_OUT_RESTART_ES_BY_ID:
585 case ES_OUT_SET_ES_DEFAULT_BY_ID:
586 case ES_OUT_GET_ES_OBJECTS_BY_ID:
587 case ES_OUT_SET_DELAY:
588 case ES_OUT_SET_RECORD_STATE:
589 assert(0);
590 return VLC_EGENERIC;
592 /* Pass-through control */
593 case ES_OUT_SET_MODE:
594 case ES_OUT_SET_GROUP:
595 case ES_OUT_SET_PCR:
596 case ES_OUT_SET_GROUP_PCR:
597 case ES_OUT_RESET_PCR:
598 case ES_OUT_SET_NEXT_DISPLAY_TIME:
599 case ES_OUT_SET_GROUP_META:
600 case ES_OUT_SET_GROUP_EPG:
601 case ES_OUT_SET_ES_SCRAMBLED_STATE:
602 case ES_OUT_DEL_GROUP:
603 case ES_OUT_SET_META:
604 case ES_OUT_SET_ES:
605 case ES_OUT_RESTART_ES:
606 case ES_OUT_SET_ES_DEFAULT:
607 case ES_OUT_SET_ES_STATE:
608 case ES_OUT_SET_ES_FMT:
609 case ES_OUT_SET_TIMES:
610 case ES_OUT_SET_JITTER:
612 ts_cmd_t cmd;
613 if( CmdInitControl( &cmd, i_query, args, p_sys->b_delayed ) )
614 return VLC_EGENERIC;
615 if( p_sys->b_delayed )
617 TsPushCmd( p_sys->p_thread, &cmd );
618 return VLC_SUCCESS;
620 return CmdExecuteControl( p_sys->p_out, &cmd );
623 /* Special control when delayed */
624 case ES_OUT_GET_ES_STATE:
626 es_out_id_t *p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
627 bool *pb_enabled = (bool*)va_arg( args, bool* );
629 if( p_sys->b_delayed )
631 *pb_enabled = true;
632 return VLC_SUCCESS;
634 return es_out_Control( p_sys->p_out, ES_OUT_GET_ES_STATE, p_es->p_es, pb_enabled );
636 /* Special internal input control */
637 case ES_OUT_GET_EMPTY:
639 bool *pb_empty = (bool*)va_arg( args, bool* );
640 return ControlLockedGetEmpty( p_out, pb_empty );
642 case ES_OUT_GET_WAKE_UP: /* TODO ? */
644 mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
645 return ControlLockedGetWakeup( p_out, pi_wakeup );
647 case ES_OUT_GET_BUFFERING:
649 bool *pb_buffering = (bool *)va_arg( args, bool* );
650 return ControlLockedGetBuffering( p_out, pb_buffering );
652 case ES_OUT_SET_PAUSE_STATE:
654 const bool b_source_paused = (bool)va_arg( args, int );
655 const bool b_paused = (bool)va_arg( args, int );
656 const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
658 return ControlLockedSetPauseState( p_out, b_source_paused, b_paused, i_date );
660 case ES_OUT_SET_RATE:
662 const int i_src_rate = (int)va_arg( args, int );
663 const int i_rate = (int)va_arg( args, int );
665 return ControlLockedSetRate( p_out, i_src_rate, i_rate );
667 case ES_OUT_SET_TIME:
669 const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
671 return ControlLockedSetTime( p_out, i_date );
673 case ES_OUT_SET_FRAME_NEXT:
675 return ControlLockedSetFrameNext( p_out );
677 case ES_OUT_GET_PCR_SYSTEM:
679 if( p_sys->b_delayed )
680 return VLC_EGENERIC;
682 mtime_t *pi_system = (mtime_t*)va_arg( args, mtime_t * );
683 mtime_t *pi_delay = (mtime_t*)va_arg( args, mtime_t * );
684 return es_out_ControlGetPcrSystem( p_sys->p_out, pi_system, pi_delay );
686 case ES_OUT_MODIFY_PCR_SYSTEM:
688 const bool b_absolute = va_arg( args, int );
689 const mtime_t i_system = va_arg( args, mtime_t );
691 if( b_absolute && p_sys->b_delayed )
692 return VLC_EGENERIC;
694 return es_out_ControlModifyPcrSystem( p_sys->p_out, b_absolute, i_system );
696 case ES_OUT_GET_GROUP_FORCED:
698 int *pi_group = va_arg( args, int * );
699 return es_out_Control( p_sys->p_out, ES_OUT_GET_GROUP_FORCED, pi_group );
703 default:
704 msg_Err( p_sys->p_input, "Unknown es_out_Control query !" );
705 assert(0);
706 return VLC_EGENERIC;
709 static int Control( es_out_t *p_out, int i_query, va_list args )
711 es_out_sys_t *p_sys = p_out->p_sys;
712 int i_ret;
714 vlc_mutex_lock( &p_sys->lock );
716 TsAutoStop( p_out );
718 i_ret = ControlLocked( p_out, i_query, args );
720 vlc_mutex_unlock( &p_sys->lock );
722 return i_ret;
725 /*****************************************************************************
727 *****************************************************************************/
728 static void TsDestructor( vlc_object_t *p_this )
730 ts_thread_t *p_ts = (ts_thread_t*)p_this;
732 vlc_cond_destroy( &p_ts->wait );
733 vlc_mutex_destroy( &p_ts->lock );
735 static int TsStart( es_out_t *p_out )
737 es_out_sys_t *p_sys = p_out->p_sys;
738 ts_thread_t *p_ts;
740 assert( !p_sys->b_delayed );
742 p_sys->p_thread = p_ts = vlc_custom_create( p_sys->p_input, sizeof(ts_thread_t),
743 VLC_OBJECT_GENERIC, "es out timeshift" );
744 if( !p_ts )
745 return VLC_EGENERIC;
747 p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
748 p_ts->psz_tmp_path = p_sys->psz_tmp_path;
749 p_ts->p_input = p_sys->p_input;
750 p_ts->p_out = p_sys->p_out;
751 vlc_mutex_init( &p_ts->lock );
752 vlc_cond_init( &p_ts->wait );
753 p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
754 p_ts->i_pause_date = p_ts->b_paused ? mdate() : -1;
755 p_ts->i_rate_source = p_sys->i_input_rate_source;
756 p_ts->i_rate = p_sys->i_input_rate;
757 p_ts->i_rate_date = -1;
758 p_ts->i_rate_delay = 0;
759 p_ts->i_buffering_delay = 0;
760 p_ts->i_cmd_delay = 0;
761 p_ts->p_storage_r = NULL;
762 p_ts->p_storage_w = NULL;
764 vlc_object_set_destructor( p_ts, TsDestructor );
766 p_sys->b_delayed = true;
767 if( vlc_thread_create( p_ts, "es out timeshift",
768 TsRun, VLC_THREAD_PRIORITY_INPUT ) )
770 msg_Err( p_sys->p_input, "cannot create input thread" );
772 vlc_object_release( p_ts );
774 p_sys->b_delayed = false;
775 return VLC_EGENERIC;
778 return VLC_SUCCESS;
780 static void TsAutoStop( es_out_t *p_out )
782 es_out_sys_t *p_sys = p_out->p_sys;
784 if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_thread ) )
785 return;
787 msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
788 TsStop( p_sys->p_thread );
790 p_sys->b_delayed = false;
792 static void TsStop( ts_thread_t *p_ts )
794 vlc_object_kill( p_ts );
795 vlc_thread_join( p_ts );
797 vlc_mutex_lock( &p_ts->lock );
798 for( ;; )
800 ts_cmd_t cmd;
802 if( TsPopCmdLocked( p_ts, &cmd, true ) )
803 break;
805 CmdClean( &cmd );
807 assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
808 if( p_ts->p_storage_r )
809 TsStorageDelete( p_ts->p_storage_r );
810 vlc_mutex_unlock( &p_ts->lock );
812 vlc_object_release( p_ts );
814 static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
816 vlc_mutex_lock( &p_ts->lock );
818 if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
820 ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
822 if( !p_storage )
824 CmdClean( p_cmd );
825 vlc_mutex_unlock( &p_ts->lock );
826 /* TODO warn the user (but only once) */
827 return;
830 if( !p_ts->p_storage_w )
832 p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
834 else
836 TsStoragePack( p_ts->p_storage_w );
837 p_ts->p_storage_w->p_next = p_storage;
838 p_ts->p_storage_w = p_storage;
842 /* TODO return error and warn the user (but only once) */
843 TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
845 vlc_cond_signal( &p_ts->wait );
847 vlc_mutex_unlock( &p_ts->lock );
849 static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
851 vlc_assert_locked( &p_ts->lock );
853 if( TsStorageIsEmpty( p_ts->p_storage_r ) )
854 return VLC_EGENERIC;
856 TsStoragePopCmd( p_ts->p_storage_r, p_cmd, b_flush );
858 while( p_ts->p_storage_r && TsStorageIsEmpty( p_ts->p_storage_r ) )
860 ts_storage_t *p_next = p_ts->p_storage_r->p_next;
861 if( !p_next )
862 break;
864 TsStorageDelete( p_ts->p_storage_r );
865 p_ts->p_storage_r = p_next;
868 return VLC_SUCCESS;
870 static bool TsHasCmd( ts_thread_t *p_ts )
872 bool b_cmd;
874 vlc_mutex_lock( &p_ts->lock );
875 b_cmd = TsStorageIsEmpty( p_ts->p_storage_r );
876 vlc_mutex_unlock( &p_ts->lock );
878 return b_cmd;
880 static bool TsIsUnused( ts_thread_t *p_ts )
882 bool b_unused;
884 vlc_mutex_lock( &p_ts->lock );
885 b_unused = !p_ts->b_paused &&
886 p_ts->i_rate == p_ts->i_rate_source &&
887 TsStorageIsEmpty( p_ts->p_storage_r );
888 vlc_mutex_unlock( &p_ts->lock );
890 return b_unused;
892 static int TsChangePause( ts_thread_t *p_ts, bool b_source_paused, bool b_paused, mtime_t i_date )
894 vlc_mutex_lock( &p_ts->lock );
896 int i_ret;
897 if( b_paused )
899 assert( !b_source_paused );
900 i_ret = es_out_SetPauseState( p_ts->p_out, true, true, i_date );
902 else
904 i_ret = es_out_SetPauseState( p_ts->p_out, false, false, i_date );
907 if( !i_ret )
909 if( !b_paused )
911 assert( p_ts->i_pause_date > 0 );
913 p_ts->i_cmd_delay += i_date - p_ts->i_pause_date;
916 p_ts->b_paused = b_paused;
917 p_ts->i_pause_date = i_date;
919 vlc_cond_signal( &p_ts->wait );
921 vlc_mutex_unlock( &p_ts->lock );
922 return i_ret;
924 static int TsChangeRate( ts_thread_t *p_ts, int i_src_rate, int i_rate )
926 int i_ret;
928 vlc_mutex_lock( &p_ts->lock );
929 p_ts->i_cmd_delay += p_ts->i_rate_delay;
931 p_ts->i_rate_date = -1;
932 p_ts->i_rate_delay = 0;
933 p_ts->i_rate = i_rate;
934 p_ts->i_rate_source = i_src_rate;
936 i_ret = es_out_SetRate( p_ts->p_out, i_rate, i_rate );
937 vlc_mutex_unlock( &p_ts->lock );
939 return i_ret;
942 static void *TsRun( vlc_object_t *p_thread )
944 ts_thread_t *p_ts = (ts_thread_t*)p_thread;
945 mtime_t i_buffering_date = -1;
947 for( ;; )
949 ts_cmd_t cmd;
950 mtime_t i_deadline;
951 bool b_buffering;
953 /* Pop a command to execute */
954 vlc_mutex_lock( &p_ts->lock );
955 mutex_cleanup_push( &p_ts->lock );
957 for( ;; )
959 const int canc = vlc_savecancel();
960 b_buffering = es_out_GetBuffering( p_ts->p_out );
962 if( ( !p_ts->b_paused || b_buffering ) && !TsPopCmdLocked( p_ts, &cmd, false ) )
964 vlc_restorecancel( canc );
965 break;
967 vlc_restorecancel( canc );
969 vlc_cond_wait( &p_ts->wait, &p_ts->lock );
972 if( b_buffering && i_buffering_date < 0 )
974 i_buffering_date = cmd.i_date;
976 else if( i_buffering_date > 0 )
978 p_ts->i_buffering_delay += i_buffering_date - cmd.i_date; /* It is < 0 */
979 if( b_buffering )
980 i_buffering_date = cmd.i_date;
981 else
982 i_buffering_date = -1;
985 if( p_ts->i_rate_date < 0 )
986 p_ts->i_rate_date = cmd.i_date;
988 p_ts->i_rate_delay = 0;
989 if( p_ts->i_rate_source != p_ts->i_rate )
991 const mtime_t i_duration = cmd.i_date - p_ts->i_rate_date;
992 p_ts->i_rate_delay = i_duration * p_ts->i_rate / p_ts->i_rate_source - i_duration;
994 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 )
996 const int canc = vlc_savecancel();
998 /* Auto reset to rate 1.0 */
999 msg_Warn( p_ts->p_input, "es out timeshift: auto reset rate to %d", p_ts->i_rate_source );
1001 p_ts->i_cmd_delay = 0;
1002 p_ts->i_buffering_delay = 0;
1004 p_ts->i_rate_delay = 0;
1005 p_ts->i_rate_date = -1;
1006 p_ts->i_rate = p_ts->i_rate_source;
1008 if( !es_out_SetRate( p_ts->p_out, p_ts->i_rate_source, p_ts->i_rate ) )
1010 vlc_value_t val = { .i_int = p_ts->i_rate };
1011 /* Warn back input
1012 * FIXME it is perfectly safe BUT it is ugly as it may hide a
1013 * rate change requested by user */
1014 input_ControlPush( p_ts->p_input, INPUT_CONTROL_SET_RATE, &val );
1017 vlc_restorecancel( canc );
1019 i_deadline = cmd.i_date + p_ts->i_cmd_delay + p_ts->i_rate_delay + p_ts->i_buffering_delay;
1021 vlc_cleanup_run();
1023 /* Regulate the speed of command processing to the same one than
1024 * reading */
1025 vlc_cleanup_push( cmd_cleanup_routine, &cmd );
1027 mwait( i_deadline );
1029 vlc_cleanup_pop();
1031 /* Execute the command */
1032 const int canc = vlc_savecancel();
1033 switch( cmd.i_type )
1035 case C_ADD:
1036 CmdExecuteAdd( p_ts->p_out, &cmd );
1037 CmdCleanAdd( &cmd );
1038 break;
1039 case C_SEND:
1040 CmdExecuteSend( p_ts->p_out, &cmd );
1041 CmdCleanSend( &cmd );
1042 break;
1043 case C_CONTROL:
1044 CmdExecuteControl( p_ts->p_out, &cmd );
1045 CmdCleanControl( &cmd );
1046 break;
1047 case C_DEL:
1048 CmdExecuteDel( p_ts->p_out, &cmd );
1049 break;
1050 default:
1051 assert(0);
1052 break;
1054 vlc_restorecancel( canc );
1057 return NULL;
1060 /*****************************************************************************
1062 *****************************************************************************/
1063 static ts_storage_t *TsStorageNew( const char *psz_tmp_path, int64_t i_tmp_size_max )
1065 ts_storage_t *p_storage = calloc( 1, sizeof(ts_storage_t) );
1066 if( !p_storage )
1067 return NULL;
1069 /* */
1070 p_storage->p_next = NULL;
1072 /* */
1073 p_storage->i_file_max = i_tmp_size_max;
1074 p_storage->i_file_size = 0;
1075 p_storage->p_filew = GetTmpFile( &p_storage->psz_file, psz_tmp_path );
1076 if( p_storage->psz_file )
1077 p_storage->p_filer = vlc_fopen( p_storage->psz_file, "rb" );
1079 /* */
1080 p_storage->i_cmd_w = 0;
1081 p_storage->i_cmd_r = 0;
1082 p_storage->i_cmd_max = 30000;
1083 p_storage->p_cmd = malloc( p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1084 //fprintf( stderr, "\nSTORAGE name=%s size=%d KiB\n", p_storage->psz_file, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) /1024 );
1086 if( !p_storage->p_cmd || !p_storage->p_filew || !p_storage->p_filer )
1088 TsStorageDelete( p_storage );
1089 return NULL;
1091 return p_storage;
1093 static void TsStorageDelete( ts_storage_t *p_storage )
1095 while( p_storage->i_cmd_r < p_storage->i_cmd_w )
1097 ts_cmd_t cmd;
1099 TsStoragePopCmd( p_storage, &cmd, true );
1101 CmdClean( &cmd );
1103 free( p_storage->p_cmd );
1105 if( p_storage->p_filer )
1106 fclose( p_storage->p_filer );
1107 if( p_storage->p_filew )
1108 fclose( p_storage->p_filew );
1110 if( p_storage->psz_file )
1112 vlc_unlink( p_storage->psz_file );
1113 free( p_storage->psz_file );
1116 free( p_storage );
1118 static void TsStoragePack( ts_storage_t *p_storage )
1120 /* Try to release a bit of memory */
1121 if( p_storage->i_cmd_w >= p_storage->i_cmd_max )
1122 return;
1124 p_storage->i_cmd_max = __MAX( p_storage->i_cmd_w, 1 );
1126 ts_cmd_t *p_new = realloc( p_storage->p_cmd, p_storage->i_cmd_max * sizeof(*p_storage->p_cmd) );
1127 if( p_new )
1128 p_storage->p_cmd = p_new;
1130 static bool TsStorageIsFull( ts_storage_t *p_storage, const ts_cmd_t *p_cmd )
1132 if( p_cmd && p_cmd->i_type == C_SEND && p_storage->i_cmd_w > 0 )
1134 size_t i_size = sizeof(*p_cmd->u.send.p_block) + p_cmd->u.send.p_block->i_buffer;
1136 if( p_storage->i_file_size + i_size >= p_storage->i_file_max )
1137 return true;
1139 return p_storage->i_cmd_w >= p_storage->i_cmd_max;
1141 static bool TsStorageIsEmpty( ts_storage_t *p_storage )
1143 return !p_storage || p_storage->i_cmd_r >= p_storage->i_cmd_w;
1145 static void TsStoragePushCmd( ts_storage_t *p_storage, const ts_cmd_t *p_cmd, bool b_flush )
1147 ts_cmd_t cmd = *p_cmd;
1149 assert( !TsStorageIsFull( p_storage, p_cmd ) );
1151 if( cmd.i_type == C_SEND )
1153 block_t *p_block = cmd.u.send.p_block;
1155 cmd.u.send.p_block = NULL;
1156 cmd.u.send.i_offset = ftell( p_storage->p_filew );
1158 if( fwrite( p_block, sizeof(*p_block), 1, p_storage->p_filew ) != 1 )
1160 block_Release( p_block );
1161 return;
1163 p_storage->i_file_size += sizeof(*p_block);
1164 if( p_block->i_buffer > 0 )
1166 if( fwrite( p_block->p_buffer, p_block->i_buffer, 1, p_storage->p_filew ) != 1 )
1168 block_Release( p_block );
1169 return;
1172 p_storage->i_file_size += p_block->i_buffer;
1173 block_Release( p_block );
1175 if( b_flush )
1176 fflush( p_storage->p_filew );
1178 p_storage->p_cmd[p_storage->i_cmd_w++] = cmd;
1180 static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush )
1182 assert( !TsStorageIsEmpty( p_storage ) );
1184 *p_cmd = p_storage->p_cmd[p_storage->i_cmd_r++];
1185 if( p_cmd->i_type == C_SEND )
1187 block_t block;
1189 if( !b_flush &&
1190 !fseek( p_storage->p_filer, p_cmd->u.send.i_offset, SEEK_SET ) &&
1191 fread( &block, sizeof(block), 1, p_storage->p_filer ) == 1 )
1193 block_t *p_block = block_Alloc( block.i_buffer );
1194 if( p_block )
1196 p_block->i_dts = block.i_dts;
1197 p_block->i_pts = block.i_pts;
1198 p_block->i_flags = block.i_flags;
1199 p_block->i_length = block.i_length;
1200 p_block->i_rate = block.i_rate;
1201 p_block->i_nb_samples = block.i_nb_samples;
1202 p_block->i_buffer = fread( p_block->p_buffer, 1, block.i_buffer, p_storage->p_filer );
1204 p_cmd->u.send.p_block = p_block;
1206 else
1208 //fprintf( stderr, "TsStoragePopCmd: %m\n" );
1209 p_cmd->u.send.p_block = block_Alloc( 1 );
1214 /*****************************************************************************
1216 *****************************************************************************/
1217 static void CmdClean( ts_cmd_t *p_cmd )
1219 switch( p_cmd->i_type )
1221 case C_ADD:
1222 CmdCleanAdd( p_cmd );
1223 break;
1224 case C_SEND:
1225 CmdCleanSend( p_cmd );
1226 break;
1227 case C_CONTROL:
1228 CmdCleanControl( p_cmd );
1229 break;
1230 case C_DEL:
1231 break;
1232 default:
1233 assert(0);
1234 break;
1238 static int CmdInitAdd( ts_cmd_t *p_cmd, es_out_id_t *p_es, const es_format_t *p_fmt, bool b_copy )
1240 p_cmd->i_type = C_ADD;
1241 p_cmd->i_date = mdate();
1242 p_cmd->u.add.p_es = p_es;
1243 if( b_copy )
1245 p_cmd->u.add.p_fmt = malloc( sizeof(*p_fmt) );
1246 if( !p_cmd->u.add.p_fmt )
1247 return VLC_EGENERIC;
1248 es_format_Copy( p_cmd->u.add.p_fmt, p_fmt );
1250 else
1252 p_cmd->u.add.p_fmt = (es_format_t*)p_fmt;
1254 return VLC_SUCCESS;
1256 static void CmdExecuteAdd( es_out_t *p_out, ts_cmd_t *p_cmd )
1258 p_cmd->u.add.p_es->p_es = es_out_Add( p_out, p_cmd->u.add.p_fmt );
1260 static void CmdCleanAdd( ts_cmd_t *p_cmd )
1262 es_format_Clean( p_cmd->u.add.p_fmt );
1263 free( p_cmd->u.add.p_fmt );
1266 static void CmdInitSend( ts_cmd_t *p_cmd, es_out_id_t *p_es, block_t *p_block )
1268 p_cmd->i_type = C_SEND;
1269 p_cmd->i_date = mdate();
1270 p_cmd->u.send.p_es = p_es;
1271 p_cmd->u.send.p_block = p_block;
1273 static int CmdExecuteSend( es_out_t *p_out, ts_cmd_t *p_cmd )
1275 block_t *p_block = p_cmd->u.send.p_block;
1277 p_cmd->u.send.p_block = NULL;
1279 if( p_block )
1281 if( p_cmd->u.send.p_es->p_es )
1282 return es_out_Send( p_out, p_cmd->u.send.p_es->p_es, p_block );
1283 block_Release( p_block );
1285 return VLC_EGENERIC;
1287 static void CmdCleanSend( ts_cmd_t *p_cmd )
1289 if( p_cmd->u.send.p_block )
1290 block_Release( p_cmd->u.send.p_block );
1293 static int CmdInitDel( ts_cmd_t *p_cmd, es_out_id_t *p_es )
1295 p_cmd->i_type = C_DEL;
1296 p_cmd->i_date = mdate();
1297 p_cmd->u.del.p_es = p_es;
1298 return VLC_SUCCESS;
1300 static void CmdExecuteDel( es_out_t *p_out, ts_cmd_t *p_cmd )
1302 if( p_cmd->u.del.p_es->p_es )
1303 es_out_Del( p_out, p_cmd->u.del.p_es->p_es );
1304 free( p_cmd->u.del.p_es );
1307 static int CmdInitControl( ts_cmd_t *p_cmd, int i_query, va_list args, bool b_copy )
1309 p_cmd->i_type = C_CONTROL;
1310 p_cmd->i_date = mdate();
1311 p_cmd->u.control.i_query = i_query;
1313 switch( i_query )
1315 /* Pass-through control */
1316 case ES_OUT_SET_MODE: /* arg1= int */
1317 case ES_OUT_SET_GROUP: /* arg1= int */
1318 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1319 p_cmd->u.control.u.i_int = (int)va_arg( args, int );
1320 break;
1322 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1323 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1324 p_cmd->u.control.u.i_i64 = (int64_t)va_arg( args, int64_t );
1325 break;
1327 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1328 p_cmd->u.control.u.int_i64.i_int = (int)va_arg( args, int );
1329 p_cmd->u.control.u.int_i64.i_i64 = (int64_t)va_arg( args, int64_t );
1330 break;
1332 case ES_OUT_SET_ES_SCRAMBLED_STATE:
1333 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1334 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1335 break;
1337 case ES_OUT_RESET_PCR: /* no arg */
1338 break;
1340 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1341 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1343 if( i_query == ES_OUT_SET_GROUP_META )
1344 p_cmd->u.control.u.int_meta.i_int = (int)va_arg( args, int );
1345 const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
1347 if( b_copy )
1349 p_cmd->u.control.u.int_meta.p_meta = vlc_meta_New();
1350 if( !p_cmd->u.control.u.int_meta.p_meta )
1351 return VLC_EGENERIC;
1352 vlc_meta_Merge( p_cmd->u.control.u.int_meta.p_meta, p_meta );
1354 else
1356 /* The cast is only needed to avoid warning */
1357 p_cmd->u.control.u.int_meta.p_meta = (vlc_meta_t*)p_meta;
1359 break;
1362 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1364 p_cmd->u.control.u.int_epg.i_int = (int)va_arg( args, int );
1365 const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
1367 if( b_copy )
1369 p_cmd->u.control.u.int_epg.p_epg = vlc_epg_New( p_epg->psz_name );
1370 if( !p_cmd->u.control.u.int_epg.p_epg )
1371 return VLC_EGENERIC;
1372 for( int i = 0; i < p_epg->i_event; i++ )
1374 vlc_epg_event_t *p_evt = p_epg->pp_event[i];
1376 vlc_epg_AddEvent( p_cmd->u.control.u.int_epg.p_epg,
1377 p_evt->i_start, p_evt->i_duration,
1378 p_evt->psz_name,
1379 p_evt->psz_short_description, p_evt->psz_description );
1381 vlc_epg_SetCurrent( p_cmd->u.control.u.int_epg.p_epg,
1382 p_epg->p_current ? p_epg->p_current->i_start : -1 );
1384 else
1386 /* The cast is only needed to avoid warning */
1387 p_cmd->u.control.u.int_epg.p_epg = (vlc_epg_t*)p_epg;
1389 break;
1392 /* Modified control */
1393 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1394 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1395 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1396 p_cmd->u.control.u.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1397 break;
1399 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1400 p_cmd->u.control.u.es_bool.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1401 p_cmd->u.control.u.es_bool.b_bool = (bool)va_arg( args, int );
1402 break;
1404 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1406 p_cmd->u.control.u.es_fmt.p_es = (es_out_id_t*)va_arg( args, es_out_id_t * );
1407 es_format_t *p_fmt = (es_format_t*)va_arg( args, es_format_t * );
1409 if( b_copy )
1411 p_cmd->u.control.u.es_fmt.p_fmt = malloc( sizeof(*p_fmt) );
1412 if( !p_cmd->u.control.u.es_fmt.p_fmt )
1413 return VLC_EGENERIC;
1414 es_format_Copy( p_cmd->u.control.u.es_fmt.p_fmt, p_fmt );
1416 else
1418 p_cmd->u.control.u.es_fmt.p_fmt = p_fmt;
1420 break;
1422 case ES_OUT_SET_TIMES:
1424 double f_position = (double)va_arg( args, double );
1425 mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
1426 mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
1428 p_cmd->u.control.u.times.f_position = f_position;
1429 p_cmd->u.control.u.times.i_time = i_time;
1430 p_cmd->u.control.u.times.i_length = i_length;
1431 break;
1433 case ES_OUT_SET_JITTER:
1435 mtime_t i_pts_delay = (mtime_t)va_arg( args, mtime_t );
1436 mtime_t i_pts_jitter = (mtime_t)va_arg( args, mtime_t );
1437 int i_cr_average = (int)va_arg( args, int );
1439 p_cmd->u.control.u.jitter.i_pts_delay = i_pts_delay;
1440 p_cmd->u.control.u.jitter.i_pts_jitter = i_pts_jitter;
1441 p_cmd->u.control.u.jitter.i_cr_average = i_cr_average;
1442 break;
1445 default:
1446 assert(0);
1447 return VLC_EGENERIC;
1450 return VLC_SUCCESS;
1452 static int CmdExecuteControl( es_out_t *p_out, ts_cmd_t *p_cmd )
1454 const int i_query = p_cmd->u.control.i_query;
1456 switch( i_query )
1458 /* Pass-through control */
1459 case ES_OUT_SET_MODE: /* arg1= int */
1460 case ES_OUT_SET_GROUP: /* arg1= int */
1461 case ES_OUT_DEL_GROUP: /* arg1=int i_group */
1462 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_int );
1464 case ES_OUT_SET_PCR: /* arg1=int64_t i_pcr(microsecond!) (using default group 0)*/
1465 case ES_OUT_SET_NEXT_DISPLAY_TIME: /* arg1=int64_t i_pts(microsecond) */
1466 return es_out_Control( p_out, i_query, p_cmd->u.control.u.i_i64 );
1468 case ES_OUT_SET_GROUP_PCR: /* arg1= int i_group, arg2=int64_t i_pcr(microsecond!)*/
1469 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_i64.i_int,
1470 p_cmd->u.control.u.int_i64.i_i64 );
1472 case ES_OUT_RESET_PCR: /* no arg */
1473 return es_out_Control( p_out, i_query );
1475 case ES_OUT_SET_GROUP_META: /* arg1=int i_group arg2=const vlc_meta_t* */
1476 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.i_int,
1477 p_cmd->u.control.u.int_meta.p_meta );
1479 case ES_OUT_SET_GROUP_EPG: /* arg1=int i_group arg2=const vlc_epg_t* */
1480 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_epg.i_int,
1481 p_cmd->u.control.u.int_epg.p_epg );
1483 case ES_OUT_SET_ES_SCRAMBLED_STATE: /* arg1=int es_out_id_t* arg2=bool */
1484 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1485 p_cmd->u.control.u.es_bool.b_bool );
1487 case ES_OUT_SET_META: /* arg1=const vlc_meta_t* */
1488 return es_out_Control( p_out, i_query, p_cmd->u.control.u.int_meta.p_meta );
1490 /* Modified control */
1491 case ES_OUT_SET_ES: /* arg1= es_out_id_t* */
1492 case ES_OUT_RESTART_ES: /* arg1= es_out_id_t* */
1493 case ES_OUT_SET_ES_DEFAULT: /* arg1= es_out_id_t* */
1494 return es_out_Control( p_out, i_query, p_cmd->u.control.u.p_es->p_es );
1496 case ES_OUT_SET_ES_STATE:/* arg1= es_out_id_t* arg2=bool */
1497 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_bool.p_es->p_es,
1498 p_cmd->u.control.u.es_bool.b_bool );
1500 case ES_OUT_SET_ES_FMT: /* arg1= es_out_id_t* arg2=es_format_t* */
1501 return es_out_Control( p_out, i_query, p_cmd->u.control.u.es_fmt.p_es->p_es,
1502 p_cmd->u.control.u.es_fmt.p_fmt );
1504 case ES_OUT_SET_TIMES:
1505 return es_out_Control( p_out, i_query, p_cmd->u.control.u.times.f_position,
1506 p_cmd->u.control.u.times.i_time,
1507 p_cmd->u.control.u.times.i_length );
1508 case ES_OUT_SET_JITTER:
1509 return es_out_Control( p_out, i_query, p_cmd->u.control.u.jitter.i_pts_delay,
1510 p_cmd->u.control.u.jitter.i_pts_jitter,
1511 p_cmd->u.control.u.jitter.i_cr_average );
1513 default:
1514 assert(0);
1515 return VLC_EGENERIC;
1518 static void CmdCleanControl( ts_cmd_t *p_cmd )
1520 if( ( p_cmd->u.control.i_query == ES_OUT_SET_GROUP_META ||
1521 p_cmd->u.control.i_query == ES_OUT_SET_META ) &&
1522 p_cmd->u.control.u.int_meta.p_meta )
1524 vlc_meta_Delete( p_cmd->u.control.u.int_meta.p_meta );
1526 else if( p_cmd->u.control.i_query == ES_OUT_SET_GROUP_EPG &&
1527 p_cmd->u.control.u.int_epg.p_epg )
1529 vlc_epg_Delete( p_cmd->u.control.u.int_epg.p_epg );
1531 else if( p_cmd->u.control.i_query == ES_OUT_SET_ES_FMT &&
1532 p_cmd->u.control.u.es_fmt.p_fmt )
1534 es_format_Clean( p_cmd->u.control.u.es_fmt.p_fmt );
1535 free( p_cmd->u.control.u.es_fmt.p_fmt );
1540 /*****************************************************************************
1541 * GetTmpFile/Path:
1542 *****************************************************************************/
1543 static char *GetTmpPath( char *psz_path )
1545 if( psz_path && *psz_path )
1547 /* Make sure that the path exists and is a directory */
1548 struct stat s;
1549 const int i_ret = vlc_stat( psz_path, &s );
1551 if( i_ret < 0 && !vlc_mkdir( psz_path, 0600 ) )
1552 return psz_path;
1553 else if( i_ret == 0 && ( s.st_mode & S_IFDIR ) )
1554 return psz_path;
1556 free( psz_path );
1558 /* Create a suitable path */
1559 #if defined (WIN32) && !defined (UNDER_CE)
1560 const DWORD dwCount = GetTempPathW( 0, NULL );
1561 wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
1562 if( psw_path )
1564 if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
1566 free( psw_path );
1568 psw_path = _wgetcwd( NULL, 0 );
1572 psz_path = NULL;
1573 if( psw_path )
1575 psz_path = FromWide( psw_path );
1576 while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
1577 psz_path[strlen( psz_path ) - 1] = '\0';
1579 free( psw_path );
1582 if( !psz_path || *psz_path == '\0' )
1584 free( psz_path );
1585 return strdup( "C:" );
1587 #else
1588 psz_path = strdup( "/tmp" );
1589 #endif
1591 return psz_path;
1594 static FILE *GetTmpFile( char **ppsz_file, const char *psz_path )
1596 char *psz_name;
1597 int fd;
1598 FILE *f;
1600 /* */
1601 *ppsz_file = NULL;
1602 if( asprintf( &psz_name, "%s/vlc-timeshift.XXXXXX", psz_path ) < 0 )
1603 return NULL;
1605 /* */
1606 fd = vlc_mkstemp( psz_name );
1607 *ppsz_file = psz_name;
1609 if( fd < 0 )
1610 return NULL;
1612 /* */
1613 f = fdopen( fd, "w+b" );
1614 if( !f )
1615 close( fd );
1617 return f;