forked from xbmc/xbmc
/
VideoPlayer.cpp
5135 lines (4440 loc) · 156 KB
/
VideoPlayer.cpp
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
/*
* Copyright (C) 2005-2015 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kodi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "VideoPlayer.h"
#include "VideoPlayerRadioRDS.h"
#include "system.h"
#include "DVDInputStreams/DVDInputStream.h"
#include "DVDInputStreams/DVDFactoryInputStream.h"
#include "DVDInputStreams/DVDInputStreamNavigator.h"
#if defined(HAVE_LIBBLURAY)
#include "DVDInputStreams/DVDInputStreamBluray.h"
#endif
#include "DVDInputStreams/DVDInputStreamPVRManager.h"
#include "DVDDemuxers/DVDDemux.h"
#include "DVDDemuxers/DVDDemuxUtils.h"
#include "DVDDemuxers/DVDDemuxVobsub.h"
#include "DVDDemuxers/DVDFactoryDemuxer.h"
#include "DVDDemuxers/DVDDemuxFFmpeg.h"
#include "DVDFileInfo.h"
#include "utils/LangCodeExpander.h"
#include "input/Key.h"
#include "guilib/LocalizeStrings.h"
#include "utils/URIUtils.h"
#include "GUIInfoManager.h"
#include "cores/DataCacheCore.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/StereoscopicsManager.h"
#include "Application.h"
#include "ServiceBroker.h"
#include "messaging/ApplicationMessenger.h"
#include "DVDDemuxers/DVDDemuxCC.h"
#include "cores/FFmpeg.h"
#include "cores/VideoPlayer/VideoRenderers/RenderManager.h"
#include "cores/VideoPlayer/Process/ProcessInfo.h"
#include "settings/AdvancedSettings.h"
#include "FileItem.h"
#include "GUIUserMessages.h"
#include "settings/Settings.h"
#include "settings/MediaSettings.h"
#include "utils/log.h"
#include "utils/StreamDetails.h"
#include "utils/StreamUtils.h"
#include "utils/Variant.h"
#include "storage/MediaManager.h"
#include "dialogs/GUIDialogKaiToast.h"
#include "utils/JobManager.h"
#include "utils/StringUtils.h"
#include "video/Bookmark.h"
#include "Util.h"
#include "LangInfo.h"
#include "URL.h"
#ifdef TARGET_RASPBERRY_PI
#include "cores/omxplayer/OMXPlayerAudio.h"
#include "cores/omxplayer/OMXPlayerVideo.h"
#include "cores/omxplayer/OMXHelper.h"
#endif
#include "VideoPlayerAudio.h"
#include "windowing/WinSystem.h"
#include "DVDCodecs/DVDCodecUtils.h"
#include "filesystem/SpecialProtocol.h"
#include <iterator>
using namespace PVR;
using namespace KODI::MESSAGING;
//#define DEBUG_PLAYBACK
static void dump_packet(DemuxPacket *packet, bool video, bool audio)
{
static CCriticalSection m_section;
static FILE *fp_video, *fp_audio;
if ((!video || !g_advancedSettings.CanLogComponent(LOGDUMPVIDEO)) &&
(!audio || !g_advancedSettings.CanLogComponent(LOGDUMPAUDIO)))
return;
const char *fname = video ? "video.dat" : "audio.dat";
FILE *&fp = video ? fp_video : fp_audio;
CSingleLock lock(m_section);
if (!packet)
{
if (fp)
{
CLog::Log(LOGNOTICE, "%s:: Closing file %p", __func__, fp);
fclose(fp);
fp = NULL;
}
return;
}
if (!fp)
{
char filename[1024];
strcpy(filename, CSpecialProtocol::TranslatePath("special://logpath").c_str());
strcat(filename, fname);
#ifdef DEBUG_PLAYBACK
fp = fopen(filename, "rb");
#else
fp = fopen(filename, "wb");
#endif
CLog::Log(LOGNOTICE, "%s:: Opening file %s = %p", __func__, filename, fp);
}
if (fp)
{
#ifdef DEBUG_PLAYBACK
DemuxPacket p = {0};
int s = fread(&p, sizeof p, 1, fp);
if (s==1)
{
packet->iSize = p.iSize;
packet->dts = p.dts;
packet->pts = p.pts;
_aligned_free(packet->pData);
packet->pData = (uint8_t*)_aligned_malloc(packet->iSize + FF_INPUT_BUFFER_PADDING_SIZE, 16);
fread(packet->pData, packet->iSize, 1, fp);
}
#else
if (fwrite(packet, sizeof *packet, 1, fp) == 1)
fwrite(packet->pData, packet->iSize, 1, fp);
#endif
}
}
//------------------------------------------------------------------------------
// selection streams
//------------------------------------------------------------------------------
#define PREDICATE_RETURN(lh, rh) \
do { \
if((lh) != (rh)) \
return (lh) > (rh); \
} while(0)
class PredicateSubtitleFilter
{
private:
std::string audiolang;
bool original;
bool nosub;
bool onlyforced;
int currentSubStream;
public:
/** \brief The class' operator() decides if the given (subtitle) SelectionStream is relevant wrt.
* preferred subtitle language and audio language. If the subtitle is relevant <B>false</B> false is returned.
*
* A subtitle is relevant if
* - it was previously selected, or
* - it's an external sub, or
* - it's a forced sub and "original stream's language" was selected and audio stream language matches, or
* - it's a default and a forced sub (could lead to users seeing forced subs in a foreign language!), or
* - its language matches the preferred subtitle's language (unequal to "original stream's language")
*/
explicit PredicateSubtitleFilter(const std::string& lang, int subStream)
: audiolang(lang),
original(StringUtils::EqualsNoCase(CServiceBroker::GetSettings().GetString(CSettings::SETTING_LOCALE_SUBTITLELANGUAGE), "original")),
nosub(StringUtils::EqualsNoCase(CServiceBroker::GetSettings().GetString(CSettings::SETTING_LOCALE_SUBTITLELANGUAGE), "none")),
onlyforced(StringUtils::EqualsNoCase(CServiceBroker::GetSettings().GetString(CSettings::SETTING_LOCALE_SUBTITLELANGUAGE), "forced_only")),
currentSubStream(subStream)
{
};
bool operator()(const SelectionStream& ss) const
{
if (ss.type_index == currentSubStream)
return false;
if (nosub)
return true;
if (onlyforced)
{
if ((ss.flags & StreamFlags::FLAG_FORCED) && g_LangCodeExpander.CompareISO639Codes(ss.language, audiolang))
return false;
else
return true;
}
if(STREAM_SOURCE_MASK(ss.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(ss.source) == STREAM_SOURCE_TEXT)
return false;
if ((ss.flags & StreamFlags::FLAG_FORCED) && g_LangCodeExpander.CompareISO639Codes(ss.language, audiolang))
return false;
if ((ss.flags & StreamFlags::FLAG_FORCED) && (ss.flags & StreamFlags::FLAG_DEFAULT))
return false;
if(!original)
{
std::string subtitle_language = g_langInfo.GetSubtitleLanguage();
if (g_LangCodeExpander.CompareISO639Codes(subtitle_language, ss.language))
return false;
}
else if (ss.flags & StreamFlags::FLAG_DEFAULT)
return false;
return true;
}
};
class PredicateAudioFilter
{
private:
int currentAudioStream;
public:
explicit PredicateAudioFilter(int audioStream) : currentAudioStream(audioStream)
{
};
bool operator()(const SelectionStream& lh, const SelectionStream& rh)
{
PREDICATE_RETURN(lh.type_index == currentAudioStream
, rh.type_index == currentAudioStream);
if (!StringUtils::EqualsNoCase(CServiceBroker::GetSettings().GetString(CSettings::SETTING_LOCALE_AUDIOLANGUAGE), "original"))
{
std::string audio_language = g_langInfo.GetAudioLanguage();
PREDICATE_RETURN(g_LangCodeExpander.CompareISO639Codes(audio_language, lh.language)
, g_LangCodeExpander.CompareISO639Codes(audio_language, rh.language));
bool hearingimp = CServiceBroker::GetSettings().GetBool(CSettings::SETTING_ACCESSIBILITY_AUDIOHEARING);
PREDICATE_RETURN(!hearingimp ? !(lh.flags & StreamFlags::FLAG_HEARING_IMPAIRED) : lh.flags & StreamFlags::FLAG_HEARING_IMPAIRED
, !hearingimp ? !(rh.flags & StreamFlags::FLAG_HEARING_IMPAIRED) : rh.flags & StreamFlags::FLAG_HEARING_IMPAIRED);
bool visualimp = CServiceBroker::GetSettings().GetBool(CSettings::SETTING_ACCESSIBILITY_AUDIOVISUAL);
PREDICATE_RETURN(!visualimp ? !(lh.flags & StreamFlags::FLAG_VISUAL_IMPAIRED) : lh.flags & StreamFlags::FLAG_VISUAL_IMPAIRED
, !visualimp ? !(rh.flags & StreamFlags::FLAG_VISUAL_IMPAIRED) : rh.flags & StreamFlags::FLAG_VISUAL_IMPAIRED);
}
if (CServiceBroker::GetSettings().GetBool(CSettings::SETTING_VIDEOPLAYER_PREFERDEFAULTFLAG))
{
PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT,
rh.flags & StreamFlags::FLAG_DEFAULT);
}
PREDICATE_RETURN(lh.channels,
rh.channels);
PREDICATE_RETURN(StreamUtils::GetCodecPriority(lh.codec),
StreamUtils::GetCodecPriority(rh.codec));
PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT,
rh.flags & StreamFlags::FLAG_DEFAULT);
return false;
};
};
/** \brief The class' operator() decides if the given (subtitle) SelectionStream lh is 'better than' the given (subtitle) SelectionStream rh.
* If lh is 'better than' rh the return value is true, false otherwise.
*
* A subtitle lh is 'better than' a subtitle rh (in evaluation order) if
* - lh was previously selected, or
* - lh is an external sub and rh not, or
* - lh is a forced sub and ("original stream's language" was selected or subtitles are off) and audio stream language matches sub language and rh not, or
* - lh is a default sub and ("original stream's language" was selected or subtitles are off) and audio stream language matches sub language and rh not, or
* - lh is a sub where audio stream language matches sub language and (original stream's language" was selected or subtitles are off) and rh not, or
* - lh is a forced sub and a default sub ("original stream's language" was selected or subtitles are off)
* - lh is an external sub and its language matches the preferred subtitle's language (unequal to "original stream's language") and rh not, or
* - lh is language matches the preferred subtitle's language (unequal to "original stream's language") and rh not, or
* - lh is a default sub and rh not
*/
class PredicateSubtitlePriority
{
private:
std::string audiolang;
bool original;
bool subson;
PredicateSubtitleFilter filter;
int subStream;
public:
explicit PredicateSubtitlePriority(const std::string& lang, int stream, bool ison)
: audiolang(lang),
original(StringUtils::EqualsNoCase(CServiceBroker::GetSettings().GetString(CSettings::SETTING_LOCALE_SUBTITLELANGUAGE), "original")),
subson(ison),
filter(lang, stream),
subStream(stream)
{
};
bool relevant(const SelectionStream& ss) const
{
return !filter(ss);
}
bool operator()(const SelectionStream& lh, const SelectionStream& rh) const
{
PREDICATE_RETURN(relevant(lh)
, relevant(rh));
PREDICATE_RETURN(lh.type_index == subStream
, rh.type_index == subStream);
// prefer external subs
PREDICATE_RETURN(STREAM_SOURCE_MASK(lh.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(lh.source) == STREAM_SOURCE_TEXT
, STREAM_SOURCE_MASK(rh.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(rh.source) == STREAM_SOURCE_TEXT);
if (!subson || original)
{
PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_FORCED && g_LangCodeExpander.CompareISO639Codes(lh.language, audiolang)
, rh.flags & StreamFlags::FLAG_FORCED && g_LangCodeExpander.CompareISO639Codes(rh.language, audiolang));
PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT && g_LangCodeExpander.CompareISO639Codes(lh.language, audiolang)
, rh.flags & StreamFlags::FLAG_DEFAULT && g_LangCodeExpander.CompareISO639Codes(rh.language, audiolang));
PREDICATE_RETURN(g_LangCodeExpander.CompareISO639Codes(lh.language, audiolang)
, g_LangCodeExpander.CompareISO639Codes(rh.language, audiolang));
PREDICATE_RETURN(lh.flags & (StreamFlags::FLAG_FORCED && StreamFlags::FLAG_DEFAULT)
, rh.flags & (StreamFlags::FLAG_FORCED && StreamFlags::FLAG_DEFAULT));
}
std::string subtitle_language = g_langInfo.GetSubtitleLanguage();
if (!original)
{
PREDICATE_RETURN((STREAM_SOURCE_MASK(lh.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(lh.source) == STREAM_SOURCE_TEXT) && g_LangCodeExpander.CompareISO639Codes(subtitle_language, lh.language)
, (STREAM_SOURCE_MASK(rh.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(rh.source) == STREAM_SOURCE_TEXT) && g_LangCodeExpander.CompareISO639Codes(subtitle_language, rh.language));
}
if (!original)
{
PREDICATE_RETURN(g_LangCodeExpander.CompareISO639Codes(subtitle_language, lh.language)
, g_LangCodeExpander.CompareISO639Codes(subtitle_language, rh.language));
bool hearingimp = CServiceBroker::GetSettings().GetBool(CSettings::SETTING_ACCESSIBILITY_SUBHEARING);
PREDICATE_RETURN(!hearingimp ? !(lh.flags & StreamFlags::FLAG_HEARING_IMPAIRED) : lh.flags & StreamFlags::FLAG_HEARING_IMPAIRED
, !hearingimp ? !(rh.flags & StreamFlags::FLAG_HEARING_IMPAIRED) : rh.flags & StreamFlags::FLAG_HEARING_IMPAIRED);
}
PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT
, rh.flags & StreamFlags::FLAG_DEFAULT);
return false;
}
};
class PredicateVideoFilter
{
private:
int currentVideoStream;
public:
explicit PredicateVideoFilter(int videoStream) : currentVideoStream(videoStream)
{
};
bool operator()(const SelectionStream& lh, const SelectionStream& rh)
{
PREDICATE_RETURN(lh.type_index == currentVideoStream,
rh.type_index == currentVideoStream);
PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT,
rh.flags & StreamFlags::FLAG_DEFAULT);
return false;
}
};
void CSelectionStreams::Clear(StreamType type, StreamSource source)
{
auto new_end = std::remove_if(m_Streams.begin(), m_Streams.end(),
[type, source](const SelectionStream &stream)
{
return (type == STREAM_NONE || stream.type == type) &&
(source == 0 || stream.source == source);
});
m_Streams.erase(new_end, m_Streams.end());
}
SelectionStream& CSelectionStreams::Get(StreamType type, int index)
{
int count = -1;
for(size_t i=0;i<m_Streams.size();i++)
{
if(m_Streams[i].type != type)
continue;
count++;
if(count == index)
return m_Streams[i];
}
return m_invalid;
}
std::vector<SelectionStream> CSelectionStreams::Get(StreamType type)
{
std::vector<SelectionStream> streams;
std::copy_if(m_Streams.begin(), m_Streams.end(), std::back_inserter(streams),
[type](const SelectionStream &stream)
{
return stream.type == type;
});
return streams;
}
bool CSelectionStreams::Get(StreamType type, StreamFlags flag, SelectionStream& out)
{
for(size_t i=0;i<m_Streams.size();i++)
{
if(m_Streams[i].type != type)
continue;
if((m_Streams[i].flags & flag) != flag)
continue;
out = m_Streams[i];
return true;
}
return false;
}
int CSelectionStreams::IndexOf(StreamType type, int source, int64_t demuxerId, int id) const
{
int count = -1;
for(size_t i=0;i<m_Streams.size();i++)
{
if(type && m_Streams[i].type != type)
continue;
count++;
if(source && m_Streams[i].source != source)
continue;
if(id < 0)
continue;
if(m_Streams[i].id == id && m_Streams[i].demuxerId == demuxerId)
return count;
}
if(id < 0)
return count;
else
return -1;
}
int CSelectionStreams::Source(StreamSource source, std::string filename)
{
int index = source - 1;
for (size_t i=0; i<m_Streams.size(); i++)
{
SelectionStream &s = m_Streams[i];
if (STREAM_SOURCE_MASK(s.source) != source)
continue;
// if it already exists, return same
if (s.filename == filename)
return s.source;
if (index < s.source)
index = s.source;
}
// return next index
return index + 1;
}
void CSelectionStreams::Update(SelectionStream& s)
{
int index = IndexOf(s.type, s.source, s.demuxerId, s.id);
if(index >= 0)
{
SelectionStream& o = Get(s.type, index);
s.type_index = o.type_index;
o = s;
}
else
{
s.type_index = Count(s.type);
m_Streams.push_back(s);
}
}
void CSelectionStreams::Update(std::shared_ptr<CDVDInputStream> input, CDVDDemux* demuxer, std::string filename2)
{
if(input && input->IsStreamType(DVDSTREAM_TYPE_DVD))
{
std::shared_ptr<CDVDInputStreamNavigator> nav = std::static_pointer_cast<CDVDInputStreamNavigator>(input);
std::string filename = nav->GetFileName();
int source = Source(STREAM_SOURCE_NAV, filename);
int count;
count = nav->GetAudioStreamCount();
for(int i=0;i<count;i++)
{
SelectionStream s;
s.source = source;
s.type = STREAM_AUDIO;
s.id = i;
s.flags = StreamFlags::FLAG_NONE;
s.filename = filename;
AudioStreamInfo info = nav->GetAudioStreamInfo(i);
s.name = info.name;
s.language = g_LangCodeExpander.ConvertToISO6392B(info.language);
s.channels = info.channels;
s.flags = info.flags;
Update(s);
}
count = nav->GetSubTitleStreamCount();
for(int i=0;i<count;i++)
{
SelectionStream s;
s.source = source;
s.type = STREAM_SUBTITLE;
s.id = i;
s.filename = filename;
s.channels = 0;
SubtitleStreamInfo info = nav->GetSubtitleStreamInfo(i);
s.name = info.name;
s.flags = info.flags;
s.language = g_LangCodeExpander.ConvertToISO6392B(info.language);
Update(s);
}
VideoStreamInfo info = nav->GetVideoStreamInfo();
for (int i = 1; i <= info.angles; i++)
{
SelectionStream s;
s.source = source;
s.type = STREAM_VIDEO;
s.id = i;
s.flags = StreamFlags::FLAG_NONE;
s.filename = filename;
s.channels = 0;
s.aspect_ratio = info.videoAspectRatio;
s.width = info.width;
s.height = info.height;
s.codec = info.codecName;
s.name = StringUtils::Format("%s %i", g_localizeStrings.Get(38032).c_str(), i);
Update(s);
}
}
else if(demuxer)
{
std::string filename = demuxer->GetFileName();
int source;
if(input) /* hack to know this is sub decoder */
source = Source(STREAM_SOURCE_DEMUX, filename);
else if (!filename2.empty())
source = Source(STREAM_SOURCE_DEMUX_SUB, filename);
else
source = Source(STREAM_SOURCE_VIDEOMUX, filename);
for (auto stream : demuxer->GetStreams())
{
/* skip streams with no type */
if (stream->type == STREAM_NONE)
continue;
/* make sure stream is marked with right source */
stream->source = source;
SelectionStream s;
s.source = source;
s.type = stream->type;
s.id = stream->uniqueId;
s.demuxerId = stream->demuxerId;
s.language = g_LangCodeExpander.ConvertToISO6392B(stream->language);
s.flags = stream->flags;
s.filename = demuxer->GetFileName();
s.filename2 = filename2;
s.name = stream->GetStreamName();
s.codec = demuxer->GetStreamCodecName(stream->demuxerId, stream->uniqueId);
s.channels = 0; // Default to 0. Overwrite if STREAM_AUDIO below.
if(stream->type == STREAM_VIDEO)
{
CDemuxStreamVideo* vstream = static_cast<CDemuxStreamVideo*>(stream);
s.width = vstream->iWidth;
s.height = vstream->iHeight;
s.stereo_mode = vstream->stereo_mode;
s.bitrate = vstream->iBitRate;
}
if(stream->type == STREAM_AUDIO)
{
std::string type;
type = static_cast<CDemuxStreamAudio*>(stream)->GetStreamType();
if(type.length() > 0)
{
if(s.name.length() > 0)
s.name += " - ";
s.name += type;
}
s.channels = static_cast<CDemuxStreamAudio*>(stream)->iChannels;
s.bitrate = static_cast<CDemuxStreamAudio*>(stream)->iBitRate;
}
Update(s);
}
}
CServiceBroker::GetDataCacheCore().SignalAudioInfoChange();
CServiceBroker::GetDataCacheCore().SignalVideoInfoChange();
}
void CSelectionStreams::Update(std::shared_ptr<CDVDInputStream> input, CDVDDemux* demuxer)
{
Update(input, demuxer, "");
}
int CSelectionStreams::CountSource(StreamType type, StreamSource source) const
{
int count = 0;
for(size_t i=0;i<m_Streams.size();i++)
{
if(type && m_Streams[i].type != type)
continue;
if (source && m_Streams[i].source != source)
continue;
count++;
}
return count;
}
int CSelectionStreams::Count(StreamType type) const
{
return IndexOf(type, STREAM_SOURCE_NONE, -1, -1) + 1;
}
//------------------------------------------------------------------------------
// main class
//------------------------------------------------------------------------------
void CVideoPlayer::CreatePlayers()
{
#ifdef TARGET_RASPBERRY_PI
bool omx_suitable = !OMXPlayerUnsuitable(m_HasVideo, m_HasAudio, m_pDemuxer, m_pInputStream, m_SelectionStreams);
if (m_omxplayer_mode != omx_suitable)
{
DestroyPlayers();
m_omxplayer_mode = omx_suitable;
}
#endif
if (m_players_created)
return;
if (m_omxplayer_mode)
{
#ifdef TARGET_RASPBERRY_PI
m_VideoPlayerVideo = new OMXPlayerVideo(&m_OmxPlayerState.av_clock, &m_overlayContainer, m_messenger, m_renderManager, *m_processInfo);
m_VideoPlayerAudio = new OMXPlayerAudio(&m_OmxPlayerState.av_clock, m_messenger, *m_processInfo);
#endif
}
else
{
m_VideoPlayerVideo = new CVideoPlayerVideo(&m_clock, &m_overlayContainer, m_messenger, m_renderManager, *m_processInfo);
m_VideoPlayerAudio = new CVideoPlayerAudio(&m_clock, m_messenger, *m_processInfo);
}
m_VideoPlayerSubtitle = new CVideoPlayerSubtitle(&m_overlayContainer, *m_processInfo);
m_VideoPlayerTeletext = new CDVDTeletextData(*m_processInfo);
m_VideoPlayerRadioRDS = new CDVDRadioRDSData(*m_processInfo);
m_players_created = true;
}
void CVideoPlayer::DestroyPlayers()
{
if (!m_players_created)
return;
delete m_VideoPlayerVideo;
delete m_VideoPlayerAudio;
delete m_VideoPlayerSubtitle;
delete m_VideoPlayerTeletext;
delete m_VideoPlayerRadioRDS;
m_players_created = false;
}
CVideoPlayer::CVideoPlayer(IPlayerCallback& callback)
: IPlayer(callback),
CThread("VideoPlayer"),
m_CurrentAudio(STREAM_AUDIO, VideoPlayer_AUDIO),
m_CurrentVideo(STREAM_VIDEO, VideoPlayer_VIDEO),
m_CurrentSubtitle(STREAM_SUBTITLE, VideoPlayer_SUBTITLE),
m_CurrentTeletext(STREAM_TELETEXT, VideoPlayer_TELETEXT),
m_CurrentRadioRDS(STREAM_RADIO_RDS, VideoPlayer_RDS),
m_messenger("player"),
m_renderManager(m_clock, this)
{
m_outboundEvents.reset(new CJobQueue(false, 1, CJob::PRIORITY_NORMAL));
m_players_created = false;
m_pDemuxer = nullptr;
m_pSubtitleDemuxer = nullptr;
m_pCCDemuxer = nullptr;
m_pInputStream = nullptr;
m_dvd.Clear();
m_State.Clear();
m_bAbortRequest = false;
m_errorCount = 0;
m_offset_pts = 0.0;
m_playSpeed = DVD_PLAYSPEED_NORMAL;
m_streamPlayerSpeed = DVD_PLAYSPEED_NORMAL;
m_canTempo = false;
m_caching = CACHESTATE_DONE;
m_HasVideo = false;
m_HasAudio = false;
memset(&m_SpeedState, 0, sizeof(m_SpeedState));
// omxplayer variables
m_OmxPlayerState.last_check_time = 0;
m_OmxPlayerState.stamp = 0.0;
m_OmxPlayerState.bOmxWaitVideo = false;
m_OmxPlayerState.bOmxWaitAudio = false;
m_OmxPlayerState.bOmxSentEOFs = false;
m_OmxPlayerState.threshold = 0.2f;
m_OmxPlayerState.interlace_method = VS_INTERLACEMETHOD_MAX;
#ifdef TARGET_RASPBERRY_PI
m_omxplayer_mode = CServiceBroker::GetSettings().GetBool(CSettings::SETTING_VIDEOPLAYER_USEOMXPLAYER);
#else
m_omxplayer_mode = false;
#endif
m_SkipCommercials = true;
m_processInfo.reset(CProcessInfo::CreateInstance());
// if we have a gui, register the cache
m_processInfo->SetDataCache(&CServiceBroker::GetDataCacheCore());
m_processInfo->SetSpeed(1.0);
m_processInfo->SetTempo(1.0);
CreatePlayers();
m_displayLost = false;
m_error = false;
CServiceBroker::GetWinSystem().Register(this);
}
CVideoPlayer::~CVideoPlayer()
{
CServiceBroker::GetWinSystem().Unregister(this);
CloseFile();
DestroyPlayers();
while (m_outboundEvents->IsProcessing())
{
Sleep(10);
}
}
bool CVideoPlayer::OpenFile(const CFileItem& file, const CPlayerOptions &options)
{
CLog::Log(LOGNOTICE, "VideoPlayer::OpenFile: %s", CURL::GetRedacted(file.GetPath()).c_str());
if (m_omxplayer_mode && IsRunning())
CloseFile();
if (IsRunning())
{
CDVDMsgOpenFile::FileParams params;
params.m_item = file;
params.m_options = options;
params.m_item.SetMimeTypeForInternetFile();
m_messenger.Put(new CDVDMsgOpenFile(params), 1);
return true;
}
m_item = file;
m_callback.OnPlayBackStarted(m_item);
m_playerOptions = options;
// Try to resolve the correct mime type
m_item.SetMimeTypeForInternetFile();
m_bAbortRequest = false;
m_error = false;
m_renderManager.PreInit();
Create();
return true;
}
bool CVideoPlayer::CloseFile(bool reopen)
{
CLog::Log(LOGNOTICE, "CVideoPlayer::CloseFile()");
// set the abort request so that other threads can finish up
m_bAbortRequest = true;
// tell demuxer to abort
if(m_pDemuxer)
m_pDemuxer->Abort();
if(m_pSubtitleDemuxer)
m_pSubtitleDemuxer->Abort();
if(m_pInputStream)
m_pInputStream->Abort();
CLog::Log(LOGNOTICE, "VideoPlayer: waiting for threads to exit");
// wait for the main thread to finish up
// since this main thread cleans up all other resources and threads
// we are done after the StopThread call
{
CSingleExit exitlock(g_graphicsContext);
StopThread();
}
m_Edl.Clear();
m_HasVideo = false;
m_HasAudio = false;
m_canTempo = false;
CLog::Log(LOGNOTICE, "VideoPlayer: finished waiting");
m_renderManager.UnInit();
return true;
}
bool CVideoPlayer::IsPlaying() const
{
return !m_bStop;
}
void CVideoPlayer::OnStartup()
{
m_CurrentVideo.Clear();
m_CurrentAudio.Clear();
m_CurrentSubtitle.Clear();
m_CurrentTeletext.Clear();
m_CurrentRadioRDS.Clear();
m_messenger.Init();
CUtil::ClearTempFonts();
}
bool CVideoPlayer::OpenInputStream()
{
if (m_pInputStream.use_count() > 1)
throw std::runtime_error("m_pInputStream reference count is greater than 1");
m_pInputStream.reset();
CLog::Log(LOGNOTICE, "Creating InputStream");
// correct the filename if needed
std::string filename(m_item.GetPath());
if (URIUtils::IsProtocol(filename, "dvd") ||
StringUtils::EqualsNoCase(filename, "iso9660://video_ts/video_ts.ifo"))
{
m_item.SetPath(g_mediaManager.TranslateDevicePath(""));
}
m_pInputStream = CDVDFactoryInputStream::CreateInputStream(this, m_item, true);
if(m_pInputStream == NULL)
{
CLog::Log(LOGERROR, "CVideoPlayer::OpenInputStream - unable to create input stream for [%s]", CURL::GetRedacted(m_item.GetPath()).c_str());
return false;
}
if (!m_pInputStream->Open())
{
CLog::Log(LOGERROR, "CVideoPlayer::OpenInputStream - error opening [%s]", CURL::GetRedacted(m_item.GetPath()).c_str());
return false;
}
// find any available external subtitles for non dvd files
if (!m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD)
&& !m_pInputStream->IsStreamType(DVDSTREAM_TYPE_PVRMANAGER)
&& !m_pInputStream->IsStreamType(DVDSTREAM_TYPE_TV))
{
// find any available external subtitles
std::vector<std::string> filenames;
CUtil::ScanForExternalSubtitles(m_item.GetPath(), filenames);
// load any subtitles from file item
std::string key("subtitle:1");
for(unsigned s = 1; m_item.HasProperty(key); key = StringUtils::Format("subtitle:%u", ++s))
filenames.push_back(m_item.GetProperty(key).asString());
for(unsigned int i=0;i<filenames.size();i++)
{
// if vobsub subtitle:
if (URIUtils::HasExtension(filenames[i], ".idx"))
{
std::string strSubFile;
if ( CUtil::FindVobSubPair( filenames, filenames[i], strSubFile ) )
AddSubtitleFile(filenames[i], strSubFile);
}
else
{
if ( !CUtil::IsVobSub(filenames, filenames[i] ) )
{
AddSubtitleFile(filenames[i]);
}
}
} // end loop over all subtitle files
}
m_clock.Reset();
m_dvd.Clear();
m_errorCount = 0;
if (CServiceBroker::GetSettings().GetBool(CSettings::SETTING_VIDEOPLAYER_USEDISPLAYASCLOCK) &&
!m_pInputStream->IsRealtime())
{
m_canTempo = true;
}
else
{
m_canTempo = false;
}
return true;
}
bool CVideoPlayer::OpenDemuxStream()
{
CloseDemuxer();
CLog::Log(LOGNOTICE, "Creating Demuxer");
int attempts = 10;
while (!m_bStop && attempts-- > 0)
{
m_pDemuxer = CDVDFactoryDemuxer::CreateDemuxer(m_pInputStream);
if(!m_pDemuxer && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_PVRMANAGER))
{
continue;
}
else if(!m_pDemuxer && m_pInputStream->NextStream() != CDVDInputStream::NEXTSTREAM_NONE)
{
CLog::Log(LOGDEBUG, "%s - New stream available from input, retry open", __FUNCTION__);
continue;
}
break;
}
if (!m_pDemuxer)
{
CLog::Log(LOGERROR, "%s - Error creating demuxer", __FUNCTION__);
return false;
}
m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_DEMUX);
m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_NAV);
m_SelectionStreams.Update(m_pInputStream, m_pDemuxer);
m_pDemuxer->GetPrograms(m_programs);
UpdateContent();
int64_t len = m_pInputStream->GetLength();
int64_t tim = m_pDemuxer->GetStreamLength();
if(len > 0 && tim > 0)
m_pInputStream->SetReadRate((unsigned int) (len * 1000 / tim));
m_offset_pts = 0;
return true;
}
void CVideoPlayer::CloseDemuxer()
{
delete m_pDemuxer;
m_pDemuxer = nullptr;
m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_DEMUX);
CServiceBroker::GetDataCacheCore().SignalAudioInfoChange();
CServiceBroker::GetDataCacheCore().SignalVideoInfoChange();
}
void CVideoPlayer::OpenDefaultStreams(bool reset)
{
// if input stream dictate, we will open later
if (m_dvd.iSelectedAudioStream >= 0 ||
m_dvd.iSelectedSPUStream >= 0)
return;
bool valid;
// open video stream
valid = false;
PredicateVideoFilter vf(m_processInfo->GetVideoSettings().m_VideoStream);
for (const auto &stream : m_SelectionStreams.Get(STREAM_VIDEO, vf))
{
if (OpenStream(m_CurrentVideo, stream.demuxerId, stream.id, stream.source, reset))
{
valid = true;
break;
}
}
if (!valid)
{
CloseStream(m_CurrentVideo, true);
m_processInfo->ResetVideoCodecInfo();
}
// open audio stream
valid = false;