]> git.vomp.tv Git - vompclient.git/blob - vradiorec.cc
20 CWFs, fix some snprintf calls
[vompclient.git] / vradiorec.cc
1 /*
2     Copyright 2004-2019 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, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "vradiorec.h"
22
23 #include "command.h"
24 #include "osd.h"
25 #include "player.h"
26 #include "wsymbol.h"
27 #include "recording.h"
28 #include "recinfo.h"
29 #include "message.h"
30 #include "vdr.h"
31 #include "video.h"
32 #include "timers.h"
33 #include "playerradio.h"
34 #include "boxstack.h"
35 #include "input.h"
36 #include "vinfo.h"
37 #include "i18n.h"
38 #include "log.h"
39 #include "messagequeue.h"
40
41 VRadioRec::VRadioRec(Recording* rec)
42 {
43   boxstack = BoxStack::getInstance();
44   vdr = VDR::getInstance();
45   video = Video::getInstance();
46   timers = Timers::getInstance();
47   myRec = rec;
48   playing = false;
49   startMargin = 0;
50   endMargin = 0;
51
52   player = new PlayerRadio(Command::getInstance(), this);
53
54   char* cstartMargin = vdr->configLoad("Timers", "Start margin");
55   char* cendMargin = vdr->configLoad("Timers", "End margin");
56   if (!cstartMargin)
57   {
58     startMargin = 300; // 5 mins default
59   }
60   else
61   {
62     startMargin = atoi(cstartMargin) * 60;
63     delete[] cstartMargin;
64   }
65
66   if (!cendMargin)
67   {
68     endMargin = 300; // 5 mins default
69   }
70   else
71   {
72     endMargin = atoi(cendMargin) * 60;
73     delete[] cendMargin;
74   }
75
76   Log::getInstance()->log("VRadioRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
77
78   setSize(video->getScreenWidth(), video->getScreenHeight());
79   createBuffer();
80   transparent.set(0, 0, 0, 0);
81   setBackgroundColour(transparent);
82   setPosition(0, 0);
83
84   barRegion.x = 0;
85   barRegion.y = video->getScreenHeight() - 58;   // FIXME, need to be - 1? and below?
86   barRegion.w = video->getScreenWidth();
87   barRegion.h = 58;
88
89   clocksRegion.x = barRegion.x + 140;
90   clocksRegion.y = barRegion.y + 12;
91   clocksRegion.w = 170;
92   clocksRegion.h = getFontHeight();
93
94
95   barBlue.set(0, 0, 0, 128);
96
97   barShowing = false;
98 }
99
100 void VRadioRec::preDelete()
101 {
102   timers->cancelTimer(this, 1);
103   timers->cancelTimer(this, 2);
104 }
105
106 VRadioRec::~VRadioRec()
107 {
108   if (playing) stopPlay();
109
110
111   // kill recInfo in case resumePoint has changed (likely)
112   myRec->dropRecInfo();
113   // FIXME - do this properly - save the resume point back to the server manually and update
114   // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
115 }
116
117 void VRadioRec::draw()
118 {
119   fillColour(transparent);
120 }
121
122 void VRadioRec::go(bool resume)
123 {
124   ULONG startFrameNum;
125   if (resume)
126     startFrameNum = myRec->recInfo->resumePoint;
127   else
128     startFrameNum = 0;
129
130   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Starting stream: %s", myRec->getFileName());
131   ULONG lengthFrames = 0;
132   bool isPesRecording;
133   ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames, &isPesRecording);
134   myRec->IsPesRecording = isPesRecording;
135
136   bool cantStart = false;
137
138   if (!lengthBytes) cantStart = true;
139   else if (!player->init(lengthBytes, lengthFrames, myRec->IsPesRecording)) cantStart = true;
140   else
141   {
142     doBar(0);
143     player->setCurrentFrameNumber(startFrameNum);
144     player->play();
145     playing = true;
146   }
147
148   if (cantStart)
149   {
150     stopPlay(); // clean up
151
152     if (!vdr->isConnected())
153     {
154       Command::getInstance()->connectionLost();
155       return;
156     }
157
158     Message* m = new Message();
159     m->message = Message::CLOSE_ME;
160     m->from = this;
161     m->to = boxstack;
162     MessageQueue::getInstance()->postMessage(m);
163
164     VInfo* vi = new VInfo();
165     vi->setSize(400, 150);
166     vi->createBuffer();
167     if (video->getFormat() == Video::PAL)
168       vi->setPosition(170, 200);
169     else
170       vi->setPosition(160, 150);
171     vi->setExitable();
172     vi->setBorderOn(1);
173     vi->setTitleBarOn(0);
174     vi->setOneLiner(tr("Error playing recording"));
175     vi->draw();
176
177     m = new Message();
178     m->message = Message::ADD_VIEW;
179     m->to = boxstack;
180     m->data = reinterpret_cast<void*>(vi);
181     MessageQueue::getInstance()->postMessage(m);
182   }
183 }
184
185 int VRadioRec::handleCommand(int command)
186 {
187   switch(command)
188   {
189     case Input::PLAY:
190     {
191       player->play();
192       doBar(0);
193       return 2;
194     }
195     case Input::PLAYPAUSE:
196     {
197         player->playpause();
198         doBar(0);
199         return 2;
200     }
201
202     case Input::STOP:
203     case Input::BACK:
204     case Input::MENU:
205     {
206       if (playing) stopPlay();
207       return 4;
208     }
209     case Input::PAUSE:
210     {
211       player->pause();
212       doBar(0);
213       return 2;
214     }
215     case Input::SKIPFORWARD:
216     {
217       doBar(3);
218       player->skipForward(60);
219       return 2;
220     }
221     case Input::SKIPBACK:
222     {
223       doBar(4);
224       player->skipBackward(60);
225       return 2;
226     }
227     case Input::YELLOW:
228     {
229       doBar(2);
230       player->skipBackward(10);
231       return 2;
232     }
233     case Input::BLUE:
234     {
235       doBar(1);
236       player->skipForward(10);
237       return 2;
238     }
239     case Input::OK:
240     {
241       if (barShowing) removeBar();
242       else doBar(0);
243       return 2;
244     }
245
246     case Input::ZERO:  player->jumpToPercent(0);  doBar(0);  return 2;
247     case Input::ONE:   player->jumpToPercent(10); doBar(0);  return 2;
248     case Input::TWO:   player->jumpToPercent(20); doBar(0);  return 2;
249     case Input::THREE: player->jumpToPercent(30); doBar(0);  return 2;
250     case Input::FOUR:  player->jumpToPercent(40); doBar(0);  return 2;
251     case Input::FIVE:  player->jumpToPercent(50); doBar(0);  return 2;
252     case Input::SIX:   player->jumpToPercent(60); doBar(0);  return 2;
253     case Input::SEVEN: player->jumpToPercent(70); doBar(0);  return 2;
254     case Input::EIGHT: player->jumpToPercent(80); doBar(0);  return 2;
255     case Input::NINE:  player->jumpToPercent(90); doBar(0);  return 2;
256
257 #ifdef DEV
258     case Input::RED:
259     {
260       //player->test1();
261
262       return 2;
263     }
264     case Input::GREEN:
265     {
266       //player->test2();
267       return 2;
268     }
269 #endif
270
271   }
272
273   return 1;
274 }
275
276 void VRadioRec::processMessage(Message* m)
277 {
278   if (m->message == Message::MOUSE_LBDOWN)
279   {
280     int x=(m->parameter>>16)-(int)getScreenX();
281     int y=(m->parameter&0xFFFF)-(int)getScreenY();
282     if (!barShowing)
283     {
284       boxstack->handleCommand(Input::OK); //simulate rok press
285     }
286     else if ((int)barRegion.x<=x && (int)barRegion.y<=y && ((int)barRegion.x+(int)barRegion.w)>=x
287              &&  ((int)barRegion.y+(int)barRegion.h)>=y)
288     {
289       int progBarXbase = barRegion.x + 300;
290       if (x>=(int)barRegion.x + progBarXbase + 24
291         && x<=(int)barRegion.x + progBarXbase + 4 + 302
292         && y>=(int)barRegion.y + 12 - 2
293         && y<=(int)barRegion.y + 12 - 2+28)
294       {
295         int cx=x-(barRegion.x + progBarXbase + 4);
296         double percent=((double)cx)/302.*100.;
297         player->jumpToPercent(percent);
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     Log::getInstance()->log("VRadioRec", Log::DEBUG, "Message received");
314
315     switch(m->parameter)
316     {
317       case Player::CONNECTION_LOST: // connection lost detected
318       {
319         // I can't handle this, send it to command
320         Message* m2 = new Message();
321         m2->to = Command::getInstance();
322         m2->message = Message::CONNECTION_LOST;
323         MessageQueue::getInstance()->postMessage(m2);
324         break;
325       }
326       case Player::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->to = Command::getInstance();
331         m2->message = Message::STOP_PLAYBACK;
332         MessageQueue::getInstance()->postMessage(m2);
333         break;
334       }
335     }
336   }
337 }
338
339 void VRadioRec::stopPlay()
340 {
341   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Pre stopPlay");
342
343   removeBar();
344   player->stop();
345   vdr->stopStreaming();
346   delete player;
347
348   playing = false;
349
350   if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
351   Log::getInstance()->log("VRadioRec", Log::DEBUG, "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 == Player::S_PAUSE_P)      w.nextSymbol = WSymbol::PAUSE;
392     else if (playerState == Player::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   logger->log("VRadioRec", Log::DEBUG, "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     logger->log("VRadioRec", Log::DEBUG, 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   logger->log("VRadioRec", Log::DEBUG, "blips");
504
505   // Now calc position for start margin blips
506   int posPix;
507
508   posPix = 302 * startMargin / lengthSeconds;
509   logger->log("VRadioRec", Log::DEBUG, "posPix %i", 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, transparent);
526   boxstack->update(this, &barRegion);
527 }
528