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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "vradiorec.h"
23 VRadioRec::VRadioRec(Recording* rec)
25 viewman = ViewMan::getInstance();
26 vdr = VDR::getInstance();
27 video = Video::getInstance();
28 timers = Timers::getInstance();
34 player = new PlayerRadio(Command::getInstance(), this, true);
36 char* cstartMargin = vdr->configLoad("Timers", "Start margin");
37 char* cendMargin = vdr->configLoad("Timers", "End margin");
40 startMargin = 300; // 5 mins default
44 startMargin = atoi(cstartMargin) * 60;
45 delete[] cstartMargin;
50 endMargin = 300; // 5 mins default
54 endMargin = atoi(cendMargin) * 60;
58 Log::getInstance()->log("VRadioRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
60 create(video->getScreenWidth(), video->getScreenHeight());
61 setBackgroundColour(Colour::BLACK);
64 barRegion.y = video->getScreenHeight() - 58; // FIXME, need to be - 1? and below?
65 barRegion.w = video->getScreenWidth();
68 clocksRegion.x = barRegion.x + 140;
69 clocksRegion.y = barRegion.y + 12;
71 clocksRegion.h = surface->getFontHeight();
74 barBlue.set(0, 0, 150, 150);
79 VRadioRec::~VRadioRec()
81 if (playing) stopPlay();
83 timers->cancelTimer(this, 1);
84 timers->cancelTimer(this, 2);
86 // kill recInfo in case resumePoint has changed (likely)
88 // FIXME - do this properly - save the resume point back to the server manually and update
89 // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
92 void VRadioRec::draw()
99 Log::getInstance()->log("VRadioRec", Log::DEBUG, "Starting stream: %s", myRec->getFileName());
100 ULONG lengthFrames = 0;
101 ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
103 bool cantStart = false;
105 if (!lengthBytes) cantStart = true;
106 else if (!player->init(lengthBytes, lengthFrames)) cantStart = true;
110 // player->setStartBytes(startBytes);
117 stopPlay(); // clean up
119 if (!vdr->isConnected())
121 Command::getInstance()->connectionLost();
125 Message* m = new Message();
126 m->message = Message::CLOSE_ME;
129 Command::getInstance()->postMessageNoLock(m);
131 VInfo* vi = new VInfo();
132 vi->create(400, 150);
133 if (video->getFormat() == Video::PAL)
134 vi->setScreenPos(170, 200);
136 vi->setScreenPos(160, 150);
139 vi->setTitleBarOn(0);
140 vi->setOneLiner(tr("Error playing recording"));
144 m->message = Message::ADD_VIEW;
146 m->parameter = (ULONG)vi;
147 Command::getInstance()->postMessageNoLock(m);
151 int VRadioRec::handleCommand(int command)
166 if (playing) stopPlay();
175 case Remote::SKIPFORWARD:
178 player->skipForward(60);
181 case Remote::SKIPBACK:
184 player->skipBackward(60);
190 player->skipBackward(10);
196 player->skipForward(10);
201 if (barShowing) removeBar();
206 case Remote::ZERO: player->jumpToPercent(0); doBar(0); return 2;
207 case Remote::ONE: player->jumpToPercent(10); doBar(0); return 2;
208 case Remote::TWO: player->jumpToPercent(20); doBar(0); return 2;
209 case Remote::THREE: player->jumpToPercent(30); doBar(0); return 2;
210 case Remote::FOUR: player->jumpToPercent(40); doBar(0); return 2;
211 case Remote::FIVE: player->jumpToPercent(50); doBar(0); return 2;
212 case Remote::SIX: player->jumpToPercent(60); doBar(0); return 2;
213 case Remote::SEVEN: player->jumpToPercent(70); doBar(0); return 2;
214 case Remote::EIGHT: player->jumpToPercent(80); doBar(0); return 2;
215 case Remote::NINE: player->jumpToPercent(90); doBar(0); return 2;
236 void VRadioRec::processMessage(Message* m)
238 if (m->message == Message::MOUSE_LBDOWN)
240 int x=(m->parameter>>16)-(int)getScreenX();
241 int y=(m->parameter&0xFFFF)-(int)getScreenY();
244 ViewMan::getInstance()->handleCommand(Remote::OK); //simulate rok press
246 else if ((int)barRegion.x<=x && (int)barRegion.y<=y && ((int)barRegion.x+(int)barRegion.w)>=x
247 && ((int)barRegion.y+(int)barRegion.h)>=y)
249 int progBarXbase = barRegion.x + 300;
250 if (x>=(int)barRegion.x + progBarXbase + 24
251 && x<=(int)barRegion.x + progBarXbase + 4 + 302
252 && y>=(int)barRegion.y + 12 - 2
253 && y<=(int)barRegion.y + 12 - 2+28)
255 int cx=x-(barRegion.x + progBarXbase + 4);
256 double percent=((double)cx)/302.*100.;
257 player->jumpToPercent(percent);
260 // int progressWidth = 302 * currentFrameNum / lengthFrames;
261 // rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
266 ViewMan::getInstance()->handleCommand(Remote::OK); //simulate rok press
269 else if (m->message == Message::PLAYER_EVENT)
271 if (m->from != player) return;
273 Log::getInstance()->log("VRadioRec", Log::DEBUG, "Message received");
277 case Player::CONNECTION_LOST: // connection lost detected
279 // I can't handle this, send it to command
280 Message* m2 = new Message();
281 m2->to = Command::getInstance();
282 m2->message = Message::CONNECTION_LOST;
283 Command::getInstance()->postMessageNoLock(m2);
286 case Player::STOP_PLAYBACK:
288 // FIXME Obselete ish - improve this
289 Message* m2 = new Message(); // Must be done after this thread finishes, and must break into master mutex
290 m2->to = Command::getInstance();
291 m2->message = Message::STOP_PLAYBACK;
292 Command::getInstance()->postMessageNoLock(m2);
299 void VRadioRec::stopPlay()
301 Log::getInstance()->log("VRadioRec", Log::DEBUG, "Pre stopPlay");
303 // FIXME work out a better soln for this
304 // Fix a problem to do with thread sync here
305 // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
306 // (or maybe just the wrong thread being selected?) for the main loop to lock and process
307 // the video stop message it is possible for a bar message to stack up after a stop message
308 // when the bar message is finally processed the prog crashes because this is deleted by then
313 vdr->stopStreaming();
318 if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
319 Log::getInstance()->log("VRadioRec", Log::DEBUG, "Post stopPlay");
322 void VRadioRec::doBar(int action)
326 rectangle(barRegion, barBlue);
328 /* Work out what to display - choices:
333 Specials, informed by parameter
343 w.setSurface(surface);
345 w.setSurfaceOffset(barRegion.x + 66, barRegion.y + 16);
347 UCHAR playerState = 0;
351 if (action == 1) w.nextSymbol = WSymbol::SKIPFORWARD;
352 else if (action == 2) w.nextSymbol = WSymbol::SKIPBACK;
353 else if (action == 3) w.nextSymbol = WSymbol::SKIPFORWARD2;
354 else if (action == 4) w.nextSymbol = WSymbol::SKIPBACK2;
358 playerState = player->getState();
359 if (playerState == Player::S_PAUSE_P) w.nextSymbol = WSymbol::PAUSE;
360 else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
361 else w.nextSymbol = WSymbol::PLAY;
368 ViewMan::getInstance()->updateView(this, &barRegion);
370 timers->setTimerD(this, 1, 4); // only set the getridofbar timer if not ffwd/fbwd
371 timers->setTimerD(this, 2, 0, 200000000);
374 void VRadioRec::timercall(int clientReference)
376 switch(clientReference)
387 if (!barShowing) break;
389 Message* m = new Message();
390 m->message = Message::REDRAW;
391 m->to = ViewMan::getInstance();
393 m->parameter = (ULONG)&barRegion;
394 Command::getInstance()->postMessageFromOuterSpace(m);
395 timers->setTimerD(this, 2, 0, 200000000);
401 void VRadioRec::drawBarClocks()
403 Log* logger = Log::getInstance();
404 logger->log("VRadioRec", Log::DEBUG, "Draw bar clocks");
407 // Blank the area first
408 rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
412 struct tm* tms = localtime(&t);
413 strftime(timeString, 19, "%H:%M", tms);
414 drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
418 rectangle(clocksRegion, barBlue);
421 ULONG currentFrameNum = player->getCurrentFrameNum();
423 if (myRec->recInfo->timerEnd > time(NULL))
426 // Work out an approximate length in frames (good to 1s...)
427 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
431 // lengthFrames = player->getLengthFrames();
435 hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
436 hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
439 if (currentFrameNum >= lengthFrames)
441 strcpy(buffer, "-:--:-- / -:--:--");
445 SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
446 logger->log("VRadioRec", Log::DEBUG, buffer);
450 ULONG currentSeconds = player->getCurrentSeconds();
451 ULONG lengthSeconds = player->getLengthSeconds();
454 if (lengthSeconds && (currentSeconds < lengthSeconds))
456 ULONG dcurrentSeconds = currentSeconds;
457 ULONG dlengthSeconds = lengthSeconds;
459 ULONG currentHours = dcurrentSeconds / 3600;
460 dcurrentSeconds %= 3600;
461 ULONG currentMinutes = dcurrentSeconds / 60;
462 dcurrentSeconds %= 60;
464 ULONG lengthHours = dlengthSeconds / 3600;
465 dlengthSeconds %= 3600;
466 ULONG lengthMinutes = dlengthSeconds / 60;
467 dlengthSeconds %= 60;
469 SNPRINTF(buffer, 99, "%01lu:%02lu:%02lu / %01lu:%02lu:%02lu", currentHours, currentMinutes, dcurrentSeconds, lengthHours, lengthMinutes, dlengthSeconds);
470 logger->log("VRadioRec", Log::DEBUG, buffer);
474 strcpy(buffer, "-:--:-- / -:--:--");
477 drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
483 int progBarXbase = barRegion.x + 300;
485 rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
486 rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
488 if (currentSeconds > lengthSeconds) return;
489 if (lengthSeconds == 0) return;
491 // Draw yellow portion
492 int progressWidth = 302 * currentSeconds / lengthSeconds;
493 rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
496 if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
498 int nrWidth = (int)(302 * ((double)(lengthFrames - 0) / lengthFrames)); // 0 inserted instead of getlengthframes
500 Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
501 // Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
502 Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
503 rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
507 logger->log("VRadioRec", Log::DEBUG, "blips");
509 // Now calc position for start margin blips
512 posPix = 302 * startMargin / lengthSeconds;
513 logger->log("VRadioRec", Log::DEBUG, "posPix %i", posPix);
515 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
516 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
518 posPix = 302 * (lengthSeconds - endMargin) / lengthSeconds;
520 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
521 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
524 void VRadioRec::removeBar()
526 if (!barShowing) return;
527 timers->cancelTimer(this, 2);
529 rectangle(barRegion, Colour::BLACK);
531 Message* m = new Message();
532 m->message = Message::REDRAW;
533 m->to = ViewMan::getInstance();
535 m->parameter = (ULONG)&barRegion;
536 Command::getInstance()->postMessageFromOuterSpace(m);