2 Copyright 2004-2006 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 "vradiorec.h"
27 #include "recording.h"
32 #include "playerradio.h"
38 VRadioRec::VRadioRec(Recording* rec)
40 boxstack = BoxStack::getInstance();
41 vdr = VDR::getInstance();
42 video = Video::getInstance();
43 timers = Timers::getInstance();
49 player = new PlayerRadio(Command::getInstance(), this);
51 char* cstartMargin = vdr->configLoad("Timers", "Start margin");
52 char* cendMargin = vdr->configLoad("Timers", "End margin");
55 startMargin = 300; // 5 mins default
59 startMargin = atoi(cstartMargin) * 60;
60 delete[] cstartMargin;
65 endMargin = 300; // 5 mins default
69 endMargin = atoi(cendMargin) * 60;
73 Log::getInstance()->log("VRadioRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
75 setSize(video->getScreenWidth(), video->getScreenHeight());
80 barRegion.y = video->getScreenHeight() - 58; // FIXME, need to be - 1? and below?
81 barRegion.w = video->getScreenWidth();
84 clocksRegion.x = barRegion.x + 140;
85 clocksRegion.y = barRegion.y + 12;
87 clocksRegion.h = surface->getFontHeight();
90 barBlue.set(0, 0, 150, 150);
95 VRadioRec::~VRadioRec()
97 if (playing) stopPlay();
99 timers->cancelTimer(this, 1);
100 timers->cancelTimer(this, 2);
102 // kill recInfo in case resumePoint has changed (likely)
103 myRec->dropRecInfo();
104 // FIXME - do this properly - save the resume point back to the server manually and update
105 // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
108 void VRadioRec::draw()
110 fillColour(Colour::BLACK);
115 Log::getInstance()->log("VRadioRec", Log::DEBUG, "Starting stream: %s", myRec->getFileName());
116 ULONG lengthFrames = 0;
117 ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
119 bool cantStart = false;
121 if (!lengthBytes) cantStart = true;
122 else if (!player->init(lengthBytes, lengthFrames)) cantStart = true;
126 // player->setStartBytes(startBytes);
133 stopPlay(); // clean up
135 if (!vdr->isConnected())
137 Command::getInstance()->connectionLost();
141 Message* m = new Message();
142 m->message = Message::CLOSE_ME;
145 Command::getInstance()->postMessageNoLock(m);
147 VInfo* vi = new VInfo();
148 vi->setSize(400, 150);
150 if (video->getFormat() == Video::PAL)
151 vi->setPosition(170, 200);
153 vi->setPosition(160, 150);
156 vi->setTitleBarOn(0);
157 vi->setOneLiner(tr("Error playing recording"));
161 m->message = Message::ADD_VIEW;
163 m->parameter = (ULONG)vi;
164 Command::getInstance()->postMessageNoLock(m);
168 int VRadioRec::handleCommand(int command)
183 if (playing) stopPlay();
192 case Remote::SKIPFORWARD:
195 player->skipForward(60);
198 case Remote::SKIPBACK:
201 player->skipBackward(60);
207 player->skipBackward(10);
213 player->skipForward(10);
218 if (barShowing) removeBar();
223 case Remote::ZERO: player->jumpToPercent(0); doBar(0); return 2;
224 case Remote::ONE: player->jumpToPercent(10); doBar(0); return 2;
225 case Remote::TWO: player->jumpToPercent(20); doBar(0); return 2;
226 case Remote::THREE: player->jumpToPercent(30); doBar(0); return 2;
227 case Remote::FOUR: player->jumpToPercent(40); doBar(0); return 2;
228 case Remote::FIVE: player->jumpToPercent(50); doBar(0); return 2;
229 case Remote::SIX: player->jumpToPercent(60); doBar(0); return 2;
230 case Remote::SEVEN: player->jumpToPercent(70); doBar(0); return 2;
231 case Remote::EIGHT: player->jumpToPercent(80); doBar(0); return 2;
232 case Remote::NINE: player->jumpToPercent(90); doBar(0); return 2;
253 void VRadioRec::processMessage(Message* m)
255 if (m->message == Message::MOUSE_LBDOWN)
257 int x=(m->parameter>>16)-(int)getScreenX();
258 int y=(m->parameter&0xFFFF)-(int)getScreenY();
261 boxstack->handleCommand(Remote::OK); //simulate rok press
263 else if ((int)barRegion.x<=x && (int)barRegion.y<=y && ((int)barRegion.x+(int)barRegion.w)>=x
264 && ((int)barRegion.y+(int)barRegion.h)>=y)
266 int progBarXbase = barRegion.x + 300;
267 if (x>=(int)barRegion.x + progBarXbase + 24
268 && x<=(int)barRegion.x + progBarXbase + 4 + 302
269 && y>=(int)barRegion.y + 12 - 2
270 && y<=(int)barRegion.y + 12 - 2+28)
272 int cx=x-(barRegion.x + progBarXbase + 4);
273 double percent=((double)cx)/302.*100.;
274 player->jumpToPercent(percent);
277 // int progressWidth = 302 * currentFrameNum / lengthFrames;
278 // rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
283 boxstack->handleCommand(Remote::OK); //simulate rok press
286 else if (m->message == Message::PLAYER_EVENT)
288 if (m->from != player) return;
290 Log::getInstance()->log("VRadioRec", Log::DEBUG, "Message received");
294 case Player::CONNECTION_LOST: // connection lost detected
296 // I can't handle this, send it to command
297 Message* m2 = new Message();
298 m2->to = Command::getInstance();
299 m2->message = Message::CONNECTION_LOST;
300 Command::getInstance()->postMessageNoLock(m2);
303 case Player::STOP_PLAYBACK:
305 // FIXME Obselete ish - improve this
306 Message* m2 = new Message(); // Must be done after this thread finishes, and must break into master mutex
307 m2->to = Command::getInstance();
308 m2->message = Message::STOP_PLAYBACK;
309 Command::getInstance()->postMessageNoLock(m2);
316 void VRadioRec::stopPlay()
318 Log::getInstance()->log("VRadioRec", Log::DEBUG, "Pre stopPlay");
322 vdr->stopStreaming();
327 if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
328 Log::getInstance()->log("VRadioRec", Log::DEBUG, "Post stopPlay");
331 void VRadioRec::doBar(int action)
335 rectangle(barRegion, barBlue);
337 /* Work out what to display - choices:
342 Specials, informed by parameter
354 w.setPosition(barRegion.x + 66, barRegion.y + 16);
356 UCHAR playerState = 0;
360 if (action == 1) w.nextSymbol = WSymbol::SKIPFORWARD;
361 else if (action == 2) w.nextSymbol = WSymbol::SKIPBACK;
362 else if (action == 3) w.nextSymbol = WSymbol::SKIPFORWARD2;
363 else if (action == 4) w.nextSymbol = WSymbol::SKIPBACK2;
367 playerState = player->getState();
368 if (playerState == Player::S_PAUSE_P) w.nextSymbol = WSymbol::PAUSE;
369 else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
370 else w.nextSymbol = WSymbol::PLAY;
377 BoxStack::getInstance()->update(this, &barRegion);
379 timers->setTimerD(this, 1, 4); // only set the getridofbar timer if not ffwd/fbwd
380 timers->setTimerD(this, 2, 0, 200000000);
383 void VRadioRec::timercall(int clientReference)
385 switch(clientReference)
396 if (!barShowing) break;
398 boxstack->update(this, &barRegion);
400 timers->setTimerD(this, 2, 0, 200000000);
406 void VRadioRec::drawBarClocks()
408 Log* logger = Log::getInstance();
409 logger->log("VRadioRec", Log::DEBUG, "Draw bar clocks");
412 // Blank the area first
413 rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
417 struct tm* tms = localtime(&t);
418 strftime(timeString, 19, "%H:%M", tms);
419 drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
423 rectangle(clocksRegion, barBlue);
426 ULONG currentFrameNum = player->getCurrentFrameNum();
428 if (myRec->recInfo->timerEnd > time(NULL))
431 // Work out an approximate length in frames (good to 1s...)
432 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
436 // lengthFrames = player->getLengthFrames();
440 hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
441 hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
444 if (currentFrameNum >= lengthFrames)
446 strcpy(buffer, "-:--:-- / -:--:--");
450 SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
451 logger->log("VRadioRec", Log::DEBUG, buffer);
455 ULONG currentSeconds = player->getCurrentSeconds();
456 ULONG lengthSeconds = player->getLengthSeconds();
459 if (lengthSeconds && (currentSeconds < lengthSeconds))
461 ULONG dcurrentSeconds = currentSeconds;
462 ULONG dlengthSeconds = lengthSeconds;
464 ULONG currentHours = dcurrentSeconds / 3600;
465 dcurrentSeconds %= 3600;
466 ULONG currentMinutes = dcurrentSeconds / 60;
467 dcurrentSeconds %= 60;
469 ULONG lengthHours = dlengthSeconds / 3600;
470 dlengthSeconds %= 3600;
471 ULONG lengthMinutes = dlengthSeconds / 60;
472 dlengthSeconds %= 60;
474 SNPRINTF(buffer, 99, "%01lu:%02lu:%02lu / %01lu:%02lu:%02lu", currentHours, currentMinutes, dcurrentSeconds, lengthHours, lengthMinutes, dlengthSeconds);
475 logger->log("VRadioRec", Log::DEBUG, buffer);
479 strcpy(buffer, "-:--:-- / -:--:--");
482 drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
485 int progBarXbase = barRegion.x + 300;
487 rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
488 rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
490 if (currentSeconds > lengthSeconds) return;
491 if (lengthSeconds == 0) return;
493 // Draw yellow portion
494 int progressWidth = 302 * currentSeconds / lengthSeconds;
495 rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
498 if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
500 int nrWidth = (int)(302 * ((double)(lengthFrames - 0) / lengthFrames)); // 0 inserted instead of getlengthframes
502 Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
503 // Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
504 Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
505 rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
509 logger->log("VRadioRec", Log::DEBUG, "blips");
511 // Now calc position for start margin blips
514 posPix = 302 * startMargin / lengthSeconds;
515 logger->log("VRadioRec", Log::DEBUG, "posPix %i", posPix);
517 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
518 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
520 posPix = 302 * (lengthSeconds - endMargin) / lengthSeconds;
522 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
523 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
526 void VRadioRec::removeBar()
528 if (!barShowing) return;
529 timers->cancelTimer(this, 2);
531 rectangle(barRegion, Colour::BLACK);
532 boxstack->update(this, &barRegion);