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