forked from carlonluca/pot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
omx_mediaprocessor.cpp
1109 lines (966 loc) · 35.7 KB
/
omx_mediaprocessor.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
/*
* Project: PiOmxTextures
* Author: Luca Carlon
* Date: 11.01.2012
*
* Copyright (c) 2012, 2013 Luca Carlon. All rights reserved.
*
* This file is part of PiOmxTextures.
*
* PiOmxTextures 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 3 of the License, or
* (at your option) any later version.
*
* PiOmxTextures 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 PiOmxTextures. If not, see <http://www.gnu.org/licenses/>.
*/
/*------------------------------------------------------------------------------
| includes
+-----------------------------------------------------------------------------*/
#include <QStringList>
#include <QOpenGLContext>
#include <QElapsedTimer>
#include <QUrl>
#include <cstring>
#include "lc_logging.h"
#include "omx_mediaprocessor.h"
#include "omx_textureprovider.h"
#include "omx_playeraudio.h"
#include "omx_reader.h"
// omxplayer lib.
#include "omxplayer_lib/linux/RBP.h"
#include "omxplayer_lib/OMXPlayerVideo.h"
#include "omxplayer_lib/OMXPlayerAudio.h"
#include "omxplayer_lib/OMXPlayerSubtitles.h"
#include "omxplayer_lib/OMXStreamInfo.h"
#include "omxplayer_lib/DllOMX.h"
#define ENABLE_HDMI_CLOCK_SYNC false
#define FONT_PATH "/usr/share/fonts/truetype/freefont/FreeSans.ttf"
#define FONT_PATH_ITALIC "/usr/share/fonts/truetype/freefont/FreeSansOblique.ttf"
#define FONT_SIZE (0.055f)
#define CENTERED false
#define BUFFERING_TIMEOUT_S 3
// when we repeatedly seek, rather than play continuously
#define TRICKPLAY(speed) (speed < 0 || speed > 4 * DVD_PLAYSPEED_NORMAL)
#define S(x) (int)(DVD_PLAYSPEED_NORMAL*(x))
int playspeeds[] = {
S(0), S(1/16.0), S(1/8.0), S(1/4.0), S(1/2.0),
S(0.975), S(1.0), S(1.125), S(-32.0), S(-16.0),
S(-8.0), S(-4), S(-2), S(-1), S(1), S(2.0), S(4.0),
S(8.0), S(16.0), S(32.0)
};
const int playspeed_slow_min = 0,
playspeed_slow_max = 7, playspeed_rew_max = 8,
playspeed_rew_min = 13, playspeed_normal = 14,
playspeed_ff_min = 15, playspeed_ff_max = 19;
/*------------------------------------------------------------------------------
| get_mem_gpu
+-----------------------------------------------------------------------------*/
static int get_mem_gpu(void)
{
char response[80] = "";
int gpu_mem = 0;
if (vc_gencmd(response, sizeof response, "get_mem gpu") == 0)
vc_gencmd_number_property(response, "gpu", &gpu_mem);
return gpu_mem;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::OMX_MediaProcessor
+-----------------------------------------------------------------------------*/
OMX_MediaProcessor::OMX_MediaProcessor(OMX_TextureProviderSh provider) :
m_textureData(NULL),
m_state(STATE_INACTIVE),
#ifdef ENABLE_SUBTITLES
m_has_subtitle(false),
#endif
m_omx_reader(new OMX_Reader),
m_omx_pkt(NULL),
m_RBP(new CRBP),
m_OMX(new COMXCore),
m_has_video(false),
m_has_audio(false),
m_buffer_empty(true),
m_pendingStop(false),
m_pendingPause(false),
m_subtitle_index(0),
m_audio_index(0),
m_provider(provider),
m_incr(0),
m_hints_audio(new COMXStreamInfo),
m_hints_video(new COMXStreamInfo),
m_audio_fifo_size(0.0f), // zero means use default
m_video_fifo_size(0.0f),
m_audio_queue_size(0.0f),
m_video_queue_size(0.0f),
m_playspeedCurrent(playspeed_normal),
m_seekFlush(false),
m_packetAfterSeek(false),
startpts(0)
{
qRegisterMetaType<OMX_MediaProcessor::OMX_MediaProcessorError>("OMX_MediaProcessor::OMX_MediaProcessorError");
qRegisterMetaType<OMX_MediaProcessor::OMX_MediaProcessorState>("OMX_MediaProcessor::OMX_MediaProcessorState");
int gpu_mem = get_mem_gpu();
int min_gpu_mem = 64;
if (gpu_mem > 0 && gpu_mem < min_gpu_mem)
LOG_WARNING(LOG_TAG, "Only %dM of gpu_mem is configured. Try running"
" \"sudo raspi-config\" and ensure that \"memory_split\" has "
"a value of %d or greater\n", gpu_mem, min_gpu_mem);
m_RBP->Initialize();
m_OMX->Initialize();
// Players.
m_av_clock = new OMXClock;
m_player_video = new OMXPlayerVideo(provider);
m_player_audio = new OMX_PlayerAudio;
#ifdef ENABLE_SUBTITLES
m_player_subtitles = new OMXPlayerSubtitles;
#endif
connect(m_player_video, SIGNAL(textureDataReady(const OMX_TextureData*)),
this, SLOT(onTextureReady(const OMX_TextureData*)), Qt::DirectConnection);
connect(m_player_video, SIGNAL(textureDataReady(const OMX_TextureData*)),
this, SIGNAL(textureReady(const OMX_TextureData*)), Qt::DirectConnection);
// Move to a new thread.
moveToThread(&m_thread);
m_thread.start();
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::~OMX_MediaProcessor
+-----------------------------------------------------------------------------*/
OMX_MediaProcessor::~OMX_MediaProcessor()
{
stop();
LOG_VERBOSE(LOG_TAG, "Waiting for thread to die...");
m_thread.quit();
m_thread.wait();
// TODO: Fix this! When freeing players, a lock seems to hang!
LOG_VERBOSE(LOG_TAG, "Freeing players...");
#ifdef ENABLE_SUBTITLES
delete m_player_subtitles;
#endif
delete m_player_audio;
delete m_player_video;
LOG_VERBOSE(LOG_TAG, "Freeing clock...");
m_av_clock->OMXDeinitialize();
delete m_av_clock;
// TODO: This should really be done, but still it seems to sefault sometimes.
LOG_VERBOSE(LOG_TAG, "Deinitializing hardware libs...");
m_OMX->Deinitialize();
m_RBP->Deinitialize();
LOG_VERBOSE(LOG_TAG, "Freeing hints...");
delete m_hints_audio;
delete m_hints_video;
LOG_VERBOSE(LOG_TAG, "Freeing OpenMAX structures...");
delete m_RBP;
delete m_OMX;
delete m_omx_pkt;
delete m_omx_reader;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::filename
+-----------------------------------------------------------------------------*/
QString OMX_MediaProcessor::filename()
{
return m_filename;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::streams
+-----------------------------------------------------------------------------*/
QStringList OMX_MediaProcessor::streams()
{
// TODO: Reimplement.
return QStringList();
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::setFilename
+-----------------------------------------------------------------------------*/
/**
* @brief OMX_MediaProcessor::setFilename Sets the filename and returns the texture
* data that will be used.
* @param filename
* @param textureData
* @return
*/
bool OMX_MediaProcessor::setFilename(QString filename, OMX_TextureData*& textureData)
{
QMutexLocker locker(&m_sendCmd);
if (!checkCurrentThread())
return false;
return setFilenameInt(filename, textureData);
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::setFilenameInt
+-----------------------------------------------------------------------------*/
inline
bool OMX_MediaProcessor::setFilenameInt(QString filename, OMX_TextureData*& textureData)
{
switch (m_state) {
case STATE_INACTIVE:
break;
case STATE_STOPPED:
cleanup();
break;
case STATE_PAUSED:
case STATE_PLAYING:
return false; // TODO: Reimplement.
}
LOG_VERBOSE(LOG_TAG, "Opening %s...", qPrintable(filename));
// It seems that omxplayer expects path and not local URIs.
QUrl url(filename);
if (url.isLocalFile() && filename.startsWith("file://"))
filename = url.path();
if (!m_omx_reader->Open(filename.toStdString(), true)) {
LOG_ERROR(LOG_TAG, "Failed to open source.");
return false;
}
// Emit a signal transmitting the matadata. Anyway, I'm not sure where to check for
// metadata actually changed or not... I emit the signal anyway, then the receiver
// decides.
LOG_VERBOSE(LOG_TAG, "Copy metatada...");
convertMetaData();
emit metadataChanged(m_metadata);
m_filename = filename;
m_has_video = m_omx_reader->VideoStreamCount();
m_has_audio = m_omx_reader->AudioStreamCount();
#ifdef ENABLE_SUBTITLES
m_has_subtitle = m_omx_reader->SubtitleStreamCount();
#endif
LOG_VERBOSE(LOG_TAG, "Initializing OMX clock...");
if (!m_av_clock->OMXInitialize())
return false;
if (ENABLE_HDMI_CLOCK_SYNC && !m_av_clock->HDMIClockSync())
return false;
m_av_clock->OMXStateIdle();
m_av_clock->OMXStop();
m_av_clock->OMXPause();
m_omx_reader->GetHints(OMXSTREAM_AUDIO, *m_hints_audio);
m_omx_reader->GetHints(OMXSTREAM_VIDEO, *m_hints_video);
// Set audio stream to use.
// TODO: Implement a way to change it runtime.
#if 0
m_omx_reader->SetActiveStream(OMXSTREAM_AUDIO, m_audio_index_use);
#endif
// Seek on start?
#if 0
if (m_seek_pos !=0 && m_omx_reader->CanSeek()) {
printf("Seeking start of video to %i seconds\n", m_seek_pos);
m_omx_reader->SeekTime(m_seek_pos * 1000.0f, false, &startpts); // from seconds to DVD_TIME_BASE
}
#endif
if (m_has_video) {
LOG_VERBOSE(LOG_TAG, "Opening video using OMX...");
if (!m_player_video->Open(
*m_hints_video,
m_av_clock,
textureData,
VS_DEINTERLACEMODE_OFF, /* deinterlace */
ENABLE_HDMI_CLOCK_SYNC,
true, /* threaded */
1.0, /* display aspect, unused */
m_video_queue_size, m_video_fifo_size
))
return false;
}
#ifdef ENABLE_SUBTITLES
LOG_VERBOSE(LOG_TAG, "Opening subtitles using OMX...");
if(m_has_subtitle &&
!m_player_subtitles.Open(m_omx_reader.SubtitleStreamCount(),
std::move(external_subtitles),
m_font_path,
m_italic_font_path,
m_font_size,
m_centered,
m_subtitle_lines,
m_av_clock))
return false;
// This is an upper bound check on the subtitle limits. When we pulled the subtitle
// index from the user we check to make sure that the value is larger than zero, but
// we couldn't know without scanning the file if it was too high. If this is the case
// then we replace the subtitle index with the maximum value possible.
if (m_has_subtitle && m_subtitle_index > (m_omx_reader->SubtitleStreamCount() - 1))
m_subtitle_index = m_omx_reader->SubtitleStreamCount() - 1;
#endif
m_omx_reader->GetHints(OMXSTREAM_AUDIO, *m_hints_audio);
#if 0
if ((m_hints_audio.codec == CODEC_ID_AC3 || m_hints_audio.codec == CODEC_ID_EAC3) &&
m_BcmHost.vc_tv_hdmi_audio_supported(EDID_AudioFormat_eAC3, 2, EDID_AudioSampleRate_e44KHz, EDID_AudioSampleSize_16bit ) != 0)
m_passthrough = false;
if (m_hints_audio.codec == CODEC_ID_DTS &&
m_BcmHost.vc_tv_hdmi_audio_supported(EDID_AudioFormat_eDTS, 2, EDID_AudioSampleRate_e44KHz, EDID_AudioSampleSize_16bit ) != 0)
m_passthrough = false;
#endif
LOG_VERBOSE(LOG_TAG, "Opening audio using OMX...");
if (m_has_audio) {
if (!m_player_audio->Open(
*m_hints_audio,
m_av_clock,
m_omx_reader,
"omx:hdmi", /* TODO: implement way to change */
false, /* TODO: passthrough */
false, /* TODO: hw decode */
false, /* TODO: downmix boost */
true, /* threaded */
false, /* is_live */
PCM_LAYOUT_2_0,
m_audio_queue_size, m_audio_fifo_size
))
return false;
if (m_has_audio)
m_player_audio->SetVolume(pow(10, 0 / 2000.0));
}
setState(STATE_STOPPED);
return true;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::play
+-----------------------------------------------------------------------------*/
bool OMX_MediaProcessor::play()
{
// I need to invoke this in another thread (this object is owned by another
// thread).
LOG_VERBOSE(LOG_TAG, "Play");
QMutexLocker locker(&m_sendCmd);
if (!checkCurrentThread())
return false;
switch (m_state) {
case STATE_INACTIVE:
return true;
case STATE_PAUSED:
break;
case STATE_PLAYING:
return true;
case STATE_STOPPED: {
setState(STATE_PLAYING);
#if 1
if (!m_omx_reader->SeekTime(0, true, &startpts)) {
LOG_WARNING(LOG_TAG, "Failed to seek to the beginning.");
return false;
}
flushStreams(startpts);
#endif
//m_av_clock->OMXStart(0.0);
m_av_clock->OMXPause();
m_av_clock->OMXStateExecute();
m_av_clock->OMXResume();
//m_av_clock->OMXMediaTime(0.0D);
LOG_VERBOSE(LOG_TAG, "Starting thread.");
return QMetaObject::invokeMethod(this, "mediaDecoding");
}
default:
return false;
}
setState(STATE_PLAYING);
if (m_av_clock->OMXPlaySpeed() != DVD_PLAYSPEED_NORMAL && m_av_clock->OMXPlaySpeed() != DVD_PLAYSPEED_PAUSE) {
LOG_VERBOSE(LOG_TAG, "resume\n");
m_playspeedCurrent = playspeed_normal;
setSpeed(playspeeds[m_playspeedCurrent]);
m_seekFlush = true;
}
setSpeed(playspeeds[m_playspeedCurrent]);
m_av_clock->OMXResume();
#ifdef ENABLE_SUBTITLES
if (m_has_subtitle)
m_player_subtitles.Resume();
#endif
LOG_INFORMATION(LOG_TAG, "Play command issued.");
return true;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::stop
+-----------------------------------------------------------------------------*/
bool OMX_MediaProcessor::stop()
{
LOG_VERBOSE(LOG_TAG, "Stop");
QMutexLocker locker(&m_sendCmd);
if (!checkCurrentThread())
return false;
switch (m_state) {
case STATE_INACTIVE:
return false;
case STATE_PAUSED:
case STATE_PLAYING:
break;
case STATE_STOPPED:
return true;
default:
return false;
}
m_pendingStop = true;
setState(STATE_STOPPED);
// Wait for command completion.
m_mutexPending.lock();
if (m_pendingStop) {
LOG_VERBOSE(LOG_TAG, "Waiting for the stop command to finish.");
m_waitPendingCommand.wait(&m_mutexPending);
}
m_mutexPending.unlock();
LOG_INFORMATION(LOG_TAG, "Stop command issued.");
return true;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::pause
+-----------------------------------------------------------------------------*/
bool OMX_MediaProcessor::pause()
{
LOG_VERBOSE(LOG_TAG, "Pause");
QMutexLocker locker(&m_sendCmd);
if (!checkCurrentThread())
return false;
switch (m_state) {
case STATE_INACTIVE:
case STATE_STOPPED:
return true;
case STATE_PAUSED:
return true;
case STATE_PLAYING:
break;
default:
return false;
}
#ifdef ENABLE_SUBTITLES
if (m_has_subtitle)
m_player_subtitles.Pause();
#endif
setState(STATE_PAUSED);
setSpeed(playspeeds[m_playspeedCurrent]);
m_av_clock->OMXPause();
// Wait for command completion.
m_mutexPending.lock();
if (m_pendingPause) {
LOG_VERBOSE(LOG_TAG, "Waiting for the pause command to finish.");
m_waitPendingCommand.wait(&m_mutexPending);
}
m_mutexPending.unlock();
LOG_INFORMATION(LOG_TAG, "Pause command issued.");
return true;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::seek
+-----------------------------------------------------------------------------*/
bool OMX_MediaProcessor::seek(qint64 position)
{
LOG_VERBOSE(LOG_TAG, "Seek %lldms.", position);
// Get current position in ms.
qint64 current = m_av_clock->OMXMediaTime(false)*1E-3;
qint64 currentS = qRound64(current/1000.0d);
// position is in ms. Translate to seconds.
int seconds = qRound64(position/1000.0d);
int increment = seconds - currentS;
if (!increment)
return true;
m_incr = increment;
return true;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::onTextureReady
+-----------------------------------------------------------------------------*/
void OMX_MediaProcessor::onTextureReady(const OMX_TextureData* textureData)
{
// It should be safe to const_cast here.
m_textureData = const_cast<OMX_TextureData*>(textureData);
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::currentPosition
+-----------------------------------------------------------------------------*/
qint64 OMX_MediaProcessor::streamPosition()
{
if (!m_av_clock)
return -1;
return m_av_clock->OMXMediaTime(false)*1E-3;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::textureId
+-----------------------------------------------------------------------------*/
OMX_TextureData* OMX_MediaProcessor::textureData()
{
return m_textureData;
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::setVolume
+-----------------------------------------------------------------------------*/
void OMX_MediaProcessor::setVolume(long volume, bool linear)
{
m_player_audio->SetCurrentVolume(volume, linear);
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::volume
+-----------------------------------------------------------------------------*/
long OMX_MediaProcessor::volume(bool linear)
{
return m_player_audio->GetCurrentVolume(linear);
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::setMute
+-----------------------------------------------------------------------------*/
void OMX_MediaProcessor::setMute(bool muted)
{
m_player_audio->SetMuted(muted);
}
bool OMX_MediaProcessor::muted()
{
return m_player_audio->GetMuted();
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::mediaDecoding
+-----------------------------------------------------------------------------*/
void OMX_MediaProcessor::mediaDecoding()
{
// See description in the qmakefile.
//#define ENABLE_PROFILE_MAIN_LOOP
#define ENABLE_PAUSE_FOR_BUFFERING
LOG_VERBOSE(LOG_TAG, "Decoding thread started.");
emit playbackStarted();
// Prealloc.
#ifdef ENABLE_PAUSE_FOR_BUFFERING
float stamp = 0;
float audio_pts = 0;
float video_pts = 0;
float audio_fifo = 0;
float video_fifo = 0;
float threshold = 0;
bool audio_fifo_low = false, video_fifo_low = false, audio_fifo_high = false, video_fifo_high = false;
float m_threshold = 1.0f; //std::min(0.1f, audio_fifo_size * 0.1f);
#endif // ENABLE_PAUSE_FOR_BUFFERING
bool sentStarted = false;
bool sendEos = false;
double m_last_check_time = 0.0;
while (!m_pendingStop) {
#ifdef ENABLE_PROFILE_MAIN_LOOP
static qint64 tot = 0;
static qint64 totNum = 0;
static QElapsedTimer timer;
static int count = 0;
if (tot == 0) {
timer.start();
tot++;
}
else
tot += timer.restart();
totNum++;
if ((count++)%30 == 0) {
//LOG_VERBOSE(LOG_TAG, "Elapsed: %lld", timer.restart());
LOG_VERBOSE(LOG_TAG, "Average: %f.", (double)tot/totNum);
}
#endif
double now = m_av_clock->GetAbsoluteClock();
bool update = false;
if (m_last_check_time == 0.0 || m_last_check_time + DVD_MSEC_TO_TIME(20) <= now) {
update = true;
m_last_check_time = now;
}
// If a request is pending then consider done here.
m_mutexPending.lock();
if (m_pendingPause) {
m_waitPendingCommand.wakeAll();
m_pendingPause = false;
}
m_mutexPending.unlock();
// TODO: Use a semaphore instead.
if (m_state == STATE_PAUSED) {
OMXClock::OMXSleep(2);
continue;
}
if (m_seekFlush || m_incr != 0) {
double seek_pos = 0;
double pts = m_av_clock->OMXMediaTime();
seek_pos = (pts / DVD_TIME_BASE) + m_incr;
seek_pos *= 1000.0;
if (m_omx_reader->SeekTime((int)seek_pos, m_incr < 0.0f, &startpts))
flushStreams(startpts);
//#define ENABLE_FAST_FORWARD_SEEK
#ifdef ENABLE_FAST_FORWARD_SEEK
if (m_incr < 0.0f) {
#endif
m_player_video->Close();
sentStarted = false;
if (m_has_video && !m_player_video->Open(
*m_hints_video,
m_av_clock,
m_textureData,
VS_DEINTERLACEMODE_OFF, /* deinterlace */
ENABLE_HDMI_CLOCK_SYNC,
true, /* threaded */
1.0, /* display aspect, unused */
m_video_queue_size, m_video_fifo_size
)) {
m_incr = 0;
break;
}
#ifdef ENABLE_FAST_FORWARD_SEEK
}
#endif
m_incr = 0;
#ifdef ENABLE_PAUSE_FOR_BUFFERING
m_av_clock->OMXPause();
#endif
#ifdef ENABLE_SUBTITLES
if (m_has_subtitle)
m_player_subtitles.Resume();
#endif
unsigned t = (unsigned)(startpts*1e-6);
LOG_VERBOSE(LOG_TAG, "Seeked to: %02d:%02d:%02d\n", (t/3600), (t/60)%60, t%60);
m_packetAfterSeek = false;
m_seekFlush = false;
}
else if (m_packetAfterSeek && TRICKPLAY(m_av_clock->OMXPlaySpeed())) {
double seek_pos = 0;
double pts = 0;
pts = m_av_clock->OMXMediaTime();
seek_pos = (pts/DVD_TIME_BASE);
seek_pos *= 1000.0;
#if 1
if (m_omx_reader->SeekTime((int)seek_pos, m_av_clock->OMXPlaySpeed() < 0, &startpts))
; //FlushStreams(DVD_NOPTS_VALUE);
#endif // 0
CLog::Log(LOGDEBUG, "Seeked %.0f %.0f %.0f\n", DVD_MSEC_TO_TIME(seek_pos), startpts, m_av_clock->OMXMediaTime());
//unsigned t = (unsigned)(startpts*1e-6);
unsigned t = (unsigned)(pts*1e-6);
printf("Seek to: %02d:%02d:%02d\n", (t/3600), (t/60)%60, t%60);
m_packetAfterSeek = false;
}
// TODO: Better error handling.
if (m_player_audio->Error()) {
LOG_ERROR(LOG_TAG, "Audio player error. emergency exit!");
break;
}
if (update) {
#ifdef ENABLE_PAUSE_FOR_BUFFERING
/* when the video/audio fifos are low, we pause clock, when high we resume */
stamp = m_av_clock->OMXMediaTime();
audio_pts = m_player_audio->GetCurrentPTS();
video_pts = m_player_video->GetCurrentPTS();
if (0 && m_av_clock->OMXIsPaused()) {
double old_stamp = stamp;
if (audio_pts != DVD_NOPTS_VALUE && (stamp == 0 || audio_pts < stamp))
stamp = audio_pts;
if (video_pts != DVD_NOPTS_VALUE && (stamp == 0 || video_pts < stamp))
stamp = video_pts;
if (old_stamp != stamp)
{
m_av_clock->OMXMediaTime(stamp);
stamp = m_av_clock->OMXMediaTime();
}
}
audio_fifo = audio_pts == DVD_NOPTS_VALUE ? 0.0f : audio_pts / DVD_TIME_BASE - stamp * 1e-6;
video_fifo = video_pts == DVD_NOPTS_VALUE ? 0.0f : video_pts / DVD_TIME_BASE - stamp * 1e-6;
threshold = min(0.1f, (float)m_player_audio->GetCacheTotal()*0.1f);
#endif // ENABLE_PAUSE_FOR_BUFFERING
#if 0
static int count;
if ((count++ & 15) == 0) {
LOG_VERBOSE(LOG_TAG, "M: %8.02f V : %8.02f %8d %8d A : %8.02f %8.02f/%8.02f Cv : %8d Ca : %8d \r", stamp,
audio_fifo, m_player_video->GetDecoderBufferSize(), m_player_video->GetDecoderFreeSpace(),
video_fifo, m_player_audio->GetDelay(), m_player_audio->GetCacheTotal(),
m_player_video->GetCached(), m_player_audio->GetCached());
}
#endif
#if 0
if(m_tv_show_info) {
static unsigned count;
if ((count++ & 15) == 0) {
char response[80];
if (m_player_video.GetDecoderBufferSize() && m_player_audio.GetCacheTotal())
vc_gencmd(response, sizeof response, "render_bar 4 video_fifo %d %d %d %d",
(int)(100.0*m_player_video.GetDecoderBufferSize()-m_player_video.GetDecoderFreeSpace())/m_player_video.GetDecoderBufferSize(),
(int)(100.0*video_fifo/m_player_audio.GetCacheTotal()),
0, 100);
if (m_player_audio.GetCacheTotal())
vc_gencmd(response, sizeof response, "render_bar 5 audio_fifo %d %d %d %d",
(int)(100.0*audio_fifo/m_player_audio.GetCacheTotal()),
(int)(100.0*m_player_audio.GetDelay()/m_player_audio.GetCacheTotal()),
0, 100);
vc_gencmd(response, sizeof response, "render_bar 6 video_queue %d %d %d %d",
m_player_video.GetLevel(), 0, 0, 100);
vc_gencmd(response, sizeof response, "render_bar 7 audio_queue %d %d %d %d",
m_player_audio.GetLevel(), 0, 0, 100);
}
}
#endif
#ifdef ENABLE_PAUSE_FOR_BUFFERING
if (audio_pts != DVD_NOPTS_VALUE) {
audio_fifo_low = m_has_audio && audio_fifo < threshold;
audio_fifo_high = !m_has_audio || (audio_pts != DVD_NOPTS_VALUE && audio_fifo > m_threshold);
}
if (video_pts != DVD_NOPTS_VALUE) {
video_fifo_low = m_has_video && video_fifo < threshold;
video_fifo_high = !m_has_video || (video_pts != DVD_NOPTS_VALUE && video_fifo > m_threshold);
}
// Enable this to enable pause for buffering.
if (m_state != STATE_PAUSED && (m_omx_reader->IsEof() || m_omx_pkt || TRICKPLAY(m_av_clock->OMXPlaySpeed()) || (audio_fifo_high && video_fifo_high)))
{
if (m_av_clock->OMXIsPaused())
{
CLog::Log(LOGDEBUG, "Resume %.2f,%.2f (%d,%d,%d,%d) EOF:%d PKT:%p\n", audio_fifo, video_fifo, audio_fifo_low, video_fifo_low, audio_fifo_high, video_fifo_high, m_omx_reader->IsEof(), m_omx_pkt);
log_verbose("Pausing for buffering...");
m_av_clock->OMXStateExecute();
m_av_clock->OMXResume();
}
}
else if (m_state == STATE_PAUSED || audio_fifo_low || video_fifo_low)
{
if (!m_av_clock->OMXIsPaused())
{
if (m_state != STATE_PAUSED)
m_threshold = std::min(2.0f*m_threshold, 16.0f);
CLog::Log(LOGDEBUG, "Pause %.2f,%.2f (%d,%d,%d,%d) %.2f\n", audio_fifo, video_fifo, audio_fifo_low, video_fifo_low, audio_fifo_high, video_fifo_high, m_threshold);
log_verbose("Buffering completed. Resuming...");
m_av_clock->OMXPause();
}
}
#endif
}
if (!sentStarted)
{
CLog::Log(LOGDEBUG, "COMXPlayer::HandleMessages - player started RESET");
m_av_clock->OMXReset(m_has_video, m_has_audio);
m_av_clock->OMXStateExecute();
sentStarted = true;
}
if (!m_omx_pkt)
m_omx_pkt = m_omx_reader->Read();
if (m_omx_pkt)
sendEos = false;
if (m_omx_reader->IsEof() && !m_omx_pkt) {
// demuxer EOF, but may have not played out data yet
if ((m_has_video && m_player_video->GetCached()) ||
(m_has_audio && m_player_audio->GetCached())) {
OMXClock::OMXSleep(10);
continue;
}
if (!sendEos && m_has_video)
m_player_video->SubmitEOS();
if (!sendEos && m_has_audio)
m_player_audio->SubmitEOS();
sendEos = true;
if ((m_has_video && !m_player_video->IsEOS()) ||
(m_has_audio && !m_player_audio->IsEOS()) ) {
OMXClock::OMXSleep(10);
continue;
}
break;
}
if (!m_omx_pkt)
m_omx_pkt = m_omx_reader->Read();
if(m_omx_pkt)
sendEos = false;
if(m_omx_reader->IsEof() && !m_omx_pkt) {
// demuxer EOF, but may have not played out data yet
if ( (m_has_video && m_player_video->GetCached()) ||
(m_has_audio && m_player_audio->GetCached()) )
{
OMXClock::OMXSleep(10);
continue;
}
if (!sendEos && m_has_video)
m_player_video->SubmitEOS();
if (!sendEos && m_has_audio)
m_player_audio->SubmitEOS();
sendEos = true;
if ( (m_has_video && !m_player_video->IsEOS()) ||
(m_has_audio && !m_player_audio->IsEOS()) )
{
OMXClock::OMXSleep(10);
continue;
}
break;
}
if(m_has_video && m_omx_pkt && m_omx_reader->IsActive(OMXSTREAM_VIDEO, m_omx_pkt->stream_index))
{
if (TRICKPLAY(m_av_clock->OMXPlaySpeed()))
{
m_packetAfterSeek = true;
}
if(m_player_video->AddPacket(m_omx_pkt))
m_omx_pkt = NULL;
else
OMXClock::OMXSleep(10);
}
else if(m_has_audio && m_omx_pkt && !TRICKPLAY(m_av_clock->OMXPlaySpeed()) && m_omx_pkt->codec_type == AVMEDIA_TYPE_AUDIO)
{
if(m_player_audio->AddPacket(m_omx_pkt))
m_omx_pkt = NULL;
else
OMXClock::OMXSleep(10);
}
#ifdef ENABLE_SUBTITLES
else if(m_has_subtitle && m_omx_pkt && !TRICKPLAY(m_av_clock->OMXPlaySpeed()) &&
m_omx_pkt->codec_type == AVMEDIA_TYPE_SUBTITLE)
{
auto result = m_player_subtitles.AddPacket(m_omx_pkt,
m_omx_reader.GetRelativeIndex(m_omx_pkt->stream_index));
if (result)
m_omx_pkt = NULL;
else
OMXClock::OMXSleep(10);
}
#endif
else
{
if(m_omx_pkt)
{
m_omx_reader->FreePacket(m_omx_pkt);
m_omx_pkt = NULL;
}
else
OMXClock::OMXSleep(10);
}
}
LOG_VERBOSE(LOG_TAG, "Stopping OMX clock...");
//m_av_clock->OMXResume();
m_av_clock->OMXStop();
m_av_clock->OMXStateIdle();
//m_av_clock->OMXStateExecute();
//m_av_clock->OMXReset(m_has_video, m_has_audio);
// Restart video player. It seems it is necessary to close
// and open again. omxplayer does this also when seeking so
// maybe there is no other way to restart it.
m_player_video->Close();
if (m_has_video)
if (!m_player_video->Open(
*m_hints_video,
m_av_clock,
m_textureData,
VS_DEINTERLACEMODE_OFF, /* deinterlace */
ENABLE_HDMI_CLOCK_SYNC,
true, /* threaded */
1.0, /* display aspect, unused */
m_video_queue_size, m_video_fifo_size
)) {
LOG_ERROR(LOG_TAG, "Failed to reopen media.");
}
// TODO: Handle failure.
setState(STATE_STOPPED);
emit playbackCompleted();
// Actually change the state here and reset flags.
m_mutexPending.lock();
if (m_pendingStop) {
m_pendingStop = false;
m_waitPendingCommand.wakeAll();
}
m_mutexPending.unlock();
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::setSpeed
+-----------------------------------------------------------------------------*/
inline
void OMX_MediaProcessor::setSpeed(int iSpeed)
{
if (!m_av_clock)
return;
m_omx_reader->SetSpeed(iSpeed);
// flush when in trickplay mode
if (TRICKPLAY(iSpeed) || TRICKPLAY(m_av_clock->OMXPlaySpeed()))
flushStreams(DVD_NOPTS_VALUE);
m_av_clock->OMXSetSpeed(iSpeed);
}
/*------------------------------------------------------------------------------
| OMX_MediaProcessor::flushStreams
+-----------------------------------------------------------------------------*/
void OMX_MediaProcessor::flushStreams(double pts)
{
m_av_clock->OMXStop();
m_av_clock->OMXPause();
if (m_has_video)
m_player_video->Flush();
if (m_has_audio)
m_player_audio->Flush();
if (pts != DVD_NOPTS_VALUE)
m_av_clock->OMXMediaTime(0.0);