vdr 2.8.1
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.14 2026/02/16 11:03:12 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:
138protected:
139 void Action(void);
140public:
143 void Clear(void);
144 void Request(cUnbufferedFile *File, int Length);
145 int Result(uchar **Buffer);
146 bool Reading(void) { return buffer; }
147 bool WaitForDataMs(int msToWait);
148 };
149
151:cThread("non blocking file reader")
152{
153 f = NULL;
154 buffer = NULL;
155 wanted = length = 0;
156 Start();
157}
158
160{
161 newSet.Signal();
162 Cancel(3);
163 free(buffer);
164}
165
167{
168 Lock();
169 f = NULL;
170 free(buffer);
171 buffer = NULL;
172 wanted = length = 0;
173 Unlock();
174}
175
177{
178 Lock();
179 Clear();
180 wanted = Length;
182 f = File;
183 Unlock();
184 newSet.Signal();
185}
186
188{
190 if (buffer && length == wanted) {
191 *Buffer = buffer;
192 buffer = NULL;
193 return wanted;
194 }
195 errno = EAGAIN;
196 return -1;
197}
198
200{
201 while (Running()) {
202 Lock();
203 if (f && buffer && length < wanted) {
204 int r = f->Read(buffer + length, wanted - length);
205 if (r > 0)
206 length += r;
207 else if (r == 0) { // r == 0 means EOF
208 if (length > 0)
209 wanted = length; // already read something, so return the rest
210 else
211 length = wanted = 0; // report EOF
212 }
213 else if (FATALERRNO) {
214 LOG_ERROR;
215 length = wanted = r; // this will forward the error status to the caller
216 }
217 if (length == wanted) {
218 cMutexLock NewDataLock(&newDataMutex);
219 newDataCond.Broadcast();
220 }
221 }
222 Unlock();
223 newSet.Wait(1000);
224 }
225}
226
228{
229 cMutexLock NewDataLock(&newDataMutex);
230 if (buffer && length == wanted)
231 return true;
232 return newDataCond.TimedWait(newDataMutex, msToWait);
233}
234
235// --- cDvbPlayer ------------------------------------------------------------
236
237#define PLAYERBUFSIZE (MAXFRAMESIZE * 5)
238
239#define RESUMEBACKUP 10 // number of seconds to back up when resuming an interrupted replay session
240#define MAXSTUCKATEOF 3 // max. number of seconds to wait in case the device doesn't play the last frame
241
242class cDvbPlayer : public cPlayer, cThread {
243private:
246 static int Speeds[];
250 const cMarks *marks;
257 bool eof;
268 void TrickSpeed(int Increment);
269 void Empty(void);
270 bool NextFile(uint16_t FileNumber = 0, off_t FileOffset = -1);
271 int Resume(void);
272 bool Save(void);
273protected:
274 virtual void Activate(bool On) override;
275 virtual void Action(void) override;
276public:
277 cDvbPlayer(const char *FileName, bool PauseLive);
278 virtual ~cDvbPlayer() override;
279 void SetMarks(const cMarks *Marks);
280 bool Active(void) { return cThread::Running(); }
281 void Pause(void);
282 void Play(void);
283 void Forward(void);
284 void Backward(void);
285 int SkipFrames(int Frames);
286 void SkipSeconds(int Seconds);
287 void Goto(int Position, bool Still = false);
288 virtual double FramesPerSecond(void) { return framesPerSecond; }
289 virtual void SetAudioTrack(eTrackType Type, const tTrackId *TrackId) override;
290 virtual const cErrors *GetErrors(void) override;
291 virtual bool GetIndex(int &Current, int &Total, bool SnapToIFrame = false) override;
292 virtual bool GetFrameNumber(int &Current, int &Total) override;
293 virtual bool GetReplayMode(bool &Play, bool &Forward, int &Speed) override;
294 };
295
296#define MAX_VIDEO_SLOWMOTION 63 // max. arg to pass to VIDEO_SLOWMOTION // TODO is this value correct?
297#define NORMAL_SPEED 4 // the index of the '1' entry in the following array
298#define MAX_SPEEDS 3 // the offset of the maximum speed from normal speed in either direction
299#define SPEED_MULT 12 // the speed multiplier
300int cDvbPlayer::Speeds[] = { 0, -2, -4, -8, 1, 2, 4, 12, 0 };
301
302cDvbPlayer::cDvbPlayer(const char *FileName, bool PauseLive)
303:cThread("dvbplayer")
304{
306 ringBuffer = NULL;
307 marks = NULL;
308 index = NULL;
309 cRecording Recording(FileName);
310 framesPerSecond = Recording.FramesPerSecond();
311 isPesRecording = Recording.IsPesRecording();
312 pauseLive = PauseLive;
313 eof = false;
314 firstPacket = true;
318 readIndex = -1;
319 readIndependent = false;
320 readFrame = NULL;
321 playFrame = NULL;
322 dropFrame = NULL;
323 resyncAfterPause = false;
324 isyslog("replay %s", FileName);
325 fileName = new cFileName(FileName, false, false, isPesRecording);
326 replayFile = fileName->Open();
327 if (!replayFile)
328 return;
330 // Create the index file:
331 index = new cIndexFile(FileName, false, isPesRecording, pauseLive);
332 if (!index)
333 esyslog("ERROR: can't allocate index");
334 else if (!index->Ok()) {
335 delete index;
336 index = NULL;
337 }
338 else if (PauseLive)
339 framesPerSecond = cRecording(FileName).FramesPerSecond(); // the fps rate might have changed from the default
340}
341
343{
344 Save();
345 Detach();
346 delete readFrame; // might not have been stored in the buffer in Action()
347 delete index;
348 delete fileName;
349 delete ringBuffer;
350 // don't delete marks here, we don't own them!
351}
352
353void cDvbPlayer::SetMarks(const cMarks *Marks)
354{
355 marks = Marks;
356}
357
358void cDvbPlayer::TrickSpeed(int Increment)
359{
360 int nts = trickSpeed + Increment;
361 if (Speeds[nts] == 1) {
362 trickSpeed = nts;
363 if (playMode == pmFast)
364 Play();
365 else
366 Pause();
367 }
368 else if (Speeds[nts]) {
369 trickSpeed = nts;
370 int Mult = (playMode == pmSlow && playDir == pdForward) ? 1 : SPEED_MULT;
371 int sp = (Speeds[nts] > 0) ? Mult / Speeds[nts] : -Speeds[nts] * Mult;
372 if (sp > MAX_VIDEO_SLOWMOTION)
375 }
376}
377
379{
382 nonBlockingFileReader->Clear();
383 if (!firstPacket) // don't set the readIndex twice if Empty() is called more than once
384 readIndex = ptsIndex.FindIndex(DeviceGetSTC(), playMode == pmStill) - 1; // Action() will first increment it!
385 delete readFrame; // might not have been stored in the buffer in Action()
386 readFrame = NULL;
387 playFrame = NULL;
388 dropFrame = 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)
400 replayFile = fileName->NextFile();
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) {
426 int Index = ptsIndex.FindIndex(DeviceGetSTC(), playMode == pmStill);
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, *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 LastReadFrame = -1;
489 int SwitchToPlayFrame = 0;
490 bool CutIn = false;
491 bool AtLastMark = false;
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)
504 }
505 {
507
508 // Read the next frame from the file:
509
510 if (playMode != pmStill && playMode != pmPause) {
511 if (!readFrame && (replayFile || readIndex >= 0)) {
512 if (!nonBlockingFileReader->Reading() && !AtLastMark) {
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++;
549 if ((Setup.SkipEdited || Setup.PauseAtLastMark) && marks) {
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)
585 nonBlockingFileReader->Request(replayFile, Length);
586 }
587 if (!eof) {
588 uchar *b = NULL;
589 int r = nonBlockingFileReader->Result(&b);
590 if (r > 0) {
591 WaitingForData = false;
592 LastReadFrame = readIndex;
593 uint32_t Pts = isPesRecording ? (PesHasPts(b) ? PesGetPts(b) : -1) : TsGetPts(b, r);
594 readFrame = new cFrame(b, -r, ftUnknown, readIndex, Pts, readIndependent); // hands over b to the ringBuffer
595 }
596 else if (r < 0) {
597 if (errno == EAGAIN)
598 WaitingForData = true;
599 else if (FATALERRNO) {
600 LOG_ERROR;
601 break;
602 }
603 }
604 else
605 eof = true;
606 }
607 }
608
609 // Store the frame in the buffer:
610
611 if (readFrame) {
612 if (CutIn) {
613 if (isPesRecording)
614 cRemux::SetBrokenLink(readFrame->Data(), readFrame->Count());
615 CutIn = false;
616 }
617 if (ringBuffer->Put(readFrame))
618 readFrame = NULL;
619 else
620 Sleep = true;
621 }
622 }
623 else {
624 Sleep = true;
625 continue;
626 }
627
628 if (dropFrame) {
629 if (!eof || (playDir != pdForward && dropFrame->Index() > 0) || (playDir == pdForward && dropFrame->Index() < readIndex)) {
630 ringBuffer->Drop(dropFrame); // the very first and last frame are continuously repeated to flush data through the device
631 dropFrame = NULL;
632 }
633 }
634
635 // Get the next frame from the buffer:
636
637 if (!playFrame) {
638 playFrame = ringBuffer->Get();
639 p = NULL;
640 pc = 0;
641 }
642
643 // Play the frame:
644
645 if (playFrame) {
646 if (!p) {
647 p = playFrame->Data();
648 pc = playFrame->Count();
649 if (p) {
650 if (playFrame->Index() >= 0 && playFrame->Pts() != 0)
651 ptsIndex.Put(playFrame->Pts(), playFrame->Index(), playFrame->Independent());
652 if (firstPacket) {
653 if (isPesRecording) {
654 PlayPes(NULL, 0);
656 }
657 else
658 PlayTs(NULL, 0);
659 firstPacket = false;
660 }
661 }
662 }
663 if (p) {
664 int w;
665 bool VideoOnly = (dropFrame || playMode != pmPlay && !(playMode == pmSlow && playDir == pdForward)) && DeviceIsPlayingVideo();
666 if (isPesRecording)
667 w = PlayPes(p, pc, VideoOnly);
668 else
669 w = PlayTs(p, pc, VideoOnly);
670 if (w > 0) {
671 p += w;
672 pc -= w;
673 }
674 else if (w < 0 && FATALERRNO)
675 LOG_ERROR;
676 else
677 Sleep = true;
678 }
679 if (pc <= 0) {
681 playFrame = NULL;
682 p = NULL;
683 }
684 }
685 else {
686 if (AtLastMark) {
687 if (Setup.PauseAtLastMark) {
688 DeviceFreeze();
690 AtLastMark = false;
691 }
692 else
693 eof = true;
694 }
695 Sleep = true;
696 }
697
698 // Handle hitting begin/end of recording:
699
700 if (eof || SwitchToPlayFrame) {
701 bool SwitchToPlay = false;
702 uint32_t Stc = DeviceGetSTC();
703 if (Stc != LastStc || playMode == pmPause)
704 StuckAtEof = 0;
705 else if (!StuckAtEof)
706 StuckAtEof = time(NULL);
707 else if (time(NULL) - StuckAtEof > MAXSTUCKATEOF) {
708 if (playDir == pdForward)
709 break; // automatically stop at end of recording
710 SwitchToPlay = true;
711 }
712 LastStc = Stc;
713 int Index = ptsIndex.FindIndex(Stc, playMode == pmStill);
714 if (playDir == pdForward && !SwitchToPlayFrame) {
715 if (Index >= LastReadFrame)
716 break; // automatically stop at end of recording
717 }
718 else if (Index <= 0 || SwitchToPlayFrame && Index >= SwitchToPlayFrame)
719 SwitchToPlay = true;
720 if (SwitchToPlay) {
721 if (!SwitchToPlayFrame)
722 Empty();
723 DevicePlay();
726 SwitchToPlayFrame = 0;
727 }
728 }
729 }
730 }
731
734 delete nbfr;
735}
736
738{
739 if (playMode == pmPause || playMode == pmStill)
740 Play();
741 else {
743 if (playMode == pmFast || (playMode == pmSlow && playDir == pdBackward)) {
745 Empty();
746 }
747 DeviceFreeze();
749 }
750}
751
753{
754 if (playMode != pmPlay) {
756 if (playMode == pmStill || playMode == pmFast || (playMode == pmSlow && playDir == pdBackward)) {
758 Empty();
759 }
760 DevicePlay();
763 if (resyncAfterPause) {
764 int Current, Total;
765 if (GetIndex(Current, Total, true))
766 Goto(Current);
767 resyncAfterPause = false;
768 }
769 }
770}
771
773{
774 if (index) {
775 switch (playMode) {
776 case pmFast:
777 if (Setup.MultiSpeedMode) {
778 TrickSpeed(playDir == pdForward ? 1 : -1);
779 break;
780 }
781 else if (playDir == pdForward) {
782 Play();
783 break;
784 }
785 // run into pmPlay
786 case pmPlay: {
789 Empty();
791 DeviceMute();
795 TrickSpeed(Setup.MultiSpeedMode ? 1 : MAX_SPEEDS);
796 }
797 break;
798 case pmSlow:
799 if (Setup.MultiSpeedMode) {
800 TrickSpeed(playDir == pdForward ? -1 : 1);
801 break;
802 }
803 else if (playDir == pdForward) {
804 Pause();
805 break;
806 }
807 Empty();
808 // run into pmPause
809 case pmStill:
810 case pmPause: {
812 DeviceMute();
816 TrickSpeed(Setup.MultiSpeedMode ? -1 : -MAX_SPEEDS);
817 }
818 break;
819 default: esyslog("ERROR: unknown playMode %d (%s)", playMode, __FUNCTION__);
820 }
821 }
822}
823
825{
826 if (index) {
827 switch (playMode) {
828 case pmFast:
829 if (Setup.MultiSpeedMode) {
830 TrickSpeed(playDir == pdBackward ? 1 : -1);
831 break;
832 }
833 else if (playDir == pdBackward) {
834 Play();
835 break;
836 }
837 // run into pmPlay
838 case pmPlay: {
841 Empty();
843 DeviceMute();
847 TrickSpeed(Setup.MultiSpeedMode ? 1 : MAX_SPEEDS);
848 }
849 break;
850 case pmSlow:
851 if (Setup.MultiSpeedMode) {
852 TrickSpeed(playDir == pdBackward ? -1 : 1);
853 break;
854 }
855 else if (playDir == pdBackward) {
856 Pause();
857 break;
858 }
859 // run into pmPause
860 case pmStill:
861 case pmPause: {
863 Empty();
864 DeviceMute();
868 TrickSpeed(Setup.MultiSpeedMode ? -1 : -MAX_SPEEDS);
869 }
870 break;
871 default: esyslog("ERROR: unknown playMode %d (%s)", playMode, __FUNCTION__);
872 }
873 }
874}
875
877{
878 if (index && Frames) {
879 int Current, Total;
880 GetIndex(Current, Total, true);
881 int OldCurrent = Current;
882 // As GetNextIFrame() increments/decrements at least once, the
883 // destination frame (= Current + Frames) must be adjusted by
884 // -1/+1 respectively.
885 Current = index->GetNextIFrame(Current + Frames + (Frames > 0 ? -1 : 1), Frames > 0);
886 return Current >= 0 ? Current : OldCurrent;
887 }
888 return -1;
889}
890
891void cDvbPlayer::SkipSeconds(int Seconds)
892{
893 if (index && Seconds) {
895 int Index = ptsIndex.FindIndex(DeviceGetSTC(), playMode == pmStill);
896 Empty();
897 if (Index >= 0) {
898 Index = max(Index + SecondsToFrames(Seconds, framesPerSecond), 0);
899 if (Index > 0)
900 Index = index->GetNextIFrame(Index, false, NULL, NULL, NULL);
901 if (Index >= 0)
902 readIndex = Index - 1; // Action() will first increment it!
903 }
904 Play();
905 }
906}
907
908void cDvbPlayer::Goto(int Index, bool Still)
909{
910 if (index) {
912 Empty();
913 if (++Index <= 0)
914 Index = 1; // not '0', to allow GetNextIFrame() below to work!
915 uint16_t FileNumber;
916 off_t FileOffset;
917 int Length;
918 Index = index->GetNextIFrame(Index, false, &FileNumber, &FileOffset, &Length);
919 if (Index >= 0) {
920 if (Still) {
921 if (NextFile(FileNumber, FileOffset)) {
923 int r = ReadFrame(replayFile, b, Length, sizeof(b));
924 if (r > 0) {
925 if (playMode == pmPause)
926 DevicePlay();
927 DeviceStillPicture(b, r);
928 ptsIndex.Put(isPesRecording ? PesGetPts(b) : TsGetPts(b, r), Index, true);
929 }
931 readIndex = Index - 1; // makes sure a later play starts with this I-frame
932 }
933 }
934 else {
935 readIndex = Index - 1; // Action() will first increment it!
936 Play();
937 }
938 }
939 }
940}
941
943{
945 return; // only do this upon user interaction
946 if (playMode == pmPlay) {
947 if (!ptsIndex.IsEmpty()) {
948 int Current, Total;
949 if (GetIndex(Current, Total, true))
950 Goto(Current);
951 }
952 }
953 else if (playMode == pmPause)
954 resyncAfterPause = true;
955}
956
958{
959 if (index)
960 return index->GetErrors();
961 return NULL;
962}
963
964bool cDvbPlayer::GetIndex(int &Current, int &Total, bool SnapToIFrame)
965{
966 if (index) {
967 Current = ptsIndex.FindIndex(DeviceGetSTC(), playMode == pmStill);
968 if (SnapToIFrame) {
969 int i1 = index->GetNextIFrame(Current + 1, false);
970 int i2 = index->GetNextIFrame(Current, true);
971 Current = (abs(Current - i1) <= abs(Current - i2)) ? i1 : i2;
972 }
973 Total = index->Last();
974 return true;
975 }
976 Current = Total = -1;
977 return false;
978}
979
980bool cDvbPlayer::GetFrameNumber(int &Current, int &Total)
981{
982 if (index) {
983 Current = ptsIndex.FindFrameNumber(DeviceGetSTC(), playDir == pdForward, playMode == pmStill);
984 Total = index->Last();
985 return true;
986 }
987 Current = Total = -1;
988 return false;
989}
990
991bool cDvbPlayer::GetReplayMode(bool &Play, bool &Forward, int &Speed)
992{
993 Play = (playMode == pmPlay || playMode == pmFast);
995 if (playMode == pmFast || playMode == pmSlow)
996 Speed = Setup.MultiSpeedMode ? abs(trickSpeed - NORMAL_SPEED) : 0;
997 else
998 Speed = -1;
999 return true;
1000}
1001
1002// --- cDvbPlayerControl -----------------------------------------------------
1003
1004cDvbPlayerControl::cDvbPlayerControl(const char *FileName, bool PauseLive)
1005:cControl(NULL)
1006{
1007 player = new cDvbPlayer(FileName, PauseLive);
1009}
1010
1015
1017{
1018 if (player)
1019 player->SetMarks(Marks);
1020}
1021
1023{
1024 return player && player->Active();
1025}
1026
1028{
1029 cControl::player = NULL;
1030 delete player;
1031 player = NULL;
1032}
1033
1035{
1036 if (player)
1037 player->Pause();
1038}
1039
1041{
1042 if (player)
1043 player->Play();
1044}
1045
1047{
1048 if (player)
1049 player->Forward();
1050}
1051
1053{
1054 if (player)
1055 player->Backward();
1056}
1057
1059{
1060 if (player)
1061 player->SkipSeconds(Seconds);
1062}
1063
1065{
1066 if (player)
1067 return player->SkipFrames(Frames);
1068 return -1;
1069}
1070
1072{
1073 if (player)
1074 return player->GetErrors();
1075 return NULL;
1076}
1077
1078bool cDvbPlayerControl::GetIndex(int &Current, int &Total, bool SnapToIFrame)
1079{
1080 if (player) {
1081 player->GetIndex(Current, Total, SnapToIFrame);
1082 return true;
1083 }
1084 return false;
1085}
1086
1087bool cDvbPlayerControl::GetFrameNumber(int &Current, int &Total)
1088{
1089 if (player) {
1090 player->GetFrameNumber(Current, Total);
1091 return true;
1092 }
1093 return false;
1094}
1095
1096bool cDvbPlayerControl::GetReplayMode(bool &Play, bool &Forward, int &Speed)
1097{
1098 return player && player->GetReplayMode(Play, Forward, Speed);
1099}
1100
1101void cDvbPlayerControl::Goto(int Position, bool Still)
1102{
1103 if (player)
1104 player->Goto(Position, Still);
1105}
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:113
cControl(cPlayer *Player, bool Hidden=false)
Definition player.c:45
cPlayer * player
Definition player.h:90
void SetMarks(const cMarks *Marks)
Definition dvbplayer.c:1016
virtual ~cDvbPlayerControl() override
Definition dvbplayer.c:1011
bool GetIndex(int &Current, int &Total, bool SnapToIFrame=false)
Definition dvbplayer.c:1078
const cErrors * GetErrors(void)
Definition dvbplayer.c:1071
void SkipSeconds(int Seconds)
Definition dvbplayer.c:1058
cDvbPlayerControl(const char *FileName, bool PauseLive=false)
Definition dvbplayer.c:1004
bool GetReplayMode(bool &Play, bool &Forward, int &Speed)
Definition dvbplayer.c:1096
void Pause(void)
Definition dvbplayer.c:1034
int SkipFrames(int Frames)
Definition dvbplayer.c:1064
void Goto(int Index, bool Still=false)
Definition dvbplayer.c:1101
void Stop(void)
Definition dvbplayer.c:1027
void Forward(void)
Definition dvbplayer.c:1046
bool Active(void)
Definition dvbplayer.c:1022
bool GetFrameNumber(int &Current, int &Total)
Definition dvbplayer.c:1087
void Play(void)
Definition dvbplayer.c:1040
void Backward(void)
Definition dvbplayer.c:1052
cDvbPlayer * player
Definition dvbplayer.h:21
const cMarks * marks
Definition dvbplayer.c:250
virtual const cErrors * GetErrors(void) override
Definition dvbplayer.c:957
cFrame * readFrame
Definition dvbplayer.c:264
cRingBufferFrame * ringBuffer
Definition dvbplayer.c:248
void SetMarks(const cMarks *Marks)
Definition dvbplayer.c:353
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:265
virtual bool GetReplayMode(bool &Play, bool &Forward, int &Speed) override
Definition dvbplayer.c:991
bool Save(void)
Definition dvbplayer.c:423
virtual void Activate(bool On) override
Definition dvbplayer.c:447
bool firstPacket
Definition dvbplayer.c:258
void SkipSeconds(int Seconds)
Definition dvbplayer.c:891
virtual double FramesPerSecond(void)
Definition dvbplayer.c:288
int Resume(void)
Definition dvbplayer.c:405
cDvbPlayer(const char *FileName, bool PauseLive)
Definition dvbplayer.c:302
cNonBlockingFileReader * nonBlockingFileReader
Definition dvbplayer.c:247
cIndexFile * index
Definition dvbplayer.c:252
cUnbufferedFile * replayFile
Definition dvbplayer.c:253
bool Active(void)
Definition dvbplayer.c:280
virtual void SetAudioTrack(eTrackType Type, const tTrackId *TrackId) override
Definition dvbplayer.c:942
void Goto(int Position, bool Still=false)
Definition dvbplayer.c:908
double framesPerSecond
Definition dvbplayer.c:254
bool isPesRecording
Definition dvbplayer.c:255
void Play(void)
Definition dvbplayer.c:752
cFileName * fileName
Definition dvbplayer.c:251
void Empty(void)
Definition dvbplayer.c:378
ePlayDirs playDir
Definition dvbplayer.c:260
static int Speeds[]
Definition dvbplayer.c:300
int trickSpeed
Definition dvbplayer.c:261
bool NextFile(uint16_t FileNumber=0, off_t FileOffset=-1)
Definition dvbplayer.c:395
void Pause(void)
Definition dvbplayer.c:737
cPtsIndex ptsIndex
Definition dvbplayer.c:249
bool resyncAfterPause
Definition dvbplayer.c:267
virtual ~cDvbPlayer() override
Definition dvbplayer.c:342
virtual bool GetFrameNumber(int &Current, int &Total) override
Definition dvbplayer.c:980
int readIndex
Definition dvbplayer.c:262
void Forward(void)
Definition dvbplayer.c:772
void TrickSpeed(int Increment)
Definition dvbplayer.c:358
int SkipFrames(int Frames)
Definition dvbplayer.c:876
void Backward(void)
Definition dvbplayer.c:824
bool readIndependent
Definition dvbplayer.c:263
virtual bool GetIndex(int &Current, int &Total, bool SnapToIFrame=false) override
Definition dvbplayer.c:964
cFrame * dropFrame
Definition dvbplayer.c:266
bool pauseLive
Definition dvbplayer.c:256
ePlayModes playMode
Definition dvbplayer.c:259
int Index(void) const
Definition tools.c:2114
int Position(void) const
Definition recording.h:412
void Request(cUnbufferedFile *File, int Length)
Definition dvbplayer.c:176
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:199
bool WaitForDataMs(int msToWait)
Definition dvbplayer.c:227
int Result(uchar **Buffer)
Definition dvbplayer.c:187
cUnbufferedFile * f
Definition dvbplayer.c:131
void Detach(void)
Definition player.c:34
void DeviceStillPicture(const uchar *Data, int Length)
Definition player.h:36
uint64_t DeviceGetSTC(void)
Definition player.h:38
int PlayTs(const uchar *Data, int Length, bool VideoOnly=false)
Definition player.h:48
void DevicePlay(void)
Definition player.h:32
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:34
void DeviceFreeze(void)
Definition player.h:33
void DeviceSetTempSubtitles(void)
Definition player.h:37
bool DeviceHasIBPTrickSpeed(void)
Definition player.h:28
cPlayer(ePlayMode PlayMode=pmAudioVideo)
Definition player.c:15
bool DeviceIsPlayingVideo(void)
Definition player.h:29
void DeviceClear(void)
Definition player.h:31
void DeviceTrickSpeed(int Speed, bool Forward)
Definition player.h:30
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
double FramesPerSecond(void) const
Definition recording.h:191
bool IsPesRecording(void) const
Definition recording.h:215
static void SetBrokenLink(uchar *Data, int Length)
Definition remux.c:102
void Remove(bool IncState=true)
Removes this key from the lock it was previously used with.
Definition thread.c:870
void Unlock(void)
Definition thread.h:95
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:305
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition thread.h:101
void Lock(void)
Definition thread.h:94
cThread(const char *Description=NULL, bool LowPriority=false)
Creates a new thread.
Definition thread.c:239
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:355
static tThreadId IsMainThread(void)
Definition thread.h:131
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner,...
Definition tools.h:507
cSetup Setup
Definition config.c:372
eTrackType
Definition device.h:63
#define MAX_VIDEO_SLOWMOTION
Definition dvbplayer.c:296
#define SPEED_MULT
Definition dvbplayer.c:299
#define PTSINDEX_ENTRIES
Definition dvbplayer.c:20
#define NORMAL_SPEED
Definition dvbplayer.c:297
#define RESUMEBACKUP
Definition dvbplayer.c:239
#define MAX_SPEEDS
Definition dvbplayer.c:298
#define MAXSTUCKATEOF
Definition dvbplayer.c:240
#define PLAYERBUFSIZE
Definition dvbplayer.c:237
cString IndexToHMSF(int Index, bool WithFrame, double FramesPerSecond)
Definition recording.c:3524
int SecondsToFrames(int Seconds, double FramesPerSecond)
Definition recording.c:3551
int ReadFrame(cUnbufferedFile *f, uchar *b, int Length, int Max)
Definition recording.c:3558
#define MAXFRAMESIZE
Definition recording.h:497
int64_t TsGetPts(const uchar *p, int l)
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:107
#define LOCK_THREAD
Definition thread.h:167
#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