]> git.vomp.tv Git - vompclient.git/blob - vvideorec.cc
Update for retrieving total recording length in frames
[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   player = new Player(Command::getInstance(), true, false); // say 0 for radio because buffering will work anyway
30   player->init();
31
32   videoMode = video->getMode();
33   myRec = rec;
34
35   playing = false;
36
37   startMargin = 0;
38   endMargin = 0;
39   char* cstartMargin = vdr->configLoad("Timers", "Start margin");
40   char* cendMargin = vdr->configLoad("Timers", "End margin");
41   if (!cstartMargin)
42   {
43     startMargin = 300; // 5 mins default
44   }
45   else
46   {
47     startMargin = atoi(cstartMargin) * 60;
48     delete[] cstartMargin;
49   }
50
51   if (!cendMargin)
52   {
53     endMargin = 300; // 5 mins default
54   }
55   else
56   {
57     endMargin = atoi(cendMargin) * 60;
58     delete[] cendMargin;
59   }
60
61   Log::getInstance()->log("VVideoRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
62
63   create(video->getScreenWidth(), video->getScreenHeight());
64   transparent.set(0, 0, 0, 0);
65   setBackgroundColour(transparent);
66
67   barRegion.x = 0;
68   barRegion.y = video->getScreenHeight() - 66;   // FIXME, need to be - 1? and below?
69   barRegion.w = video->getScreenWidth();
70   barRegion.h = 66;
71
72   clocksRegion.x = barRegion.x + 160;
73   clocksRegion.y = barRegion.y + 12;
74   clocksRegion.w = 170;
75   clocksRegion.h = surface->getFontHeight();
76
77   barBlue.set(0, 0, 150, 150);
78
79   barShowing = false;
80 }
81
82 VVideoRec::~VVideoRec()
83 {
84   if (playing) stopPlay();
85   Video::getInstance()->setDefaultAspect();
86
87   timers->cancelTimer(this, 1);
88   timers->cancelTimer(this, 2);
89 }
90
91 void VVideoRec::draw()
92 {
93   View::draw();
94 }
95
96 void VVideoRec::go(ULLONG startPosition)
97 {
98   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Starting stream: %s", myRec->getFileName());
99   ULONG lengthFrames = 0;
100   ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
101   if (lengthBytes)
102   {
103     doBar(0);
104     player->setLengthBytes(lengthBytes);
105     player->setLengthFrames(lengthFrames);
106     player->setPosition(startPosition);
107     player->play();
108     playing = true;
109   }
110   else
111   {
112     stopPlay(); // clean up
113
114     if (!vdr->isConnected())
115     {
116       Command::getInstance()->connectionLost();
117       return;
118     }
119
120     ViewMan* viewman = ViewMan::getInstance();
121
122     Message* m = new Message();
123     m->message = Message::CLOSE_ME;
124     m->from = this;
125     m->to = viewman;
126     viewman->postMessage(m);
127
128     VInfo* vi = new VInfo();
129     vi->create(400, 150);
130     if (Video::getInstance()->getFormat() == Video::PAL)
131       vi->setScreenPos(170, 200);
132     else
133       vi->setScreenPos(160, 150);
134     vi->setExitable();
135     vi->setBorderOn(1);
136     vi->setTitleBarOn(0);
137     vi->setOneLiner(tr("Error playing recording"));
138     vi->draw();
139
140     m = new Message();
141     m->message = Message::ADD_VIEW;
142     m->to = viewman;
143     m->parameter = (ULONG)vi;
144     viewman->postMessage(m);
145   }
146 }
147
148 int VVideoRec::handleCommand(int command)
149 {
150   switch(command)
151   {
152     case Remote::PLAY:
153     {
154       player->play();
155       doBar(0);
156       return 2;
157     }
158
159     case Remote::STOP:
160     case Remote::BACK:
161     case Remote::MENU:
162     {
163       if (playing) stopPlay();
164       return 4;
165     }
166     case Remote::PAUSE:
167     {
168       player->togglePause();
169       doBar(0);
170       return 2;
171     }
172     case Remote::SKIPFORWARD:
173     {
174       doBar(3);
175       player->skipForward(60);
176       return 2;
177     }
178     case Remote::SKIPBACK:
179     {
180       doBar(4);
181       player->skipBackward(60);
182       return 2;
183     }
184     case Remote::FORWARD:
185     {
186       player->toggleFastForward();
187       doBar(0);
188       return 2;
189     }
190     case Remote::YELLOW:
191     {
192       doBar(2);
193       player->skipBackward(10);
194       return 2;
195     }
196     case Remote::BLUE:
197     {
198       doBar(1);
199       player->skipForward(10);
200       return 2;
201     }
202     case Remote::FULL:
203     case Remote::TV:
204     {
205       toggleChopSides();
206       return 2;
207     }
208
209     case Remote::OK:
210     {
211       if (barShowing) removeBar();
212       else doBar(0);
213       return 2;
214     }
215
216
217 //    case Remote::REVERSE:
218 //    {
219 //      player->toggleFastBackward();
220 //      return 2;
221 //    }
222
223     case Remote::ZERO:  player->jumpToPercent(0);  doBar(0);  return 2;
224     case Remote::ONE:   player->jumpToPercent(10); doBar(0);  return 2;
225     case Remote::TWO:   player->jumpToPercent(20); doBar(0);  return 2;
226     case Remote::THREE: player->jumpToPercent(30); doBar(0);  return 2;
227     case Remote::FOUR:  player->jumpToPercent(40); doBar(0);  return 2;
228     case Remote::FIVE:  player->jumpToPercent(50); doBar(0);  return 2;
229     case Remote::SIX:   player->jumpToPercent(60); doBar(0);  return 2;
230     case Remote::SEVEN: player->jumpToPercent(70); doBar(0);  return 2;
231     case Remote::EIGHT: player->jumpToPercent(80); doBar(0);  return 2;
232     case Remote::NINE:  player->jumpToPercent(90); doBar(0);  return 2;
233
234 #ifdef DEV
235     case Remote::RED:
236     {
237       //player->test1();
238
239       /*
240       // for testing EPG in NTSC with a NTSC test video
241       Video::getInstance()->setMode(Video::QUARTER);
242       Video::getInstance()->setPosition(170, 5);
243       VEpg* vepg = new VEpg(NULL, 0);
244       vepg->draw();
245       ViewMan::getInstance()->add(vepg);
246       ViewMan::getInstance()->updateView(vepg);
247       */
248
249       return 2;
250     }
251     case Remote::GREEN:
252     {
253       player->test2();
254       return 2;
255     }
256 #endif
257
258   }
259
260   return 1;
261 }
262
263 void VVideoRec::stopPlay()
264 {
265   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Pre stopPlay");
266
267   // FIXME work out a better soln for this
268   // Fix a problem to do with thread sync here
269   // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
270   // (or maybe just the wrong thread being selected?) for the main loop to lock and process
271   // the video stop message it is possible for a bar message to stack up after a stop message
272   // when the bar message is finally processed the prog crashes because this is deleted by then
273   removeBar();
274   //
275
276   player->stop();
277   vdr->stopStreaming();
278   delete player;
279
280   playing = false;
281
282   if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
283   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Post stopPlay");
284 }
285
286 void VVideoRec::toggleChopSides()
287 {
288   if (video->getTVsize() == Video::ASPECT16X9) return; // Means nothing for 16:9 TVs
289
290   if (videoMode == Video::NORMAL)
291   {
292     videoMode = Video::LETTERBOX;
293     video->setMode(Video::LETTERBOX);
294   }
295   else
296   {
297     videoMode = Video::NORMAL;
298     video->setMode(Video::NORMAL);
299   }
300 }
301
302 void VVideoRec::doBar(int action)
303 {
304   barShowing = true;
305
306   rectangle(barRegion, barBlue);
307
308   /* Work out what to display - choices:
309
310   Playing  >
311   Paused   ||
312   FFwd     >>
313   FBwd     <<
314
315   Specials, informed by parameter
316
317   Skip forward 10s    >|
318   Skip backward 10s   |<
319   Skip forward 1m     >>|
320   Skip backward 1m    |<<
321
322   */
323
324   WSymbol w;
325   w.setSurface(surface);
326   w.nextSymbol = 0;
327   w.setSurfaceOffset(76, barRegion.y + 16);
328
329   if (action)
330   {
331     if (action == 1)       w.nextSymbol = WSymbol::SKIPFORWARD;
332     else if (action == 2)  w.nextSymbol = WSymbol::SKIPBACK;
333     else if (action == 3)  w.nextSymbol = WSymbol::SKIPFORWARD2;
334     else if (action == 4)  w.nextSymbol = WSymbol::SKIPBACK2;
335   }
336   else
337   {
338     if (player->isPaused())     w.nextSymbol = WSymbol::PAUSE;
339     else if (player->isFfwd())  w.nextSymbol = WSymbol::FFWD;
340     else if (player->isFbwd())  w.nextSymbol = WSymbol::FBWD;
341     else                        w.nextSymbol = WSymbol::PLAY;
342   }
343
344   w.draw();
345
346   drawBarClocks();
347
348   ViewMan::getInstance()->updateView(this, &barRegion);
349   timers->setTimerD(this, 1, 4);
350   timers->setTimerD(this, 2, 0, 200000000);
351 }
352
353 void VVideoRec::timercall(int clientReference)
354 {
355   switch(clientReference)
356   {
357     case 1:
358     {
359       // Remove bar
360       removeBar();
361       break;
362     }
363     case 2:
364     {
365       // Update clock
366       if (!barShowing) break;
367       drawBarClocks();
368       ViewMan::getInstance()->updateView(this, &barRegion);
369       timers->setTimerD(this, 2, 0, 200000000);
370       break;
371     }
372   }
373 }
374
375 void VVideoRec::drawBarClocks()
376 {
377   Log* logger = Log::getInstance();
378   logger->log("VVideoRec", Log::DEBUG, "Draw bar clocks");
379
380   rectangle(clocksRegion, barBlue);
381
382   ULONG currentTS = (player->getPositionTS() / 90000);
383   int chours = currentTS / 3600;
384   int cminutes = (currentTS - (chours * 3600)) / 60;
385   int cseconds = currentTS - (chours * 3600) - (cminutes * 60);
386
387   ULONG endTS = (player->getEndTS() / 90000);
388   int ehours = endTS / 3600;
389   int eminutes = (endTS - (ehours * 3600)) / 60;
390   int eseconds = endTS - (ehours * 3600) - (eminutes * 60);
391
392   char buffer[100];
393   if ((currentTS > 95441) // it's at the 33bit rollover point where the calc doesn't work because of the 1s diff
394                           // between demuxer values and video chip return values ... ?
395       || (!endTS))        // No values yet
396   {
397     strcpy(buffer, "-:--:-- / -:--:--");
398   }
399   else
400   {
401     SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", chours, cminutes, cseconds, ehours, eminutes, eseconds);
402     logger->log("VVideoRec", Log::DEBUG, buffer);
403
404 //    if (chours > 0) abort();
405   }
406
407   drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
408
409   // Draw progress bar
410
411   rectangle(barRegion.x + 350, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
412   rectangle(barRegion.x + 352, barRegion.y + 14, 306, 20, barBlue);
413
414   if ((currentTS > 95441) || (!endTS)) return;    // No values yet
415
416   double progress01 = (double)currentTS / (double)endTS;
417   // total width of bar = 302
418   int progressWidth = (int)(302 * progress01);
419
420   rectangle(barRegion.x + 354, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
421
422   // Now calc position for start margin blips
423   double pos01;
424   int posPix;
425
426   pos01 = (double)startMargin / (double)endTS;
427   posPix = (int)(302 * pos01);
428
429   rectangle(barRegion.x + 352 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
430   rectangle(barRegion.x + 352 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
431
432   pos01 = (double)(endTS - endMargin) / (double)endTS;
433   posPix = (int)(302 * pos01);
434
435   rectangle(barRegion.x + 352 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
436   rectangle(barRegion.x + 352 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
437 }
438
439 void VVideoRec::removeBar()
440 {
441   if (!barShowing) return;
442   timers->cancelTimer(this, 2);
443   barShowing = false;
444   rectangle(barRegion, transparent);
445   ViewMan::getInstance()->updateView(this, &barRegion);
446 }