]> git.vomp.tv Git - vompclient.git/blob - vvideorec.cc
Seperate the two pause types into their own states
[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 + 140;
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->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(ULONG startFrameNum)
97 {
98   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Starting stream: %s at frame: %lu", myRec->getFileName(), startFrameNum);
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->setStartFrame(startFrameNum);
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->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::REVERSE:
191     {
192       player->toggleFastBackward();
193       doBar(0);
194       return 2;
195     }
196     case Remote::YELLOW:
197     {
198       doBar(2);
199       player->skipBackward(10);
200       return 2;
201     }
202     case Remote::BLUE:
203     {
204       doBar(1);
205       player->skipForward(10);
206       return 2;
207     }
208     case Remote::FULL:
209     case Remote::TV:
210     {
211       toggleChopSides();
212       return 2;
213     }
214
215     case Remote::OK:
216     {
217       if (barShowing) removeBar();
218       else doBar(0);
219       return 2;
220     }
221
222
223 //    case Remote::REVERSE:
224 //    {
225 //      player->toggleFastBackward();
226 //      return 2;
227 //    }
228
229     case Remote::ZERO:  player->jumpToPercent(0);  doBar(0);  return 2;
230     case Remote::ONE:   player->jumpToPercent(10); doBar(0);  return 2;
231     case Remote::TWO:   player->jumpToPercent(20); doBar(0);  return 2;
232     case Remote::THREE: player->jumpToPercent(30); doBar(0);  return 2;
233     case Remote::FOUR:  player->jumpToPercent(40); doBar(0);  return 2;
234     case Remote::FIVE:  player->jumpToPercent(50); doBar(0);  return 2;
235     case Remote::SIX:   player->jumpToPercent(60); doBar(0);  return 2;
236     case Remote::SEVEN: player->jumpToPercent(70); doBar(0);  return 2;
237     case Remote::EIGHT: player->jumpToPercent(80); doBar(0);  return 2;
238     case Remote::NINE:  player->jumpToPercent(90); doBar(0);  return 2;
239
240 #ifdef DEV
241     case Remote::RED:
242     {
243       //player->test1();
244
245       /*
246       // for testing EPG in NTSC with a NTSC test video
247       Video::getInstance()->setMode(Video::QUARTER);
248       Video::getInstance()->setPosition(170, 5);
249       VEpg* vepg = new VEpg(NULL, 0);
250       vepg->draw();
251       ViewMan::getInstance()->add(vepg);
252       ViewMan::getInstance()->updateView(vepg);
253       */
254
255       return 2;
256     }
257     case Remote::GREEN:
258     {
259       player->test2();
260       return 2;
261     }
262 #endif
263
264   }
265
266   return 1;
267 }
268
269 void VVideoRec::stopPlay()
270 {
271   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Pre stopPlay");
272
273   // FIXME work out a better soln for this
274   // Fix a problem to do with thread sync here
275   // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
276   // (or maybe just the wrong thread being selected?) for the main loop to lock and process
277   // the video stop message it is possible for a bar message to stack up after a stop message
278   // when the bar message is finally processed the prog crashes because this is deleted by then
279   removeBar();
280   //
281
282   player->stop();
283   vdr->stopStreaming();
284   delete player;
285
286   playing = false;
287
288   if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
289   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Post stopPlay");
290 }
291
292 void VVideoRec::toggleChopSides()
293 {
294   if (video->getTVsize() == Video::ASPECT16X9) return; // Means nothing for 16:9 TVs
295
296   if (videoMode == Video::NORMAL)
297   {
298     videoMode = Video::LETTERBOX;
299     video->setMode(Video::LETTERBOX);
300   }
301   else
302   {
303     videoMode = Video::NORMAL;
304     video->setMode(Video::NORMAL);
305   }
306 }
307
308 void VVideoRec::doBar(int action)
309 {
310   barShowing = true;
311
312   rectangle(barRegion, barBlue);
313
314   /* Work out what to display - choices:
315
316   Playing  >
317   Paused   ||
318   FFwd     >>
319   FBwd     <<
320
321   Specials, informed by parameter
322
323   Skip forward 10s    >|
324   Skip backward 10s   |<
325   Skip forward 1m     >>|
326   Skip backward 1m    |<<
327
328   */
329
330   WSymbol w;
331   w.setSurface(surface);
332   w.nextSymbol = 0;
333   w.setSurfaceOffset(76, barRegion.y + 16);
334
335   if (action)
336   {
337     if (action == 1)       w.nextSymbol = WSymbol::SKIPFORWARD;
338     else if (action == 2)  w.nextSymbol = WSymbol::SKIPBACK;
339     else if (action == 3)  w.nextSymbol = WSymbol::SKIPFORWARD2;
340     else if (action == 4)  w.nextSymbol = WSymbol::SKIPBACK2;
341   }
342   else
343   {
344     UCHAR playerState = player->getState();
345     if (playerState == Player::S_PAUSE_P)      w.nextSymbol = WSymbol::PAUSE;
346     else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
347     else if (playerState == Player::S_FFWD)    w.nextSymbol = WSymbol::FFWD;
348     else if (playerState == Player::S_FBWD)    w.nextSymbol = WSymbol::FBWD;
349     else                                       w.nextSymbol = WSymbol::PLAY;
350   }
351
352   w.draw();
353
354   drawBarClocks();
355
356   ViewMan::getInstance()->updateView(this, &barRegion);
357   timers->setTimerD(this, 1, 4);
358   timers->setTimerD(this, 2, 0, 200000000);
359 }
360
361 void VVideoRec::timercall(int clientReference)
362 {
363   switch(clientReference)
364   {
365     case 1:
366     {
367       // Remove bar
368       removeBar();
369       break;
370     }
371     case 2:
372     {
373       // Update clock
374       if (!barShowing) break;
375       drawBarClocks();
376       ViewMan::getInstance()->updateView(this, &barRegion);
377       timers->setTimerD(this, 2, 0, 200000000);
378       break;
379     }
380   }
381 }
382
383 void VVideoRec::drawBarClocks()
384 {
385   Log* logger = Log::getInstance();
386   logger->log("VVideoRec", Log::DEBUG, "Draw bar clocks");
387
388   // Draw RTC
389   // Blank the area first
390   rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
391   char timeString[20];
392   time_t t;
393   time(&t);
394   struct tm* tms = localtime(&t);
395   strftime(timeString, 19, "%H:%M", tms);
396   drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
397
398   // Draw clocks
399
400   rectangle(clocksRegion, barBlue);
401
402   ULONG currentFrameNum = player->getCurrentFrameNum();
403   ULONG lengthFrames = player->getLengthFrames();
404
405   hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
406   hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
407
408   char buffer[100];
409   if (currentFrameNum >= lengthFrames)
410   {
411     strcpy(buffer, "-:--:-- / -:--:--");
412   }
413   else
414   {
415     SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
416     logger->log("VVideoRec", Log::DEBUG, buffer);
417   }
418
419   drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
420
421
422
423
424
425
426
427   // Draw progress bar
428   int progBarXbase = barRegion.x + 300;
429
430   rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
431   rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
432
433   if (currentFrameNum < lengthFrames)
434   {
435
436     int progressWidth = 302 * currentFrameNum / lengthFrames;
437
438     rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
439
440     // Now calc position for start margin blips
441     int posPix;
442
443     posPix = 302 * startMargin * video->getFPS() / lengthFrames;
444
445     rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
446     rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
447
448     posPix = 302 * (lengthFrames - endMargin * video->getFPS()) / lengthFrames;
449
450     rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
451     rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
452   }
453 }
454
455 void VVideoRec::removeBar()
456 {
457   if (!barShowing) return;
458   timers->cancelTimer(this, 2);
459   barShowing = false;
460   rectangle(barRegion, transparent);
461   ViewMan::getInstance()->updateView(this, &barRegion);
462 }