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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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);
99 barBlue.set(0, 0, 0, 128);
107 char* optionWSS = vdr->configLoad("General", "WSS");
110 if (strstr(optionWSS, "Yes")) dowss = true;
113 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Do WSS: %u", dowss);
117 wss.setFormat(video->getFormat());
123 wssRegion.w = video->getScreenWidth();
128 VVideoRec::~VVideoRec()
130 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Entering vvideorec destructor");
134 boxstack->remove(vas);
138 if (vsummary) delete vsummary;
140 if (playing) stopPlay();
141 video->setDefaultAspect();
143 timers->cancelTimer(this, 1);
144 timers->cancelTimer(this, 2);
146 // kill recInfo in case resumePoint has changed (likely)
147 myRec->dropRecInfo();
148 // FIXME - do this properly - save the resume point back to the server manually and update
149 // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
152 void VVideoRec::go(bool resume)
156 startFrameNum = myRec->recInfo->resumePoint;
160 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Starting stream: %s at frame: %lu", myRec->getFileName(), startFrameNum);
161 ULONG lengthFrames = 0;
162 ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
165 player->setLengthBytes(lengthBytes);
166 player->setLengthFrames(lengthFrames);
167 player->setStartFrame(startFrameNum);
174 stopPlay(); // clean up
176 if (!vdr->isConnected())
178 Command::getInstance()->connectionLost();
182 Message* m = new Message();
183 m->message = Message::CLOSE_ME;
186 Command::getInstance()->postMessageNoLock(m);
188 VInfo* vi = new VInfo();
189 vi->setSize(400, 150);
191 if (video->getFormat() == Video::PAL)
192 vi->setPosition(170, 200);
194 vi->setPosition(160, 150);
197 vi->setTitleBarOn(0);
198 vi->setOneLiner(tr("Error playing recording"));
202 m->message = Message::ADD_VIEW;
204 m->parameter = (ULONG)vi;
205 Command::getInstance()->postMessageNoLock(m);
209 int VVideoRec::handleCommand(int command)
231 if (playing) stopPlay();
241 case Remote::SKIPFORWARD:
244 player->skipForward(60);
247 case Remote::SKIPBACK:
250 player->skipBackward(60);
253 case Remote::FORWARD:
255 player->fastForward();
259 case Remote::REVERSE:
261 player->fastBackward();
267 if (vsummary) removeSummary();
278 if (myRec->hasMarks())
280 // skip to previous mark
281 Log* logger = Log::getInstance();
282 int currentFrame = (player->getCurrentFrameNum()); // get current Frame
283 currentFrame -= 5 * video->getFPS(); // subtrack 5 seconds, else you cannot skip more than once back ..
285 int prevMark = myRec->getPrevMark(currentFrame); // find previous Frame
288 logger->log("VVideoRec", Log::NOTICE, "jump back from pos %i to mark at %i",currentFrame,prevMark);
289 player->jumpToMark(prevMark);
296 player->skipBackward(10);
302 if (myRec->hasMarks())
305 Log* logger = Log::getInstance();
306 int currentFrame = (player->getCurrentFrameNum());
308 int nextMark = myRec->getNextMark(currentFrame);
312 logger->log("VVideoRec", Log::NOTICE, "jump forward from pos %i to mark at %i",currentFrame,nextMark);
313 player->jumpToMark(nextMark);
320 player->skipForward(10);
327 player->skipBackward(10);
333 player->skipForward(10);
351 if (barShowing) removeBar();
356 case Remote::ZERO: player->jumpToPercent(0); doBar(0); return 2;
357 case Remote::ONE: player->jumpToPercent(10); doBar(0); return 2;
358 case Remote::TWO: player->jumpToPercent(20); doBar(0); return 2;
359 case Remote::THREE: player->jumpToPercent(30); doBar(0); return 2;
360 case Remote::FOUR: player->jumpToPercent(40); doBar(0); return 2;
361 case Remote::FIVE: player->jumpToPercent(50); doBar(0); return 2;
362 case Remote::SIX: player->jumpToPercent(60); doBar(0); return 2;
363 case Remote::SEVEN: player->jumpToPercent(70); doBar(0); return 2;
364 case Remote::EIGHT: player->jumpToPercent(80); doBar(0); return 2;
365 case Remote::NINE: player->jumpToPercent(90); doBar(0); return 2;
370 //Don't use RED for anything. It will eventually be recording summary
376 // for testing EPG in NTSC with a NTSC test video
377 Video::getInstance()->setMode(Video::QUARTER);
378 Video::getInstance()->setPosition(170, 5);
379 VEpg* vepg = new VEpg(NULL, 0);
381 BoxStack::getInstance()->add(vepg);
382 BoxStack::getInstance()->update(vepg);
395 void VVideoRec::processMessage(Message* m)
397 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Message received");
399 if (m->message == Message::MOUSE_LBDOWN)
401 UINT x = (m->parameter>>16) - getScreenX();
402 UINT y = (m->parameter&0xFFFF) - getScreenY();
406 BoxStack::getInstance()->handleCommand(Remote::OK); //simulate rok press
408 else if (barRegion.x<=x && barRegion.y<=y && (barRegion.x+barRegion.w)>=x && (barRegion.y+barRegion.h)>=y)
410 int progBarXbase = barRegion.x + 300;
411 if (myRec->hasMarks())
413 MarkList* markList = myRec->getMarkList();
414 MarkList::iterator i;
415 Mark* loopMark = NULL;
418 if (myRec->recInfo->timerEnd > time(NULL))
421 // Work out an approximate length in frames (good to 1s...)
422 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
426 lengthFrames = player->getLengthFrames();
428 for(i = markList->begin(); i != markList->end(); i++)
433 posPix = 302 * loopMark->pos / lengthFrames;
434 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, Colour::DANGER);
435 if (x>=barRegion.x + progBarXbase + 2 + posPix
436 && x<=barRegion.x + progBarXbase + 2 + posPix+3
437 && y>=barRegion.y + 12 - 2
438 && y<=barRegion.y + 12 - 2+28)
440 player->jumpToMark(loopMark->pos);
448 if (x>=barRegion.x + progBarXbase + 24
449 && x<=barRegion.x + progBarXbase + 4 + 302
450 && y>=barRegion.y + 12 - 2
451 && y<=barRegion.y + 12 - 2+28)
453 int cx=x-(barRegion.x + progBarXbase + 4);
454 double percent=((double)cx)/302.*100.;
455 player->jumpToPercent(percent);
458 // int progressWidth = 302 * currentFrameNum / lengthFrames;
459 // rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
464 BoxStack::getInstance()->handleCommand(Remote::OK); //simulate rok press
467 else if (m->from == player)
469 if (m->message != Message::PLAYER_EVENT) return;
472 case Player::CONNECTION_LOST: // connection lost detected
474 // I can't handle this, send it to command
475 Message* m2 = new Message();
476 m2->to = Command::getInstance();
477 m2->message = Message::CONNECTION_LOST;
478 Command::getInstance()->postMessageNoLock(m2);
481 case Player::STOP_PLAYBACK:
483 // FIXME Obselete ish - improve this
484 Message* m2 = new Message(); // Must be done after this thread finishes, and must break into master mutex
485 m2->to = Command::getInstance();
486 m2->message = Message::STOP_PLAYBACK;
487 Command::getInstance()->postMessageNoLock(m2);
490 case Player::ASPECT43:
494 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 43");
497 boxstack->update(this, &wssRegion);
501 case Player::ASPECT169:
505 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 169");
508 boxstack->update(this, &wssRegion);
514 else if (m->message == Message::AUDIO_CHANGE_CHANNEL)
516 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received change audio channel to %i", m->parameter);
517 player->setAudioChannel(m->parameter&0xFFFF,(m->parameter&0xFF0000)>> 16 );
519 else if (m->message == Message::CHILD_CLOSE)
525 if (!barGenHold && !barScanHold && !barVasHold) removeBar();
530 void VVideoRec::stopPlay()
532 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Pre stopPlay");
534 // FIXME work out a better soln for this
535 // Fix a problem to do with thread sync here
536 // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
537 // (or maybe just the wrong thread being selected?) for the main loop to lock and process
538 // the video stop message it is possible for a bar message to stack up after a stop message
539 // when the bar message is finally processed the prog crashes because this is deleted by then
544 vdr->stopStreaming();
549 if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
550 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Post stopPlay");
553 void VVideoRec::toggleChopSides()
555 if (video->getTVsize() == Video::ASPECT16X9) return; // Means nothing for 16:9 TVs
557 if (videoMode == Video::NORMAL)
559 videoMode = Video::LETTERBOX;
560 video->setMode(Video::LETTERBOX);
564 videoMode = Video::NORMAL;
565 video->setMode(Video::NORMAL);
569 void VVideoRec::doAudioSelector()
571 bool* availableMpegAudioChannels = player->getDemuxerMpegAudioChannels();
572 bool* availableAc3AudioChannels = 0;
573 int currentAudioChannel = player->getCurrentAudioChannel();
574 if (Audio::getInstance()->supportsAc3())
576 availableAc3AudioChannels = player->getDemuxerAc3AudioChannels();
579 vas = new VAudioSelector(this, availableMpegAudioChannels, availableAc3AudioChannels, currentAudioChannel, myRec->recInfo);
580 vas->setBackgroundColour(barBlue);
581 vas->setPosition(0, barRegion.y - 120);
590 boxstack->update(vas);
593 void VVideoRec::doBar(int action)
597 rectangle(barRegion, barBlue);
599 /* Work out what to display - choices:
606 Specials, informed by parameter
618 w.setPosition(barRegion.x + 66, barRegion.y + 16);
620 UCHAR playerState = 0;
624 if (action == 1) w.nextSymbol = WSymbol::SKIPFORWARD;
625 else if (action == 2) w.nextSymbol = WSymbol::SKIPBACK;
626 else if (action == 3) w.nextSymbol = WSymbol::SKIPFORWARD2;
627 else if (action == 4) w.nextSymbol = WSymbol::SKIPBACK2;
631 playerState = player->getState();
632 if (playerState == Player::S_PAUSE_P) w.nextSymbol = WSymbol::PAUSE;
633 else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
634 else if (playerState == Player::S_FFWD) w.nextSymbol = WSymbol::FFWD;
635 else if (playerState == Player::S_FBWD) w.nextSymbol = WSymbol::FBWD;
636 else w.nextSymbol = WSymbol::PLAY;
641 if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD))
643 // draw blips to show how fast the scan is
644 UCHAR scanrate = player->getIScanRate();
648 SNPRINTF(text, 5, "%ux", scanrate);
649 drawText(text, barRegion.x + 102, barRegion.y + 12, Colour::LIGHTTEXT);
655 boxstack->update(this, &barRegion);
657 timers->cancelTimer(this, 1);
660 if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD)) barScanHold = true;
661 else barScanHold = false;
663 if (!barGenHold && !barScanHold && !barVasHold) timers->setTimerD(this, 1, 4);
665 timers->setTimerD(this, 2, 0, 200000000);
668 void VVideoRec::timercall(int clientReference)
670 switch(clientReference)
681 if (!barShowing) break;
683 Message* m = new Message();
684 m->message = Message::REDRAW;
685 m->to = BoxStack::getInstance();
687 m->parameter = (ULONG)&barRegion;
688 Command::getInstance()->postMessageFromOuterSpace(m);
689 timers->setTimerD(this, 2, 0, 200000000);
695 void VVideoRec::drawBarClocks()
699 UCHAR playerState = player->getState();
700 // sticky bar is set if we are in ffwd/fbwd mode
701 // if player has gone to S_PLAY then kill stickyBar, and run doBar(0) which
702 // will repaint all the bar (it will call this function again, but
703 // this section won't run because stickyBarF will then == false)
705 if ((playerState != Player::S_FFWD) && (playerState != Player::S_FBWD))
709 return; // doBar will call this function and do the rest
713 Log* logger = Log::getInstance();
714 logger->log("VVideoRec", Log::DEBUG, "Draw bar clocks");
717 // Blank the area first
718 rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
722 struct tm* tms = localtime(&t);
723 strftime(timeString, 19, "%H:%M", tms);
724 drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
728 rectangle(clocksRegion, barBlue);
730 ULONG currentFrameNum = player->getCurrentFrameNum();
732 if (myRec->recInfo->timerEnd > time(NULL))
735 // Work out an approximate length in frames (good to 1s...)
736 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
740 lengthFrames = player->getLengthFrames();
743 hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
744 hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
747 if (currentFrameNum >= lengthFrames)
749 strcpy(buffer, "-:--:-- / -:--:--");
753 SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
754 logger->log("VVideoRec", Log::DEBUG, buffer);
757 drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
766 int progBarXbase = barRegion.x + 300;
768 rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
769 rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
771 if (currentFrameNum > lengthFrames) return;
772 if (lengthFrames == 0) return;
774 // Draw yellow portion
775 int progressWidth = 302 * currentFrameNum / lengthFrames;
776 rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
778 if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
780 int nrWidth = (int)(302 * ((double)(lengthFrames - player->getLengthFrames()) / lengthFrames));
782 Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
783 Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
784 Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
785 rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
789 // Now calc position for blips
791 if (myRec->hasMarks())
793 // Draw blips where there are cut marks
794 MarkList* markList = myRec->getMarkList();
795 MarkList::iterator i;
796 Mark* loopMark = NULL;
798 for(i = markList->begin(); i != markList->end(); i++)
803 logger->log("VVideoRec", Log::DEBUG, "Drawing mark at frame %i", loopMark->pos);
804 posPix = 302 * loopMark->pos / lengthFrames;
805 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, Colour::DANGER);
811 // Draw blips where start and end margins probably are
813 posPix = 302 * startMargin * video->getFPS() / lengthFrames;
815 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
816 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
818 posPix = 302 * (lengthFrames - endMargin * video->getFPS()) / lengthFrames;
820 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
821 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
825 void VVideoRec::removeBar()
827 if (!barShowing) return;
828 timers->cancelTimer(this, 2);
833 rectangle(barRegion, transparent);
835 Message* m = new Message();
836 m->message = Message::REDRAW;
837 m->to = BoxStack::getInstance();
839 m->parameter = (ULONG)&barRegion;
840 Command::getInstance()->postMessageFromOuterSpace(m);
843 void VVideoRec::doSummary()
845 vsummary = new VInfo();
846 vsummary->setTitleText(myRec->getProgName());
847 vsummary->setBorderOn(1);
848 vsummary->setExitable();
849 if (myRec->recInfo->summary) vsummary->setMainText(myRec->recInfo->summary);
850 else vsummary->setMainText(tr("Summary unavailable"));
851 if (Video::getInstance()->getFormat() == Video::PAL)
853 vsummary->setPosition(120, 130);
857 vsummary->setPosition(110, 90);
859 vsummary->setSize(510, 270);
863 BoxStack::getInstance()->update(this);
866 void VVideoRec::removeSummary()
874 BoxStack::getInstance()->update(this);