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