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