]> git.vomp.tv Git - vompclient.git/blob - vradiorec.cc
Rewrite timers class using std::thread/mutex/cond/chrono
[vompclient.git] / vradiorec.cc
1 /*
2     Copyright 2004-2020 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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/>.
18 */
19
20 #include "command.h"
21 #include "osd.h"
22 #include "player.h"
23 #include "wsymbol.h"
24 #include "recording.h"
25 #include "recinfo.h"
26 #include "message.h"
27 #include "vdr.h"
28 #include "video.h"
29 #include "playerradio.h"
30 #include "boxstack.h"
31 #include "input.h"
32 #include "vinfo.h"
33 #include "i18n.h"
34 #include "log.h"
35 #include "messagequeue.h"
36
37 #include "vradiorec.h"
38
39 VRadioRec::VRadioRec(Recording* rec)
40 {
41   boxstack = BoxStack::getInstance();
42   vdr = VDR::getInstance();
43   video = Video::getInstance();
44   timers = Timers::getInstance();
45   myRec = rec;
46   playing = false;
47   startMargin = 0;
48   endMargin = 0;
49
50   player = new PlayerRadio(Command::getInstance(), this);
51
52   char* cstartMargin = vdr->configLoad("Timers", "Start margin");
53   char* cendMargin = vdr->configLoad("Timers", "End margin");
54   if (!cstartMargin)
55   {
56     startMargin = 300; // 5 mins default
57   }
58   else
59   {
60     startMargin = atoi(cstartMargin) * 60;
61     delete[] cstartMargin;
62   }
63
64   if (!cendMargin)
65   {
66     endMargin = 300; // 5 mins default
67   }
68   else
69   {
70     endMargin = atoi(cendMargin) * 60;
71     delete[] cendMargin;
72   }
73
74   Log::getInstance()->log("VRadioRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
75
76   setSize(video->getScreenWidth(), video->getScreenHeight());
77   createBuffer();
78   transparent.set(0, 0, 0, 0);
79   setBackgroundColour(transparent);
80   setPosition(0, 0);
81
82   barRegion.x = 0;
83   barRegion.y = video->getScreenHeight() - 58;   // FIXME, need to be - 1? and below?
84   barRegion.w = video->getScreenWidth();
85   barRegion.h = 58;
86
87   clocksRegion.x = barRegion.x + 140;
88   clocksRegion.y = barRegion.y + 12;
89   clocksRegion.w = 170;
90   clocksRegion.h = getFontHeight();
91
92
93   barBlue.set(0, 0, 0, 128);
94
95   barShowing = false;
96 }
97
98 void VRadioRec::preDelete()
99 {
100   timers->cancelTimer(this, 1);
101   timers->cancelTimer(this, 2);
102 }
103
104 VRadioRec::~VRadioRec()
105 {
106   if (playing) stopPlay();
107
108
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
113 }
114
115 void VRadioRec::draw()
116 {
117   fillColour(transparent);
118 }
119
120 void VRadioRec::go(bool resume)
121 {
122   ULONG startFrameNum;
123   if (resume)
124     startFrameNum = myRec->recInfo->resumePoint;
125   else
126     startFrameNum = 0;
127
128   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Starting stream: %s", myRec->getFileName());
129   ULONG lengthFrames = 0;
130   bool isPesRecording;
131   ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames, &isPesRecording);
132   myRec->IsPesRecording = isPesRecording;
133
134   bool cantStart = false;
135
136   if (!lengthBytes) cantStart = true;
137   else if (!player->init(lengthBytes, lengthFrames, myRec->IsPesRecording)) cantStart = true;
138   else
139   {
140     doBar(0);
141     player->setCurrentFrameNumber(startFrameNum);
142     player->play();
143     playing = true;
144   }
145
146   if (cantStart)
147   {
148     stopPlay(); // clean up
149
150     if (!vdr->isConnected())
151     {
152       Command::getInstance()->connectionLost();
153       return;
154     }
155
156     Message* m = new Message();
157     m->message = Message::CLOSE_ME;
158     m->from = this;
159     m->to = boxstack;
160     MessageQueue::getInstance()->postMessage(m);
161
162     VInfo* vi = new VInfo();
163     vi->setSize(400, 150);
164     vi->createBuffer();
165     if (video->getFormat() == Video::PAL)
166       vi->setPosition(170, 200);
167     else
168       vi->setPosition(160, 150);
169     vi->setExitable();
170     vi->setBorderOn(1);
171     vi->setTitleBarOn(0);
172     vi->setOneLiner(tr("Error playing recording"));
173     vi->draw();
174
175     m = new Message();
176     m->message = Message::ADD_VIEW;
177     m->to = boxstack;
178     m->data = reinterpret_cast<void*>(vi);
179     MessageQueue::getInstance()->postMessage(m);
180   }
181 }
182
183 int VRadioRec::handleCommand(int command)
184 {
185   switch(command)
186   {
187     case Input::PLAY:
188     {
189       player->play();
190       doBar(0);
191       return 2;
192     }
193     case Input::PLAYPAUSE:
194     {
195         player->playpause();
196         doBar(0);
197         return 2;
198     }
199
200     case Input::STOP:
201     case Input::BACK:
202     case Input::MENU:
203     {
204       if (playing) stopPlay();
205       return 4;
206     }
207     case Input::PAUSE:
208     {
209       player->pause();
210       doBar(0);
211       return 2;
212     }
213     case Input::SKIPFORWARD:
214     {
215       doBar(3);
216       player->skipForward(60);
217       return 2;
218     }
219     case Input::SKIPBACK:
220     {
221       doBar(4);
222       player->skipBackward(60);
223       return 2;
224     }
225     case Input::YELLOW:
226     {
227       doBar(2);
228       player->skipBackward(10);
229       return 2;
230     }
231     case Input::BLUE:
232     {
233       doBar(1);
234       player->skipForward(10);
235       return 2;
236     }
237     case Input::OK:
238     {
239       if (barShowing) removeBar();
240       else doBar(0);
241       return 2;
242     }
243
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;
254
255 #ifdef DEV
256     case Input::RED:
257     {
258       //player->test1();
259
260       return 2;
261     }
262     case Input::GREEN:
263     {
264       //player->test2();
265       return 2;
266     }
267 #endif
268
269   }
270
271   return 1;
272 }
273
274 void VRadioRec::processMessage(Message* m)
275 {
276   if (m->message == Message::MOUSE_LBDOWN)
277   {
278     int x=(m->parameter>>16)-(int)getScreenX();
279     int y=(m->parameter&0xFFFF)-(int)getScreenY();
280     if (!barShowing)
281     {
282       boxstack->handleCommand(Input::OK); //simulate rok press
283     }
284     else if ((int)barRegion.x<=x && (int)barRegion.y<=y && ((int)barRegion.x+(int)barRegion.w)>=x
285              &&  ((int)barRegion.y+(int)barRegion.h)>=y)
286     {
287       int progBarXbase = barRegion.x + 300;
288       if (x>=(int)barRegion.x + progBarXbase + 24
289         && x<=(int)barRegion.x + progBarXbase + 4 + 302
290         && y>=(int)barRegion.y + 12 - 2
291         && y<=(int)barRegion.y + 12 - 2+28)
292       {
293         int cx=x-(barRegion.x + progBarXbase + 4);
294         double percent=((double)cx)/302.*100.;
295         player->jumpToPercent(percent);
296         doBar(3);
297         return;
298         //  int progressWidth = 302 * currentFrameNum / lengthFrames;
299         //  rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, DrawStyle::SELECTHIGHLIGHT);
300       }
301     }
302     else
303     {
304       boxstack->handleCommand(Input::OK); //simulate rok press
305     }
306   }
307   else if (m->message == Message::PLAYER_EVENT)
308   {
309     if (m->from != player) return;
310
311     Log::getInstance()->log("VRadioRec", Log::DEBUG, "Message received");
312
313     switch(m->parameter)
314     {
315       case Player::CONNECTION_LOST: // connection lost detected
316       {
317         // I can't handle this, send it to command
318         Message* m2 = new Message();
319         m2->to = Command::getInstance();
320         m2->message = Message::CONNECTION_LOST;
321         MessageQueue::getInstance()->postMessage(m2);
322         break;
323       }
324       case Player::STOP_PLAYBACK:
325       {
326         // FIXME Obselete ish - improve this
327         Message* m2 = new Message(); // Must be done after this thread finishes, and must break into master mutex
328         m2->to = Command::getInstance();
329         m2->message = Message::STOP_PLAYBACK;
330         MessageQueue::getInstance()->postMessage(m2);
331         break;
332       }
333     }
334   }
335 }
336
337 void VRadioRec::stopPlay()
338 {
339   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Pre stopPlay");
340
341   removeBar();
342   player->stop();
343   vdr->stopStreaming();
344   delete player;
345
346   playing = false;
347
348   if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
349   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Post stopPlay");
350 }
351
352 void VRadioRec::doBar(int action)
353 {
354   barShowing = true;
355
356   rectangle(barRegion, barBlue);
357
358   /* Work out what to display - choices:
359
360   Playing  >
361   Paused   ||
362
363   Specials, informed by parameter
364
365   Skip forward 10s    >|
366   Skip backward 10s   |<
367   Skip forward 1m     >>|
368   Skip backward 1m    |<<
369
370   */
371
372   WSymbol w;
373   TEMPADD(&w);
374   w.nextSymbol = 0;
375   w.setPosition(barRegion.x + 66, barRegion.y + 16);
376
377   UCHAR playerState = 0;
378
379   if (action)
380   {
381     if (action == 1)       w.nextSymbol = WSymbol::SKIPFORWARD;
382     else if (action == 2)  w.nextSymbol = WSymbol::SKIPBACK;
383     else if (action == 3)  w.nextSymbol = WSymbol::SKIPFORWARD2;
384     else if (action == 4)  w.nextSymbol = WSymbol::SKIPBACK2;
385   }
386   else
387   {
388     playerState = player->getState();
389     if (playerState == Player::S_PAUSE_P)      w.nextSymbol = WSymbol::PAUSE;
390     else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
391     else                                       w.nextSymbol = WSymbol::PLAY;
392   }
393
394   w.draw();
395
396   drawBarClocks();
397
398   BoxStack::getInstance()->update(this, &barRegion);
399
400   timers->setTimerD(this, 1, 4); // only set the getridofbar timer if not ffwd/fbwd
401   timers->setTimerD(this, 2, 0, 200000000);
402 }
403
404 void VRadioRec::timercall(int clientReference)
405 {
406   switch(clientReference)
407   {
408     case 1:
409     {
410       // Remove bar
411       removeBar();
412       break;
413     }
414     case 2:
415     {
416       // Update clock
417       if (!barShowing) break;
418       drawBarClocks();
419       boxstack->update(this, &barRegion);
420       
421       timers->setTimerD(this, 2, 0, 200000000);
422       break;
423     }
424   }
425 }
426
427 void VRadioRec::drawBarClocks()
428 {
429   Log* logger = Log::getInstance();
430   logger->log("VRadioRec", Log::DEBUG, "Draw bar clocks");
431
432   // Draw RTC
433   // Blank the area first
434   rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
435   char timeString[20];
436   time_t t;
437   time(&t);
438   struct tm tms;
439   LOCALTIME_R(&t, &tms);
440   strftime(timeString, 19, "%H:%M", &tms);
441   drawText(timeString, barRegion.x + 624, barRegion.y + 12, DrawStyle::LIGHTTEXT);
442
443   // Draw clocks
444
445   rectangle(clocksRegion, barBlue);
446
447   ULONG currentSeconds = player->getCurrentSeconds();
448   ULONG lengthSeconds = player->getLengthSeconds();
449   char buffer[100];
450
451   if (lengthSeconds && (currentSeconds < lengthSeconds))
452   {
453     ULONG dcurrentSeconds = currentSeconds;
454     ULONG dlengthSeconds = lengthSeconds;
455
456     ULONG currentHours = dcurrentSeconds / 3600;
457     dcurrentSeconds %= 3600;
458     ULONG currentMinutes = dcurrentSeconds / 60;
459     dcurrentSeconds %= 60;
460
461     ULONG lengthHours = dlengthSeconds / 3600;
462     dlengthSeconds %= 3600;
463     ULONG lengthMinutes = dlengthSeconds / 60;
464     dlengthSeconds %= 60;
465
466     SNPRINTF(buffer, 99, "%01lu:%02lu:%02lu / %01lu:%02lu:%02lu", currentHours, currentMinutes, dcurrentSeconds, lengthHours, lengthMinutes, dlengthSeconds);
467     logger->log("VRadioRec", Log::DEBUG, buffer);
468   }
469   else
470   {
471     strcpy(buffer, "-:--:-- / -:--:--");
472   }
473
474   drawText(buffer, clocksRegion.x, clocksRegion.y, DrawStyle::LIGHTTEXT);
475
476   // Draw progress bar
477   int progBarXbase = barRegion.x + 300;
478
479   rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, DrawStyle::LIGHTTEXT);
480   rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
481
482   if (currentSeconds > lengthSeconds) return;
483   if (lengthSeconds == 0) return;
484
485   // Draw yellow portion
486   int progressWidth = 302 * currentSeconds / lengthSeconds;
487   rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, DrawStyle::SELECTHIGHLIGHT);
488 /*
489
490   if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
491   {
492     int nrWidth = (int)(302 * ((double)(lengthFrames - 0) / lengthFrames)); // 0 inserted instead of getlengthframes
493
494     Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
495 //    Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
496     Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
497     rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, DrawStyle::RED);
498   }
499 */
500
501   logger->log("VRadioRec", Log::DEBUG, "blips");
502
503   // Now calc position for start margin blips
504   int posPix;
505
506   posPix = 302 * startMargin / lengthSeconds;
507   logger->log("VRadioRec", Log::DEBUG, "posPix %i", posPix);
508
509   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, DrawStyle::LIGHTTEXT);
510   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, DrawStyle::LIGHTTEXT);
511
512   posPix = 302 * (lengthSeconds - endMargin) / lengthSeconds;
513
514   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, DrawStyle::LIGHTTEXT);
515   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, DrawStyle::LIGHTTEXT);
516 }
517
518 void VRadioRec::removeBar()
519 {
520   if (!barShowing) return;
521   timers->cancelTimer(this, 2);
522   barShowing = false;
523   rectangle(barRegion, transparent);
524   boxstack->update(this, &barRegion);
525 }
526