vdr 2.8.4
dvbplayer.c
Go to the documentation of this file.
1/*
2 * dvbplayer.c: The DVB player
3 *
4 * See the main source file 'vdr.c' for copyright information and
5 * how to reach the author.
6 *
7 * $Id: dvbplayer.c 5.21 2026/10/24 09:32:41 kls Exp $
8 */
9
10#include "dvbplayer.h"
11#include <math.h>
12#include <stdlib.h>
13#include "remux.h"
14#include "ringbuffer.h"
15#include "thread.h"
16#include "tools.h"
17
18// --- cPtsIndex -------------------------------------------------------------
19
20#define PTSINDEX_ENTRIES 1024
21
22class cPtsIndex {
23private:
24 struct tPtsIndex {
25 uint32_t pts; // no need for 33 bit - some devices don't even supply the msb
26 int index;
28 };
30 int w, r;
33public:
34 cPtsIndex(void);
35 void Clear(void);
36 bool IsEmpty(void);
37 void Put(uint32_t Pts, int Index, bool Independent);
38 int FindIndex(uint32_t Pts, bool Still);
39 int FindFrameNumber(uint32_t Pts, bool Forward, bool Still);
40 };
41
43{
44 lastFound = 0;
45 Clear();
46}
47
49{
50 cMutexLock MutexLock(&mutex);
51 w = r = 0;
52}
53
55{
56 cMutexLock MutexLock(&mutex);
57 return w == r;
58}
59
60void cPtsIndex::Put(uint32_t Pts, int Index, bool Independent)
61{
62 cMutexLock MutexLock(&mutex);
63 pi[w].pts = Pts;
64 pi[w].independent = Independent;
65 pi[w].index = Index;
66 w = (w + 1) % PTSINDEX_ENTRIES;
67 if (w == r)
68 r = (r + 1) % PTSINDEX_ENTRIES;
69}
70
71int cPtsIndex::FindIndex(uint32_t Pts, bool Still)
72{
73 cMutexLock MutexLock(&mutex);
74 if (w == r || Pts == 0 && !Still) // while 0 is a valid PTS, DeviceGetSTC() might return 0 if, after a jump, the device hasn't displayed a frame, yet
75 return lastFound; // list is empty, let's not jump way off the last known position
76 uint32_t Delta = 0xFFFFFFFF;
77 int Index = -1;
78 for (int i = w; i != r; ) {
79 if (--i < 0)
80 i = PTSINDEX_ENTRIES - 1;
81 uint32_t d = pi[i].pts < Pts ? Pts - pi[i].pts : pi[i].pts - Pts;
82 if (d > 0x7FFFFFFF)
83 d = 0xFFFFFFFF - d; // handle rollover
84 if (d < Delta) {
85 Delta = d;
86 Index = pi[i].index;
87 }
88 }
89 lastFound = Index;
90 return Index;
91}
92
93int cPtsIndex::FindFrameNumber(uint32_t Pts, bool Forward, bool Still)
94{
95 if (!Forward)
96 return FindIndex(Pts, Still); // there are only I frames in backward
97 cMutexLock MutexLock(&mutex);
98 if (w == r || Pts == 0 && !Still) // while 0 is a valid PTS, DeviceGetSTC() might return 0 if, after a jump, the device hasn't displayed a frame, yet
99 return lastFound; // replay always starts at an I frame
100 bool Valid = false;
101 int FrameNumber = 0;
102 int UnplayedIFrame = 2; // GOPs may intersect, so we loop until we processed a complete unplayed GOP
103 for (int i = r; i != w && UnplayedIFrame; ) {
104 int32_t d = int32_t(Pts - pi[i].pts); // typecast handles rollover
105 if (d >= 0) {
106 if (pi[i].independent) {
107 FrameNumber = pi[i].index; // an I frame's index represents its frame number
108 Valid = true;
109 if (d == 0)
110 UnplayedIFrame = 1; // if Pts is at an I frame we only need to check up to the next I frame
111 }
112 else
113 FrameNumber++; // for every played non-I frame, increase frame number
114 }
115 else if (pi[i].independent)
116 --UnplayedIFrame;
117 if (++i >= PTSINDEX_ENTRIES)
118 i = 0;
119 }
120 if (Valid) {
121 lastFound = FrameNumber;
122 return FrameNumber;
123 }
124 return FindIndex(Pts, Still); // fall back during trick speeds
125}
126
127// --- cNonBlockingFileReader ------------------------------------------------
128
130private:
132 // changed under the thread lock, read without it by Reading() and WaitForDataMs():
133 std::atomic<uchar *> buffer;
134 std::atomic<int> wanted;
135 std::atomic<int> length;
139protected:
140 void Action(void);
141public:
144 void Clear(void);
145 void Request(cUnbufferedFile *File, int Length);
146 int Result(uchar **Buffer);
147 bool Reading(void) { return buffer; }
148 bool WaitForDataMs(int msToWait);
149 };
150
152:cThread("non blocking file reader")
153{
154 f = NULL;
155 buffer = NULL;
156 wanted = length = 0;
157 Start();
158}
159
166
168{
169 Lock();
170 f = NULL;
171 free(buffer);
172 buffer = NULL;
173 wanted = length = 0;
174 Unlock();
175}
176
178{
179 Lock();
180 Clear();
181 wanted = Length;
183 f = File;
184 Unlock();
185 newSet.Signal();
186}
187
189{
191 if (buffer && length == wanted) {
192 *Buffer = buffer;
193 buffer = NULL;
194 return wanted;
195 }
196 errno = EAGAIN;
197 return -1;
198}
199
201{
202 while (Running()) {
203 Lock();
204 if (f && buffer && length < wanted) {
205 uchar *b = buffer;
206 int r = f->Read(b + length, wanted - length);
207 if (r > 0)
208 length += r;
209 else if (r == 0) { // r == 0 means EOF
210 if (length > 0)
211 wanted = length.load(); // already read something, so return the rest
212 else
213 length = wanted = 0; // report EOF
214 }
215 else if (FATALERRNO) {
216 LOG_ERROR;
217 length = wanted = r; // this will forward the error status to the caller
218 }
219 if (length == wanted) {
220 cMutexLock NewDataLock(&newDataMutex);
222 }
223 }
224 Unlock();
225 newSet.Wait(1000);
226 }
227}
228
230{
231 cMutexLock NewDataLock(&newDataMutex);
232 if (buffer && length == wanted)
233 return true;
234 return newDataCond.TimedWait(newDataMutex, msToWait);
235}
236
237// --- cDvbPlayer ------------------------------------------------------------
238
239#define PLAYERBUFSIZE (MAXFRAMESIZE * 5)
240
241#define RESUMEBACKUP 10 // number of seconds to back up when resuming an interrupted replay session
242#define MAXSTUCKATEOF 3 // max. number of seconds to wait in case the device doesn't play the last frame
243
244class cDvbPlayer : public cPlayer, cThread {
245private:
248 static int Speeds[];
252 const cMarks *marks;
259 std::atomic<bool> eof; // cleared by NextFile() from the main thread, tested by Action() before sleeping
261 // changed under LOCK_THREAD, read without it in Action() and by the Get*() functions:
262 std::atomic<ePlayModes> playMode;
263 std::atomic<ePlayDirs> playDir;
264 std::atomic<int> trickSpeed;
265 std::atomic<int> readIndex;
270 void TrickSpeed(int Increment);
271 void Empty(void);
272 bool NextFile(uint16_t FileNumber = 0, off_t FileOffset = -1);
273 int Resume(void);
274 bool Save(void);
275protected:
276 virtual void Activate(bool On) override;
277 virtual void Action(void) override;
278public:
279 cDvbPlayer(const char *FileName, bool PauseLive);
280 virtual ~cDvbPlayer() override;
281 void SetMarks(const cMarks *Marks);
282 bool Active(void) { return cThread::Running(); }
283 void Pause(void);
284 void Play(void);
285 void Forward(void);
286 void Backward(void);
287 int SkipFrames(int Frames);
288 void SkipSeconds(int Seconds);
289 void Goto(int Position, bool Still = false);
290 virtual double FramesPerSecond(void) override { return framesPerSecond; }
291 virtual void SetAudioTrack(eTrackType Type, const tTrackId *TrackId) override;
292 virtual const cErrors *GetErrors(void) override;
293 virtual bool GetIndex(int &Current, int &Total, bool SnapToIFrame = false) override;
294 virtual bool GetFrameNumber(int &Current, int &Total) override;
295 virtual bool GetReplayMode(bool &Play, bool &Forward, int &Speed) override;
296 };
297
298#define MAX_VIDEO_SLOWMOTION 63 // max. arg to pass to VIDEO_SLOWMOTION // TODO is this value correct?
299#define NORMAL_SPEED 4 // the index of the '1' entry in the following array
300#define MAX_SPEEDS 3 // the offset of the maximum speed from normal speed in either direction
301#define SPEED_MULT 12 // the speed multiplier
302int cDvbPlayer::Speeds[] = { 0, -2, -4, -8, 1, 2, 4, 12, 0 };
303
304cDvbPlayer::cDvbPlayer(const char *FileName, bool PauseLive)
305:cThread("dvbplayer")
306{
308 ringBuffer = NULL;
309 marks = NULL;
310 index = NULL;
311 cRecording Recording(FileName);
312 framesPerSecond = Recording.FramesPerSecond();
313 isPesRecording = Recording.IsPesRecording();
314 pauseLive = PauseLive;
315 eof = false;
316 firstPacket = true;
320 readIndex = -1;
321 readIndependent = false;
322 readFrame = NULL;
323 playFrame = NULL;
324 resyncAfterPause = false;
325 isyslog("replay %s", FileName);
326 fileName = new cFileName(FileName, false, false, isPesRecording);
328 if (!replayFile)
329 return;
331 // Create the index file:
332 index = new cIndexFile(FileName, false, isPesRecording, pauseLive);
333 if (!index)
334 esyslog("ERROR: can't allocate index");
335 else if (!index->Ok()) {
336 delete index;
337 index = NULL;
338 }
339 else if (PauseLive)
340 framesPerSecond = cRecording(FileName).FramesPerSecond(); // the fps rate might have changed from the default
341}
342
344{
345 Save();
346 Detach();
347 delete readFrame; // might not have been stored in the buffer in Action()
348 delete index;
349 delete fileName;
350 delete ringBuffer;
351 // don't delete marks here, we don't own them!
352}
353
354void cDvbPlayer::SetMarks(const cMarks *Marks)
355{
356 marks = Marks;
357}
358
359void cDvbPlayer::TrickSpeed(int Increment)
360{
361 int nts = trickSpeed + Increment;
362 if (Speeds[nts] == 1) {
363 trickSpeed = nts;
364 if (playMode == pmFast)
365 Play();
366 else
367 Pause();
368 }
369 else if (Speeds[nts]) {
370 trickSpeed = nts;
371 int Mult = (playMode == pmSlow && playDir == pdForward) ? 1 : SPEED_MULT;
372 int sp = (Speeds[nts] > 0) ? Mult / Speeds[nts] : -Speeds[nts] * Mult;
373 if (sp > MAX_VIDEO_SLOWMOTION)
376 }
377}
378
380{
384 if (!firstPacket) // don't set the readIndex twice if Empty() is called more than once
385 readIndex = ptsIndex.FindIndex(DeviceGetSTC(), playMode == pmStill) - 1; // Action() will first increment it!
386 delete readFrame; // might not have been stored in the buffer in Action()
387 readFrame = NULL;
388 playFrame = NULL;
389 ringBuffer->Clear();
390 ptsIndex.Clear();
391 DeviceClear();
392 firstPacket = true;
393}
394
395bool cDvbPlayer::NextFile(uint16_t FileNumber, off_t FileOffset)
396{
397 if (FileNumber > 0)
398 replayFile = fileName->SetOffset(FileNumber, FileOffset);
399 else if (replayFile && eof)
401 eof = false;
402 return replayFile != NULL;
403}
404
406{
407 if (index) {
408 int Index = index->GetResume();
409 if (Index < 0)
410 index->StoreResume(0); // resume file doesn't exist, so create it to have the recording marked as "last replayed"
411 else {
412 uint16_t FileNumber;
413 off_t FileOffset;
414 if (index->Get(Index, &FileNumber, &FileOffset) && NextFile(FileNumber, FileOffset)) {
415 index->StoreResume(Index); // to have the recording marked as "last replayed"
416 return Index;
417 }
418 }
419 }
420 return -1;
421}
422
424{
425 if (index) {
427 if (Index >= 0) {
428 if (Setup.SkipEdited && marks) {
429 cStateKey StateKey;
430 marks->Lock(StateKey);
431 if (marks->First() && abs(Index - marks->First()->Position()) <= int(round(RESUMEBACKUP * framesPerSecond)))
432 Index = 0; // when stopping within RESUMEBACKUP seconds of the first mark the recording shall still be considered unviewed
433 StateKey.Remove();
434 }
435 Index -= int(round(RESUMEBACKUP * framesPerSecond));
436 if (Index > 0)
437 Index = index->GetNextIFrame(Index, false);
438 else
439 Index = 0;
440 if (Index >= 0)
441 return index->StoreResume(Index);
442 }
443 }
444 return false;
445}
446
448{
449 if (On) {
450 if (replayFile)
451 Start();
452 }
453 else
454 Cancel(9);
455}
456
458{
459 uchar *p = NULL;
460 int pc = 0;
461
462 readIndex = Resume();
463 if (readIndex > 0)
464 isyslog("resuming replay at index %d (%s)", readIndex.load(), *IndexToHMSF(readIndex, true, framesPerSecond));
465 else if (Setup.SkipEdited && marks) {
466 cStateKey StateKey;
467 marks->Lock(StateKey);
468 if (marks->First() && index) {
469 int Index = marks->First()->Position();
470 uint16_t FileNumber;
471 off_t FileOffset;
472 if (index->Get(Index, &FileNumber, &FileOffset) && NextFile(FileNumber, FileOffset)) {
473 isyslog("starting replay at first mark %d (%s)", Index, *IndexToHMSF(Index, true, framesPerSecond));
474 readIndex = Index;
475 }
476 }
477 StateKey.Remove();
478 }
479 if (readIndex > 0) // will first be incremented in the loop!
480 --readIndex;
481
483 int Length = 0;
484 bool Sleep = false;
485 bool WaitingForData = false;
486 time_t StuckAtEof = 0;
487 uint32_t LastStc = 0;
488 int SwitchToPlayFrame = 0;
489 bool CutIn = false;
490 bool AtLastMark = false;
491 int64_t EndPts = -1;
492
493 if (pauseLive)
494 Goto(0, true);
495 while (Running()) {
496 if (WaitingForData)
497 WaitingForData = !nonBlockingFileReader->WaitForDataMs(3); // this keeps the CPU load low, but reacts immediately on new data
498 else if (Sleep) {
499 cPoller Poller;
500 DevicePoll(Poller, 10);
501 Sleep = false;
502 if (playMode == pmStill || playMode == pmPause || eof)
504 }
505 {
507
508 // Read the next frame from the file:
509
510 if (playMode != pmStill && playMode != pmPause) {
511 if (!readFrame && (replayFile || readIndex >= 0)) {
513 if (!SwitchToPlayFrame && (playMode == pmFast || (playMode == pmSlow && playDir == pdBackward))) {
514 uint16_t FileNumber;
515 off_t FileOffset;
516 bool TimeShiftMode = index->IsStillRecording();
517 int Index = -1;
518 readIndependent = false;
520 if (index->Get(readIndex + 1, &FileNumber, &FileOffset, &readIndependent, &Length))
521 Index = readIndex + 1;
522 }
523 else {
524 int d = int(round(0.4 * framesPerSecond));
525 if (playDir != pdForward)
526 d = -d;
527 int NewIndex = readIndex + d;
528 if (NewIndex <= 0 && readIndex > 0)
529 NewIndex = 1; // make sure the very first frame is delivered
530 NewIndex = index->GetNextIFrame(NewIndex, playDir == pdForward, &FileNumber, &FileOffset, &Length);
531 if (NewIndex < 0 && TimeShiftMode && playDir == pdForward)
532 SwitchToPlayFrame = readIndex;
533 Index = NewIndex;
534 readIndependent = true;
535 }
536 if (Index >= 0) {
537 readIndex = Index;
538 if (!NextFile(FileNumber, FileOffset))
539 continue;
540 }
541 else if (!(TimeShiftMode && playDir == pdForward))
542 eof = true;
543 }
544 else if (index) {
545 uint16_t FileNumber;
546 off_t FileOffset;
547 if (index->Get(readIndex + 1, &FileNumber, &FileOffset, &readIndependent, &Length) && NextFile(FileNumber, FileOffset)) {
548 readIndex++;
550 cStateKey StateKey;
551 marks->Lock(StateKey);
552 const cMark *m = marks->Get(readIndex);
553 if (m && (m->Index() & 0x01) != 0) { // we're at an end mark
554 m = marks->GetNextBegin(m);
555 int Index = -1;
556 if (m)
557 Index = m->Position(); // skip to next begin mark
558 else if (Setup.PauseAtLastMark)
559 AtLastMark = true; // triggers going into Pause mode
560 else if (index->IsStillRecording())
561 Index = index->GetNextIFrame(index->Last() - int(round(MAXSTUCKATEOF * framesPerSecond)), false); // skip, but stay off end of live-recordings
562 else
563 AtLastMark = true; // triggers stopping replay
564 if (Setup.SkipEdited && Index > readIndex) {
565 isyslog("skipping from %d (%s) to %d (%s)", readIndex - 1, *IndexToHMSF(readIndex - 1, true, framesPerSecond), Index, *IndexToHMSF(Index, true, framesPerSecond));
566 readIndex = Index;
567 CutIn = true;
568 }
569 }
570 StateKey.Remove();
571 }
572 }
573 else
574 eof = true;
575 }
576 else // allows replay even if the index file is missing
577 Length = MAXFRAMESIZE;
578 if (Length == -1)
579 Length = MAXFRAMESIZE; // this means we read up to EOF (see cIndex)
580 else if (Length > MAXFRAMESIZE) {
581 esyslog("ERROR: frame larger than buffer (%d > %d)", Length, MAXFRAMESIZE);
582 Length = MAXFRAMESIZE;
583 }
584 if (!eof)
586 }
587 if (!eof) {
588 uchar *b = NULL;
589 int r = nonBlockingFileReader->Result(&b);
590 if (r > 0) {
591 WaitingForData = false;
592 int64_t Pts = isPesRecording ? (PesHasPts(b) ? PesGetPts(b) : -1) : TsGetPts(b, r);
593 readFrame = new cFrame(b, -r, ftUnknown, readIndex, Pts, readIndependent); // hands over b to the ringBuffer
594 if (AtLastMark) {
595 EndPts = Pts;
596 AtLastMark = false;
597 }
598 }
599 else if (r < 0) {
600 if (errno == EAGAIN)
601 WaitingForData = true;
602 else if (FATALERRNO) {
603 LOG_ERROR;
604 break;
605 }
606 }
607 else
608 eof = true;
609 }
610 }
611
612 // Store the frame in the buffer:
613
614 if (readFrame) {
615 if (CutIn) {
616 if (isPesRecording)
618 CutIn = false;
619 }
621 readFrame = NULL;
622 else
623 Sleep = true;
624 }
625 }
626 else {
627 StuckAtEof = 0; // the STC doesn't advance while paused, so this is no stall
628 Sleep = true;
629 continue;
630 }
631
632 // Get the next frame from the buffer:
633
634 if (!playFrame) {
636 p = NULL;
637 pc = 0;
638 }
639
640 // Play the frame:
641
642 if (playFrame) {
643 eof = false;
644 StuckAtEof = 0;
645 if (!p) {
646 p = playFrame->Data();
647 pc = playFrame->Count();
648 if (p) {
649 if (playFrame->Index() >= 0 && playFrame->Pts() != 0)
651 if (firstPacket) {
652 if (isPesRecording) {
653 PlayPes(NULL, 0);
655 }
656 else
657 PlayTs(NULL, 0);
658 firstPacket = false;
659 }
660 }
661 }
662 if (p) {
663 int w;
664 bool VideoOnly = playMode != pmPlay && !(playMode == pmSlow && playDir == pdForward) && DeviceIsPlayingVideo();
665 if (isPesRecording)
666 w = PlayPes(p, pc, VideoOnly);
667 else
668 w = PlayTs(p, pc, VideoOnly);
669 if (w > 0) {
670 p += w;
671 pc -= w;
672 }
673 else if (w < 0 && FATALERRNO)
674 LOG_ERROR;
675 else
676 Sleep = true;
677 }
678 if (pc <= 0) {
680 playFrame = NULL;
681 p = NULL;
682 }
683 if ((Setup.SkipEdited || Setup.PauseAtLastMark) && EndPts >= 0) {
684 if (PtsDiff(DeviceGetSTC(), EndPts) <= 0) {
686 DeviceFreeze();
688 }
689 else
690 break; // stop replay
691 EndPts = -1;
692 }
693 }
694 }
695 else
696 Sleep = true;
697
698 // Handle hitting begin/end of recording:
699
700 if (eof || SwitchToPlayFrame) {
701 bool SwitchToPlay = false;
702 // In a recording that is still being written, eof in forward play is only a stall:
703 bool Ended = eof && (!index || !index->IsStillRecording() || playDir != pdForward);
704 uint32_t Stc = DeviceGetSTC();
705 if (playMode == pmPause)
706 StuckAtEof = 0;
707 else if (Ended && DeviceDrain() || (!eof || Ended) && StuckAtEof && time(NULL) - StuckAtEof > MAXSTUCKATEOF) {
708 if (playDir == pdForward)
709 break; // automatically stop at end of recording
710 SwitchToPlay = true;
711 }
712 else if (Stc != LastStc)
713 StuckAtEof = 0;
714 else if (!StuckAtEof)
715 StuckAtEof = time(NULL);
716 LastStc = Stc;
717 int Index = ptsIndex.FindIndex(Stc, playMode == pmStill);
718 if ((playDir != pdForward || SwitchToPlayFrame) && (Index <= 0 || SwitchToPlayFrame && Index >= SwitchToPlayFrame))
719 SwitchToPlay = true;
720 if (SwitchToPlay) {
721 if (!SwitchToPlayFrame) {
722 Empty();
723 StuckAtEof = 0;
724 }
725 DevicePlay();
728 SwitchToPlayFrame = 0;
729 }
730 }
731 }
732 }
733
736 delete nbfr;
737}
738
740{
741 if (playMode == pmPause || playMode == pmStill)
742 Play();
743 else {
745 if (playMode == pmFast || (playMode == pmSlow && playDir == pdBackward)) {
747 Empty();
748 }
749 DeviceFreeze();
751 }
752}
753
755{
756 if (playMode != pmPlay) {
758 if (playMode == pmStill || playMode == pmFast || (playMode == pmSlow && playDir == pdBackward)) {
760 Empty();
761 }
762 DevicePlay();
765 if (resyncAfterPause) {
766 int Current, Total;
767 if (GetIndex(Current, Total, true))
768 Goto(Current);
769 resyncAfterPause = false;
770 }
771 }
772}
773
775{
776 if (index) {
777 switch (playMode) {
778 case pmFast:
779 if (Setup.MultiSpeedMode) {
780 TrickSpeed(playDir == pdForward ? 1 : -1);
781 break;
782 }
783 else if (playDir == pdForward) {
784 Play();
785 break;
786 }
787 // run into pmPlay
788 case pmPlay: {
791 Empty();
793 DeviceMute();
798 }
799 break;
800 case pmSlow:
801 if (Setup.MultiSpeedMode) {
802 TrickSpeed(playDir == pdForward ? -1 : 1);
803 break;
804 }
805 else if (playDir == pdForward) {
806 Pause();
807 break;
808 }
809 Empty();
810 // run into pmPause
811 case pmStill:
812 case pmPause: {
814 DeviceMute();
819 }
820 break;
821 default: esyslog("ERROR: unknown playMode %d (%s)", playMode.load(), __FUNCTION__);
822 }
823 }
824}
825
827{
828 if (index) {
829 switch (playMode) {
830 case pmFast:
831 if (Setup.MultiSpeedMode) {
832 TrickSpeed(playDir == pdBackward ? 1 : -1);
833 break;
834 }
835 else if (playDir == pdBackward) {
836 Play();
837 break;
838 }
839 // run into pmPlay
840 case pmPlay: {
843 Empty();
845 DeviceMute();
850 }
851 break;
852 case pmSlow:
853 if (Setup.MultiSpeedMode) {
854 TrickSpeed(playDir == pdBackward ? -1 : 1);
855 break;
856 }
857 else if (playDir == pdBackward) {
858 Pause();
859 break;
860 }
861 // run into pmPause
862 case pmStill:
863 case pmPause: {
865 Empty();
866 DeviceMute();
871 }
872 break;
873 default: esyslog("ERROR: unknown playMode %d (%s)", playMode.load(), __FUNCTION__);
874 }
875 }
876}
877
879{
880 if (index && Frames) {
881 int Current, Total;
882 GetIndex(Current, Total, true);
883 int OldCurrent = Current;
884 // As GetNextIFrame() increments/decrements at least once, the
885 // destination frame (= Current + Frames) must be adjusted by
886 // -1/+1 respectively.
887 Current = index->GetNextIFrame(Current + Frames + (Frames > 0 ? -1 : 1), Frames > 0);
888 return Current >= 0 ? Current : OldCurrent;
889 }
890 return -1;
891}
892
893void cDvbPlayer::SkipSeconds(int Seconds)
894{
895 if (index && Seconds) {
898 Empty();
899 if (Index >= 0) {
900 Index = max(Index + SecondsToFrames(Seconds, framesPerSecond), 0);
901 if (Index > 0)
902 Index = index->GetNextIFrame(Index, false, NULL, NULL, NULL);
903 if (Index >= 0)
904 readIndex = Index - 1; // Action() will first increment it!
905 }
906 Play();
907 }
908}
909
910void cDvbPlayer::Goto(int Index, bool Still)
911{
912 if (index) {
914 Empty();
915 if (++Index <= 0)
916 Index = 1; // not '0', to allow GetNextIFrame() below to work!
917 uint16_t FileNumber;
918 off_t FileOffset;
919 int Length;
920 Index = index->GetNextIFrame(Index, false, &FileNumber, &FileOffset, &Length);
921 if (Index >= 0) {
922 if (Still) {
923 if (NextFile(FileNumber, FileOffset)) {
925 int r = ReadFrame(replayFile, b, Length, sizeof(b));
926 if (r > 0) {
927 if (playMode == pmPause)
928 DevicePlay();
929 DeviceStillPicture(b, r);
930 ptsIndex.Put(isPesRecording ? PesGetPts(b) : TsGetPts(b, r), Index, true);
931 }
933 readIndex = Index - 1; // makes sure a later play starts with this I-frame
934 }
935 }
936 else {
937 readIndex = Index - 1; // Action() will first increment it!
938 Play();
939 }
940 }
941 }
942}
943
945{
947 return; // only do this upon user interaction
948 if (playMode == pmPlay) {
949 if (!ptsIndex.IsEmpty()) {
950 int Current, Total;
951 if (GetIndex(Current, Total, true))
952 Goto(Current);
953 }
954 }
955 else if (playMode == pmPause)
956 resyncAfterPause = true;
957}
958
960{
961 if (index)
962 return index->GetErrors();
963 return NULL;
964}
965
966bool cDvbPlayer::GetIndex(int &Current, int &Total, bool SnapToIFrame)
967{
968 if (index) {
970 if (SnapToIFrame) {
971 int i1 = index->GetNextIFrame(Current + 1, false);
972 int i2 = index->GetNextIFrame(Current, true);
973 Current = (abs(Current - i1) <= abs(Current - i2)) ? i1 : i2;
974 }
975 Total = index->Last();
976 return true;
977 }
978 Current = Total = -1;
979 return false;
980}
981
982bool cDvbPlayer::GetFrameNumber(int &Current, int &Total)
983{
984 if (index) {
986 Total = index->Last();
987 return true;
988 }
989 Current = Total = -1;
990 return false;
991}
992
993bool cDvbPlayer::GetReplayMode(bool &Play, bool &Forward, int &Speed)
994{
995 Play = (playMode == pmPlay || playMode == pmFast);
997 if (playMode == pmFast || playMode == pmSlow)
998 Speed = Setup.MultiSpeedMode ? abs(trickSpeed - NORMAL_SPEED) : 0;
999 else
1000 Speed = -1;
1001 return true;
1002}
1003
1004// --- cDvbPlayerControl -----------------------------------------------------
1005
1006cDvbPlayerControl::cDvbPlayerControl(const char *FileName, bool PauseLive)
1007:cControl(NULL)
1008{
1009 player = new cDvbPlayer(FileName, PauseLive);
1011}
1012
1017
1019{
1020 if (player)
1021 player->SetMarks(Marks);
1022}
1023
1025{
1026 return player && player->Active();
1027}
1028
1030{
1031 cControl::player = NULL;
1032 delete player;
1033 player = NULL;
1034}
1035
1037{
1038 if (player)
1039 player->Pause();
1040}
1041
1043{
1044 if (player)
1045 player->Play();
1046}
1047
1049{
1050 if (player)
1051 player->Forward();
1052}
1053
1055{
1056 if (player)
1057 player->Backward();
1058}
1059
1061{
1062 if (player)
1063 player->SkipSeconds(Seconds);
1064}
1065
1067{
1068 if (player)
1069 return player->SkipFrames(Frames);
1070 return -1;
1071}
1072
1074{
1075 if (player)
1076 return player->GetErrors();
1077 return NULL;
1078}
1079
1080bool cDvbPlayerControl::GetIndex(int &Current, int &Total, bool SnapToIFrame)
1081{
1082 if (player) {
1083 player->GetIndex(Current, Total, SnapToIFrame);
1084 return true;
1085 }
1086 return false;
1087}
1088
1089bool cDvbPlayerControl::GetFrameNumber(int &Current, int &Total)
1090{
1091 if (player) {
1092 player->GetFrameNumber(Current, Total);
1093 return true;
1094 }
1095 return false;
1096}
1097
1098bool cDvbPlayerControl::GetReplayMode(bool &Play, bool &Forward, int &Speed)
1099{
1100 return player && player->GetReplayMode(Play, Forward, Speed);
1101}
1102
1103void cDvbPlayerControl::Goto(int Position, bool Still)
1104{
1105 if (player)
1106 player->Goto(Position, Still);
1107}
bool TimedWait(cMutex &Mutex, int TimeoutMs)
Definition thread.c:135
void Broadcast(void)
Definition thread.c:155
bool Wait(int TimeoutMs=0)
Waits at most TimeoutMs milliseconds for a call to Signal(), or forever if TimeoutMs is 0.
Definition thread.c:79
void Signal(void)
Signals a caller of Wait() that the condition it is waiting for is met.
Definition thread.c:101
static void SleepMs(int TimeoutMs)
Creates a cCondWait object and uses it to sleep for TimeoutMs milliseconds, immediately giving up the...
Definition thread.c:73
void SetPlayer(cPlayer *Player)
Definition player.h:115
cControl(cPlayer *Player, bool Hidden=false)
Definition player.c:45
cPlayer * player
Definition player.h:92
void SetMarks(const cMarks *Marks)
Definition dvbplayer.c:1018
virtual ~cDvbPlayerControl() override
Definition dvbplayer.c:1013
bool GetIndex(int &Current, int &Total, bool SnapToIFrame=false)
Definition dvbplayer.c:1080
const cErrors * GetErrors(void)
Definition dvbplayer.c:1073
void SkipSeconds(int Seconds)
Definition dvbplayer.c:1060
cDvbPlayerControl(const char *FileName, bool PauseLive=false)
Definition dvbplayer.c:1006
bool GetReplayMode(bool &Play, bool &Forward, int &Speed)
Definition dvbplayer.c:1098
void Pause(void)
Definition dvbplayer.c:1036
int SkipFrames(int Frames)
Definition dvbplayer.c:1066
void Goto(int Index, bool Still=false)
Definition dvbplayer.c:1103
void Stop(void)
Definition dvbplayer.c:1029
void Forward(void)
Definition dvbplayer.c:1048
bool Active(void)
Definition dvbplayer.c:1024
bool GetFrameNumber(int &Current, int &Total)
Definition dvbplayer.c:1089
void Play(void)
Definition dvbplayer.c:1042
void Backward(void)
Definition dvbplayer.c:1054
cDvbPlayer * player
Definition dvbplayer.h:21
const cMarks * marks
Definition dvbplayer.c:252
virtual const cErrors * GetErrors(void) override
Definition dvbplayer.c:959
cFrame * readFrame
Definition dvbplayer.c:267
std::atomic< ePlayModes > playMode
Definition dvbplayer.c:262
cRingBufferFrame * ringBuffer
Definition dvbplayer.c:250
void SetMarks(const cMarks *Marks)
Definition dvbplayer.c:354
virtual void Action(void) override
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
Definition dvbplayer.c:457
cFrame * playFrame
Definition dvbplayer.c:268
virtual bool GetReplayMode(bool &Play, bool &Forward, int &Speed) override
Definition dvbplayer.c:993
bool Save(void)
Definition dvbplayer.c:423
virtual void Activate(bool On) override
Definition dvbplayer.c:447
std::atomic< int > trickSpeed
Definition dvbplayer.c:264
std::atomic< bool > eof
Definition dvbplayer.c:259
bool firstPacket
Definition dvbplayer.c:260
void SkipSeconds(int Seconds)
Definition dvbplayer.c:893
int Resume(void)
Definition dvbplayer.c:405
cDvbPlayer(const char *FileName, bool PauseLive)
Definition dvbplayer.c:304
cNonBlockingFileReader * nonBlockingFileReader
Definition dvbplayer.c:249
cIndexFile * index
Definition dvbplayer.c:254
cUnbufferedFile * replayFile
Definition dvbplayer.c:255
bool Active(void)
Definition dvbplayer.c:282
virtual void SetAudioTrack(eTrackType Type, const tTrackId *TrackId) override
Definition dvbplayer.c:944
void Goto(int Position, bool Still=false)
Definition dvbplayer.c:910
double framesPerSecond
Definition dvbplayer.c:256
bool isPesRecording
Definition dvbplayer.c:257
void Play(void)
Definition dvbplayer.c:754
std::atomic< ePlayDirs > playDir
Definition dvbplayer.c:263
cFileName * fileName
Definition dvbplayer.c:253
void Empty(void)
Definition dvbplayer.c:379
static int Speeds[]
Definition dvbplayer.c:302
bool NextFile(uint16_t FileNumber=0, off_t FileOffset=-1)
Definition dvbplayer.c:395
void Pause(void)
Definition dvbplayer.c:739
cPtsIndex ptsIndex
Definition dvbplayer.c:251
bool resyncAfterPause
Definition dvbplayer.c:269
std::atomic< int > readIndex
Definition dvbplayer.c:265
virtual ~cDvbPlayer() override
Definition dvbplayer.c:343
virtual bool GetFrameNumber(int &Current, int &Total) override
Definition dvbplayer.c:982
void Forward(void)
Definition dvbplayer.c:774
void TrickSpeed(int Increment)
Definition dvbplayer.c:359
virtual double FramesPerSecond(void) override
Definition dvbplayer.c:290
int SkipFrames(int Frames)
Definition dvbplayer.c:878
void Backward(void)
Definition dvbplayer.c:826
bool readIndependent
Definition dvbplayer.c:266
virtual bool GetIndex(int &Current, int &Total, bool SnapToIFrame=false) override
Definition dvbplayer.c:966
bool pauseLive
Definition dvbplayer.c:258
cUnbufferedFile * NextFile(void)
Definition recording.c:3463
cUnbufferedFile * Open(void)
Definition recording.c:3387
cFileName(const char *FileName, bool Record, bool Blocking=false, bool IsPesRecording=false)
Definition recording.c:3312
cUnbufferedFile * SetOffset(int Number, off_t Offset=0)
Definition recording.c:3421
uchar * Data(void) const
Definition ringbuffer.h:127
uint32_t Pts(void) const
Definition ringbuffer.h:131
int Index(void) const
Definition ringbuffer.h:130
bool Independent(void) const
Definition ringbuffer.h:132
cFrame(const uchar *Data, int Count, eFrameType=ftUnknown, int Index=-1, uint32_t Pts=0, bool independent=false)
Creates a new cFrame object.
Definition ringbuffer.c:394
int Count(void) const
Definition ringbuffer.h:128
int GetNextIFrame(int Index, bool Forward, uint16_t *FileNumber=NULL, off_t *FileOffset=NULL, int *Length=NULL)
Definition recording.c:3167
bool IsStillRecording(void)
Definition recording.c:3250
cIndexFile(const char *FileName, bool Record, bool IsPesRecording=false, bool PauseLive=false)
Definition recording.c:2915
const cErrors * GetErrors(void)
Returns the frame indexes of errors in the recording (if any).
Definition recording.c:3155
int GetResume(void)
Definition recording.h:555
bool StoreResume(int Index)
Definition recording.h:556
bool Ok(void)
Definition recording.h:541
bool Get(int Index, uint16_t *FileNumber, off_t *FileOffset, bool *Independent=NULL, int *Length=NULL, bool *Errors=NULL, bool *Missing=NULL)
Definition recording.c:3124
int Last(void)
Returns the index of the last entry in this file, or -1 if the file is empty.
Definition recording.h:553
bool Lock(cStateKey &StateKey, bool Write=false, int TimeoutMs=0) const
Tries to get a lock on this list and returns true if successful.
Definition tools.c:2219
int Index(void) const
Definition tools.c:2148
const T * First(void) const
Returns the first element in this list, or NULL if the list is empty.
Definition tools.h:662
int Position(void) const
Definition recording.h:420
const cMark * GetNextBegin(const cMark *EndMark=NULL) const
Returns the next "begin" mark after EndMark, skipping any marks at the same position as EndMark.
Definition recording.c:2587
const cMark * Get(int Position) const
Definition recording.c:2560
std::atomic< int > wanted
Definition dvbplayer.c:134
std::atomic< uchar * > buffer
Definition dvbplayer.c:133
std::atomic< int > length
Definition dvbplayer.c:135
void Request(cUnbufferedFile *File, int Length)
Definition dvbplayer.c:177
void Action(void)
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
Definition dvbplayer.c:200
bool WaitForDataMs(int msToWait)
Definition dvbplayer.c:229
int Result(uchar **Buffer)
Definition dvbplayer.c:188
cUnbufferedFile * f
Definition dvbplayer.c:131
void Detach(void)
Definition player.c:34
void DeviceStillPicture(const uchar *Data, int Length)
Definition player.h:38
uint64_t DeviceGetSTC(void)
Definition player.h:40
int PlayTs(const uchar *Data, int Length, bool VideoOnly=false)
Definition player.h:50
void DevicePlay(void)
Definition player.h:34
int PlayPes(const uchar *Data, int Length, bool VideoOnly=false)
Definition player.c:26
bool DevicePoll(cPoller &Poller, int TimeoutMs=0)
Definition player.h:26
void DeviceMute(void)
Definition player.h:36
void DeviceFreeze(void)
Definition player.h:35
void DeviceSetTempSubtitles(void)
Definition player.h:39
bool DeviceDrain(void)
Definition player.h:29
bool DeviceHasIBPTrickSpeed(void)
Definition player.h:30
cPlayer(ePlayMode PlayMode=pmAudioVideo)
Definition player.c:15
bool DeviceIsPlayingVideo(void)
Definition player.h:31
void DeviceClear(void)
Definition player.h:33
void DeviceTrickSpeed(int Speed, bool Forward)
Definition player.h:32
int FindIndex(uint32_t Pts, bool Still)
Definition dvbplayer.c:71
int FindFrameNumber(uint32_t Pts, bool Forward, bool Still)
Definition dvbplayer.c:93
cPtsIndex(void)
Definition dvbplayer.c:42
tPtsIndex pi[PTSINDEX_ENTRIES]
Definition dvbplayer.c:29
void Clear(void)
Definition dvbplayer.c:48
bool IsEmpty(void)
Definition dvbplayer.c:54
int lastFound
Definition dvbplayer.c:31
cMutex mutex
Definition dvbplayer.c:32
void Put(uint32_t Pts, int Index, bool Independent)
Definition dvbplayer.c:60
cRecording(const cRecording &)
double FramesPerSecond(void) const
Definition recording.h:192
bool IsPesRecording(void) const
Definition recording.h:216
static void SetBrokenLink(uchar *Data, int Length)
Definition remux.c:102
virtual void Clear(void) override
Definition ringbuffer.c:432
cFrame * Get(void)
Definition ringbuffer.c:463
bool Put(cFrame *Frame)
Definition ringbuffer.c:443
cRingBufferFrame(int Size, bool Statistics=false)
Definition ringbuffer.c:420
void Drop(cFrame *Frame)
Definition ringbuffer.c:477
int PauseAtLastMark
Definition config.h:367
int MultiSpeedMode
Definition config.h:360
int SkipEdited
Definition config.h:366
void Remove(bool IncState=true)
Removes this key from the lock it was previously used with.
Definition thread.c:905
void Unlock(void)
Definition thread.h:104
void bool Start(void)
Sets the description of this thread, which will be used when logging starting or stopping of the thre...
Definition thread.c:330
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition thread.h:110
void Lock(void)
Definition thread.h:103
cThread(const char *Description=NULL, bool LowPriority=false)
Creates a new thread.
Definition thread.c:264
void Cancel(int WaitSeconds=0)
Cancels the thread by first setting 'running' to false, so that the Action() loop can finish in an or...
Definition thread.c:381
static tThreadId IsMainThread(void)
Definition thread.h:140
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner,...
Definition tools.h:513
ssize_t Read(void *Data, size_t Size)
Definition tools.c:1929
cSetup Setup
Definition config.c:373
eTrackType
Definition device.h:64
#define MAX_VIDEO_SLOWMOTION
Definition dvbplayer.c:298
#define SPEED_MULT
Definition dvbplayer.c:301
#define PTSINDEX_ENTRIES
Definition dvbplayer.c:20
#define NORMAL_SPEED
Definition dvbplayer.c:299
#define RESUMEBACKUP
Definition dvbplayer.c:241
#define MAX_SPEEDS
Definition dvbplayer.c:300
#define MAXSTUCKATEOF
Definition dvbplayer.c:242
#define PLAYERBUFSIZE
Definition dvbplayer.c:239
cString IndexToHMSF(int Index, bool WithFrame, double FramesPerSecond)
Definition recording.c:3563
int SecondsToFrames(int Seconds, double FramesPerSecond)
Definition recording.c:3590
int ReadFrame(cUnbufferedFile *f, uchar *b, int Length, int Max)
Definition recording.c:3597
#define MAXFRAMESIZE
Definition recording.h:505
int64_t PtsDiff(int64_t Pts1, int64_t Pts2)
Returns the difference between two PTS values.
Definition remux.c:236
int64_t TsGetPts(const uchar *p, int l, int Pid)
Definition remux.c:160
bool PesHasPts(const uchar *p)
Definition remux.h:183
int64_t PesGetPts(const uchar *p)
Definition remux.h:193
@ ftUnknown
Definition ringbuffer.h:109
#define LOCK_THREAD
Definition thread.h:181
#define FATALERRNO
Definition tools.h:52
unsigned char uchar
Definition tools.h:31
#define MALLOC(type, size)
Definition tools.h:47
T max(T a, T b)
Definition tools.h:64
#define esyslog(a...)
Definition tools.h:35
#define LOG_ERROR
Definition tools.h:39
#define isyslog(a...)
Definition tools.h:36