]> git.vomp.tv Git - vompclient.git/blob - vradiorec.cc
Log conversion
[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 "control.h"
21 #include "osd.h"
22 #include "wsymbol.h"
23 #include "recording.h"
24 #include "recinfo.h"
25 #include "message.h"
26 #include "vdr.h"
27 #include "video.h"
28 #include "playerradiorec.h"
29 #include "boxstack.h"
30 #include "input.h"
31 #include "vinfo.h"
32 #include "i18n.h"
33 #include "log.h"
34 #include "messagequeue.h"
35
36 #include "vradiorec.h"
37
38 static const char* TAG = "VRadioRec";
39
40 VRadioRec::VRadioRec(Recording* rec)
41 {
42   boxstack = BoxStack::getInstance();
43   vdr = VDR::getInstance();
44   video = Video::getInstance();
45   timers = Timers::getInstance();
46   myRec = rec;
47   playing = false;
48   startMargin = 0;
49   endMargin = 0;
50
51   player = new PlayerRadioRec(Control::getInstance(), this);
52
53   char* cstartMargin = vdr->configLoad("Timers", "Start margin");
54   char* cendMargin = vdr->configLoad("Timers", "End margin");
55   if (!cstartMargin)
56   {
57     startMargin = 300; // 5 mins default
58   }
59   else
60   {
61     startMargin = atoi(cstartMargin) * 60;
62     delete[] cstartMargin;
63   }
64
65   if (!cendMargin)
66   {
67     endMargin = 300; // 5 mins default
68   }
69   else
70   {
71     endMargin = atoi(cendMargin) * 60;
72     delete[] cendMargin;
73   }
74
75   LogNT::getInstance()->debug(TAG, "SM: {} EM: {}", startMargin, endMargin);
76
77   setSize(video->getScreenWidth(), video->getScreenHeight());
78   createBuffer();
79   setBackgroundColour(DrawStyle::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(DrawStyle::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   LogNT::getInstance()->debug(TAG, "Starting stream: {}", 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       Control::getInstance()->connectionLost();
153       return;
154     }
155
156     Message* m = new Message();
157     m->message = Message::CLOSE_ME;
158     m->from = this;
159     m->p_to = Message::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->p_to = Message::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     UINT x = m->parameter - getScreenX();
279     UINT y = m->tag - getScreenY();
280     if (!barShowing)
281     {
282       boxstack->handleCommand(Input::OK); //simulate rok press
283     }
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))
288     {
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)
294       {
295         int cx = x - (barRegion.x + progBarXbase + 4);
296         double percent = 100 * cx / 302.;
297         player->jumpToPercent(percent); // FIXME check this still works
298         doBar(3);
299         return;
300         //  int progressWidth = 302 * currentFrameNum / lengthFrames;
301         //  rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, DrawStyle::SELECTHIGHLIGHT);
302       }
303     }
304     else
305     {
306       boxstack->handleCommand(Input::OK); //simulate rok press
307     }
308   }
309   else if (m->message == Message::PLAYER_EVENT)
310   {
311     if (m->from != player) return;
312
313     LogNT::getInstance()->debug(TAG, "Message received");
314
315     switch(m->parameter)
316     {
317       case PlayerRadioRec::CONNECTION_LOST: // connection lost detected
318       {
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);
324         break;
325       }
326       case PlayerRadioRec::STOP_PLAYBACK:
327       {
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);
333         break;
334       }
335     }
336   }
337 }
338
339 void VRadioRec::stopPlay()
340 {
341   LogNT::getInstance()->debug(TAG, "Pre stopPlay");
342
343   removeBar();
344   player->stop();
345   vdr->stopStreaming();
346   delete player;
347
348   playing = false;
349
350   if (!vdr->isConnected()) { Control::getInstance()->connectionLost(); return; }
351   LogNT::getInstance()->debug(TAG, "Post stopPlay");
352 }
353
354 void VRadioRec::doBar(int action)
355 {
356   barShowing = true;
357
358   rectangle(barRegion, barBlue);
359
360   /* Work out what to display - choices:
361
362   Playing  >
363   Paused   ||
364
365   Specials, informed by parameter
366
367   Skip forward 10s    >|
368   Skip backward 10s   |<
369   Skip forward 1m     >>|
370   Skip backward 1m    |<<
371
372   */
373
374   WSymbol w;
375   TEMPADD(&w);
376   w.nextSymbol = 0;
377   w.setPosition(barRegion.x + 66, barRegion.y + 16);
378
379   UCHAR playerState = 0;
380
381   if (action)
382   {
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;
387   }
388   else
389   {
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;
394   }
395
396   w.draw();
397
398   drawBarClocks();
399
400   BoxStack::getInstance()->update(this, &barRegion);
401
402   timers->setTimerD(this, 1, 4); // only set the getridofbar timer if not ffwd/fbwd
403   timers->setTimerD(this, 2, 0, 200000000);
404 }
405
406 void VRadioRec::timercall(int clientReference)
407 {
408   switch(clientReference)
409   {
410     case 1:
411     {
412       // Remove bar
413       removeBar();
414       break;
415     }
416     case 2:
417     {
418       // Update clock
419       if (!barShowing) break;
420       drawBarClocks();
421       boxstack->update(this, &barRegion);
422       
423       timers->setTimerD(this, 2, 0, 200000000);
424       break;
425     }
426   }
427 }
428
429 void VRadioRec::drawBarClocks()
430 {
431   Log* logger = Log::getInstance();
432   LogNT::getInstance()->debug(TAG, "Draw bar clocks");
433
434   // Draw RTC
435   // Blank the area first
436   rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
437   char timeString[20];
438   time_t t;
439   time(&t);
440   struct tm tms;
441   LOCALTIME_R(&t, &tms);
442   strftime(timeString, 19, "%H:%M", &tms);
443   drawText(timeString, barRegion.x + 624, barRegion.y + 12, DrawStyle::LIGHTTEXT);
444
445   // Draw clocks
446
447   rectangle(clocksRegion, barBlue);
448
449   ULONG currentSeconds = player->getCurrentSeconds();
450   ULONG lengthSeconds = player->getLengthSeconds();
451   char buffer[100];
452
453   if (lengthSeconds && (currentSeconds < lengthSeconds))
454   {
455     ULONG dcurrentSeconds = currentSeconds;
456     ULONG dlengthSeconds = lengthSeconds;
457
458     ULONG currentHours = dcurrentSeconds / 3600;
459     dcurrentSeconds %= 3600;
460     ULONG currentMinutes = dcurrentSeconds / 60;
461     dcurrentSeconds %= 60;
462
463     ULONG lengthHours = dlengthSeconds / 3600;
464     dlengthSeconds %= 3600;
465     ULONG lengthMinutes = dlengthSeconds / 60;
466     dlengthSeconds %= 60;
467
468     SNPRINTF(buffer, 99, "%01lu:%02lu:%02lu / %01lu:%02lu:%02lu", currentHours, currentMinutes, dcurrentSeconds, lengthHours, lengthMinutes, dlengthSeconds);
469     LogNT::getInstance()->debug(TAG, buffer);
470   }
471   else
472   {
473     strcpy(buffer, "-:--:-- / -:--:--");
474   }
475
476   drawText(buffer, clocksRegion.x, clocksRegion.y, DrawStyle::LIGHTTEXT);
477
478   // Draw progress bar
479   int progBarXbase = barRegion.x + 300;
480
481   rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, DrawStyle::LIGHTTEXT);
482   rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
483
484   if (currentSeconds > lengthSeconds) return;
485   if (lengthSeconds == 0) return;
486
487   // Draw yellow portion
488   int progressWidth = 302 * currentSeconds / lengthSeconds;
489   rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, DrawStyle::SELECTHIGHLIGHT);
490 /*
491
492   if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
493   {
494     int nrWidth = (int)(302 * ((double)(lengthFrames - 0) / lengthFrames)); // 0 inserted instead of getlengthframes
495
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);
500   }
501 */
502
503   LogNT::getInstance()->debug(TAG, "blips");
504
505   // Now calc position for start margin blips
506   int posPix;
507
508   posPix = 302 * startMargin / lengthSeconds;
509   LogNT::getInstance()->debug(TAG, "posPix {}", posPix);
510
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);
513
514   posPix = 302 * (lengthSeconds - endMargin) / lengthSeconds;
515
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);
518 }
519
520 void VRadioRec::removeBar()
521 {
522   if (!barShowing) return;
523   timers->cancelTimer(this, 2);
524   barShowing = false;
525   rectangle(barRegion, DrawStyle::TRANSPARENT);
526   boxstack->update(this, &barRegion);
527 }
528