forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes_out_timeshift.c
1849 lines (1594 loc) · 54.2 KB
/
es_out_timeshift.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* es_out_timeshift.c: Es Out timeshift.
*****************************************************************************
* Copyright (C) 2008 Laurent Aimar
*
* Authors: Laurent Aimar < fenrir _AT_ videolan _DOT_ org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#if defined (_WIN32)
# include <direct.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <vlc_common.h>
#include <vlc_fs.h>
#include <vlc_mouse.h>
#ifdef _WIN32
# include <vlc_charset.h>
#endif
#include <vlc_es_out.h>
#include <vlc_block.h>
#include "input_internal.h"
#include "es_out.h"
/*****************************************************************************
* Local prototypes
*****************************************************************************/
/* XXX attribute_packed is (and MUST be) used ONLY to reduce memory usage */
#ifdef HAVE_ATTRIBUTE_PACKED
# define attribute_packed __attribute__((__packed__))
#else
# define attribute_packed
#endif
enum ts_storage_cmd_type_e
{
C_ADD = 0,
C_SEND,
C_DEL,
C_CONTROL,
C_PRIVCONTROL,
};
typedef struct attribute_packed
{
int8_t i_type;
vlc_tick_t i_date;
} ts_cmd_header_t;
typedef struct attribute_packed
{
ts_cmd_header_t header;
input_source_t *in;
es_out_id_t *p_es;
es_format_t *p_fmt;
} ts_cmd_add_t;
typedef struct attribute_packed
{
ts_cmd_header_t header;
es_out_id_t *p_es;
} ts_cmd_del_t;
typedef struct attribute_packed
{
ts_cmd_header_t header;
es_out_id_t *p_es;
union{
block_t *p_block;
int i_offset; /* We do not use file > INT_MAX */
};
} ts_cmd_send_t;
typedef struct attribute_packed
{
ts_cmd_header_t header;
int i_query;
input_source_t *in;
union
{
bool b_bool;
int i_int;
int64_t i_i64;
es_out_id_t *p_es;
struct
{
int i_int;
int64_t i_i64;
} int_i64;
struct
{
int i_int;
vlc_meta_t *p_meta;
} int_meta;
struct
{
int i_int;
vlc_epg_t *p_epg;
} int_epg;
struct
{
int i_int;
vlc_epg_event_t *p_evt;
} int_epg_evt;
struct
{
es_out_id_t *p_es;
bool b_bool;
} es_bool;
struct
{
es_out_id_t *p_es;
es_format_t *p_fmt;
} es_fmt;
struct
{
int i_cat;
int i_policy;
} es_policy;
} u;
} ts_cmd_control_t;
typedef struct attribute_packed
{
ts_cmd_header_t header;
int i_query;
union
{
int i_int;
struct
{
vlc_tick_t i_pts_delay;
vlc_tick_t i_pts_jitter;
int i_cr_average;
} jitter;
struct
{
double f_position;
vlc_tick_t i_time;
vlc_tick_t i_normal_time;
vlc_tick_t i_length;
} times;
} u;
} ts_cmd_privcontrol_t;
typedef union
{
ts_cmd_header_t header;
ts_cmd_add_t add;
ts_cmd_del_t del;
ts_cmd_send_t send;
ts_cmd_control_t control;
ts_cmd_privcontrol_t privcontrol;
} ts_cmd_t;
static_assert(offsetof(ts_cmd_t, header) == offsetof(ts_cmd_add_t, header), "invalid packing");
static_assert(offsetof(ts_cmd_t, header) == offsetof(ts_cmd_del_t, header), "invalid packing");
static_assert(offsetof(ts_cmd_t, header) == offsetof(ts_cmd_send_t, header), "invalid packing");
static_assert(offsetof(ts_cmd_t, header) == offsetof(ts_cmd_control_t, header), "invalid packing");
static_assert(offsetof(ts_cmd_t, header) == offsetof(ts_cmd_privcontrol_t, header), "invalid packing");
typedef struct ts_storage_t ts_storage_t;
struct ts_storage_t
{
ts_storage_t *p_next;
/* */
#ifdef _WIN32
char *psz_file; /* Filename */
#endif
size_t i_file_max; /* Max size in bytes */
int64_t i_file_size;/* Current size in bytes */
FILE *p_filew; /* FILE handle for data writing */
FILE *p_filer; /* FILE handle for data reading */
/* */
uint8_t *p_cmd_r;
uint8_t *p_cmd_w;
uint8_t *p_cmd_buf;
size_t i_cmd_buf;
};
typedef struct
{
vlc_thread_t thread;
input_thread_t *p_input;
es_out_t *p_tsout;
es_out_t *p_out;
int64_t i_tmp_size_max;
const char *psz_tmp_path;
/* Lock for all following fields */
vlc_mutex_t lock;
vlc_cond_t wait;
vlc_sem_t done;
/* */
bool b_paused;
vlc_tick_t i_pause_date;
/* */
float rate;
float rate_source;
vlc_tick_t i_rate_date;
vlc_tick_t i_rate_delay;
/* */
vlc_tick_t i_buffering_delay;
/* */
ts_storage_t *p_storage_r;
ts_storage_t *p_storage_w;
vlc_tick_t i_cmd_delay;
} ts_thread_t;
struct es_out_id_t
{
es_out_id_t *p_es;
};
typedef struct
{
input_thread_t *p_input;
es_out_t *p_out;
/* Configuration */
int64_t i_tmp_size_max; /* Maximal temporary file size in byte */
char *psz_tmp_path; /* Path for temporary files */
/* Lock for all following fields */
vlc_mutex_t lock;
/* */
bool b_delayed;
ts_thread_t *p_ts;
/* */
bool b_input_paused;
bool b_input_paused_source;
float input_rate;
float input_rate_source;
/* */
int i_es;
es_out_id_t **pp_es;
es_out_t out;
} es_out_sys_t;
static void Del ( es_out_t *, es_out_id_t * );
static int TsStart( es_out_t * );
static void TsAutoStop( es_out_t * );
static void TsStop( ts_thread_t * );
static void TsPushCmd( ts_thread_t *, ts_cmd_t * );
static int TsPopCmdLocked( ts_thread_t *, ts_cmd_t *, bool b_flush );
static bool TsHasCmd( ts_thread_t * );
static bool TsIsUnused( ts_thread_t * );
static int TsChangePause( ts_thread_t *, bool b_source_paused, bool b_paused, vlc_tick_t i_date );
static int TsChangeRate( ts_thread_t *, float src_rate, float rate );
static void *TsRun( void * );
static ts_storage_t *TsStorageNew( const char *psz_path, int64_t i_tmp_size_max );
static void TsStorageDelete( ts_storage_t * );
static void TsStoragePack( ts_storage_t *p_storage );
static bool TsStorageIsFull( ts_storage_t *, const ts_cmd_t *p_cmd );
static bool TsStorageIsEmpty( ts_storage_t * );
static void TsStoragePushCmd( ts_storage_t *, const ts_cmd_t *p_cmd, bool b_flush );
static void TsStoragePopCmd( ts_storage_t *p_storage, ts_cmd_t *p_cmd, bool b_flush );
static void CmdClean( ts_cmd_t * );
static int CmdInitAdd ( ts_cmd_add_t *, input_source_t *, es_out_id_t *, const es_format_t *, bool b_copy );
static void CmdInitSend ( ts_cmd_send_t *, es_out_id_t *, block_t * );
static int CmdInitDel ( ts_cmd_del_t *, es_out_id_t * );
static int CmdInitControl( ts_cmd_control_t *, input_source_t *, int i_query, va_list, bool b_copy );
static int CmdInitPrivControl( ts_cmd_privcontrol_t *, int i_query, va_list, bool b_copy );
/* */
static void CmdCleanAdd ( ts_cmd_add_t * );
static void CmdCleanSend ( ts_cmd_send_t * );
static void CmdCleanControl( ts_cmd_control_t * );
/* */
static void CmdExecuteAdd ( es_out_t *, ts_cmd_add_t * );
static int CmdExecuteSend ( es_out_t *, ts_cmd_send_t * );
static void CmdExecuteDel ( es_out_t *, ts_cmd_del_t * );
static int CmdExecuteControl( es_out_t *, ts_cmd_control_t * );
static int CmdExecutePrivControl( es_out_t *, ts_cmd_privcontrol_t * );
/* File helpers */
static int GetTmpFile( char **ppsz_file, const char *psz_path );
static const struct es_out_callbacks es_out_timeshift_cbs;
/*****************************************************************************
* input_EsOutTimeshiftNew:
*****************************************************************************/
es_out_t *input_EsOutTimeshiftNew( input_thread_t *p_input, es_out_t *p_next_out, float rate )
{
es_out_sys_t *p_sys = malloc( sizeof(*p_sys) );
if( !p_sys )
return NULL;
p_sys->out.cbs = &es_out_timeshift_cbs;
/* */
p_sys->b_input_paused = false;
p_sys->b_input_paused_source = false;
p_sys->p_input = p_input;
p_sys->input_rate = rate;
p_sys->input_rate_source = rate;
p_sys->p_out = p_next_out;
vlc_mutex_init_recursive( &p_sys->lock );
p_sys->b_delayed = false;
p_sys->p_ts = NULL;
TAB_INIT( p_sys->i_es, p_sys->pp_es );
/* */
const int i_tmp_size_max = var_CreateGetInteger( p_input, "input-timeshift-granularity" );
if( i_tmp_size_max < 0 )
p_sys->i_tmp_size_max = 50*1024*1024;
else
p_sys->i_tmp_size_max = __MAX( i_tmp_size_max, 1*1024*1024 );
msg_Dbg( p_input, "using timeshift granularity of %d MiB",
(int)p_sys->i_tmp_size_max/(1024*1024) );
p_sys->psz_tmp_path = var_InheritString( p_input, "input-timeshift-path" );
#if defined (_WIN32) && !VLC_WINSTORE_APP
if( p_sys->psz_tmp_path == NULL )
{
const DWORD count = GetTempPath( 0, NULL );
if( count > 0 )
{
WCHAR *path = vlc_alloc( count + 1, sizeof(WCHAR) );
if( path != NULL )
{
DWORD ret = GetTempPath( count + 1, path );
if( ret != 0 && ret <= count )
p_sys->psz_tmp_path = FromWide( path );
free( path );
}
}
}
if( p_sys->psz_tmp_path == NULL )
{
wchar_t *wpath = _wgetcwd( NULL, 0 );
if( wpath != NULL )
{
p_sys->psz_tmp_path = FromWide( wpath );
free( wpath );
}
}
if( p_sys->psz_tmp_path == NULL )
p_sys->psz_tmp_path = strdup( "C:" );
if( p_sys->psz_tmp_path != NULL )
{
size_t len = strlen( p_sys->psz_tmp_path );
while( len > 0 && p_sys->psz_tmp_path[len - 1] == DIR_SEP_CHAR )
len--;
p_sys->psz_tmp_path[len] = '\0';
}
#endif
if( p_sys->psz_tmp_path != NULL )
msg_Dbg( p_input, "using timeshift path: %s", p_sys->psz_tmp_path );
else
msg_Dbg( p_input, "using default timeshift path" );
#if 0
#define S(t) msg_Err( p_input, "SIZEOF("#t")=%zd", sizeof(t) )
S(ts_cmd_t);
S(ts_cmd_control_t);
S(ts_cmd_privcontrol_t);
S(ts_cmd_send_t);
S(ts_cmd_del_t);
S(ts_cmd_add_t);
#undef S
#endif
return &p_sys->out;
}
/*****************************************************************************
* Internal functions
*****************************************************************************/
static void Destroy( es_out_t *p_out )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
if( p_sys->b_delayed )
{
TsStop( p_sys->p_ts );
p_sys->b_delayed = false;
}
while( p_sys->i_es > 0 )
Del( p_out, p_sys->pp_es[0] );
TAB_CLEAN( p_sys->i_es, p_sys->pp_es );
free( p_sys->psz_tmp_path );
free( p_sys );
}
static es_out_id_t *Add( es_out_t *p_out, input_source_t *in, const es_format_t *p_fmt )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
ts_cmd_add_t cmd;
es_out_id_t *p_es = malloc( sizeof( *p_es ) );
if( !p_es )
return NULL;
vlc_mutex_lock( &p_sys->lock );
TsAutoStop( p_out );
if( CmdInitAdd( &cmd, in, p_es, p_fmt, p_sys->b_delayed ) )
{
vlc_mutex_unlock( &p_sys->lock );
free( p_es );
return NULL;
}
if( p_sys->b_delayed )
TsPushCmd( p_sys->p_ts, (ts_cmd_t *) &cmd );
else
CmdExecuteAdd( p_out, &cmd );
vlc_mutex_unlock( &p_sys->lock );
return p_es;
}
static int Send( es_out_t *p_out, es_out_id_t *p_es, block_t *p_block )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
ts_cmd_send_t cmd;
int i_ret = VLC_SUCCESS;
vlc_mutex_lock( &p_sys->lock );
TsAutoStop( p_out );
CmdInitSend( &cmd, p_es, p_block );
if( p_sys->b_delayed )
TsPushCmd( p_sys->p_ts, (ts_cmd_t *)&cmd );
else
i_ret = CmdExecuteSend( p_out, &cmd) ;
vlc_mutex_unlock( &p_sys->lock );
return i_ret;
}
static void Del( es_out_t *p_out, es_out_id_t *p_es )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
ts_cmd_del_t cmd;
vlc_mutex_lock( &p_sys->lock );
TsAutoStop( p_out );
CmdInitDel( &cmd, p_es );
if( p_sys->b_delayed )
TsPushCmd( p_sys->p_ts, (ts_cmd_t *)&cmd );
else
CmdExecuteDel( p_out, &cmd );
vlc_mutex_unlock( &p_sys->lock );
}
static inline int es_out_in_vaControl( es_out_t *p_out, input_source_t *in,
int i_query, va_list args)
{
return p_out->cbs->control( p_out, in, i_query, args );
}
static inline int es_out_in_Control( es_out_t *p_out, input_source_t *in,
int i_query, ... )
{
va_list args;
int i_result;
va_start( args, i_query );
i_result = es_out_in_vaControl( p_out, in, i_query, args );
va_end( args );
return i_result;
}
static int ControlLockedGetEmpty( es_out_t *p_out, input_source_t *in,
bool *pb_empty )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
if( p_sys->b_delayed && TsHasCmd( p_sys->p_ts ) )
*pb_empty = false;
else
{
int ret = es_out_in_Control( p_sys->p_out, in, ES_OUT_GET_EMPTY, pb_empty );
assert( ret == VLC_SUCCESS );
}
return VLC_SUCCESS;
}
static int ControlLockedGetWakeup( es_out_t *p_out, vlc_tick_t *pi_wakeup )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
if( p_sys->b_delayed )
{
assert( !input_priv(p_sys->p_input)->b_can_pace_control );
*pi_wakeup = 0;
}
else
{
*pi_wakeup = es_out_GetWakeup( p_sys->p_out );
}
return VLC_SUCCESS;
}
static int ControlLockedGetBuffering( es_out_t *p_out, bool *pb_buffering )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
if( p_sys->b_delayed )
*pb_buffering = true;
else
*pb_buffering = es_out_GetBuffering( p_sys->p_out );
return VLC_SUCCESS;
}
static int ControlLockedSetPauseState( es_out_t *p_out, bool b_source_paused, bool b_paused, vlc_tick_t i_date )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
int i_ret;
if( !p_sys->b_delayed && !b_source_paused == !b_paused )
{
i_ret = es_out_SetPauseState( p_sys->p_out, b_source_paused, b_paused, i_date );
}
else
{
i_ret = VLC_EGENERIC;
if( !input_priv(p_sys->p_input)->b_can_pace_control )
{
if( !p_sys->b_delayed )
TsStart( p_out );
if( p_sys->b_delayed )
i_ret = TsChangePause( p_sys->p_ts, b_source_paused, b_paused, i_date );
}
else
{
/* XXX we may do it BUT it would be better to finish the clock clean up+improvements
* and so be able to advertize correctly pace control property in access
* module */
msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
}
}
if( !i_ret )
{
p_sys->b_input_paused_source = b_source_paused;
p_sys->b_input_paused = b_paused;
}
return i_ret;
}
static int ControlLockedSetRate( es_out_t *p_out, float src_rate, float rate )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
int i_ret;
if( !p_sys->b_delayed && src_rate == rate )
{
i_ret = es_out_SetRate( p_sys->p_out, src_rate, rate );
}
else
{
i_ret = VLC_EGENERIC;
if( !input_priv(p_sys->p_input)->b_can_pace_control )
{
if( !p_sys->b_delayed )
TsStart( p_out );
if( p_sys->b_delayed )
i_ret = TsChangeRate( p_sys->p_ts, src_rate, rate );
}
else
{
/* XXX we may do it BUT it would be better to finish the clock clean up+improvements
* and so be able to advertize correctly pace control property in access
* module */
msg_Err( p_sys->p_input, "EsOutTimeshift does not work with streams that have pace control" );
}
}
if( !i_ret )
{
p_sys->input_rate_source = src_rate;
p_sys->input_rate = rate;
}
return i_ret;
}
static int ControlLockedSetFrameNext( es_out_t *p_out )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
return es_out_SetFrameNext( p_sys->p_out );
}
static int ControlLocked( es_out_t *p_out, input_source_t *in, int i_query,
va_list args )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
switch( i_query )
{
/* Pass-through control */
case ES_OUT_SET_GROUP:
case ES_OUT_SET_PCR:
case ES_OUT_SET_GROUP_PCR:
case ES_OUT_RESET_PCR:
case ES_OUT_SET_NEXT_DISPLAY_TIME:
case ES_OUT_SET_GROUP_META:
case ES_OUT_SET_GROUP_EPG:
case ES_OUT_SET_GROUP_EPG_EVENT:
case ES_OUT_SET_EPG_TIME:
case ES_OUT_SET_ES_SCRAMBLED_STATE:
case ES_OUT_DEL_GROUP:
case ES_OUT_SET_META:
case ES_OUT_SET_ES:
case ES_OUT_UNSET_ES:
case ES_OUT_RESTART_ES:
case ES_OUT_SET_ES_DEFAULT:
case ES_OUT_SET_ES_STATE:
case ES_OUT_SET_ES_CAT_POLICY:
case ES_OUT_SET_ES_FMT:
{
ts_cmd_control_t cmd;
if( CmdInitControl( &cmd, in, i_query, args, p_sys->b_delayed ) )
return VLC_EGENERIC;
if( p_sys->b_delayed )
{
TsPushCmd( p_sys->p_ts, (ts_cmd_t *) &cmd );
return VLC_SUCCESS;
}
return CmdExecuteControl( p_out, &cmd );
}
/* Special control when delayed */
case ES_OUT_GET_ES_STATE:
{
es_out_id_t *p_es = va_arg( args, es_out_id_t * );
bool *pb_enabled = va_arg( args, bool* );
if( p_sys->b_delayed )
{
*pb_enabled = true;
return VLC_SUCCESS;
}
return es_out_in_Control( p_sys->p_out, in, ES_OUT_GET_ES_STATE,
p_es->p_es, pb_enabled );
}
case ES_OUT_VOUT_SET_MOUSE_EVENT:
{
es_out_id_t *p_es = va_arg( args, es_out_id_t * );
vlc_mouse_event cb = va_arg( args, vlc_mouse_event );
void *user_data = va_arg( args, void * );
return es_out_in_Control( p_sys->p_out, in, ES_OUT_VOUT_SET_MOUSE_EVENT,
p_es->p_es, cb, user_data );
}
case ES_OUT_VOUT_ADD_OVERLAY:
{
es_out_id_t *p_es = va_arg( args, es_out_id_t * );
subpicture_t *sub = va_arg( args, subpicture_t * );
size_t *channel = va_arg( args, size_t * );
return es_out_in_Control( p_sys->p_out, in, ES_OUT_VOUT_ADD_OVERLAY,
p_es->p_es, sub, channel );
}
case ES_OUT_VOUT_DEL_OVERLAY:
{
es_out_id_t *p_es = va_arg( args, es_out_id_t * );
size_t channel = va_arg( args, size_t );
return es_out_in_Control( p_sys->p_out, in, ES_OUT_VOUT_DEL_OVERLAY,
p_es->p_es, channel );
}
case ES_OUT_SPU_SET_HIGHLIGHT:
{
es_out_id_t *p_es = va_arg( args, es_out_id_t * );
const vlc_spu_highlight_t *p_hl = va_arg( args, const vlc_spu_highlight_t * );
return es_out_in_Control( p_sys->p_out, in, ES_OUT_SPU_SET_HIGHLIGHT,
p_es->p_es, p_hl );
}
/* Special internal input control */
case ES_OUT_GET_EMPTY:
{
bool *pb_empty = va_arg( args, bool* );
return ControlLockedGetEmpty( p_out, in, pb_empty );
}
case ES_OUT_GET_PCR_SYSTEM:
if( p_sys->b_delayed )
return VLC_EGENERIC;
/* fall through */
case ES_OUT_POST_SUBNODE:
return es_out_in_vaControl( p_sys->p_out, in, i_query, args );
case ES_OUT_MODIFY_PCR_SYSTEM:
{
const bool b_absolute = va_arg( args, int );
const vlc_tick_t i_system = va_arg( args, vlc_tick_t );
if( b_absolute && p_sys->b_delayed )
return VLC_EGENERIC;
return es_out_in_Control( p_sys->p_out, in, i_query, b_absolute,
i_system );
}
default:
vlc_assert_unreachable();
return VLC_EGENERIC;
}
}
static int Control( es_out_t *p_tsout, input_source_t *in, int i_query, va_list args )
{
es_out_sys_t *p_sys = container_of(p_tsout, es_out_sys_t, out);
int i_ret;
vlc_mutex_lock( &p_sys->lock );
TsAutoStop( p_tsout );
i_ret = ControlLocked( p_tsout, in, i_query, args );
vlc_mutex_unlock( &p_sys->lock );
return i_ret;
}
static int PrivControlLocked( es_out_t *p_tsout, int i_query, va_list args )
{
es_out_sys_t *p_sys = container_of(p_tsout, es_out_sys_t, out);
switch( i_query )
{
/* Pass-through control */
case ES_OUT_PRIV_SET_MODE:
case ES_OUT_PRIV_SET_TIMES:
case ES_OUT_PRIV_SET_JITTER:
case ES_OUT_PRIV_SET_EOS:
{
ts_cmd_t cmd;
if( CmdInitPrivControl( &cmd.privcontrol, i_query, args, p_sys->b_delayed ) )
return VLC_EGENERIC;
if( p_sys->b_delayed )
{
TsPushCmd( p_sys->p_ts, &cmd );
return VLC_SUCCESS;
}
return CmdExecutePrivControl( p_tsout, &cmd.privcontrol );
}
case ES_OUT_PRIV_GET_WAKE_UP: /* TODO ? */
{
vlc_tick_t *pi_wakeup = va_arg( args, vlc_tick_t* );
return ControlLockedGetWakeup( p_tsout, pi_wakeup );
}
case ES_OUT_PRIV_GET_BUFFERING:
{
bool *pb_buffering = va_arg( args, bool* );
return ControlLockedGetBuffering( p_tsout, pb_buffering );
}
case ES_OUT_PRIV_SET_PAUSE_STATE:
{
const bool b_source_paused = (bool)va_arg( args, int );
const bool b_paused = (bool)va_arg( args, int );
const vlc_tick_t i_date = va_arg( args, vlc_tick_t );
return ControlLockedSetPauseState( p_tsout, b_source_paused, b_paused, i_date );
}
case ES_OUT_PRIV_SET_RATE:
{
const float src_rate = va_arg( args, double );
const float rate = va_arg( args, double );
return ControlLockedSetRate( p_tsout, src_rate, rate );
}
case ES_OUT_PRIV_SET_FRAME_NEXT:
{
return ControlLockedSetFrameNext( p_tsout );
}
case ES_OUT_PRIV_GET_GROUP_FORCED:
return es_out_vaPrivControl( p_sys->p_out, i_query, args );
/* Invalid queries for this es_out level */
case ES_OUT_PRIV_SET_ES:
case ES_OUT_PRIV_UNSET_ES:
case ES_OUT_PRIV_RESTART_ES:
case ES_OUT_PRIV_SET_ES_CAT_IDS:
case ES_OUT_PRIV_SET_ES_LIST:
case ES_OUT_PRIV_STOP_ALL_ES:
case ES_OUT_PRIV_START_ALL_ES:
case ES_OUT_PRIV_SET_ES_DELAY:
case ES_OUT_PRIV_SET_DELAY:
case ES_OUT_PRIV_SET_RECORD_STATE:
case ES_OUT_PRIV_SET_VBI_PAGE:
case ES_OUT_PRIV_SET_VBI_TRANSPARENCY:
default: vlc_assert_unreachable();
}
}
static int PrivControl( es_out_t *p_tsout, int i_query, va_list args )
{
es_out_sys_t *p_sys = container_of(p_tsout, es_out_sys_t, out);
int i_ret;
vlc_mutex_lock( &p_sys->lock );
TsAutoStop( p_tsout );
i_ret = PrivControlLocked( p_tsout, i_query, args );
vlc_mutex_unlock( &p_sys->lock );
return i_ret;
}
static const struct es_out_callbacks es_out_timeshift_cbs =
{
.add = Add,
.send = Send,
.del = Del,
.control = Control,
.destroy = Destroy,
.priv_control = PrivControl,
};
/*****************************************************************************
*
*****************************************************************************/
static void TsDestroy( ts_thread_t *p_ts )
{
free( p_ts );
}
static int TsStart( es_out_t *p_out )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
ts_thread_t *p_ts;
assert( !p_sys->b_delayed );
p_sys->p_ts = p_ts = calloc(1, sizeof(*p_ts));
if( !p_ts )
return VLC_EGENERIC;
p_ts->i_tmp_size_max = p_sys->i_tmp_size_max;
p_ts->psz_tmp_path = p_sys->psz_tmp_path;
p_ts->p_input = p_sys->p_input;
p_ts->p_out = p_sys->p_out;
p_ts->p_tsout = p_out;
vlc_mutex_init( &p_ts->lock );
vlc_cond_init( &p_ts->wait );
vlc_sem_init( &p_ts->done, 0 );
p_ts->b_paused = p_sys->b_input_paused && !p_sys->b_input_paused_source;
p_ts->i_pause_date = p_ts->b_paused ? vlc_tick_now() : -1;
p_ts->rate_source = p_sys->input_rate_source;
p_ts->rate = p_sys->input_rate;
p_ts->i_rate_date = -1;
p_ts->i_rate_delay = 0;
p_ts->i_buffering_delay = 0;
p_ts->i_cmd_delay = 0;
p_ts->p_storage_r = NULL;
p_ts->p_storage_w = NULL;
p_sys->b_delayed = true;
if( vlc_clone( &p_ts->thread, TsRun, p_ts, VLC_THREAD_PRIORITY_INPUT ) )
{
msg_Err( p_sys->p_input, "cannot create timeshift thread" );
TsDestroy( p_ts );
p_sys->b_delayed = false;
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
static void TsAutoStop( es_out_t *p_out )
{
es_out_sys_t *p_sys = container_of(p_out, es_out_sys_t, out);
if( !p_sys->b_delayed || !TsIsUnused( p_sys->p_ts ) )
return;
msg_Warn( p_sys->p_input, "es out timeshift: auto stop" );
TsStop( p_sys->p_ts );
p_sys->b_delayed = false;
}
static void TsStop( ts_thread_t *p_ts )
{
vlc_mutex_lock( &p_ts->lock );
vlc_sem_post( &p_ts->done );
vlc_cond_signal( &p_ts->wait );
vlc_mutex_unlock( &p_ts->lock );
vlc_join( p_ts->thread, NULL );
vlc_mutex_lock( &p_ts->lock );
for( ;; )
{
ts_cmd_t cmd;
if( TsPopCmdLocked( p_ts, &cmd, true ) )
break;
CmdClean( &cmd );
}
assert( !p_ts->p_storage_r || !p_ts->p_storage_r->p_next );
if( p_ts->p_storage_r )
TsStorageDelete( p_ts->p_storage_r );
vlc_mutex_unlock( &p_ts->lock );
TsDestroy( p_ts );
}
static void TsPushCmd( ts_thread_t *p_ts, ts_cmd_t *p_cmd )
{
vlc_mutex_lock( &p_ts->lock );
if( !p_ts->p_storage_w || TsStorageIsFull( p_ts->p_storage_w, p_cmd ) )
{
ts_storage_t *p_storage = TsStorageNew( p_ts->psz_tmp_path, p_ts->i_tmp_size_max );
if( !p_storage )
{
CmdClean( p_cmd );
vlc_mutex_unlock( &p_ts->lock );
/* TODO warn the user (but only once) */
return;
}
if( !p_ts->p_storage_w )
{
p_ts->p_storage_r = p_ts->p_storage_w = p_storage;
}
else
{
TsStoragePack( p_ts->p_storage_w );
p_ts->p_storage_w->p_next = p_storage;
p_ts->p_storage_w = p_storage;
}
}
/* TODO return error and warn the user (but only once) */
TsStoragePushCmd( p_ts->p_storage_w, p_cmd, p_ts->p_storage_r == p_ts->p_storage_w );
vlc_cond_signal( &p_ts->wait );
vlc_mutex_unlock( &p_ts->lock );
}
static int TsPopCmdLocked( ts_thread_t *p_ts, ts_cmd_t *p_cmd, bool b_flush )
{
vlc_mutex_assert( &p_ts->lock );