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"
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, true);
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");
320 // FIXME work out a better soln for this
321 // Fix a problem to do with thread sync here
322 // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
323 // (or maybe just the wrong thread being selected?) for the main loop to lock and process
324 // the video stop message it is possible for a bar message to stack up after a stop message
325 // when the bar message is finally processed the prog crashes because this is deleted by then
330 vdr->stopStreaming();
335 if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
336 Log::getInstance()->log("VRadioRec", Log::DEBUG, "Post stopPlay");
339 void VRadioRec::doBar(int action)
343 rectangle(barRegion, barBlue);
345 /* Work out what to display - choices:
350 Specials, informed by parameter
362 w.setPosition(barRegion.x + 66, barRegion.y + 16);
364 UCHAR playerState = 0;
368 if (action == 1) w.nextSymbol = WSymbol::SKIPFORWARD;
369 else if (action == 2) w.nextSymbol = WSymbol::SKIPBACK;
370 else if (action == 3) w.nextSymbol = WSymbol::SKIPFORWARD2;
371 else if (action == 4) w.nextSymbol = WSymbol::SKIPBACK2;
375 playerState = player->getState();
376 if (playerState == Player::S_PAUSE_P) w.nextSymbol = WSymbol::PAUSE;
377 else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
378 else w.nextSymbol = WSymbol::PLAY;
385 BoxStack::getInstance()->update(this, &barRegion);
387 timers->setTimerD(this, 1, 4); // only set the getridofbar timer if not ffwd/fbwd
388 timers->setTimerD(this, 2, 0, 200000000);
391 void VRadioRec::timercall(int clientReference)
393 switch(clientReference)
404 if (!barShowing) break;
406 Message* m = new Message();
407 m->message = Message::REDRAW;
410 m->parameter = (ULONG)&barRegion;
411 Command::getInstance()->postMessageFromOuterSpace(m);
412 timers->setTimerD(this, 2, 0, 200000000);
418 void VRadioRec::drawBarClocks()
420 Log* logger = Log::getInstance();
421 logger->log("VRadioRec", Log::DEBUG, "Draw bar clocks");
424 // Blank the area first
425 rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
429 struct tm* tms = localtime(&t);
430 strftime(timeString, 19, "%H:%M", tms);
431 drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
435 rectangle(clocksRegion, barBlue);
438 ULONG currentFrameNum = player->getCurrentFrameNum();
440 if (myRec->recInfo->timerEnd > time(NULL))
443 // Work out an approximate length in frames (good to 1s...)
444 lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
448 // lengthFrames = player->getLengthFrames();
452 hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
453 hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
456 if (currentFrameNum >= lengthFrames)
458 strcpy(buffer, "-:--:-- / -:--:--");
462 SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
463 logger->log("VRadioRec", Log::DEBUG, buffer);
467 ULONG currentSeconds = player->getCurrentSeconds();
468 ULONG lengthSeconds = player->getLengthSeconds();
471 if (lengthSeconds && (currentSeconds < lengthSeconds))
473 ULONG dcurrentSeconds = currentSeconds;
474 ULONG dlengthSeconds = lengthSeconds;
476 ULONG currentHours = dcurrentSeconds / 3600;
477 dcurrentSeconds %= 3600;
478 ULONG currentMinutes = dcurrentSeconds / 60;
479 dcurrentSeconds %= 60;
481 ULONG lengthHours = dlengthSeconds / 3600;
482 dlengthSeconds %= 3600;
483 ULONG lengthMinutes = dlengthSeconds / 60;
484 dlengthSeconds %= 60;
486 SNPRINTF(buffer, 99, "%01lu:%02lu:%02lu / %01lu:%02lu:%02lu", currentHours, currentMinutes, dcurrentSeconds, lengthHours, lengthMinutes, dlengthSeconds);
487 logger->log("VRadioRec", Log::DEBUG, buffer);
491 strcpy(buffer, "-:--:-- / -:--:--");
494 drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
500 int progBarXbase = barRegion.x + 300;
502 rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
503 rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
505 if (currentSeconds > lengthSeconds) return;
506 if (lengthSeconds == 0) return;
508 // Draw yellow portion
509 int progressWidth = 302 * currentSeconds / lengthSeconds;
510 rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
513 if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
515 int nrWidth = (int)(302 * ((double)(lengthFrames - 0) / lengthFrames)); // 0 inserted instead of getlengthframes
517 Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
518 // Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
519 Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
520 rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
524 logger->log("VRadioRec", Log::DEBUG, "blips");
526 // Now calc position for start margin blips
529 posPix = 302 * startMargin / lengthSeconds;
530 logger->log("VRadioRec", Log::DEBUG, "posPix %i", posPix);
532 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
533 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
535 posPix = 302 * (lengthSeconds - endMargin) / lengthSeconds;
537 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
538 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
541 void VRadioRec::removeBar()
543 if (!barShowing) return;
544 timers->cancelTimer(this, 2);
546 rectangle(barRegion, Colour::BLACK);
548 Message* m = new Message();
549 m->message = Message::REDRAW;
552 m->parameter = (ULONG)&barRegion;
553 Command::getInstance()->postMessageFromOuterSpace(m);