2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "vvideorec.h"
31 #include "recording.h"
32 #include "vaudioselector.h"
39 VVideoRec::VVideoRec(Recording* rec)
41 boxstack = BoxStack::getInstance();
42 vdr = VDR::getInstance();
43 video = Video::getInstance();
44 timers = Timers::getInstance();
48 player = new Player(Command::getInstance(), this, true);
51 videoMode = video->getMode();
58 char* cstartMargin = vdr->configLoad("Timers", "Start margin");
59 char* cendMargin = vdr->configLoad("Timers", "End margin");
62 startMargin = 300; // 5 mins default
66 startMargin = atoi(cstartMargin) * 60;
67 delete[] cstartMargin;
72 endMargin = 300; // 5 mins default
76 endMargin = atoi(cendMargin) * 60;
80 Log::getInstance()->log("VVideoRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
82 setSize(video->getScreenWidth(), video->getScreenHeight());
84 transparent.set(0, 0, 0, 0);
85 setBackgroundColour(transparent);
88 barRegion.y = video->getScreenHeight() - 58; // FIXME, need to be - 1? and below?
89 barRegion.w = video->getScreenWidth();
92 clocksRegion.x = barRegion.x + 140;
93 clocksRegion.y = barRegion.y + 12;
95 clocksRegion.h = surface->getFontHeight();
98 barBlue.set(0, 0, 150, 150);
106 char* optionWSS = vdr->configLoad("General", "WSS");
109 if (strstr(optionWSS, "Yes")) dowss = true;
112 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Do WSS: %u", dowss);
116 wss.setFormat(video->getFormat());
122 wssRegion.w = video->getScreenWidth();
127 VVideoRec::~VVideoRec()
129 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Entering vvideorec destructor");
133 boxstack->remove(vas);
137 if (vsummary) delete vsummary;
139 if (playing) stopPlay();
140 video->setDefaultAspect();
142 timers->cancelTimer(this, 1);
143 timers->cancelTimer(this, 2);
145 // kill recInfo in case resumePoint has changed (likely)
146 myRec->dropRecInfo();
147 // FIXME - do this properly - save the resume point back to the server manually and update
148 // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
151 void VVideoRec::go(bool resume)
155 startFrameNum = myRec->recInfo->resumePoint;
159 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Starting stream: %s at frame: %lu", myRec->getFileName(), startFrameNum);
160 ULONG lengthFrames = 0;
161 ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
164 player->setLengthBytes(lengthBytes);
165 player->setLengthFrames(lengthFrames);
166 player->setStartFrame(startFrameNum);
173 stopPlay(); // clean up
175 if (!vdr->isConnected())
177 Command::getInstance()->connectionLost();
181 Message* m = new Message();
182 m->message = Message::CLOSE_ME;
185 Command::getInstance()->postMessageNoLock(m);
187 VInfo* vi = new VInfo();
188 vi->setSize(400, 150);
190 if (video->getFormat() == Video::PAL)
191 vi->setPosition(170, 200);
193 vi->setPosition(160, 150);
196 vi->setTitleBarOn(0);
197 vi->setOneLiner(tr("Error playing recording"));
201 m->message = Message::ADD_VIEW;
203 m->parameter = (ULONG)vi;
204 Command::getInstance()->postMessageNoLock(m);
208 int VVideoRec::handleCommand(int command)
230 if (playing) stopPlay();
240 case Remote::SKIPFORWARD:
243 player->skipForward(60);
246 case Remote::SKIPBACK:
249 player->skipBackward(60);
252 case Remote::FORWARD:
254 player->fastForward();
258 case Remote::REVERSE:
260 player->fastBackward();
266 if (vsummary) removeSummary();
277 if (myRec->hasMarks())
279 // skip to previous mark
280 Log* logger = Log::getInstance();
281 int currentFrame = (player->getCurrentFrameNum()); // get current Frame
282 currentFrame -= 5 * video->getFPS(); // subtrack 5 seconds, else you cannot skip more than once back ..
284 int prevMark = myRec->getPrevMark(currentFrame); // find previous Frame
287 logger->log("VVideoRec", Log::NOTICE, "jump back from pos %i to mark at %i",currentFrame,prevMark);
288 player->jumpToMark(prevMark);
295 player->skipBackward(10);
301 if (myRec->hasMarks())
304 Log* logger = Log::getInstance();
305 int currentFrame = (player->getCurrentFrameNum());
307 int nextMark = myRec->getNextMark(currentFrame);
311 logger->log("VVideoRec", Log::NOTICE, "jump forward from pos %i to mark at %i",currentFrame,nextMark);
312 player->jumpToMark(nextMark);
319 player->skipForward(10);
326 player->skipBackward(10);
332 player->skipForward(10);
350 if (barShowing) removeBar();
355 case Remote::ZERO: player->jumpToPercent(0); doBar(0); return 2;
356 case Remote::ONE: player->jumpToPercent(10); doBar(0); return 2;
357 case Remote::TWO: player->jumpToPercent(20); doBar(0); return 2;
358 case Remote::THREE: player->jumpToPercent(30); doBar(0); return 2;
359 case Remote::FOUR: player->jumpToPercent(40); doBar(0); return 2;
360 case Remote::FIVE: player->jumpToPercent(50); doBar(0); return 2;
361 case Remote::SIX: player->jumpToPercent(60); doBar(0); return 2;
362 case Remote::SEVEN: player->jumpToPercent(70); doBar(0); return 2;
363 case Remote::EIGHT: player->jumpToPercent(80); doBar(0); return 2;
364 case Remote::NINE: player->jumpToPercent(90); doBar(0); return 2;
369 //Don't use RED for anything. It will eventually be recording summary
375 // for testing EPG in NTSC with a NTSC test video
376 Video::getInstance()->setMode(Video::QUARTER);
377 Video::getInstance()->setPosition(170, 5);
378 VEpg* vepg = new VEpg(NULL, 0);
380 BoxStack::getInstance()->add(vepg);
381 BoxStack::getInstance()->update(vepg);
394 void VVideoRec::processMessage(Message* m)
396 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Message received");
398 if (m->message == Message::MOUSE_LBDOWN)
400 UINT x = (m->parameter>>16) - getScreenX();
401 UINT y = (m->parameter&0xFFFF) - getScreenY();
405 BoxStack::getInstance()->handleCommand(Remote::OK); //simulate rok press
407 else if (barRegion.x<=x && barRegion.y<=y && (barRegion.x+barRegion.w)>=x && (barRegion.y+barRegion.h)>=y)
409 int progBarXbase = barRegion.x + 300;
410 if (myRec->hasMarks())
412 MarkList* markList = myRec->getMarkList();
413 MarkList::iterator i;
414 Mark* loopMark = NULL;
417 if (myRec->recInfo->timerEnd > time(NULL))
420 // Work out an approximate length in frames (good to 1s...)
421 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
425 lengthFrames = player->getLengthFrames();
427 for(i = markList->begin(); i != markList->end(); i++)
432 posPix = 302 * loopMark->pos / lengthFrames;
433 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, Colour::DANGER);
434 if (x>=barRegion.x + progBarXbase + 2 + posPix
435 && x<=barRegion.x + progBarXbase + 2 + posPix+3
436 && y>=barRegion.y + 12 - 2
437 && y<=barRegion.y + 12 - 2+28)
439 player->jumpToMark(loopMark->pos);
447 if (x>=barRegion.x + progBarXbase + 24
448 && x<=barRegion.x + progBarXbase + 4 + 302
449 && y>=barRegion.y + 12 - 2
450 && y<=barRegion.y + 12 - 2+28)
452 int cx=x-(barRegion.x + progBarXbase + 4);
453 double percent=((double)cx)/302.*100.;
454 player->jumpToPercent(percent);
457 // int progressWidth = 302 * currentFrameNum / lengthFrames;
458 // rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
463 BoxStack::getInstance()->handleCommand(Remote::OK); //simulate rok press
466 else if (m->from == player)
468 if (m->message != Message::PLAYER_EVENT) return;
471 case Player::CONNECTION_LOST: // connection lost detected
473 // I can't handle this, send it to command
474 Message* m2 = new Message();
475 m2->to = Command::getInstance();
476 m2->message = Message::CONNECTION_LOST;
477 Command::getInstance()->postMessageNoLock(m2);
480 case Player::STOP_PLAYBACK:
482 // FIXME Obselete ish - improve this
483 Message* m2 = new Message(); // Must be done after this thread finishes, and must break into master mutex
484 m2->to = Command::getInstance();
485 m2->message = Message::STOP_PLAYBACK;
486 Command::getInstance()->postMessageNoLock(m2);
489 case Player::ASPECT43:
493 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 43");
496 boxstack->update(this, &wssRegion);
500 case Player::ASPECT169:
504 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 169");
507 boxstack->update(this, &wssRegion);
513 else if (m->message == Message::AUDIO_CHANGE_CHANNEL)
515 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received change audio channel to %i", m->parameter);
516 player->setAudioChannel(m->parameter);
518 else if (m->message == Message::CHILD_CLOSE)
524 if (!barGenHold && !barScanHold && !barVasHold) removeBar();
529 void VVideoRec::stopPlay()
531 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Pre stopPlay");
533 // FIXME work out a better soln for this
534 // Fix a problem to do with thread sync here
535 // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
536 // (or maybe just the wrong thread being selected?) for the main loop to lock and process
537 // the video stop message it is possible for a bar message to stack up after a stop message
538 // when the bar message is finally processed the prog crashes because this is deleted by then
543 vdr->stopStreaming();
548 if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
549 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Post stopPlay");
552 void VVideoRec::toggleChopSides()
554 if (video->getTVsize() == Video::ASPECT16X9) return; // Means nothing for 16:9 TVs
556 if (videoMode == Video::NORMAL)
558 videoMode = Video::LETTERBOX;
559 video->setMode(Video::LETTERBOX);
563 videoMode = Video::NORMAL;
564 video->setMode(Video::NORMAL);
568 void VVideoRec::doAudioSelector()
570 bool* availableMpegAudioChannels = player->getDemuxerMpegAudioChannels();
571 bool* availableAc3AudioChannels = 0;
572 int currentAudioChannel = player->getCurrentAudioChannel();
573 if (Audio::getInstance()->supportsAc3())
575 availableAc3AudioChannels = player->getDemuxerAc3AudioChannels();
578 vas = new VAudioSelector(this, availableMpegAudioChannels, availableAc3AudioChannels, currentAudioChannel, myRec->recInfo);
579 vas->setBackgroundColour(barBlue);
580 vas->setPosition(0, barRegion.y - 120);
589 boxstack->update(vas);
592 void VVideoRec::doBar(int action)
596 rectangle(barRegion, barBlue);
598 /* Work out what to display - choices:
605 Specials, informed by parameter
617 w.setPosition(barRegion.x + 66, barRegion.y + 16);
619 UCHAR playerState = 0;
623 if (action == 1) w.nextSymbol = WSymbol::SKIPFORWARD;
624 else if (action == 2) w.nextSymbol = WSymbol::SKIPBACK;
625 else if (action == 3) w.nextSymbol = WSymbol::SKIPFORWARD2;
626 else if (action == 4) w.nextSymbol = WSymbol::SKIPBACK2;
630 playerState = player->getState();
631 if (playerState == Player::S_PAUSE_P) w.nextSymbol = WSymbol::PAUSE;
632 else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
633 else if (playerState == Player::S_FFWD) w.nextSymbol = WSymbol::FFWD;
634 else if (playerState == Player::S_FBWD) w.nextSymbol = WSymbol::FBWD;
635 else w.nextSymbol = WSymbol::PLAY;
640 if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD))
642 // draw blips to show how fast the scan is
643 UCHAR scanrate = player->getIScanRate();
647 SNPRINTF(text, 5, "%ux", scanrate);
648 drawText(text, barRegion.x + 102, barRegion.y + 12, Colour::LIGHTTEXT);
654 boxstack->update(this, &barRegion);
656 timers->cancelTimer(this, 1);
659 if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD)) barScanHold = true;
660 else barScanHold = false;
662 if (!barGenHold && !barScanHold && !barVasHold) timers->setTimerD(this, 1, 4);
664 timers->setTimerD(this, 2, 0, 200000000);
667 void VVideoRec::timercall(int clientReference)
669 switch(clientReference)
680 if (!barShowing) break;
682 Message* m = new Message();
683 m->message = Message::REDRAW;
684 m->to = BoxStack::getInstance();
686 m->parameter = (ULONG)&barRegion;
687 Command::getInstance()->postMessageFromOuterSpace(m);
688 timers->setTimerD(this, 2, 0, 200000000);
694 void VVideoRec::drawBarClocks()
698 UCHAR playerState = player->getState();
699 // sticky bar is set if we are in ffwd/fbwd mode
700 // if player has gone to S_PLAY then kill stickyBar, and run doBar(0) which
701 // will repaint all the bar (it will call this function again, but
702 // this section won't run because stickyBarF will then == false)
704 if ((playerState != Player::S_FFWD) && (playerState != Player::S_FBWD))
708 return; // doBar will call this function and do the rest
712 Log* logger = Log::getInstance();
713 logger->log("VVideoRec", Log::DEBUG, "Draw bar clocks");
716 // Blank the area first
717 rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
721 struct tm* tms = localtime(&t);
722 strftime(timeString, 19, "%H:%M", tms);
723 drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
727 rectangle(clocksRegion, barBlue);
729 ULONG currentFrameNum = player->getCurrentFrameNum();
731 if (myRec->recInfo->timerEnd > time(NULL))
734 // Work out an approximate length in frames (good to 1s...)
735 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
739 lengthFrames = player->getLengthFrames();
742 hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
743 hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
746 if (currentFrameNum >= lengthFrames)
748 strcpy(buffer, "-:--:-- / -:--:--");
752 SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
753 logger->log("VVideoRec", Log::DEBUG, buffer);
756 drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
765 int progBarXbase = barRegion.x + 300;
767 rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
768 rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
770 if (currentFrameNum > lengthFrames) return;
771 if (lengthFrames == 0) return;
773 // Draw yellow portion
774 int progressWidth = 302 * currentFrameNum / lengthFrames;
775 rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
777 if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
779 int nrWidth = (int)(302 * ((double)(lengthFrames - player->getLengthFrames()) / lengthFrames));
781 Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
782 Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
783 Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
784 rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
788 // Now calc position for blips
790 if (myRec->hasMarks())
792 // Draw blips where there are cut marks
793 MarkList* markList = myRec->getMarkList();
794 MarkList::iterator i;
795 Mark* loopMark = NULL;
797 for(i = markList->begin(); i != markList->end(); i++)
802 logger->log("VVideoRec", Log::DEBUG, "Drawing mark at frame %i", loopMark->pos);
803 posPix = 302 * loopMark->pos / lengthFrames;
804 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, Colour::DANGER);
810 // Draw blips where start and end margins probably are
812 posPix = 302 * startMargin * video->getFPS() / lengthFrames;
814 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
815 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
817 posPix = 302 * (lengthFrames - endMargin * video->getFPS()) / lengthFrames;
819 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
820 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
824 void VVideoRec::removeBar()
826 if (!barShowing) return;
827 timers->cancelTimer(this, 2);
832 rectangle(barRegion, transparent);
834 Message* m = new Message();
835 m->message = Message::REDRAW;
836 m->to = BoxStack::getInstance();
838 m->parameter = (ULONG)&barRegion;
839 Command::getInstance()->postMessageFromOuterSpace(m);
842 void VVideoRec::doSummary()
844 vsummary = new VInfo();
845 vsummary->setTitleText(myRec->getProgName());
846 vsummary->setBorderOn(1);
847 vsummary->setExitable();
848 if (myRec->recInfo->summary) vsummary->setMainText(myRec->recInfo->summary);
849 else vsummary->setMainText(tr("Summary unavailable"));
850 if (Video::getInstance()->getFormat() == Video::PAL)
852 vsummary->setPosition(120, 130);
856 vsummary->setPosition(110, 90);
858 vsummary->setSize(510, 270);
862 BoxStack::getInstance()->update(this);
865 void VVideoRec::removeSummary()
873 BoxStack::getInstance()->update(this);