Repository navigation
Expand file tree
/
Copy pathinput.c
More file actions
3372 lines (2896 loc) · 109 KB
/
Copy pathinput.c
File metadata and controls
3372 lines (2896 loc) · 109 KB
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
/*****************************************************************************
* input.c: input thread
*****************************************************************************
* Copyright (C) 1998-2007 VLC authors and VideoLAN
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Laurent Aimar <fenrir@via.ecp.fr>
*
* 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 <vlc_common.h>
#include <vlc_decoder.h>
#include <limits.h>
#include <assert.h>
#include <sys/stat.h>
#include "input_internal.h"
#include "event.h"
#include "es_out.h"
#include "demux.h"
#include "item.h"
#include "resource.h"
#include "stream.h"
#include <vlc_aout.h>
#include <vlc_sout.h>
#include <vlc_dialog.h>
#include <vlc_url.h>
#include <vlc_charset.h>
#include <vlc_fs.h>
#include <vlc_strings.h>
#include <vlc_modules.h>
#include <vlc_stream.h>
#include <vlc_stream_extractor.h>
#include <vlc_renderer_discovery.h>
/*****************************************************************************
* Local prototypes
*****************************************************************************/
enum input_create_option {
INPUT_CREATE_OPTION_NONE,
INPUT_CREATE_OPTION_PREPARSING,
INPUT_CREATE_OPTION_THUMBNAILING,
};
static void *Run( void * );
static void *Preparse( void * );
static input_thread_t * Create ( vlc_object_t *, input_thread_events_cb, void *,
input_item_t *, enum input_create_option option,
input_resource_t *, vlc_renderer_item_t * );
static void Destroy ( input_thread_t *p_input );
static int Init ( input_thread_t *p_input );
static void End ( input_thread_t *p_input );
static void MainLoop( input_thread_t *p_input, bool b_interactive );
static inline int ControlPop( input_thread_t *, int *, input_control_param_t *, vlc_tick_t i_deadline, bool b_postpone_seek );
static void ControlRelease( int i_type, const input_control_param_t *p_param );
static bool ControlIsSeekRequest( int i_type );
static bool Control( input_thread_t *, int, input_control_param_t );
static void ControlPause( input_thread_t *, vlc_tick_t );
static int UpdateTitleSeekpointFromDemux( input_thread_t * );
static void UpdateGenericFromDemux( input_thread_t * );
static void UpdateTitleListfromDemux( input_thread_t * );
static void MRLSections( const char *, int *, int *, int *, int *);
static input_source_t *InputSourceNew( input_thread_t *, const char *,
const char *psz_forced_demux,
bool b_in_can_fail );
static void InputSourceDestroy( input_source_t * );
static void InputSourceMeta( input_thread_t *, input_source_t *, vlc_meta_t * );
/* TODO */
//static void InputGetAttachments( input_thread_t *, input_source_t * );
static void SlaveDemux( input_thread_t *p_input );
static void SlaveSeek( input_thread_t *p_input );
static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
static void InputUpdateMeta( input_thread_t *p_input, demux_t *p_demux );
static void InputGetExtraFiles( input_thread_t *p_input,
int *pi_list, char ***pppsz_list,
const char **psz_access, const char *psz_path );
static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
const demux_t ***ppp_attachment_demux,
int i_new, input_attachment_t **pp_new, const demux_t *p_demux );
#define SLAVE_ADD_NOFLAG 0
#define SLAVE_ADD_FORCED (1<<0)
#define SLAVE_ADD_CANFAIL (1<<1)
#define SLAVE_ADD_SET_TIME (1<<2)
static int input_SlaveSourceAdd( input_thread_t *, enum slave_type,
const char *, unsigned );
static char *input_SubtitleFile2Uri( input_thread_t *, const char * );
static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */
#undef input_Create
/**
* Create a new input_thread_t.
*
* You need to call input_Start on it when you are done
* adding callback on the variables/events you want to monitor.
*
* \param p_parent a vlc_object
* \param p_item an input item
* \param p_resource an optional input ressource
* \return a pointer to the spawned input thread
*/
input_thread_t *input_Create( vlc_object_t *p_parent,
input_thread_events_cb events_cb, void *events_data,
input_item_t *p_item,
input_resource_t *p_resource,
vlc_renderer_item_t *p_renderer )
{
return Create( p_parent, events_cb, events_data, p_item,
INPUT_CREATE_OPTION_NONE, p_resource, p_renderer );
}
input_thread_t *input_CreatePreparser( vlc_object_t *parent,
input_thread_events_cb events_cb,
void *events_data, input_item_t *item )
{
return Create( parent, events_cb, events_data, item,
INPUT_CREATE_OPTION_PREPARSING, NULL, NULL );
}
input_thread_t *input_CreateThumbnailer(vlc_object_t *obj,
input_thread_events_cb events_cb,
void *events_data, input_item_t *item)
{
return Create( obj, events_cb, events_data, item,
INPUT_CREATE_OPTION_THUMBNAILING, NULL, NULL );
}
/**
* Start a input_thread_t created by input_Create.
*
* You must not start an already running input_thread_t.
*
* \param the input thread to start
*/
int input_Start( input_thread_t *p_input )
{
input_thread_private_t *priv = input_priv(p_input);
void *(*func)(void *) = Run;
if( priv->b_preparsing )
func = Preparse;
assert( !priv->is_running );
/* Create thread and wait for its readiness. */
priv->is_running = !vlc_clone( &priv->thread, func, priv,
VLC_THREAD_PRIORITY_INPUT );
if( !priv->is_running )
{
msg_Err( p_input, "cannot create input thread" );
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
/**
* Request a running input thread to stop and die
*
* \param p_input the input thread to stop
*/
void input_Stop( input_thread_t *p_input )
{
input_thread_private_t *sys = input_priv(p_input);
vlc_mutex_lock( &sys->lock_control );
/* Discard all pending controls */
for( size_t i = 0; i < sys->i_control; i++ )
{
input_control_t *ctrl = &sys->control[i];
ControlRelease( ctrl->i_type, &ctrl->param );
}
sys->i_control = 0;
sys->is_stopped = true;
vlc_cond_signal( &sys->wait_control );
vlc_mutex_unlock( &sys->lock_control );
vlc_interrupt_kill( &sys->interrupt );
}
/**
* Close an input
*
* It does not call input_Stop itself.
*/
void input_Close( input_thread_t *p_input )
{
if( input_priv(p_input)->is_running )
vlc_join( input_priv(p_input)->thread, NULL );
vlc_interrupt_deinit( &input_priv(p_input)->interrupt );
Destroy(p_input);
}
void input_SetTime( input_thread_t *p_input, vlc_tick_t i_time, bool b_fast )
{
input_control_param_t param;
param.time.i_val = i_time;
param.time.b_fast_seek = b_fast;
input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, ¶m );
}
void input_SetPosition( input_thread_t *p_input, float f_position, bool b_fast )
{
input_control_param_t param;
param.pos.f_val = f_position;
param.pos.b_fast_seek = b_fast;
input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION, ¶m );
}
/**
* Get the item from an input thread
* FIXME it does not increase ref count of the item.
* if it is used after p_input is destroyed nothing prevent it from
* being freed.
*/
input_item_t *input_GetItem( input_thread_t *p_input )
{
assert( p_input != NULL );
return input_priv(p_input)->p_item;
}
/*****************************************************************************
* This function creates a new input, and returns a pointer
* to its description. On error, it returns NULL.
*
* XXX Do not forget to update vlc_input.h if you add new variables.
*****************************************************************************/
static input_thread_t *Create( vlc_object_t *p_parent,
input_thread_events_cb events_cb, void *events_data,
input_item_t *p_item,
enum input_create_option option,
input_resource_t *p_resource,
vlc_renderer_item_t *p_renderer )
{
/* Allocate descriptor */
input_thread_private_t *priv;
priv = vlc_custom_create( p_parent, sizeof( *priv ), "input" );
if( unlikely(priv == NULL) )
return NULL;
input_thread_t *p_input = &priv->input;
char * psz_name = input_item_GetName( p_item );
const char *option_str;
switch (option)
{
case INPUT_CREATE_OPTION_PREPARSING:
option_str = "preparsing ";
break;
case INPUT_CREATE_OPTION_THUMBNAILING:
option_str = "thumbnailing ";
break;
default:
option_str = "";
break;
}
msg_Dbg( p_input, "Creating an input for %s'%s'", option_str, psz_name);
free( psz_name );
/* Parse input options */
input_item_ApplyOptions( VLC_OBJECT(p_input), p_item );
/* Init Common fields */
priv->events_cb = events_cb;
priv->events_data = events_data;
priv->b_preparsing = option == INPUT_CREATE_OPTION_PREPARSING;
priv->b_thumbnailing = option == INPUT_CREATE_OPTION_THUMBNAILING;
priv->b_can_pace_control = true;
priv->i_start = 0;
priv->i_time = 0;
priv->i_stop = 0;
priv->i_title_offset = input_priv(p_input)->i_seekpoint_offset = 0;
priv->i_state = INIT_S;
priv->is_running = false;
priv->is_stopped = false;
priv->b_recording = false;
priv->rate = 1.f;
TAB_INIT( priv->i_attachment, priv->attachment );
priv->attachment_demux = NULL;
priv->p_sout = NULL;
priv->b_out_pace_control = priv->b_thumbnailing;
priv->p_renderer = p_renderer && priv->b_preparsing == false ?
vlc_renderer_item_hold( p_renderer ) : NULL;
priv->viewpoint_changed = false;
/* Fetch the viewpoint from the mediaplayer or the playlist if any */
vlc_viewpoint_t *p_viewpoint = var_InheritAddress( p_input, "viewpoint" );
if (p_viewpoint != NULL)
priv->viewpoint = *p_viewpoint;
else
vlc_viewpoint_init( &priv->viewpoint );
priv->i_last_es_cat = UNKNOWN_ES;
input_item_Hold( p_item ); /* Released in Destructor() */
priv->p_item = p_item;
/* Init Input fields */
priv->master = NULL;
vlc_mutex_lock( &p_item->lock );
if( !p_item->p_stats )
p_item->p_stats = calloc( 1, sizeof(*p_item->p_stats) );
/* setup the preparse depth of the item
* if we are preparsing, use the i_preparse_depth of the parent item */
if( priv->b_preparsing || priv->b_thumbnailing )
{
p_input->obj.logger = NULL;
p_input->obj.no_interact = true;
}
else
{
char *psz_rec = var_InheritString( p_parent, "recursive" );
if( psz_rec != NULL )
{
if ( !strcasecmp( psz_rec, "none" ) )
p_item->i_preparse_depth = 0;
else if ( !strcasecmp( psz_rec, "collapse" ) )
p_item->i_preparse_depth = 1;
else
p_item->i_preparse_depth = -1; /* default is expand */
free (psz_rec);
} else
p_item->i_preparse_depth = -1;
}
/* Make sure the interaction option is honored */
if( !var_InheritBool( p_input, "interact" ) )
p_input->obj.no_interact = true;
else if( p_item->b_preparse_interact )
{
/* If true, this item was asked explicitly to interact with the user
* (via libvlc_MetadataRequest). Sub items created from this input won't
* have this flag and won't interact with the user */
p_input->obj.no_interact = false;
}
vlc_mutex_unlock( &p_item->lock );
/* No slave */
priv->i_slave = 0;
priv->slave = NULL;
/* */
if( p_resource )
priv->p_resource = input_resource_Hold( p_resource );
else
priv->p_resource = input_resource_New( VLC_OBJECT( p_input ) );
input_resource_SetInput( priv->p_resource, p_input );
/* Init control buffer */
vlc_mutex_init( &priv->lock_control );
vlc_cond_init( &priv->wait_control );
priv->i_control = 0;
vlc_interrupt_init(&priv->interrupt);
/* Create Object Variables for private use only */
input_ConfigVarInit( p_input );
/* Remove 'Now playing' info as it is probably outdated */
input_item_SetNowPlaying( p_item, NULL );
input_item_SetESNowPlaying( p_item, NULL );
/* */
if( !priv->b_preparsing && var_InheritBool( p_input, "stats" ) )
priv->stats = input_stats_Create();
else
priv->stats = NULL;
priv->p_es_out_display = input_EsOutNew( p_input, priv->rate );
priv->p_es_out = NULL;
return p_input;
}
static void Destroy(input_thread_t *input)
{
input_thread_private_t *priv = input_priv(input);
#ifndef NDEBUG
char *name = input_item_GetName(priv->p_item);
msg_Dbg(input, "destroying input for '%s'", name);
free(name);
#endif
if (priv->p_renderer != NULL)
vlc_renderer_item_release(priv->p_renderer);
if (priv->p_es_out_display != NULL)
es_out_Delete(priv->p_es_out_display);
if (priv->p_resource != NULL)
input_resource_Release(priv->p_resource);
input_item_Release(priv->p_item);
if (priv->stats != NULL)
input_stats_Destroy(priv->stats);
for (size_t i = 0; i < priv->i_control; i++)
{
input_control_t *ctrl = &priv->control[i];
ControlRelease(ctrl->i_type, &ctrl->param);
}
vlc_cond_destroy(&priv->wait_control);
vlc_mutex_destroy(&priv->lock_control);
vlc_object_delete(VLC_OBJECT(input));
}
/*****************************************************************************
* Run: main thread loop
* This is the "normal" thread that spawns the input processing chain,
* reads the stream, cleans up and waits
*****************************************************************************/
static void *Run( void *data )
{
input_thread_private_t *priv = data;
input_thread_t *p_input = &priv->input;
vlc_interrupt_set(&priv->interrupt);
if( !Init( p_input ) )
{
if( priv->b_can_pace_control && priv->b_out_pace_control )
{
/* We don't want a high input priority here or we'll
* end-up sucking up all the CPU time */
vlc_set_priority( priv->thread, VLC_THREAD_PRIORITY_LOW );
}
MainLoop( p_input, true ); /* FIXME it can be wrong (like with VLM) */
/* Clean up */
End( p_input );
}
input_SendEventDead( p_input );
return NULL;
}
static void *Preparse( void *data )
{
input_thread_private_t *priv = data;
input_thread_t *p_input = &priv->input;
vlc_interrupt_set(&priv->interrupt);
if( !Init( p_input ) )
{ /* if the demux is a playlist, call Mainloop that will call
* demux_Demux in order to fetch sub items */
bool b_is_playlist = false;
if ( input_item_ShouldPreparseSubItems( priv->p_item )
&& demux_Control( priv->master->p_demux, DEMUX_IS_PLAYLIST,
&b_is_playlist ) )
b_is_playlist = false;
if( b_is_playlist )
MainLoop( p_input, false );
End( p_input );
}
input_SendEventDead( p_input );
return NULL;
}
bool input_Stopped( input_thread_t *input )
{
input_thread_private_t *sys = input_priv(input);
bool ret;
vlc_mutex_lock( &sys->lock_control );
ret = sys->is_stopped;
vlc_mutex_unlock( &sys->lock_control );
return ret;
}
/*****************************************************************************
* Main loop: Fill buffers from access, and demux
*****************************************************************************/
/**
* MainLoopDemux
* It asks the demuxer to demux some data
*/
static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed )
{
input_thread_private_t* p_priv = input_priv(p_input);
demux_t *p_demux = p_priv->master->p_demux;
int i_ret = VLC_DEMUXER_SUCCESS;
*pb_changed = false;
if( p_priv->i_stop > 0 )
{
if( demux_Control( p_demux, DEMUX_GET_TIME, &p_priv->i_time ) )
p_priv->i_time = 0;
if( p_priv->i_stop <= p_priv->i_time )
i_ret = VLC_DEMUXER_EOF;
}
if( i_ret == VLC_DEMUXER_SUCCESS )
i_ret = demux_Demux( p_demux );
i_ret = i_ret > 0 ? VLC_DEMUXER_SUCCESS : ( i_ret < 0 ? VLC_DEMUXER_EGENERIC : VLC_DEMUXER_EOF);
if( i_ret == VLC_DEMUXER_SUCCESS )
{
if( demux_TestAndClearFlags( p_demux, INPUT_UPDATE_TITLE_LIST ) )
UpdateTitleListfromDemux( p_input );
if( p_priv->master->b_title_demux )
{
i_ret = UpdateTitleSeekpointFromDemux( p_input );
*pb_changed = true;
}
UpdateGenericFromDemux( p_input );
}
if( i_ret == VLC_DEMUXER_EOF )
{
msg_Dbg( p_input, "EOF reached" );
p_priv->master->b_eof = true;
es_out_Eos(p_priv->p_es_out);
}
else if( i_ret == VLC_DEMUXER_EGENERIC )
{
input_ChangeState( p_input, ERROR_S );
}
else if( p_priv->i_slave > 0 )
SlaveDemux( p_input );
}
static int MainLoopTryRepeat( input_thread_t *p_input )
{
int i_repeat = var_GetInteger( p_input, "input-repeat" );
if( i_repeat <= 0 )
return VLC_EGENERIC;
vlc_value_t val;
msg_Dbg( p_input, "repeating the same input (%d)", i_repeat );
if( i_repeat > 0 )
{
i_repeat--;
var_SetInteger( p_input, "input-repeat", i_repeat );
}
input_thread_private_t *priv = input_priv(p_input);
/* Seek to start title/seekpoint */
val.i_int = priv->master->i_title_start - priv->master->i_title_offset;
if( val.i_int < 0 || val.i_int >= priv->master->i_title )
val.i_int = 0;
input_ControlPushHelper( p_input,
INPUT_CONTROL_SET_TITLE, &val );
val.i_int = priv->master->i_seekpoint_start -
priv->master->i_seekpoint_offset;
if( val.i_int > 0 /* TODO: check upper boundary */ )
input_ControlPushHelper( p_input,
INPUT_CONTROL_SET_SEEKPOINT, &val );
/* Seek to start position */
if( priv->i_start > 0 )
input_SetTime( p_input, priv->i_start, false );
else
input_SetPosition( p_input, 0.0f, false );
return VLC_SUCCESS;
}
/**
* Update timing infos and statistics.
*/
static void MainLoopStatistics( input_thread_t *p_input )
{
input_thread_private_t *priv = input_priv(p_input);
double f_position = 0.0;
vlc_tick_t i_time = 0;
vlc_tick_t i_length = 0;
/* update input status variables */
if( demux_Control( priv->master->p_demux,
DEMUX_GET_POSITION, &f_position ) )
f_position = 0.0;
if( demux_Control( priv->master->p_demux, DEMUX_GET_TIME, &i_time ) )
i_time = 0;
input_priv(p_input)->i_time = i_time;
if( demux_Control( priv->master->p_demux, DEMUX_GET_LENGTH, &i_length ) )
i_length = 0;
es_out_SetTimes( priv->p_es_out, f_position, i_time, i_length );
struct input_stats_t new_stats;
if( priv->stats != NULL )
input_stats_Compute( priv->stats, &new_stats );
vlc_mutex_lock( &priv->p_item->lock );
if( priv->stats != NULL )
*priv->p_item->p_stats = new_stats;
vlc_mutex_unlock( &priv->p_item->lock );
input_SendEventStatistics( p_input, &new_stats );
}
/**
* MainLoop
* The main input loop.
*/
static void MainLoop( input_thread_t *p_input, bool b_interactive )
{
vlc_tick_t i_intf_update = 0;
vlc_tick_t i_last_seek_mdate = 0;
if( b_interactive && var_InheritBool( p_input, "start-paused" ) )
ControlPause( p_input, vlc_tick_now() );
bool b_pause_after_eof = b_interactive &&
var_InheritBool( p_input, "play-and-pause" );
bool b_paused_at_eof = false;
demux_t *p_demux = input_priv(p_input)->master->p_demux;
const bool b_can_demux = p_demux->pf_demux != NULL;
while( !input_Stopped( p_input ) && input_priv(p_input)->i_state != ERROR_S )
{
vlc_tick_t i_wakeup = -1;
bool b_paused = input_priv(p_input)->i_state == PAUSE_S;
/* FIXME if input_priv(p_input)->i_state == PAUSE_S the access/access_demux
* is paused -> this may cause problem with some of them
* The same problem can be seen when seeking while paused */
if( b_paused )
b_paused = !es_out_GetBuffering( input_priv(p_input)->p_es_out )
|| input_priv(p_input)->master->b_eof;
if( !b_paused )
{
if( !input_priv(p_input)->master->b_eof )
{
bool b_force_update = false;
MainLoopDemux( p_input, &b_force_update );
if( b_can_demux )
i_wakeup = es_out_GetWakeup( input_priv(p_input)->p_es_out );
if( b_force_update )
i_intf_update = 0;
b_paused_at_eof = false;
}
else if( !es_out_GetEmpty( input_priv(p_input)->p_es_out ) )
{
msg_Dbg( p_input, "waiting decoder fifos to empty" );
i_wakeup = vlc_tick_now() + INPUT_IDLE_SLEEP;
}
/* Pause after eof only if the input is pausable.
* This way we won't trigger timeshifting for nothing */
else if( b_pause_after_eof && input_priv(p_input)->b_can_pause )
{
if( b_paused_at_eof )
break;
input_control_param_t param;
param.val.i_int = PAUSE_S;
msg_Dbg( p_input, "pausing at EOF (pause after each)");
Control( p_input, INPUT_CONTROL_SET_STATE, param );
b_paused = true;
b_paused_at_eof = true;
}
else
{
if( MainLoopTryRepeat( p_input ) )
break;
}
/* Update interface and statistics */
vlc_tick_t now = vlc_tick_now();
if( now >= i_intf_update )
{
MainLoopStatistics( p_input );
i_intf_update = now + VLC_TICK_FROM_MS(250);
}
}
/* Handle control */
for( ;; )
{
vlc_tick_t i_deadline = i_wakeup;
/* Postpone seeking until ES buffering is complete or at most
* 125 ms. */
bool b_postpone = es_out_GetBuffering( input_priv(p_input)->p_es_out )
&& !input_priv(p_input)->master->b_eof;
if( b_postpone )
{
vlc_tick_t now = vlc_tick_now();
/* Recheck ES buffer level every 20 ms when seeking */
if( now < i_last_seek_mdate + VLC_TICK_FROM_MS(125)
&& (i_deadline < 0 || i_deadline > now + VLC_TICK_FROM_MS(20)) )
i_deadline = now + VLC_TICK_FROM_MS(20);
else
b_postpone = false;
}
int i_type;
input_control_param_t param;
if( ControlPop( p_input, &i_type, ¶m, i_deadline, b_postpone ) )
{
if( b_postpone )
continue;
break; /* Wake-up time reached */
}
#ifndef NDEBUG
msg_Dbg( p_input, "control type=%d", i_type );
#endif
if( Control( p_input, i_type, param ) )
{
if( ControlIsSeekRequest( i_type ) )
i_last_seek_mdate = vlc_tick_now();
i_intf_update = 0;
}
/* Update the wakeup time */
if( i_wakeup != 0 )
i_wakeup = es_out_GetWakeup( input_priv(p_input)->p_es_out );
}
}
}
#ifdef ENABLE_SOUT
static int InitSout( input_thread_t * p_input )
{
input_thread_private_t *priv = input_priv(p_input);
if( priv->b_preparsing )
return VLC_SUCCESS;
/* Find a usable sout and attach it to p_input */
char *psz = var_GetNonEmptyString( p_input, "sout" );
if( priv->p_renderer )
{
/* Keep sout if it comes from a renderer and if the user didn't touch
* the sout config */
bool keep_sout = psz == NULL;
free(psz);
const char *psz_renderer_sout = vlc_renderer_item_sout( priv->p_renderer );
if( asprintf( &psz, "#%s", psz_renderer_sout ) < 0 )
return VLC_ENOMEM;
if( keep_sout )
var_SetBool( p_input, "sout-keep", true );
}
if( psz && strncasecmp( priv->p_item->psz_uri, "vlc:", 4 ) )
{
priv->p_sout = input_resource_RequestSout( priv->p_resource, NULL, psz );
if( priv->p_sout == NULL )
{
input_ChangeState( p_input, ERROR_S );
msg_Err( p_input, "cannot start stream output instance, " \
"aborting" );
free( psz );
return VLC_EGENERIC;
}
}
else
{
input_resource_RequestSout( priv->p_resource, NULL, NULL );
}
free( psz );
return VLC_SUCCESS;
}
#endif
static void InitTitle( input_thread_t * p_input, bool had_titles )
{
input_thread_private_t *priv = input_priv(p_input);
input_source_t *p_master = priv->master;
if( priv->b_preparsing )
return;
vlc_mutex_lock( &priv->p_item->lock );
priv->i_title_offset = p_master->i_title_offset;
priv->i_seekpoint_offset = p_master->i_seekpoint_offset;
/* Global flag */
priv->b_can_pace_control = p_master->b_can_pace_control;
priv->b_can_pause = p_master->b_can_pause;
priv->b_can_rate_control = p_master->b_can_rate_control;
vlc_mutex_unlock( &priv->p_item->lock );
/* Send event only if the count is valid or if titles are gone */
if (had_titles || p_master->i_title > 0)
input_SendEventTitle( p_input, &(struct vlc_input_event_title) {
.action = VLC_INPUT_TITLE_NEW_LIST,
.list = {
.array = p_master->title,
.count = p_master->i_title,
},
});
}
static void StartTitle( input_thread_t * p_input )
{
input_thread_private_t *priv = input_priv(p_input);
vlc_value_t val;
/* Start title/chapter */
val.i_int = priv->master->i_title_start - priv->master->i_title_offset;
if( val.i_int > 0 && val.i_int < priv->master->i_title )
input_ControlPushHelper( p_input, INPUT_CONTROL_SET_TITLE, &val );
val.i_int = priv->master->i_seekpoint_start -
priv->master->i_seekpoint_offset;
if( val.i_int > 0 /* TODO: check upper boundary */ )
input_ControlPushHelper( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
/* Start/stop/run time */
priv->i_start = llroundf((float)CLOCK_FREQ
* var_GetFloat( p_input, "start-time" ));
priv->i_stop = llroundf((float)CLOCK_FREQ
* var_GetFloat( p_input, "stop-time" ));
if( priv->i_stop <= 0 )
{
priv->i_stop = llroundf((float)CLOCK_FREQ
* var_GetFloat( p_input, "run-time" ));
if( priv->i_stop < 0 )
{
msg_Warn( p_input, "invalid run-time ignored" );
priv->i_stop = 0;
}
else
priv->i_stop += priv->i_start;
}
if( priv->i_start > 0 )
{
msg_Dbg( p_input, "starting at time: %"PRId64"s",
SEC_FROM_VLC_TICK(priv->i_start) );
input_SetTime( p_input, priv->i_start, false );
}
if( priv->i_stop > 0 && priv->i_stop <= priv->i_start )
{
msg_Warn( p_input, "invalid stop-time ignored" );
priv->i_stop = 0;
}
}
static int SlaveCompare(const void *a, const void *b)
{
const input_item_slave_t *p_slave0 = *((const input_item_slave_t **) a);
const input_item_slave_t *p_slave1 = *((const input_item_slave_t **) b);
if( p_slave0 == NULL || p_slave1 == NULL )
{
/* Put NULL (or rejected) subs at the end */
return p_slave0 == NULL ? 1 : p_slave1 == NULL ? -1 : 0;
}
if( p_slave0->i_priority > p_slave1->i_priority )
return -1;
if( p_slave0->i_priority < p_slave1->i_priority )
return 1;
return 0;
}
static bool SlaveExists( input_item_slave_t **pp_slaves, int i_slaves,
const char *psz_uri)
{
for( int i = 0; i < i_slaves; i++ )
{
if( pp_slaves[i] != NULL
&& !strcmp( pp_slaves[i]->psz_uri, psz_uri ) )
return true;
}
return false;
}
static void RequestSubRate( input_thread_t *p_input, float f_slave_fps )
{
input_thread_private_t *priv = input_priv(p_input);
const float f_fps = input_priv(p_input)->master->f_fps;
if( f_fps > 1.f && f_slave_fps > 1.f )
priv->slave_subs_rate = f_fps / f_slave_fps;
else if ( priv->slave_subs_rate != 0 )
priv->slave_subs_rate = 1.f;
}
static void SetSubtitlesOptions( input_thread_t *p_input )
{
/* Get fps and set it if not already set */
const float f_fps = input_priv(p_input)->master->f_fps;
if( f_fps > 1.f )
{
var_SetFloat( p_input, "sub-original-fps", f_fps );
RequestSubRate( p_input, var_InheritFloat( p_input, "sub-fps" ) );
}
}
static void GetVarSlaves( input_thread_t *p_input,
input_item_slave_t ***ppp_slaves, int *p_slaves )
{
char *psz = var_GetNonEmptyString( p_input, "input-slave" );
if( !psz )
return;
input_item_slave_t **pp_slaves = *ppp_slaves;
int i_slaves = *p_slaves;
char *psz_org = psz;
while( psz && *psz )
{
while( *psz == ' ' || *psz == '#' )
psz++;
char *psz_delim = strchr( psz, '#' );
if( psz_delim )
*psz_delim++ = '\0';
if( *psz == 0 )
break;
char *uri = strstr(psz, "://")
? strdup( psz ) : vlc_path2uri( psz, NULL );
psz = psz_delim;
if( uri == NULL )
continue;
input_item_slave_t *p_slave =
input_item_slave_New( uri, SLAVE_TYPE_AUDIO, SLAVE_PRIORITY_USER );
free( uri );
if( unlikely( p_slave == NULL ) )
break;
TAB_APPEND(i_slaves, pp_slaves, p_slave);
}
free( psz_org );
*ppp_slaves = pp_slaves; /* in case of realloc */
*p_slaves = i_slaves;
}
static void LoadSlaves( input_thread_t *p_input )
{
input_item_slave_t **pp_slaves;
int i_slaves;
TAB_INIT( i_slaves, pp_slaves );
/* Look for and add slaves */
char *psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
if( psz_subtitle != NULL )
{
msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );