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"
23 VVideoRec::VVideoRec(Recording* rec)
25 viewman = ViewMan::getInstance();
26 vdr = VDR::getInstance();
27 video = Video::getInstance();
28 timers = Timers::getInstance();
31 player = new Player(Command::getInstance(), this, true);
34 videoMode = video->getMode();
41 char* cstartMargin = vdr->configLoad("Timers", "Start margin");
42 char* cendMargin = vdr->configLoad("Timers", "End margin");
45 startMargin = 300; // 5 mins default
49 startMargin = atoi(cstartMargin) * 60;
50 delete[] cstartMargin;
55 endMargin = 300; // 5 mins default
59 endMargin = atoi(cendMargin) * 60;
63 Log::getInstance()->log("VVideoRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
65 create(video->getScreenWidth(), video->getScreenHeight());
66 transparent.set(0, 0, 0, 0);
67 setBackgroundColour(transparent);
70 barRegion.y = video->getScreenHeight() - 58; // FIXME, need to be - 1? and below?
71 barRegion.w = video->getScreenWidth();
74 clocksRegion.x = barRegion.x + 140;
75 clocksRegion.y = barRegion.y + 12;
77 clocksRegion.h = surface->getFontHeight();
80 barBlue.set(0, 0, 150, 150);
88 char* optionWSS = vdr->configLoad("General", "WSS");
91 if (strstr(optionWSS, "Yes")) dowss = true;
94 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Do WSS: %u", dowss);
96 wss.setFormat(video->getFormat());
97 wss.setSurface(surface);
103 wssRegion.w = video->getScreenWidth();
108 wssRegion.w = video->getScreenWidth();
112 VVideoRec::~VVideoRec()
116 viewman->removeView(vas);
120 if (playing) stopPlay();
121 video->setDefaultAspect();
123 timers->cancelTimer(this, 1);
124 timers->cancelTimer(this, 2);
126 // kill recInfo in case resumePoint has changed (likely)
127 myRec->dropRecInfo();
128 // FIXME - do this properly - save the resume point back to the server manually and update
129 // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
132 void VVideoRec::draw()
137 void VVideoRec::go(bool resume)
141 startFrameNum = myRec->recInfo->resumePoint;
145 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Starting stream: %s at frame: %lu", myRec->getFileName(), startFrameNum);
146 ULONG lengthFrames = 0;
147 ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
150 player->setLengthBytes(lengthBytes);
151 player->setLengthFrames(lengthFrames);
152 player->setStartFrame(startFrameNum);
159 stopPlay(); // clean up
161 if (!vdr->isConnected())
163 Command::getInstance()->connectionLost();
167 Message* m = new Message();
168 m->message = Message::CLOSE_ME;
171 Command::getInstance()->postMessageNoLock(m);
173 VInfo* vi = new VInfo();
174 vi->create(400, 150);
175 if (video->getFormat() == Video::PAL)
176 vi->setScreenPos(170, 200);
178 vi->setScreenPos(160, 150);
181 vi->setTitleBarOn(0);
182 vi->setOneLiner(tr("Error playing recording"));
186 m->message = Message::ADD_VIEW;
188 m->parameter = (ULONG)vi;
189 Command::getInstance()->postMessageNoLock(m);
193 int VVideoRec::handleCommand(int command)
208 if (playing) stopPlay();
217 case Remote::SKIPFORWARD:
220 player->skipForward(60);
223 case Remote::SKIPBACK:
226 player->skipBackward(60);
229 case Remote::FORWARD:
231 player->fastForward();
235 case Remote::REVERSE:
237 player->fastBackward();
243 if (myRec->hasMarks())
245 // skip to previous mark
246 Log* logger = Log::getInstance();
247 int currentFrame = (player->getCurrentFrameNum()); // get current Frame
248 currentFrame -= 5 * video->getFPS(); // subtrack 5 seconds, else you cannot skip more than once back ..
250 int prevMark = myRec->getPrevMark(currentFrame); // find previous Frame
253 logger->log("VVideoRec", Log::NOTICE, "jump back from pos %i to mark at %i",currentFrame,prevMark);
254 player->jumpToMark(prevMark);
261 player->skipBackward(10);
267 if (myRec->hasMarks())
270 Log* logger = Log::getInstance();
271 int currentFrame = (player->getCurrentFrameNum());
273 int nextMark = myRec->getNextMark(currentFrame);
277 logger->log("VVideoRec", Log::NOTICE, "jump forward from pos %i to mark at %i",currentFrame,nextMark);
278 player->jumpToMark(nextMark);
285 player->skipForward(10);
292 player->skipBackward(10);
298 player->skipForward(10);
315 if (barShowing) removeBar();
320 case Remote::ZERO: player->jumpToPercent(0); doBar(0); return 2;
321 case Remote::ONE: player->jumpToPercent(10); doBar(0); return 2;
322 case Remote::TWO: player->jumpToPercent(20); doBar(0); return 2;
323 case Remote::THREE: player->jumpToPercent(30); doBar(0); return 2;
324 case Remote::FOUR: player->jumpToPercent(40); doBar(0); return 2;
325 case Remote::FIVE: player->jumpToPercent(50); doBar(0); return 2;
326 case Remote::SIX: player->jumpToPercent(60); doBar(0); return 2;
327 case Remote::SEVEN: player->jumpToPercent(70); doBar(0); return 2;
328 case Remote::EIGHT: player->jumpToPercent(80); doBar(0); return 2;
329 case Remote::NINE: player->jumpToPercent(90); doBar(0); return 2;
334 //Don't use RED for anything. It will eventually be recording summary
340 // for testing EPG in NTSC with a NTSC test video
341 Video::getInstance()->setMode(Video::QUARTER);
342 Video::getInstance()->setPosition(170, 5);
343 VEpg* vepg = new VEpg(NULL, 0);
345 ViewMan::getInstance()->add(vepg);
346 ViewMan::getInstance()->updateView(vepg);
359 void VVideoRec::processMessage(Message* m)
361 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Message received");
363 if (m->message == Message::MOUSE_LBDOWN)
365 UINT x = (m->parameter>>16) - getScreenX();
366 UINT y = (m->parameter&0xFFFF) - getScreenY();
370 ViewMan::getInstance()->handleCommand(Remote::OK); //simulate rok press
372 else if (barRegion.x<=x && barRegion.y<=y && (barRegion.x+barRegion.w)>=x && (barRegion.y+barRegion.h)>=y)
374 int progBarXbase = barRegion.x + 300;
375 if (myRec->hasMarks())
377 MarkList* markList = myRec->getMarkList();
378 MarkList::iterator i;
379 Mark* loopMark = NULL;
382 if (myRec->recInfo->timerEnd > time(NULL))
385 // Work out an approximate length in frames (good to 1s...)
386 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
390 lengthFrames = player->getLengthFrames();
392 for(i = markList->begin(); i != markList->end(); i++)
397 posPix = 302 * loopMark->pos / lengthFrames;
398 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, Colour::DANGER);
399 if (x>=barRegion.x + progBarXbase + 2 + posPix
400 && x<=barRegion.x + progBarXbase + 2 + posPix+3
401 && y>=barRegion.y + 12 - 2
402 && y<=barRegion.y + 12 - 2+28)
404 player->jumpToMark(loopMark->pos);
412 if (x>=barRegion.x + progBarXbase + 24
413 && x<=barRegion.x + progBarXbase + 4 + 302
414 && y>=barRegion.y + 12 - 2
415 && y<=barRegion.y + 12 - 2+28)
417 int cx=x-(barRegion.x + progBarXbase + 4);
418 double percent=((double)cx)/302.*100.;
419 player->jumpToPercent(percent);
422 // int progressWidth = 302 * currentFrameNum / lengthFrames;
423 // rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
428 ViewMan::getInstance()->handleCommand(Remote::OK); //simulate rok press
431 else if (m->from == player)
433 if (m->message != Message::PLAYER_EVENT) return;
436 case Player::CONNECTION_LOST: // connection lost detected
438 // I can't handle this, send it to command
439 Message* m = new Message();
440 m->to = Command::getInstance();
441 m->message = Message::CONNECTION_LOST;
442 Command::getInstance()->postMessageNoLock(m);
445 case Player::STOP_PLAYBACK:
447 // FIXME Obselete ish - improve this
448 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
449 m->to = Command::getInstance();
450 m->message = Message::STOP_PLAYBACK;
451 Command::getInstance()->postMessageNoLock(m);
454 case Player::ASPECT43:
458 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 43");
461 viewman->updateView(this, &wssRegion);
465 case Player::ASPECT169:
469 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 169");
472 viewman->updateView(this, &wssRegion);
478 else if (m->message == Message::AUDIO_CHANGE_CHANNEL)
480 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received change audio channel to %i", m->parameter);
481 player->setAudioChannel(m->parameter);
483 else if (m->message == Message::CHILD_CLOSE)
489 if (!barGenHold && !barScanHold && !barVasHold) removeBar();
494 void VVideoRec::stopPlay()
496 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Pre stopPlay");
498 // FIXME work out a better soln for this
499 // Fix a problem to do with thread sync here
500 // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
501 // (or maybe just the wrong thread being selected?) for the main loop to lock and process
502 // the video stop message it is possible for a bar message to stack up after a stop message
503 // when the bar message is finally processed the prog crashes because this is deleted by then
508 vdr->stopStreaming();
513 if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
514 Log::getInstance()->log("VVideoRec", Log::DEBUG, "Post stopPlay");
517 void VVideoRec::toggleChopSides()
519 if (video->getTVsize() == Video::ASPECT16X9) return; // Means nothing for 16:9 TVs
521 if (videoMode == Video::NORMAL)
523 videoMode = Video::LETTERBOX;
524 video->setMode(Video::LETTERBOX);
528 videoMode = Video::NORMAL;
529 video->setMode(Video::NORMAL);
533 void VVideoRec::doAudioSelector()
535 bool* availableAudioChannels = player->getDemuxerAudioChannels();
536 int currentAudioChannel = player->getCurrentAudioChannel();
538 vas = new VAudioSelector(this, availableAudioChannels, currentAudioChannel, myRec->recInfo);
539 vas->setBackgroundColour(barBlue);
540 if (video->getFormat() == Video::PAL)
542 // vas->setScreenPos(62, barRegion.y - 120);
543 vas->setScreenPos(0, barRegion.y - 120);
547 // vas->setScreenPos(57, barRegion.y - 120);
548 vas->setScreenPos(0, barRegion.y - 120);
556 viewman->updateView(vas);
559 void VVideoRec::doBar(int action)
563 rectangle(barRegion, barBlue);
565 /* Work out what to display - choices:
572 Specials, informed by parameter
582 w.setSurface(surface);
584 w.setSurfaceOffset(barRegion.x + 66, barRegion.y + 16);
586 UCHAR playerState = 0;
590 if (action == 1) w.nextSymbol = WSymbol::SKIPFORWARD;
591 else if (action == 2) w.nextSymbol = WSymbol::SKIPBACK;
592 else if (action == 3) w.nextSymbol = WSymbol::SKIPFORWARD2;
593 else if (action == 4) w.nextSymbol = WSymbol::SKIPBACK2;
597 playerState = player->getState();
598 if (playerState == Player::S_PAUSE_P) w.nextSymbol = WSymbol::PAUSE;
599 else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
600 else if (playerState == Player::S_FFWD) w.nextSymbol = WSymbol::FFWD;
601 else if (playerState == Player::S_FBWD) w.nextSymbol = WSymbol::FBWD;
602 else w.nextSymbol = WSymbol::PLAY;
607 if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD))
609 // draw blips to show how fast the scan is
610 UCHAR scanrate = player->getIScanRate();
613 char* text = new char[5];
614 SNPRINTF(text, 5, "%ux", scanrate);
615 drawText(text, barRegion.x + 102, barRegion.y + 12, Colour::LIGHTTEXT);
621 viewman->updateView(this, &barRegion);
623 timers->cancelTimer(this, 1);
626 if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD)) barScanHold = true;
627 else barScanHold = false;
629 if (!barGenHold && !barScanHold && !barVasHold) timers->setTimerD(this, 1, 4);
631 timers->setTimerD(this, 2, 0, 200000000);
634 void VVideoRec::timercall(int clientReference)
636 switch(clientReference)
647 if (!barShowing) break;
649 viewman->updateView(this, &barRegion);
650 timers->setTimerD(this, 2, 0, 200000000);
656 void VVideoRec::drawBarClocks()
660 UCHAR playerState = player->getState();
661 // sticky bar is set if we are in ffwd/fbwd mode
662 // if player has gone to S_PLAY then kill stickyBar, and run doBar(0) which
663 // will repaint all the bar (it will call this function again, but
664 // this section won't run because stickyBarF will then == false)
666 if ((playerState != Player::S_FFWD) && (playerState != Player::S_FBWD))
670 return; // doBar will call this function and do the rest
674 Log* logger = Log::getInstance();
675 logger->log("VVideoRec", Log::DEBUG, "Draw bar clocks");
678 // Blank the area first
679 rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
683 struct tm* tms = localtime(&t);
684 strftime(timeString, 19, "%H:%M", tms);
685 drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
689 rectangle(clocksRegion, barBlue);
691 ULONG currentFrameNum = player->getCurrentFrameNum();
693 if (myRec->recInfo->timerEnd > time(NULL))
696 // Work out an approximate length in frames (good to 1s...)
697 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
701 lengthFrames = player->getLengthFrames();
704 hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
705 hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
708 if (currentFrameNum >= lengthFrames)
710 strcpy(buffer, "-:--:-- / -:--:--");
714 SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
715 logger->log("VVideoRec", Log::DEBUG, buffer);
718 drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
727 int progBarXbase = barRegion.x + 300;
729 rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
730 rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
732 if (currentFrameNum > lengthFrames) return;
733 if (lengthFrames == 0) return;
735 // Draw yellow portion
736 int progressWidth = 302 * currentFrameNum / lengthFrames;
737 rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
739 if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
741 int nrWidth = (int)(302 * ((double)(lengthFrames - player->getLengthFrames()) / lengthFrames));
743 Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
744 Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
745 Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
746 rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
750 // Now calc position for blips
752 if (myRec->hasMarks())
754 // Draw blips where there are cut marks
755 MarkList* markList = myRec->getMarkList();
756 MarkList::iterator i;
757 Mark* loopMark = NULL;
759 for(i = markList->begin(); i != markList->end(); i++)
764 logger->log("VVideoRec", Log::DEBUG, "Drawing mark at frame %i", loopMark->pos);
765 posPix = 302 * loopMark->pos / lengthFrames;
766 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, Colour::DANGER);
772 // Draw blips where start and end margins probably are
774 posPix = 302 * startMargin * video->getFPS() / lengthFrames;
776 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
777 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
779 posPix = 302 * (lengthFrames - endMargin * video->getFPS()) / lengthFrames;
781 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
782 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
786 void VVideoRec::removeBar()
788 if (!barShowing) return;
789 timers->cancelTimer(this, 2);
794 rectangle(barRegion, transparent);
795 viewman->updateView(this, &barRegion);