2 Copyright 2004-2020 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, see <https://www.gnu.org/licenses/>.
23 #include "recording.h"
28 #include "playerradiorec.h"
34 #include "messagequeue.h"
36 #include "vradiorec.h"
38 static const char* TAG = "VRadioRec";
40 VRadioRec::VRadioRec(Recording* rec)
42 boxstack = BoxStack::getInstance();
43 vdr = VDR::getInstance();
44 video = Video::getInstance();
45 timers = Timers::getInstance();
51 player = new PlayerRadioRec(Control::getInstance(), this);
53 char* cstartMargin = vdr->configLoad("Timers", "Start margin");
54 char* cendMargin = vdr->configLoad("Timers", "End margin");
57 startMargin = 300; // 5 mins default
61 startMargin = atoi(cstartMargin) * 60;
62 delete[] cstartMargin;
67 endMargin = 300; // 5 mins default
71 endMargin = atoi(cendMargin) * 60;
75 LogNT::getInstance()->debug(TAG, "SM: {} EM: {}", startMargin, endMargin);
77 setSize(video->getScreenWidth(), video->getScreenHeight());
79 setBackgroundColour(DrawStyle::TRANSPARENT);
83 barRegion.y = video->getScreenHeight() - 58; // FIXME, need to be - 1? and below?
84 barRegion.w = video->getScreenWidth();
87 clocksRegion.x = barRegion.x + 140;
88 clocksRegion.y = barRegion.y + 12;
90 clocksRegion.h = getFontHeight();
93 barBlue.set(0, 0, 0, 128);
98 void VRadioRec::preDelete()
100 timers->cancelTimer(this, 1);
101 timers->cancelTimer(this, 2);
104 VRadioRec::~VRadioRec()
106 if (playing) stopPlay();
109 // kill recInfo in case resumePoint has changed (likely)
110 myRec->dropRecInfo();
111 // FIXME - do this properly - save the resume point back to the server manually and update
112 // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
115 void VRadioRec::draw()
117 fillColour(DrawStyle::TRANSPARENT);
120 void VRadioRec::go(bool resume)
124 startFrameNum = myRec->recInfo->resumePoint;
128 LogNT::getInstance()->debug(TAG, "Starting stream: {}", myRec->getFileName());
129 ULONG lengthFrames = 0;
131 ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames, &isPesRecording);
132 myRec->IsPesRecording = isPesRecording;
134 bool cantStart = false;
136 if (!lengthBytes) cantStart = true;
137 else if (!player->init(lengthBytes, lengthFrames, myRec->IsPesRecording)) cantStart = true;
141 player->setCurrentFrameNumber(startFrameNum);
148 stopPlay(); // clean up
150 if (!vdr->isConnected())
152 Control::getInstance()->connectionLost();
156 Message* m = new Message();
157 m->message = Message::CLOSE_ME;
159 m->p_to = Message::BOXSTACK;
160 MessageQueue::getInstance()->postMessage(m);
162 VInfo* vi = new VInfo();
163 vi->setSize(400, 150);
165 if (video->getFormat() == Video::PAL)
166 vi->setPosition(170, 200);
168 vi->setPosition(160, 150);
171 vi->setTitleBarOn(0);
172 vi->setOneLiner(tr("Error playing recording"));
176 m->message = Message::ADD_VIEW;
177 m->p_to = Message::BOXSTACK;
178 m->data = reinterpret_cast<void*>(vi);
179 MessageQueue::getInstance()->postMessage(m);
183 int VRadioRec::handleCommand(int command)
193 case Input::PLAYPAUSE:
204 if (playing) stopPlay();
213 case Input::SKIPFORWARD:
216 player->skipForward(60);
219 case Input::SKIPBACK:
222 player->skipBackward(60);
228 player->skipBackward(10);
234 player->skipForward(10);
239 if (barShowing) removeBar();
244 case Input::ZERO: player->jumpToPercent(0); doBar(0); return 2;
245 case Input::ONE: player->jumpToPercent(10); doBar(0); return 2;
246 case Input::TWO: player->jumpToPercent(20); doBar(0); return 2;
247 case Input::THREE: player->jumpToPercent(30); doBar(0); return 2;
248 case Input::FOUR: player->jumpToPercent(40); doBar(0); return 2;
249 case Input::FIVE: player->jumpToPercent(50); doBar(0); return 2;
250 case Input::SIX: player->jumpToPercent(60); doBar(0); return 2;
251 case Input::SEVEN: player->jumpToPercent(70); doBar(0); return 2;
252 case Input::EIGHT: player->jumpToPercent(80); doBar(0); return 2;
253 case Input::NINE: player->jumpToPercent(90); doBar(0); return 2;
274 void VRadioRec::processMessage(Message* m)
276 if (m->message == Message::MOUSE_LBDOWN)
278 UINT x = m->parameter - getScreenX();
279 UINT y = m->tag - getScreenY();
282 boxstack->handleCommand(Input::OK); //simulate rok press
284 else if ( (barRegion.x <= x) // If the click happened within the bar region...
285 && (barRegion.y <= y)
286 && ((barRegion.x + barRegion.w) >= x)
287 && ((barRegion.y + barRegion.h) >= y))
289 UINT progBarXbase = barRegion.x + 300;
290 if ( x >= barRegion.x + progBarXbase + 24
291 && x <= barRegion.x + progBarXbase + 4 + 302
292 && y >= barRegion.y + 12 - 2
293 && y <= barRegion.y + 12 - 2+28)
295 int cx = x - (barRegion.x + progBarXbase + 4);
296 double percent = 100 * cx / 302.;
297 player->jumpToPercent(percent); // FIXME check this still works
300 // int progressWidth = 302 * currentFrameNum / lengthFrames;
301 // rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, DrawStyle::SELECTHIGHLIGHT);
306 boxstack->handleCommand(Input::OK); //simulate rok press
309 else if (m->message == Message::PLAYER_EVENT)
311 if (m->from != player) return;
313 LogNT::getInstance()->debug(TAG, "Message received");
317 case PlayerRadioRec::CONNECTION_LOST: // connection lost detected
319 // I can't handle this, send it to control
320 Message* m2 = new Message();
321 m2->p_to = Message::CONTROL;
322 m2->message = Message::CONNECTION_LOST;
323 MessageQueue::getInstance()->postMessage(m2);
326 case PlayerRadioRec::STOP_PLAYBACK:
328 // FIXME Obselete ish - improve this
329 Message* m2 = new Message(); // Must be done after this thread finishes, and must break into master mutex
330 m2->p_to = Message::CONTROL;
331 m2->message = Message::STOP_PLAYBACK;
332 MessageQueue::getInstance()->postMessage(m2);
339 void VRadioRec::stopPlay()
341 LogNT::getInstance()->debug(TAG, "Pre stopPlay");
345 vdr->stopStreaming();
350 if (!vdr->isConnected()) { Control::getInstance()->connectionLost(); return; }
351 LogNT::getInstance()->debug(TAG, "Post stopPlay");
354 void VRadioRec::doBar(int action)
358 rectangle(barRegion, barBlue);
360 /* Work out what to display - choices:
365 Specials, informed by parameter
377 w.setPosition(barRegion.x + 66, barRegion.y + 16);
379 UCHAR playerState = 0;
383 if (action == 1) w.nextSymbol = WSymbol::SKIPFORWARD;
384 else if (action == 2) w.nextSymbol = WSymbol::SKIPBACK;
385 else if (action == 3) w.nextSymbol = WSymbol::SKIPFORWARD2;
386 else if (action == 4) w.nextSymbol = WSymbol::SKIPBACK2;
390 playerState = player->getState();
391 if (playerState == PlayerRadioRec::S_PAUSE_P) w.nextSymbol = WSymbol::PAUSE;
392 else if (playerState == PlayerRadioRec::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
393 else w.nextSymbol = WSymbol::PLAY;
400 BoxStack::getInstance()->update(this, &barRegion);
402 timers->setTimerD(this, 1, 4); // only set the getridofbar timer if not ffwd/fbwd
403 timers->setTimerD(this, 2, 0, 200000000);
406 void VRadioRec::timercall(int clientReference)
408 switch(clientReference)
419 if (!barShowing) break;
421 boxstack->update(this, &barRegion);
423 timers->setTimerD(this, 2, 0, 200000000);
429 void VRadioRec::drawBarClocks()
431 LogNT* logger = LogNT::getInstance();
432 logger->debug(TAG, "Draw bar clocks");
435 // Blank the area first
436 rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
441 LOCALTIME_R(&t, &tms);
442 strftime(timeString, 19, "%H:%M", &tms);
443 drawText(timeString, barRegion.x + 624, barRegion.y + 12, DrawStyle::LIGHTTEXT);
447 rectangle(clocksRegion, barBlue);
449 ULONG currentSeconds = player->getCurrentSeconds();
450 ULONG lengthSeconds = player->getLengthSeconds();
453 if (lengthSeconds && (currentSeconds < lengthSeconds))
455 ULONG dcurrentSeconds = currentSeconds;
456 ULONG dlengthSeconds = lengthSeconds;
458 ULONG currentHours = dcurrentSeconds / 3600;
459 dcurrentSeconds %= 3600;
460 ULONG currentMinutes = dcurrentSeconds / 60;
461 dcurrentSeconds %= 60;
463 ULONG lengthHours = dlengthSeconds / 3600;
464 dlengthSeconds %= 3600;
465 ULONG lengthMinutes = dlengthSeconds / 60;
466 dlengthSeconds %= 60;
468 SNPRINTF(buffer, 99, "%01lu:%02lu:%02lu / %01lu:%02lu:%02lu", currentHours, currentMinutes, dcurrentSeconds, lengthHours, lengthMinutes, dlengthSeconds);
469 LogNT::getInstance()->debug(TAG, buffer);
473 strcpy(buffer, "-:--:-- / -:--:--");
476 drawText(buffer, clocksRegion.x, clocksRegion.y, DrawStyle::LIGHTTEXT);
479 int progBarXbase = barRegion.x + 300;
481 rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, DrawStyle::LIGHTTEXT);
482 rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
484 if (currentSeconds > lengthSeconds) return;
485 if (lengthSeconds == 0) return;
487 // Draw yellow portion
488 int progressWidth = 302 * currentSeconds / lengthSeconds;
489 rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, DrawStyle::SELECTHIGHLIGHT);
492 if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
494 int nrWidth = (int)(302 * ((double)(lengthFrames - 0) / lengthFrames)); // 0 inserted instead of getlengthframes
496 Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
497 // Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
498 Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
499 rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, DrawStyle::RED);
503 LogNT::getInstance()->debug(TAG, "blips");
505 // Now calc position for start margin blips
508 posPix = 302 * startMargin / lengthSeconds;
509 LogNT::getInstance()->debug(TAG, "posPix {}", posPix);
511 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, DrawStyle::LIGHTTEXT);
512 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, DrawStyle::LIGHTTEXT);
514 posPix = 302 * (lengthSeconds - endMargin) / lengthSeconds;
516 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, DrawStyle::LIGHTTEXT);
517 rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, DrawStyle::LIGHTTEXT);
520 void VRadioRec::removeBar()
522 if (!barShowing) return;
523 timers->cancelTimer(this, 2);
525 rectangle(barRegion, DrawStyle::TRANSPARENT);
526 boxstack->update(this, &barRegion);