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