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