/
AMLPlayer.cpp
2342 lines (2060 loc) · 69.1 KB
/
AMLPlayer.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) 2011-2013 Team XBMC
* http://www.xbmc.org
*
* 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 XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "system.h"
#include "AMLPlayer.h"
#include "Application.h"
#include "FileItem.h"
#include "FileURLProtocol.h"
#include "GUIInfoManager.h"
#include "video/VideoThumbLoader.h"
#include "Util.h"
#include "cores/AudioEngine/Utils/AEUtil.h"
#include "cores/VideoRenderers/RenderFlags.h"
#include "cores/VideoRenderers/RenderFormats.h"
#include "cores/VideoRenderers/RenderManager.h"
#include "dialogs/GUIDialogKaiToast.h"
#include "filesystem/File.h"
#include "filesystem/SpecialProtocol.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/LocalizeStrings.h"
#include "settings/AdvancedSettings.h"
#include "settings/GUISettings.h"
#include "settings/Settings.h"
#include "settings/VideoSettings.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
#include "utils/TimeUtils.h"
#include "utils/URIUtils.h"
#include "utils/LangCodeExpander.h"
#include "utils/Variant.h"
#include "windowing/WindowingFactory.h"
// for external subtitles
#include "xbmc/cores/dvdplayer/DVDClock.h"
#include "xbmc/cores/dvdplayer/DVDPlayerSubtitle.h"
#include "xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxVobsub.h"
#include "settings/VideoSettings.h"
// amlogic libplayer
#include "AMLUtils.h"
#include "DllLibamplayer.h"
struct AMLChapterInfo
{
std::string name;
int64_t seekto_ms;
};
struct AMLPlayerStreamInfo
{
void Clear()
{
id = 0;
width = 0;
height = 0;
aspect_ratio_num = 0;
aspect_ratio_den = 0;
frame_rate_num = 0;
frame_rate_den = 0;
bit_rate = 0;
duration = 0;
channel = 0;
sample_rate = 0;
language = "";
type = STREAM_NONE;
source = STREAM_SOURCE_NONE;
name = "";
filename = "";
filename2 = "";
}
int id;
StreamType type;
StreamSource source;
int width;
int height;
int aspect_ratio_num;
int aspect_ratio_den;
int frame_rate_num;
int frame_rate_den;
int bit_rate;
int duration;
int channel;
int sample_rate;
int format;
std::string language;
std::string name;
std::string filename;
std::string filename2; // for vobsub subtitles, 2 files are necessary (idx/sub)
};
////////////////////////////////////////////////////////////////////////////////////////////
static int media_info_dump(media_info_t* minfo)
{
int i = 0;
CLog::Log(LOGDEBUG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
CLog::Log(LOGDEBUG, "======||file size:%lld", minfo->stream_info.file_size);
CLog::Log(LOGDEBUG, "======||file type:%d", minfo->stream_info.type);
CLog::Log(LOGDEBUG, "======||duration:%d", minfo->stream_info.duration);
CLog::Log(LOGDEBUG, "======||has video track?:%s", minfo->stream_info.has_video>0?"YES!":"NO!");
CLog::Log(LOGDEBUG, "======||has audio track?:%s", minfo->stream_info.has_audio>0?"YES!":"NO!");
CLog::Log(LOGDEBUG, "======||has internal subtitle?:%s", minfo->stream_info.has_sub>0?"YES!":"NO!");
CLog::Log(LOGDEBUG, "======||internal subtile counts:%d", minfo->stream_info.total_sub_num);
if (minfo->stream_info.has_video && minfo->stream_info.total_video_num > 0)
{
CLog::Log(LOGDEBUG, "======||video index:%d", minfo->stream_info.cur_video_index);
CLog::Log(LOGDEBUG, "======||video counts:%d", minfo->stream_info.total_video_num);
CLog::Log(LOGDEBUG, "======||video width :%d", minfo->video_info[0]->width);
CLog::Log(LOGDEBUG, "======||video height:%d", minfo->video_info[0]->height);
CLog::Log(LOGDEBUG, "======||video ratio :%d:%d", minfo->video_info[0]->aspect_ratio_num,minfo->video_info[0]->aspect_ratio_den);
CLog::Log(LOGDEBUG, "======||frame_rate :%.2f", (float)minfo->video_info[0]->frame_rate_num/minfo->video_info[0]->frame_rate_den);
CLog::Log(LOGDEBUG, "======||video bitrate:%d", minfo->video_info[0]->bit_rate);
CLog::Log(LOGDEBUG, "======||video format:%d", minfo->video_info[0]->format);
CLog::Log(LOGDEBUG, "======||video duration:%d", minfo->video_info[0]->duartion);
}
if (minfo->stream_info.has_audio && minfo->stream_info.total_audio_num > 0)
{
CLog::Log(LOGDEBUG, "======||audio index:%d", minfo->stream_info.cur_audio_index);
CLog::Log(LOGDEBUG, "======||audio counts:%d", minfo->stream_info.total_audio_num);
for (i = 0; i < minfo->stream_info.total_audio_num; i++)
{
CLog::Log(LOGDEBUG, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
CLog::Log(LOGDEBUG, "======||audio track(%d) id:%d", i, minfo->audio_info[i]->id);
CLog::Log(LOGDEBUG, "======||audio track(%d) codec type:%d", i, minfo->audio_info[i]->aformat);
CLog::Log(LOGDEBUG, "======||audio track(%d) audio_channel:%d", i, minfo->audio_info[i]->channel);
CLog::Log(LOGDEBUG, "======||audio track(%d) bit_rate:%d", i, minfo->audio_info[i]->bit_rate);
CLog::Log(LOGDEBUG, "======||audio track(%d) audio_samplerate:%d", i, minfo->audio_info[i]->sample_rate);
CLog::Log(LOGDEBUG, "======||audio track(%d) duration:%d", i, minfo->audio_info[i]->duration);
CLog::Log(LOGDEBUG, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
if (NULL != minfo->audio_info[i]->audio_tag)
{
CLog::Log(LOGDEBUG, "======||audio track title:%s", minfo->audio_info[i]->audio_tag->title!=NULL?minfo->audio_info[i]->audio_tag->title:"unknown");
CLog::Log(LOGDEBUG, "======||audio track album:%s", minfo->audio_info[i]->audio_tag->album!=NULL?minfo->audio_info[i]->audio_tag->album:"unknown");
CLog::Log(LOGDEBUG, "======||audio track author:%s", minfo->audio_info[i]->audio_tag->author!=NULL?minfo->audio_info[i]->audio_tag->author:"unknown");
CLog::Log(LOGDEBUG, "======||audio track year:%s", minfo->audio_info[i]->audio_tag->year!=NULL?minfo->audio_info[i]->audio_tag->year:"unknown");
CLog::Log(LOGDEBUG, "======||audio track comment:%s", minfo->audio_info[i]->audio_tag->comment!=NULL?minfo->audio_info[i]->audio_tag->comment:"unknown");
CLog::Log(LOGDEBUG, "======||audio track genre:%s", minfo->audio_info[i]->audio_tag->genre!=NULL?minfo->audio_info[i]->audio_tag->genre:"unknown");
CLog::Log(LOGDEBUG, "======||audio track copyright:%s", minfo->audio_info[i]->audio_tag->copyright!=NULL?minfo->audio_info[i]->audio_tag->copyright:"unknown");
CLog::Log(LOGDEBUG, "======||audio track track:%d", minfo->audio_info[i]->audio_tag->track);
}
}
}
if (minfo->stream_info.has_sub && minfo->stream_info.total_sub_num > 0)
{
CLog::Log(LOGDEBUG, "======||subtitle index:%d", minfo->stream_info.cur_sub_index);
CLog::Log(LOGDEBUG, "======||subtitle counts:%d", minfo->stream_info.total_sub_num);
for (i = 0; i < minfo->stream_info.total_sub_num; i++)
{
if (0 == minfo->sub_info[i]->internal_external){
CLog::Log(LOGDEBUG, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
CLog::Log(LOGDEBUG, "======||internal subtitle(%d) pid:%d", i, minfo->sub_info[i]->id);
CLog::Log(LOGDEBUG, "======||internal subtitle(%d) language:%s", i, minfo->sub_info[i]->sub_language?minfo->sub_info[i]->sub_language:"unknown");
CLog::Log(LOGDEBUG, "======||internal subtitle(%d) width:%d", i, minfo->sub_info[i]->width);
CLog::Log(LOGDEBUG, "======||internal subtitle(%d) height:%d", i, minfo->sub_info[i]->height);
CLog::Log(LOGDEBUG, "======||internal subtitle(%d) resolution:%d", i, minfo->sub_info[i]->resolution);
CLog::Log(LOGDEBUG, "======||internal subtitle(%d) subtitle size:%lld", i, minfo->sub_info[i]->subtitle_size);
CLog::Log(LOGDEBUG, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
}
}
}
CLog::Log(LOGDEBUG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
return 0;
}
static const char* VideoCodecName(int vformat)
{
const char *format = "";
switch(vformat)
{
case VFORMAT_MPEG12:
format = "mpeg12";
break;
case VFORMAT_MPEG4:
format = "mpeg4";
break;
case VFORMAT_H264:
format = "h264";
break;
case VFORMAT_MJPEG:
format = "mjpeg";
break;
case VFORMAT_REAL:
format = "real";
break;
case VFORMAT_JPEG:
format = "jpeg";
break;
case VFORMAT_VC1:
format = "vc1";
break;
case VFORMAT_AVS:
format = "avs";
break;
case VFORMAT_SW:
format = "sw";
break;
case VFORMAT_H264MVC:
format = "h264mvc";
break;
default:
format = "unknown";
break;
}
return format;
}
static const char* AudioCodecName(int aformat)
{
const char *format = "";
switch(aformat)
{
case AFORMAT_MPEG:
format = "mpeg";
break;
case AFORMAT_PCM_S16LE:
format = "pcm";
break;
case AFORMAT_AAC:
format = "aac";
break;
case AFORMAT_AC3:
format = "ac3";
break;
case AFORMAT_ALAW:
format = "alaw";
break;
case AFORMAT_MULAW:
format = "mulaw";
break;
case AFORMAT_DTS:
format = "dts";
break;
case AFORMAT_PCM_S16BE:
format = "pcm";
break;
case AFORMAT_FLAC:
format = "flac";
break;
case AFORMAT_COOK:
format = "cook";
break;
case AFORMAT_PCM_U8:
format = "pcm";
break;
case AFORMAT_ADPCM:
format = "adpcm";
break;
case AFORMAT_AMR:
format = "amr";
break;
case AFORMAT_RAAC:
format = "raac";
break;
case AFORMAT_WMA:
format = "wma";
break;
case AFORMAT_WMAPRO:
format = "wmapro";
break;
case AFORMAT_PCM_BLURAY:
format = "lpcm";
break;
case AFORMAT_ALAC:
format = "alac";
break;
case AFORMAT_VORBIS:
format = "vorbis";
break;
case AFORMAT_AAC_LATM:
format = "aac-latm";
break;
case AFORMAT_APE:
format = "ape";
break;
default:
format = "unknown";
break;
}
return format;
}
////////////////////////////////////////////////////////////////////////////////////////////
CAMLSubTitleThread::CAMLSubTitleThread(DllLibAmplayer *dll) :
CThread("CAMLSubTitleThread"),
m_dll(dll),
m_subtitle_codec(-1)
{
}
CAMLSubTitleThread::~CAMLSubTitleThread()
{
StopThread();
}
void CAMLSubTitleThread::UpdateSubtitle(CStdString &subtitle, int64_t elapsed_ms)
{
CSingleLock lock(m_subtitle_csection);
if (m_subtitle_strings.size())
{
AMLSubtitle *amlsubtitle;
// remove any expired subtitles
std::deque<AMLSubtitle*>::iterator it = m_subtitle_strings.begin();
while (it != m_subtitle_strings.end())
{
amlsubtitle = *it;
if (elapsed_ms > amlsubtitle->endtime)
it = m_subtitle_strings.erase(it);
else
it++;
}
// find the current subtitle
it = m_subtitle_strings.begin();
while (it != m_subtitle_strings.end())
{
amlsubtitle = *it;
if (elapsed_ms > amlsubtitle->bgntime && elapsed_ms < amlsubtitle->endtime)
{
subtitle = amlsubtitle->string;
break;
}
it++;
}
}
}
void CAMLSubTitleThread::Process(void)
{
CLog::Log(LOGDEBUG, "CAMLSubTitleThread::Process begin");
m_subtitle_codec = m_dll->codec_open_sub_read();
if (m_subtitle_codec < 0)
CLog::Log(LOGERROR, "CAMLSubTitleThread::Process: codec_open_sub_read failed");
while (!m_bStop)
{
if (m_subtitle_codec > 0)
{
// poll sub codec, we return on timeout or when a sub gets loaded
// TODO: codec_poll_sub_fd has a bug in kernel driver, it trashes
// subs in certain conditions so we read garbage, manual poll for now.
//codec_poll_sub_fd(m_subtitle_codec, 1000);
int sub_size = m_dll->codec_get_sub_size_fd(m_subtitle_codec);
if (sub_size > 0)
{
int sub_type = 0, sub_pts = 0;
// calloc sub_size + 1 so we auto terminate the string
char *sub_buffer = (char*)calloc(sub_size + 1, 1);
m_dll->codec_read_sub_data_fd(m_subtitle_codec, sub_buffer, sub_size);
// check subtitle header stamp
if ((sub_buffer[0] == 0x41) && (sub_buffer[1] == 0x4d) &&
(sub_buffer[2] == 0x4c) && (sub_buffer[3] == 0x55) &&
(sub_buffer[4] == 0xaa))
{
// 20 byte header, then subtitle string
if (sub_size >= 20)
{
// csection lock it now as we are diddling shared vars
CSingleLock lock(m_subtitle_csection);
AMLSubtitle *subtitle = new AMLSubtitle;
sub_type = (sub_buffer[5] << 16) | (sub_buffer[6] << 8) | sub_buffer[7];
// sub_pts are in ffmpeg timebase, not ms timebase, convert it.
sub_pts = (sub_buffer[12] << 24) | (sub_buffer[13] << 16) | (sub_buffer[14] << 8) | sub_buffer[15];
/* TODO: handle other subtitle codec types
// subtitle codecs
CODEC_ID_DVD_SUBTITLE= 0x17000,
CODEC_ID_DVB_SUBTITLE,
CODEC_ID_TEXT, ///< raw UTF-8 text
CODEC_ID_XSUB,
CODEC_ID_SSA,
CODEC_ID_MOV_TEXT,
CODEC_ID_HDMV_PGS_SUBTITLE,
CODEC_ID_DVB_TELETEXT,
CODEC_ID_SRT,
CODEC_ID_MICRODVD,
*/
switch(sub_type)
{
default:
CLog::Log(LOGDEBUG, "CAMLSubTitleThread::Process: fixme :) "
"sub_type(0x%x), size(%d), bgntime(%lld), endtime(%lld), string(%s)",
sub_type, sub_size-20, subtitle->bgntime, subtitle->endtime, &sub_buffer[20]);
break;
case CODEC_ID_TEXT:
subtitle->bgntime = sub_pts/ 90;
subtitle->endtime = subtitle->bgntime + 4000;
subtitle->string = &sub_buffer[20];
break;
case CODEC_ID_SSA:
if (strncmp((const char*)&sub_buffer[20], "Dialogue:", 9) == 0)
{
int vars_found, hour1, min1, sec1, hunsec1, hour2, min2, sec2, hunsec2, nothing;
char line3[sub_size];
char *line = &sub_buffer[20];
memset(line3, 0x00, sub_size);
vars_found = sscanf(line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\n\r]",
¬hing, &hour1, &min1, &sec1, &hunsec1, &hour2, &min2, &sec2, &hunsec2, line3);
if (vars_found < 10)
vars_found = sscanf(line, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\n\r]",
¬hing, &hour1, &min1, &sec1, &hunsec1, &hour2, &min2, &sec2, &hunsec2, line3);
if (vars_found > 9)
{
char *tmp, *line2 = strchr(line3, ',');
// use 32 for the case that the amount of commas increase with newer SSA versions
for (int comma = 4; comma < 32; comma++)
{
tmp = strchr(line2 + 1, ',');
if (!tmp)
break;
if (*(++tmp) == ' ')
break;
// a space after a comma means we are already in a sentence
line2 = tmp;
}
// eliminate the trailing comma
if (*line2 == ',')
line2++;
subtitle->bgntime = 10 * (360000 * hour1 + 6000 * min1 + 100 * sec1 + hunsec1);
subtitle->endtime = 10 * (360000 * hour2 + 6000 * min2 + 100 * sec2 + hunsec2);
subtitle->string = line2;
// convert tags to what we understand
if (subtitle->string.Replace("{\\i1}","[I]"))
subtitle->string.Replace("{\\i0}","[/I]");
if (subtitle->string.Replace("{\\b1}","[B]"))
subtitle->string.Replace("{\\b0}","[/B]");
// remove anything other tags
for (std::string::const_iterator it = subtitle->string.begin(); it != subtitle->string.end(); ++it)
{
size_t beg = subtitle->string.find("{\\");
if (beg != std::string::npos)
{
size_t end = subtitle->string.find("}", beg);
if (end != std::string::npos)
subtitle->string.erase(beg, end-beg+1);
}
}
}
}
break;
}
free(sub_buffer);
if (subtitle->string.length())
{
// quirks
subtitle->string.Replace("'","\'");
m_subtitle_strings.push_back(subtitle);
// fixup existing endtimes so they do not exceed bgntime of previous subtitle
for (size_t i = 0; i < m_subtitle_strings.size() - 1; i++)
{
if (m_subtitle_strings[i]->endtime > m_subtitle_strings[i+1]->bgntime)
m_subtitle_strings[i]->endtime = m_subtitle_strings[i+1]->bgntime;
}
}
}
}
}
else
{
usleep(100 * 1000);
}
}
else
{
usleep(250 * 1000);
}
}
m_subtitle_strings.clear();
if (m_subtitle_codec > 0)
m_dll->codec_close_sub_fd(m_subtitle_codec);
m_subtitle_codec = -1;
CLog::Log(LOGDEBUG, "CAMLSubTitleThread::Process end");
}
////////////////////////////////////////////////////////////////////////////////////////////
CAMLPlayer::CAMLPlayer(IPlayerCallback &callback)
: IPlayer(callback),
CThread("CAMLPlayer"),
m_ready(true)
{
m_dll = new DllLibAmplayer;
m_dll->Load();
m_pid = -1;
m_speed = 0;
m_paused = false;
#if defined(_DEBUG)
m_log_level = 5;
#else
m_log_level = 3;
#endif
m_bAbortRequest = false;
// for external subtitles
m_dvdOverlayContainer = new CDVDOverlayContainer;
m_dvdPlayerSubtitle = new CDVDPlayerSubtitle(m_dvdOverlayContainer);
}
CAMLPlayer::~CAMLPlayer()
{
CloseFile();
delete m_dvdPlayerSubtitle;
delete m_dvdOverlayContainer;
delete m_dll, m_dll = NULL;
}
bool CAMLPlayer::OpenFile(const CFileItem &file, const CPlayerOptions &options)
{
try
{
CLog::Log(LOGNOTICE, "CAMLPlayer: Opening: %s", file.GetPath().c_str());
// if playing a file close it first
// this has to be changed so we won't have to close it.
if (IsRunning())
CloseFile();
m_bAbortRequest = false;
m_item = file;
m_options = options;
m_elapsed_ms = 0;
m_duration_ms = 0;
m_audio_info = "none";
m_audio_delay = 0;
m_audio_passthrough_ac3 = g_guiSettings.GetBool("audiooutput.ac3passthrough");
m_audio_passthrough_dts = g_guiSettings.GetBool("audiooutput.dtspassthrough");
m_video_info = "none";
m_video_width = 0;
m_video_height = 0;
m_video_fps_numerator = 25;
m_video_fps_denominator = 1;
m_subtitle_delay = 0;
m_subtitle_thread = NULL;
m_chapter_index = 0;
m_chapter_count = 0;
m_show_mainvideo = -1;
m_dst_rect.SetRect(0, 0, 0, 0);
m_zoom = -1;
m_contrast = -1;
m_brightness = -1;
ClearStreamInfos();
// setup to spin the busy dialog until we are playing
m_ready.Reset();
g_renderManager.PreInit();
// create the playing thread
Create();
if (!m_ready.WaitMSec(100))
{
CGUIDialogBusy *dialog = (CGUIDialogBusy*)g_windowManager.GetWindow(WINDOW_DIALOG_BUSY);
dialog->Show();
while (!m_ready.WaitMSec(1))
g_windowManager.ProcessRenderLoop(false);
dialog->Close();
}
// Playback might have been stopped due to some error.
if (m_bStop || m_bAbortRequest)
return false;
return true;
}
catch (...)
{
CLog::Log(LOGERROR, "%s - Exception thrown on open", __FUNCTION__);
return false;
}
}
bool CAMLPlayer::CloseFile()
{
CLog::Log(LOGDEBUG, "CAMLPlayer::CloseFile");
// set the abort request so that other threads can finish up
m_bAbortRequest = true;
CLog::Log(LOGDEBUG, "CAMLPlayer: 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
StopThread();
CLog::Log(LOGDEBUG, "CAMLPlayer: finished waiting");
g_renderManager.UnInit();
return true;
}
bool CAMLPlayer::IsPlaying() const
{
return !m_bStop;
}
void CAMLPlayer::Pause()
{
CLog::Log(LOGDEBUG, "CAMLPlayer::Pause");
CSingleLock lock(m_aml_csection);
if ((m_pid < 0) && m_bAbortRequest)
return;
if (m_paused)
m_dll->player_resume(m_pid);
else
m_dll->player_pause(m_pid);
m_paused = !m_paused;
}
bool CAMLPlayer::IsPaused() const
{
return m_paused;
}
bool CAMLPlayer::HasVideo() const
{
return m_video_count > 0;
}
bool CAMLPlayer::HasAudio() const
{
return m_audio_count > 0;
}
void CAMLPlayer::ToggleFrameDrop()
{
CLog::Log(LOGDEBUG, "CAMLPlayer::ToggleFrameDrop");
}
bool CAMLPlayer::CanSeek()
{
return GetTotalTime() > 0;
}
void CAMLPlayer::Seek(bool bPlus, bool bLargeStep)
{
// force updated to m_elapsed_ms, m_duration_ms.
GetStatus();
CSingleLock lock(m_aml_csection);
// try chapter seeking first, chapter_index is ones based.
int chapter_index = GetChapter();
if (bLargeStep)
{
// seek to next chapter
if (bPlus && (chapter_index < GetChapterCount()))
{
SeekChapter(chapter_index + 1);
return;
}
// seek to previous chapter
if (!bPlus && chapter_index)
{
SeekChapter(chapter_index - 1);
return;
}
}
int64_t seek_ms;
if (g_advancedSettings.m_videoUseTimeSeeking)
{
if (bLargeStep && (GetTotalTime() > (2000 * g_advancedSettings.m_videoTimeSeekForwardBig)))
seek_ms = bPlus ? g_advancedSettings.m_videoTimeSeekForwardBig : g_advancedSettings.m_videoTimeSeekBackwardBig;
else
seek_ms = bPlus ? g_advancedSettings.m_videoTimeSeekForward : g_advancedSettings.m_videoTimeSeekBackward;
// convert to milliseconds
seek_ms *= 1000;
seek_ms += m_elapsed_ms;
}
else
{
float percent;
if (bLargeStep)
percent = bPlus ? g_advancedSettings.m_videoPercentSeekForwardBig : g_advancedSettings.m_videoPercentSeekBackwardBig;
else
percent = bPlus ? g_advancedSettings.m_videoPercentSeekForward : g_advancedSettings.m_videoPercentSeekBackward;
percent /= 100.0f;
percent += (float)m_elapsed_ms/(float)m_duration_ms;
// convert to milliseconds
seek_ms = m_duration_ms * percent;
}
// handle stacked videos, dvdplayer does it so we do it too.
if (g_application.CurrentFileItem().IsStack() &&
(seek_ms > m_duration_ms || seek_ms < 0))
{
CLog::Log(LOGDEBUG, "CAMLPlayer::Seek: In mystery code, what did I do");
g_application.SeekTime((seek_ms - m_elapsed_ms) * 0.001 + g_application.GetTime());
// warning, don't access any object variables here as
// the object may have been destroyed
return;
}
if (seek_ms <= 1000)
seek_ms = 1000;
if (seek_ms > m_duration_ms)
seek_ms = m_duration_ms;
// do seek here
g_infoManager.SetDisplayAfterSeek(100000);
SeekTime(seek_ms);
m_callback.OnPlayBackSeek((int)seek_ms, (int)(seek_ms - m_elapsed_ms));
g_infoManager.SetDisplayAfterSeek();
}
bool CAMLPlayer::SeekScene(bool bPlus)
{
CLog::Log(LOGDEBUG, "CAMLPlayer::SeekScene");
return false;
}
void CAMLPlayer::SeekPercentage(float fPercent)
{
CSingleLock lock(m_aml_csection);
// force updated to m_elapsed_ms, m_duration_ms.
GetStatus();
if (m_duration_ms)
{
int64_t seek_ms = fPercent * m_duration_ms / 100.0;
if (seek_ms <= 1000)
seek_ms = 1000;
// do seek here
g_infoManager.SetDisplayAfterSeek(100000);
SeekTime(seek_ms);
m_callback.OnPlayBackSeek((int)seek_ms, (int)(seek_ms - m_elapsed_ms));
g_infoManager.SetDisplayAfterSeek();
}
}
float CAMLPlayer::GetPercentage()
{
GetStatus();
if (m_duration_ms)
return 100.0f * (float)m_elapsed_ms/(float)m_duration_ms;
else
return 0.0f;
}
void CAMLPlayer::SetMute(bool bOnOff)
{
m_audio_mute = bOnOff;
#if !defined(TARGET_ANDROID)
CSingleLock lock(m_aml_csection);
if (m_dll->check_pid_valid(m_pid))
{
if (m_audio_mute)
m_dll->audio_set_volume(m_pid, 0.0);
else
m_dll->audio_set_volume(m_pid, m_audio_volume);
}
#endif
}
void CAMLPlayer::SetVolume(float volume)
{
m_audio_volume = 0.0f;
if (volume > VOLUME_MINIMUM)
{
float dB = CAEUtil::PercentToGain(volume);
m_audio_volume = CAEUtil::GainToScale(dB);
}
if (m_audio_volume >= 0.99f)
m_audio_volume = 1.0f;
#if !defined(TARGET_ANDROID)
CSingleLock lock(m_aml_csection);
if (!m_audio_mute && m_dll->check_pid_valid(m_pid))
m_dll->audio_set_volume(m_pid, m_audio_volume);
#endif
}
void CAMLPlayer::GetAudioInfo(CStdString &strAudioInfo)
{
CSingleLock lock(m_aml_csection);
if (m_audio_streams.size() == 0 || m_audio_index > (int)(m_audio_streams.size() - 1))
return;
strAudioInfo.Format("Audio stream (%s) [Kb/s:%.2f]",
AudioCodecName(m_audio_streams[m_audio_index]->format),
(double)m_audio_streams[m_audio_index]->bit_rate / 1024.0);
}
void CAMLPlayer::GetVideoInfo(CStdString &strVideoInfo)
{
CSingleLock lock(m_aml_csection);
if (m_video_streams.size() == 0 || m_video_index > (int)(m_video_streams.size() - 1))
return;
strVideoInfo.Format("Video stream (%s) [fr:%.3f Mb/s:%.2f]",
VideoCodecName(m_video_streams[m_video_index]->format),
GetActualFPS(),
(double)m_video_streams[m_video_index]->bit_rate / (1024.0*1024.0));
}
int CAMLPlayer::GetAudioStreamCount()
{
//CLog::Log(LOGDEBUG, "CAMLPlayer::GetAudioStreamCount");
return m_audio_count;
}
int CAMLPlayer::GetAudioStream()
{
//CLog::Log(LOGDEBUG, "CAMLPlayer::GetAudioStream");
return m_audio_index;
}
void CAMLPlayer::SetAudioStream(int SetAudioStream)
{
//CLog::Log(LOGDEBUG, "CAMLPlayer::SetAudioStream");
CSingleLock lock(m_aml_csection);
if (SetAudioStream > (int)m_audio_streams.size() || SetAudioStream < 0)
return;
m_audio_index = SetAudioStream;
SetAudioPassThrough(m_audio_streams[m_audio_index]->format);
if (m_dll->check_pid_valid(m_pid))
{
m_dll->player_aid(m_pid, m_audio_streams[m_audio_index]->id);
}
}
void CAMLPlayer::SetAVDelay(float fValue)
{
CLog::Log(LOGDEBUG, "CAMLPlayer::SetAVDelay (%f)", fValue);
m_audio_delay = fValue * 1000.0;
if (m_audio_streams.size() && m_dll->check_pid_valid(m_pid))
{
CSingleLock lock(m_aml_csection);
m_dll->audio_set_delay(m_pid, m_audio_delay);
}
}
float CAMLPlayer::GetAVDelay()
{
return (float)m_audio_delay / 1000.0;
}
void CAMLPlayer::SetSubTitleDelay(float fValue = 0.0f)
{
if (GetSubtitleCount())
{
CSingleLock lock(m_aml_csection);
m_subtitle_delay = fValue * 1000.0;
}
}
float CAMLPlayer::GetSubTitleDelay()
{
return (float)m_subtitle_delay / 1000.0;
}
int CAMLPlayer::GetSubtitleCount()
{
return m_subtitle_count;
}
int CAMLPlayer::GetSubtitle()
{
if (m_subtitle_show)
return m_subtitle_index;
else
return -1;
}
void CAMLPlayer::GetSubtitleName(int iStream, CStdString &strStreamName)
{
CSingleLock lock(m_aml_csection);
strStreamName = "";
if (iStream > (int)m_subtitle_streams.size() || iStream < 0)
return;
if (m_subtitle_streams[m_subtitle_index]->source == STREAM_SOURCE_NONE)
{
if ( m_subtitle_streams[iStream]->language.size())
{
CStdString name;
g_LangCodeExpander.Lookup(name, m_subtitle_streams[iStream]->language);
strStreamName = name;
}
else
strStreamName = g_localizeStrings.Get(13205); // Unknown
}
else
{
if(m_subtitle_streams[m_subtitle_index]->name.length() > 0)
strStreamName = m_subtitle_streams[m_subtitle_index]->name;
else
strStreamName = g_localizeStrings.Get(13205); // Unknown
}
if (m_log_level > 5)
CLog::Log(LOGDEBUG, "CAMLPlayer::GetSubtitleName, iStream(%d)", iStream);
}
void CAMLPlayer::SetSubtitle(int iStream)
{
CSingleLock lock(m_aml_csection);
if (iStream > (int)m_subtitle_streams.size() || iStream < 0)
return;
m_subtitle_index = iStream;
// smells like a bug, if no showing subs and we get called
// to set the subtitle, we are expected to update internal state
// but not show the subtitle.
if (!m_subtitle_show)
return;
if (m_dll->check_pid_valid(m_pid) && m_subtitle_streams[m_subtitle_index]->source == STREAM_SOURCE_NONE)
m_dll->player_sid(m_pid, m_subtitle_streams[m_subtitle_index]->id);
else
{
m_dvdPlayerSubtitle->CloseStream(true);
OpenSubtitleStream(m_subtitle_index);
}
}
bool CAMLPlayer::GetSubtitleVisible()
{
return m_subtitle_show;
}
void CAMLPlayer::SetSubtitleVisible(bool bVisible)
{
m_subtitle_show = (bVisible && m_subtitle_count);
g_settings.m_currentVideoSettings.m_SubtitleOn = bVisible;
if (m_subtitle_show && m_subtitle_count)
{
// on startup, if asked to show subs and SetSubtitle has not
// been called, we are expected to switch/show the 1st subtitle
if (m_subtitle_index < 0)
m_subtitle_index = 0;
if (m_dll->check_pid_valid(m_pid) && m_subtitle_streams[m_subtitle_index]->source == STREAM_SOURCE_NONE)
m_dll->player_sid(m_pid, m_subtitle_streams[m_subtitle_index]->id);
else
OpenSubtitleStream(m_subtitle_index);
}
}
int CAMLPlayer::AddSubtitle(const CStdString& strSubPath)
{
CSingleLock lock(m_aml_csection);
return AddSubtitleFile(strSubPath);
}
void CAMLPlayer::Update(bool bPauseDrawing)
{
g_renderManager.Update(bPauseDrawing);
}
void CAMLPlayer::GetVideoRect(CRect& SrcRect, CRect& DestRect)