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