]> git.vomp.tv Git - vompclient.git/blob - vvideorec.cc
Fix segfault if no WSS info in config
[vompclient.git] / vvideorec.cc
1 /*
2     Copyright 2004-2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "vvideorec.h"
22
23 VVideoRec::VVideoRec(Recording* rec)
24 {
25   vdr = VDR::getInstance();
26   video = Video::getInstance();
27   timers = Timers::getInstance();
28
29   isRadio = !(rec->recInfo->hasVideo());
30
31   Log::getInstance()->log("VVideoRec", Log::DEBUG, "hasVideo = %i", isRadio);
32
33   player = new Player(Command::getInstance(), this, true, isRadio);
34   player->init();
35
36   videoMode = video->getMode();
37   myRec = rec;
38
39   playing = false;
40
41   startMargin = 0;
42   endMargin = 0;
43   char* cstartMargin = vdr->configLoad("Timers", "Start margin");
44   char* cendMargin = vdr->configLoad("Timers", "End margin");
45   if (!cstartMargin)
46   {
47     startMargin = 300; // 5 mins default
48   }
49   else
50   {
51     startMargin = atoi(cstartMargin) * 60;
52     delete[] cstartMargin;
53   }
54
55   if (!cendMargin)
56   {
57     endMargin = 300; // 5 mins default
58   }
59   else
60   {
61     endMargin = atoi(cendMargin) * 60;
62     delete[] cendMargin;
63   }
64
65   Log::getInstance()->log("VVideoRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
66
67   create(video->getScreenWidth(), video->getScreenHeight());
68   transparent.set(0, 0, 0, 0);
69   setBackgroundColour(transparent);
70
71   barRegion.x = 0;
72   barRegion.y = video->getScreenHeight() - 58;   // FIXME, need to be - 1? and below?
73   barRegion.w = video->getScreenWidth();
74   barRegion.h = 58;
75
76   clocksRegion.x = barRegion.x + 140;
77   clocksRegion.y = barRegion.y + 12;
78   clocksRegion.w = 170;
79   clocksRegion.h = surface->getFontHeight();
80
81
82   barBlue.set(0, 0, 150, 150);
83
84   barShowing = false;
85   stickyBar = false;
86
87   dowss = false;
88   char* optionWSS = vdr->configLoad("General", "WSS");
89   if (optionWSS)
90   {
91     if (strstr(optionWSS, "Yes")) dowss = true;
92     delete[] optionWSS;
93   }
94   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Do WSS: %u", dowss);
95
96   wss.setSurface(surface);
97   wss.setWide(true);
98
99   wssRegion.x = 0;
100   wssRegion.y = 6;
101   wssRegion.w = video->getScreenWidth();
102   wssRegion.h = 2;
103 }
104
105 VVideoRec::~VVideoRec()
106 {
107   if (playing) stopPlay();
108   video->setDefaultAspect();
109
110   timers->cancelTimer(this, 1);
111   timers->cancelTimer(this, 2);
112
113   // kill recInfo in case resumePoint has changed (likely)
114   myRec->dropRecInfo();
115   // FIXME - do this properly - save the resume point back to the server manually and update
116   // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
117 }
118
119 void VVideoRec::draw()
120 {
121   View::draw();
122 }
123
124 void VVideoRec::go(bool resume)
125 {
126   ULONG startFrameNum;
127   if (resume)
128     startFrameNum = myRec->recInfo->resumePoint;
129   else
130     startFrameNum = 0;
131
132   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Starting stream: %s at frame: %lu", myRec->getFileName(), startFrameNum);
133   ULONG lengthFrames = 0;
134   ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
135   if (lengthBytes)
136   {
137     doBar(0);
138     player->setLengthBytes(lengthBytes);
139     if (!isRadio) player->setLengthFrames(lengthFrames);
140     player->setStartFrame(startFrameNum); // means bytes if radio (FIXME not done yet!)
141     player->play();
142     playing = true;
143   }
144   else
145   {
146     stopPlay(); // clean up
147
148     if (!vdr->isConnected())
149     {
150       Command::getInstance()->connectionLost();
151       return;
152     }
153
154     ViewMan* viewman = ViewMan::getInstance();
155
156     Message* m = new Message();
157     m->message = Message::CLOSE_ME;
158     m->from = this;
159     m->to = viewman;
160     Command::getInstance()->postMessageNoLock(m);
161
162     VInfo* vi = new VInfo();
163     vi->create(400, 150);
164     if (video->getFormat() == Video::PAL)
165       vi->setScreenPos(170, 200);
166     else
167       vi->setScreenPos(160, 150);
168     vi->setExitable();
169     vi->setBorderOn(1);
170     vi->setTitleBarOn(0);
171     vi->setOneLiner(tr("Error playing recording"));
172     vi->draw();
173
174     m = new Message();
175     m->message = Message::ADD_VIEW;
176     m->to = viewman;
177     m->parameter = (ULONG)vi;
178     Command::getInstance()->postMessageNoLock(m);
179   }
180 }
181
182 int VVideoRec::handleCommand(int command)
183 {
184   switch(command)
185   {
186     case Remote::PLAY:
187     {
188       player->play();
189       doBar(0);
190       return 2;
191     }
192
193     case Remote::STOP:
194     case Remote::BACK:
195     case Remote::MENU:
196     {
197       if (playing) stopPlay();
198       return 4;
199     }
200     case Remote::PAUSE:
201     {
202       player->pause();
203       doBar(0);
204       return 2;
205     }
206     case Remote::SKIPFORWARD:
207     {
208       doBar(3);
209       player->skipForward(60);
210       return 2;
211     }
212     case Remote::SKIPBACK:
213     {
214       doBar(4);
215       player->skipBackward(60);
216       return 2;
217     }
218     case Remote::FORWARD:
219     {
220       if (isRadio) return 2;
221       player->fastForward();
222       doBar(0);
223       return 2;
224     }
225     case Remote::REVERSE:
226     {
227       if (isRadio) return 2;
228       player->fastBackward();
229       doBar(0);
230       return 2;
231     }
232     case Remote::YELLOW:
233     {
234       doBar(2);
235       player->skipBackward(10);
236       return 2;
237     }
238     case Remote::BLUE:
239     {
240       doBar(1);
241       player->skipForward(10);
242       return 2;
243     }
244     case Remote::FULL:
245     case Remote::TV:
246     {
247       if (isRadio) return 2;
248       toggleChopSides();
249       return 2;
250     }
251
252     case Remote::OK:
253     {
254       if (barShowing) removeBar();
255       else doBar(0);
256       return 2;
257     }
258
259     case Remote::ZERO:  player->jumpToPercent(0);  doBar(0);  return 2;
260     case Remote::ONE:   player->jumpToPercent(10); doBar(0);  return 2;
261     case Remote::TWO:   player->jumpToPercent(20); doBar(0);  return 2;
262     case Remote::THREE: player->jumpToPercent(30); doBar(0);  return 2;
263     case Remote::FOUR:  player->jumpToPercent(40); doBar(0);  return 2;
264     case Remote::FIVE:  player->jumpToPercent(50); doBar(0);  return 2;
265     case Remote::SIX:   player->jumpToPercent(60); doBar(0);  return 2;
266     case Remote::SEVEN: player->jumpToPercent(70); doBar(0);  return 2;
267     case Remote::EIGHT: player->jumpToPercent(80); doBar(0);  return 2;
268     case Remote::NINE:  player->jumpToPercent(90); doBar(0);  return 2;
269
270 #ifdef DEV
271     case Remote::RED:
272     {
273       //player->test1();
274
275
276       /*
277       // for testing EPG in NTSC with a NTSC test video
278       Video::getInstance()->setMode(Video::QUARTER);
279       Video::getInstance()->setPosition(170, 5);
280       VEpg* vepg = new VEpg(NULL, 0);
281       vepg->draw();
282       ViewMan::getInstance()->add(vepg);
283       ViewMan::getInstance()->updateView(vepg);
284       */
285
286       return 2;
287     }
288     case Remote::GREEN:
289     {
290       //player->test2();
291       return 2;
292     }
293 #endif
294
295   }
296
297   return 1;
298 }
299
300 void VVideoRec::processMessage(Message* m)
301 {
302   if (m->from != player) return;
303   if (m->message != Message::PLAYER_EVENT) return;
304
305   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Message received");
306
307   switch(m->parameter)
308   {
309     case Player::CONNECTION_LOST: // connection lost detected
310     {
311       // I can't handle this, send it to command
312       Message* m = new Message();
313       m->to = Command::getInstance();
314       m->message = Message::CONNECTION_LOST;
315       Command::getInstance()->postMessageNoLock(m);
316       break;
317     }
318     case Player::STOP_PLAYBACK:
319     {
320       // FIXME Obselete ish - improve this
321       Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
322       m->to = Command::getInstance();
323       m->message = Message::STOP_PLAYBACK;
324       Command::getInstance()->postMessageNoLock(m);
325       break;
326     }
327     case Player::ASPECT43:
328     {
329       if (dowss)
330       {
331         Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 43");
332         wss.setWide(false);
333         wss.draw();
334         ViewMan::getInstance()->updateView(this, &wssRegion);
335       }
336       break;
337     }
338     case Player::ASPECT169:
339     {
340       if (dowss)
341       {
342         Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received do WSS 169");
343         wss.setWide(true);
344         wss.draw();
345         ViewMan::getInstance()->updateView(this, &wssRegion);
346       }
347       break;
348     }
349   }
350 }
351
352 void VVideoRec::stopPlay()
353 {
354   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Pre stopPlay");
355
356   // FIXME work out a better soln for this
357   // Fix a problem to do with thread sync here
358   // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
359   // (or maybe just the wrong thread being selected?) for the main loop to lock and process
360   // the video stop message it is possible for a bar message to stack up after a stop message
361   // when the bar message is finally processed the prog crashes because this is deleted by then
362   removeBar();
363   //
364
365   player->stop();
366   vdr->stopStreaming();
367   delete player;
368
369   playing = false;
370
371   if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
372   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Post stopPlay");
373 }
374
375 void VVideoRec::toggleChopSides()
376 {
377   if (video->getTVsize() == Video::ASPECT16X9) return; // Means nothing for 16:9 TVs
378
379   if (videoMode == Video::NORMAL)
380   {
381     videoMode = Video::LETTERBOX;
382     video->setMode(Video::LETTERBOX);
383   }
384   else
385   {
386     videoMode = Video::NORMAL;
387     video->setMode(Video::NORMAL);
388   }
389 }
390
391 void VVideoRec::doBar(int action)
392 {
393   barShowing = true;
394
395   rectangle(barRegion, barBlue);
396
397   /* Work out what to display - choices:
398
399   Playing  >
400   Paused   ||
401   FFwd     >>
402   FBwd     <<
403
404   Specials, informed by parameter
405
406   Skip forward 10s    >|
407   Skip backward 10s   |<
408   Skip forward 1m     >>|
409   Skip backward 1m    |<<
410
411   */
412
413   WSymbol w;
414   w.setSurface(surface);
415   w.nextSymbol = 0;
416   w.setSurfaceOffset(barRegion.x + 66, barRegion.y + 16);
417
418   UCHAR playerState = 0;
419
420   if (action)
421   {
422     if (action == 1)       w.nextSymbol = WSymbol::SKIPFORWARD;
423     else if (action == 2)  w.nextSymbol = WSymbol::SKIPBACK;
424     else if (action == 3)  w.nextSymbol = WSymbol::SKIPFORWARD2;
425     else if (action == 4)  w.nextSymbol = WSymbol::SKIPBACK2;
426   }
427   else
428   {
429     playerState = player->getState();
430     if (playerState == Player::S_PAUSE_P)      w.nextSymbol = WSymbol::PAUSE;
431     else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
432     else if (playerState == Player::S_FFWD)    w.nextSymbol = WSymbol::FFWD;
433     else if (playerState == Player::S_FBWD)    w.nextSymbol = WSymbol::FBWD;
434     else                                       w.nextSymbol = WSymbol::PLAY;
435   }
436
437   w.draw();
438
439   if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD))
440   {
441     // draw blips to show how fast the scan is
442     UCHAR scanrate = player->getIScanRate();
443     if (scanrate >= 2)
444     {
445       char* text = new char[5];
446       SNPRINTF(text, 5, "%ux", scanrate);
447       drawText(text, barRegion.x + 102, barRegion.y + 12, Colour::LIGHTTEXT);
448     }
449   }
450
451   drawBarClocks();
452
453   ViewMan::getInstance()->updateView(this, &barRegion);
454
455   if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD))
456   {
457     timers->cancelTimer(this, 1);
458     stickyBar = true;
459   }
460   else
461   {
462     timers->setTimerD(this, 1, 4); // only set the getridofbar timer if not ffwd/fbwd
463     stickyBar = false;
464   }
465   timers->setTimerD(this, 2, 0, 200000000);
466 }
467
468 void VVideoRec::timercall(int clientReference)
469 {
470   switch(clientReference)
471   {
472     case 1:
473     {
474       // Remove bar
475       removeBar();
476       break;
477     }
478     case 2:
479     {
480       // Update clock
481       if (!barShowing) break;
482       drawBarClocks();
483       ViewMan::getInstance()->updateView(this, &barRegion);
484       timers->setTimerD(this, 2, 0, 200000000);
485       break;
486     }
487   }
488 }
489
490 void VVideoRec::drawBarClocks()
491 {
492   if (stickyBar)
493   {
494     UCHAR playerState = player->getState();
495     // sticky bar is set if we are in ffwd/fbwd mode
496     // if player has gone to S_PLAY then kill stickyBar, and run doBar(0) which
497     // will repaint all the bar (it will call this function again, but
498     // this section won't run because stickyBar will then == false)
499
500     if ((playerState != Player::S_FFWD) && (playerState != Player::S_FBWD))
501     {
502       stickyBar = false;
503       doBar(0);
504       return; // doBar will call this function and do the rest
505     }
506   }
507
508   Log* logger = Log::getInstance();
509   logger->log("VVideoRec", Log::DEBUG, "Draw bar clocks");
510
511   // Draw RTC
512   // Blank the area first
513   rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
514   char timeString[20];
515   time_t t;
516   time(&t);
517   struct tm* tms = localtime(&t);
518   strftime(timeString, 19, "%H:%M", tms);
519   drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
520
521   // Draw clocks
522
523   rectangle(clocksRegion, barBlue);
524
525   ULONG currentFrameNum = player->getCurrentFrameNum();
526   ULONG lengthFrames;
527   if (myRec->recInfo->timerEnd > time(NULL))
528   {
529     // chasing playback
530     // Work out an approximate length in frames (good to 1s...)
531     lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
532   }
533   else
534   {
535     lengthFrames = player->getLengthFrames();
536   }
537
538   hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
539   hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
540
541   char buffer[100];
542   if (currentFrameNum >= lengthFrames)
543   {
544     strcpy(buffer, "-:--:-- / -:--:--");
545   }
546   else
547   {
548     SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
549     logger->log("VVideoRec", Log::DEBUG, buffer);
550   }
551
552   drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
553
554
555
556
557
558
559
560   // Draw progress bar
561   int progBarXbase = barRegion.x + 300;
562
563   rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
564   rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
565
566   if (currentFrameNum > lengthFrames) return;
567   if (lengthFrames == 0) return;
568
569   // Draw yellow portion
570   int progressWidth = 302 * currentFrameNum / lengthFrames;
571   rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
572
573   if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
574   {
575     int nrWidth = (int)(302 * ((double)(lengthFrames - player->getLengthFrames()) / lengthFrames));
576
577     Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
578     Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
579     Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
580     rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
581   }
582
583   // Now calc position for start margin blips
584   int posPix;
585
586   posPix = 302 * startMargin * video->getFPS() / lengthFrames;
587
588   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
589   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
590
591   posPix = 302 * (lengthFrames - endMargin * video->getFPS()) / lengthFrames;
592
593   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
594   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
595 }
596
597 void VVideoRec::removeBar()
598 {
599   if (!barShowing) return;
600   timers->cancelTimer(this, 2);
601   barShowing = false;
602   stickyBar = false;
603   rectangle(barRegion, transparent);
604   ViewMan::getInstance()->updateView(this, &barRegion);
605 }