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);
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 void VVideoRec::preDelete()
130 timers->cancelTimer(this, 1);
131 timers->cancelTimer(this, 2);
135 boxstack->remove(vas);
139 if (vsummary) delete vsummary;
141 if (playing) stopPlay();
144 VVideoRec::~VVideoRec()
146 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Entering vvideorec destructor");
148 video->setDefaultAspect();
150 // kill recInfo in case resumePoint has changed (likely)
151 myRec->dropRecInfo();
152 // FIXME - do this properly - save the resume point back to the server manually and update
153 // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
156 void VVideoRec::go(bool resume)
160 startFrameNum = myRec->recInfo->resumePoint;
164 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Starting stream: %s at frame: %lu", myRec->getFileName(), startFrameNum);
165 ULONG lengthFrames = 0;
166 ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
169 player->setLengthBytes(lengthBytes);
170 player->setLengthFrames(lengthFrames);
171 player->setStartFrame(startFrameNum);
178 stopPlay(); // clean up
180 if (!vdr->isConnected())
182 Command::getInstance()->connectionLost();
186 Message* m = new Message();
187 m->message = Message::CLOSE_ME;
190 Command::getInstance()->postMessageNoLock(m);
192 VInfo* vi = new VInfo();
193 vi->setSize(400, 150);
195 if (video->getFormat() == Video::PAL)
196 vi->setPosition(170, 200);
198 vi->setPosition(160, 150);
201 vi->setTitleBarOn(0);
202 vi->setOneLiner(tr("Error playing recording"));
206 m->message = Message::ADD_VIEW;
208 m->parameter = (ULONG)vi;
209 Command::getInstance()->postMessageNoLock(m);
213 int VVideoRec::handleCommand(int command)
235 if (playing) stopPlay();
245 case Remote::SKIPFORWARD:
248 player->skipForward(60);
251 case Remote::SKIPBACK:
254 player->skipBackward(60);
257 case Remote::FORWARD:
259 player->fastForward();
263 case Remote::REVERSE:
265 player->fastBackward();
271 if (vsummary) removeSummary();
282 if (myRec->hasMarks())
284 // skip to previous mark
285 Log* logger = Log::getInstance();
286 int currentFrame = (player->getCurrentFrameNum()); // get current Frame
287 currentFrame -= 5 * video->getFPS(); // subtrack 5 seconds, else you cannot skip more than once back ..
289 int prevMark = myRec->getPrevMark(currentFrame); // find previous Frame
292 logger->log("VVideoRec", Log::NOTICE, "jump back from pos %i to mark at %i",currentFrame,prevMark);
293 player->jumpToMark(prevMark);
300 player->skipBackward(10);
306 if (myRec->hasMarks())
309 Log* logger = Log::getInstance();
310 int currentFrame = (player->getCurrentFrameNum());
312 int nextMark = myRec->getNextMark(currentFrame);
316 logger->log("VVideoRec", Log::NOTICE, "jump forward from pos %i to mark at %i",currentFrame,nextMark);
317 player->jumpToMark(nextMark);
324 player->skipForward(10);
331 player->skipBackward(10);
337 player->skipForward(10);
355 if (barShowing) removeBar();
360 case Remote::ZERO: player->jumpToPercent(0); doBar(0); return 2;
361 case Remote::ONE: player->jumpToPercent(10); doBar(0); return 2;
362 case Remote::TWO: player->jumpToPercent(20); doBar(0); return 2;
363 case Remote::THREE: player->jumpToPercent(30); doBar(0); return 2;
364 case Remote::FOUR: player->jumpToPercent(40); doBar(0); return 2;
365 case Remote::FIVE: player->jumpToPercent(50); doBar(0); return 2;
366 case Remote::SIX: player->jumpToPercent(60); doBar(0); return 2;
367 case Remote::SEVEN: player->jumpToPercent(70); doBar(0); return 2;
368 case Remote::EIGHT: player->jumpToPercent(80); doBar(0); return 2;
369 case Remote::NINE: player->jumpToPercent(90); doBar(0); return 2;
374 //Don't use RED for anything. It will eventually be recording summary
380 // for testing EPG in NTSC with a NTSC test video
381 Video::getInstance()->setMode(Video::QUARTER);
382 Video::getInstance()->setPosition(170, 5);
383 VEpg* vepg = new VEpg(NULL, 0);
385 BoxStack::getInstance()->add(vepg);
386 BoxStack::getInstance()->update(vepg);
399 void VVideoRec::processMessage(Message* m)
401 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Message received");
403 if (m->message == Message::MOUSE_LBDOWN)
405 UINT x = (m->parameter>>16) - getScreenX();
406 UINT y = (m->parameter&0xFFFF) - getScreenY();
410 BoxStack::getInstance()->handleCommand(Remote::OK); //simulate rok press
412 else if (barRegion.x<=x && barRegion.y<=y && (barRegion.x+barRegion.w)>=x && (barRegion.y+barRegion.h)>=y)
414 int progBarXbase = barRegion.x + 300;
415 if (myRec->hasMarks())
417 MarkList* markList = myRec->getMarkList();
418 MarkList::iterator i;
419 Mark* loopMark = NULL;
422 if (myRec->recInfo->timerEnd > time(NULL))
425 // Work out an approximate length in frames (good to 1s...)
426 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
430 lengthFrames = player->getLengthFrames();
432 for(i = markList->begin(); i != markList->end(); i++)
437 posPix = 302 * loopMark->pos / lengthFrames;
438 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, Colour::DANGER);
439 if (x>=barRegion.x + progBarXbase + 2 + posPix
440 && x<=barRegion.x + progBarXbase + 2 + posPix+3
441 && y>=barRegion.y + 12 - 2
442 && y<=barRegion.y + 12 - 2+28)
444 player->jumpToMark(loopMark->pos);
452 if (x>=barRegion.x + progBarXbase + 24
453 && x<=barRegion.x + progBarXbase + 4 + 302
454 && y>=barRegion.y + 12 - 2
455 && y<=barRegion.y + 12 - 2+28)
457 int cx=x-(barRegion.x + progBarXbase + 4);
458 double percent=((double)cx)/302.*100.;
459 player->jumpToPercent(percent);
462 // int progressWidth = 302 * currentFrameNum / lengthFrames;
463 // rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
468 BoxStack::getInstance()->handleCommand(Remote::OK); //simulate rok press
471 else if (m->from == player)
473 if (m->message != Message::PLAYER_EVENT) return;
476 case Player::CONNECTION_LOST: // connection lost detected
478 // I can't handle this, send it to command
479 Message* m2 = new Message();
480 m2->to = Command::getInstance();
481 m2->message = Message::CONNECTION_LOST;
482 Command::getInstance()->postMessageNoLock(m2);
485 case Player::STOP_PLAYBACK:
487 // FIXME Obselete ish - improve this
488 Message* m2 = new Message(); // Must be done after this thread finishes, and must break into master mutex
489 m2->to = Command::getInstance();
490 m2->message = Message::STOP_PLAYBACK;
491 Command::getInstance()->postMessageNoLock(m2);
494 case Player::ASPECT43:
498 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 43");
501 boxstack->update(this, &wssRegion);
505 case Player::ASPECT169:
509 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 169");
512 boxstack->update(this, &wssRegion);
518 else if (m->message == Message::AUDIO_CHANGE_CHANNEL)
520 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received change audio channel to %i", m->parameter);
521 player->setAudioChannel(m->parameter&0xFFFF,(m->parameter&0xFF0000)>> 16 );
523 else if (m->message == Message::CHILD_CLOSE)
529 if (!barGenHold && !barScanHold && !barVasHold) removeBar();
534 void VVideoRec::stopPlay()
536 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Pre stopPlay");
539 Log::getInstance()->log("VVideoRec", Log::DEBUG, "1");
541 Log::getInstance()->log("VVideoRec", Log::DEBUG, "2");
542 vdr->stopStreaming();
543 Log::getInstance()->log("VVideoRec", Log::DEBUG, "3");
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 boxstack->update(this, &barRegion);
684 timers->setTimerD(this, 2, 0, 200000000);
690 void VVideoRec::drawBarClocks()
694 UCHAR playerState = player->getState();
695 // sticky bar is set if we are in ffwd/fbwd mode
696 // if player has gone to S_PLAY then kill stickyBar, and run doBar(0) which
697 // will repaint all the bar (it will call this function again, but
698 // this section won't run because stickyBarF will then == false)
700 if ((playerState != Player::S_FFWD) && (playerState != Player::S_FBWD))
704 return; // doBar will call this function and do the rest
708 Log* logger = Log::getInstance();
709 logger->log("VVideoRec", Log::DEBUG, "Draw bar clocks");
712 // Blank the area first
713 rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
717 struct tm* tms = localtime(&t);
718 strftime(timeString, 19, "%H:%M", tms);
719 drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
723 rectangle(clocksRegion, barBlue);
725 ULONG currentFrameNum = player->getCurrentFrameNum();
727 if (myRec->recInfo->timerEnd > time(NULL))
730 // Work out an approximate length in frames (good to 1s...)
731 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
735 lengthFrames = player->getLengthFrames();
738 hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
739 hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
742 if (currentFrameNum >= lengthFrames)
744 strcpy(buffer, "-:--:-- / -:--:--");
748 SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
749 logger->log("VVideoRec", Log::DEBUG, buffer);
752 drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
761 int progBarXbase = barRegion.x + 300;
763 rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
764 rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
766 if (currentFrameNum > lengthFrames) return;
767 if (lengthFrames == 0) return;
769 // Draw yellow portion
770 int progressWidth = 302 * currentFrameNum / lengthFrames;
771 rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
773 if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
775 int nrWidth = (int)(302 * ((double)(lengthFrames - player->getLengthFrames()) / lengthFrames));
777 Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
778 Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
779 Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
780 rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
784 // Now calc position for blips
786 if (myRec->hasMarks())
788 // Draw blips where there are cut marks
789 MarkList* markList = myRec->getMarkList();
790 MarkList::iterator i;
791 Mark* loopMark = NULL;
793 for(i = markList->begin(); i != markList->end(); i++)
798 logger->log("VVideoRec", Log::DEBUG, "Drawing mark at frame %i", loopMark->pos);
799 posPix = 302 * loopMark->pos / lengthFrames;
800 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, Colour::DANGER);
806 // Draw blips where start and end margins probably are
808 posPix = 302 * startMargin * video->getFPS() / lengthFrames;
810 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
811 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
813 posPix = 302 * (lengthFrames - endMargin * 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);
820 void VVideoRec::removeBar()
822 if (!barShowing) return;
823 timers->cancelTimer(this, 2);
828 rectangle(barRegion, transparent);
829 boxstack->update(this, &barRegion);
832 void VVideoRec::doSummary()
834 vsummary = new VInfo();
835 vsummary->setTitleText(myRec->getProgName());
836 vsummary->setBorderOn(1);
837 vsummary->setExitable();
838 if (myRec->recInfo->summary) vsummary->setMainText(myRec->recInfo->summary);
839 else vsummary->setMainText(tr("Summary unavailable"));
840 if (Video::getInstance()->getFormat() == Video::PAL)
842 vsummary->setPosition(120, 130);
846 vsummary->setPosition(110, 90);
848 vsummary->setSize(510, 270);
852 BoxStack::getInstance()->update(this);
855 void VVideoRec::removeSummary()
863 BoxStack::getInstance()->update(this);