]> git.vomp.tv Git - vompclient.git/blob - vradiorec.cc
Translation updates
[vompclient.git] / vradiorec.cc
1 /*
2     Copyright 2004-2006 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 "vradiorec.h"
22
23 VRadioRec::VRadioRec(Recording* rec)
24 {
25   vdr = VDR::getInstance();
26   video = Video::getInstance();
27   timers = Timers::getInstance();
28   myRec = rec;
29   playing = false;
30   startMargin = 0;
31   endMargin = 0;
32
33   player = new PlayerRadio(Command::getInstance(), this, true);
34
35   char* cstartMargin = vdr->configLoad("Timers", "Start margin");
36   char* cendMargin = vdr->configLoad("Timers", "End margin");
37   if (!cstartMargin)
38   {
39     startMargin = 300; // 5 mins default
40   }
41   else
42   {
43     startMargin = atoi(cstartMargin) * 60;
44     delete[] cstartMargin;
45   }
46
47   if (!cendMargin)
48   {
49     endMargin = 300; // 5 mins default
50   }
51   else
52   {
53     endMargin = atoi(cendMargin) * 60;
54     delete[] cendMargin;
55   }
56
57   Log::getInstance()->log("VRadioRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
58
59   create(video->getScreenWidth(), video->getScreenHeight());
60   setBackgroundColour(Colour::BLACK);
61
62   barRegion.x = 0;
63   barRegion.y = video->getScreenHeight() - 58;   // FIXME, need to be - 1? and below?
64   barRegion.w = video->getScreenWidth();
65   barRegion.h = 58;
66
67   clocksRegion.x = barRegion.x + 140;
68   clocksRegion.y = barRegion.y + 12;
69   clocksRegion.w = 170;
70   clocksRegion.h = surface->getFontHeight();
71
72
73   barBlue.set(0, 0, 150, 150);
74
75   barShowing = false;
76 }
77
78 VRadioRec::~VRadioRec()
79 {
80   if (playing) stopPlay();
81
82   timers->cancelTimer(this, 1);
83   timers->cancelTimer(this, 2);
84
85   // kill recInfo in case resumePoint has changed (likely)
86   myRec->dropRecInfo();
87   // FIXME - do this properly - save the resume point back to the server manually and update
88   // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
89 }
90
91 void VRadioRec::draw()
92 {
93   View::draw();
94 }
95
96 void VRadioRec::go()
97 {
98   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Starting stream: %s", myRec->getFileName());
99   ULONG lengthFrames = 0;
100   ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
101
102   bool cantStart = false;
103
104   if (!lengthBytes) cantStart = true;
105   else if (!player->init(lengthBytes, lengthFrames)) cantStart = true;
106   else
107   {
108     doBar(0);
109   //  player->setStartBytes(startBytes);
110     player->play();
111     playing = true;
112   }
113
114   if (cantStart)
115   {
116     stopPlay(); // clean up
117
118     if (!vdr->isConnected())
119     {
120       Command::getInstance()->connectionLost();
121       return;
122     }
123
124     ViewMan* viewman = ViewMan::getInstance();
125
126     Message* m = new Message();
127     m->message = Message::CLOSE_ME;
128     m->from = this;
129     m->to = viewman;
130     Command::getInstance()->postMessageNoLock(m);
131
132     VInfo* vi = new VInfo();
133     vi->create(400, 150);
134     if (video->getFormat() == Video::PAL)
135       vi->setScreenPos(170, 200);
136     else
137       vi->setScreenPos(160, 150);
138     vi->setExitable();
139     vi->setBorderOn(1);
140     vi->setTitleBarOn(0);
141     vi->setOneLiner(tr("Error playing recording"));
142     vi->draw();
143
144     m = new Message();
145     m->message = Message::ADD_VIEW;
146     m->to = viewman;
147     m->parameter = (ULONG)vi;
148     Command::getInstance()->postMessageNoLock(m);
149   }
150 }
151
152 int VRadioRec::handleCommand(int command)
153 {
154   switch(command)
155   {
156     case Remote::PLAY:
157     {
158       player->play();
159       doBar(0);
160       return 2;
161     }
162
163     case Remote::STOP:
164     case Remote::BACK:
165     case Remote::MENU:
166     {
167       if (playing) stopPlay();
168       return 4;
169     }
170     case Remote::PAUSE:
171     {
172       player->pause();
173       doBar(0);
174       return 2;
175     }
176     case Remote::SKIPFORWARD:
177     {
178       doBar(3);
179       player->skipForward(60);
180       return 2;
181     }
182     case Remote::SKIPBACK:
183     {
184       doBar(4);
185       player->skipBackward(60);
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::OK:
201     {
202       if (barShowing) removeBar();
203       else doBar(0);
204       return 2;
205     }
206
207     case Remote::ZERO:  player->jumpToPercent(0);  doBar(0);  return 2;
208     case Remote::ONE:   player->jumpToPercent(10); doBar(0);  return 2;
209     case Remote::TWO:   player->jumpToPercent(20); doBar(0);  return 2;
210     case Remote::THREE: player->jumpToPercent(30); doBar(0);  return 2;
211     case Remote::FOUR:  player->jumpToPercent(40); doBar(0);  return 2;
212     case Remote::FIVE:  player->jumpToPercent(50); doBar(0);  return 2;
213     case Remote::SIX:   player->jumpToPercent(60); doBar(0);  return 2;
214     case Remote::SEVEN: player->jumpToPercent(70); doBar(0);  return 2;
215     case Remote::EIGHT: player->jumpToPercent(80); doBar(0);  return 2;
216     case Remote::NINE:  player->jumpToPercent(90); doBar(0);  return 2;
217
218 #ifdef DEV
219     case Remote::RED:
220     {
221       //player->test1();
222
223
224       /*
225       // for testing EPG in NTSC with a NTSC test video
226       Video::getInstance()->setMode(Video::QUARTER);
227       Video::getInstance()->setPosition(170, 5);
228       VEpg* vepg = new VEpg(NULL, 0);
229       vepg->draw();
230       ViewMan::getInstance()->add(vepg);
231       ViewMan::getInstance()->updateView(vepg);
232       */
233
234       return 2;
235     }
236     case Remote::GREEN:
237     {
238       //player->test2();
239       return 2;
240     }
241 #endif
242
243   }
244
245   return 1;
246 }
247
248 void VRadioRec::processMessage(Message* m)
249 {
250   if (m->message == Message::MOUSE_LBDOWN)
251   {
252     int x=(m->parameter>>16)-(int)getScreenX();
253     int y=(m->parameter&0xFFFF)-(int)getScreenY();
254     if (!barShowing)
255     {
256       ViewMan::getInstance()->handleCommand(Remote::OK); //simulate rok press
257     }
258     else if ((int)barRegion.x<=x && (int)barRegion.y<=y && ((int)barRegion.x+(int)barRegion.w)>=x
259              &&  ((int)barRegion.y+(int)barRegion.h)>=y)
260     {
261       int progBarXbase = barRegion.x + 300;
262       if (x>=(int)barRegion.x + progBarXbase + 24
263         && x<=(int)barRegion.x + progBarXbase + 4 + 302
264         && y>=(int)barRegion.y + 12 - 2
265         && y<=(int)barRegion.y + 12 - 2+28)
266       {
267         int cx=x-(barRegion.x + progBarXbase + 4);
268         double percent=((double)cx)/302.*100.;
269         player->jumpToPercent(percent);
270         doBar(3);
271         return;
272         //  int progressWidth = 302 * currentFrameNum / lengthFrames;
273         //  rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
274       }
275     }
276     else
277     {
278       ViewMan::getInstance()->handleCommand(Remote::OK); //simulate rok press
279     }
280   }
281   else if (m->message == Message::PLAYER_EVENT)
282   {
283     if (m->from != player) return;
284
285     Log::getInstance()->log("VRadioRec", Log::DEBUG, "Message received");
286
287     switch(m->parameter)
288     {
289       case Player::CONNECTION_LOST: // connection lost detected
290       {
291         // I can't handle this, send it to command
292         Message* m2 = new Message();
293         m2->to = Command::getInstance();
294         m2->message = Message::CONNECTION_LOST;
295         Command::getInstance()->postMessageNoLock(m2);
296         break;
297       }
298       case Player::STOP_PLAYBACK:
299       {
300         // FIXME Obselete ish - improve this
301         Message* m2 = new Message(); // Must be done after this thread finishes, and must break into master mutex
302         m2->to = Command::getInstance();
303         m2->message = Message::STOP_PLAYBACK;
304         Command::getInstance()->postMessageNoLock(m2);
305         break;
306       }
307     }
308   }
309 }
310
311 void VRadioRec::stopPlay()
312 {
313   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Pre stopPlay");
314
315   // FIXME work out a better soln for this
316   // Fix a problem to do with thread sync here
317   // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
318   // (or maybe just the wrong thread being selected?) for the main loop to lock and process
319   // the video stop message it is possible for a bar message to stack up after a stop message
320   // when the bar message is finally processed the prog crashes because this is deleted by then
321   removeBar();
322   //
323
324   player->stop();
325   vdr->stopStreaming();
326   delete player;
327
328   playing = false;
329
330   if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
331   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Post stopPlay");
332 }
333
334 void VRadioRec::doBar(int action)
335 {
336   barShowing = true;
337
338   rectangle(barRegion, barBlue);
339
340   /* Work out what to display - choices:
341
342   Playing  >
343   Paused   ||
344
345   Specials, informed by parameter
346
347   Skip forward 10s    >|
348   Skip backward 10s   |<
349   Skip forward 1m     >>|
350   Skip backward 1m    |<<
351
352   */
353
354   WSymbol w;
355   w.setSurface(surface);
356   w.nextSymbol = 0;
357   w.setSurfaceOffset(barRegion.x + 66, barRegion.y + 16);
358
359   UCHAR playerState = 0;
360
361   if (action)
362   {
363     if (action == 1)       w.nextSymbol = WSymbol::SKIPFORWARD;
364     else if (action == 2)  w.nextSymbol = WSymbol::SKIPBACK;
365     else if (action == 3)  w.nextSymbol = WSymbol::SKIPFORWARD2;
366     else if (action == 4)  w.nextSymbol = WSymbol::SKIPBACK2;
367   }
368   else
369   {
370     playerState = player->getState();
371     if (playerState == Player::S_PAUSE_P)      w.nextSymbol = WSymbol::PAUSE;
372     else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
373     else                                       w.nextSymbol = WSymbol::PLAY;
374   }
375
376   w.draw();
377
378   drawBarClocks();
379
380   ViewMan::getInstance()->updateView(this, &barRegion);
381
382   timers->setTimerD(this, 1, 4); // only set the getridofbar timer if not ffwd/fbwd
383   timers->setTimerD(this, 2, 0, 200000000);
384 }
385
386 void VRadioRec::timercall(int clientReference)
387 {
388   switch(clientReference)
389   {
390     case 1:
391     {
392       // Remove bar
393       removeBar();
394       break;
395     }
396     case 2:
397     {
398       // Update clock
399       if (!barShowing) break;
400       drawBarClocks();
401       ViewMan::getInstance()->updateView(this, &barRegion);
402       timers->setTimerD(this, 2, 0, 200000000);
403       break;
404     }
405   }
406 }
407
408 void VRadioRec::drawBarClocks()
409 {
410   Log* logger = Log::getInstance();
411   logger->log("VRadioRec", Log::DEBUG, "Draw bar clocks");
412
413   // Draw RTC
414   // Blank the area first
415   rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
416   char timeString[20];
417   time_t t;
418   time(&t);
419   struct tm* tms = localtime(&t);
420   strftime(timeString, 19, "%H:%M", tms);
421   drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
422
423   // Draw clocks
424
425   rectangle(clocksRegion, barBlue);
426
427 /*
428   ULONG currentFrameNum = player->getCurrentFrameNum();
429   ULONG lengthFrames;
430   if (myRec->recInfo->timerEnd > time(NULL))
431   {
432     // chasing playback
433     // Work out an approximate length in frames (good to 1s...)
434     lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
435   }
436   else
437   {
438 //    lengthFrames = player->getLengthFrames();
439     lengthFrames = 0;
440   }
441
442   hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
443   hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
444
445   char buffer[100];
446   if (currentFrameNum >= lengthFrames)
447   {
448     strcpy(buffer, "-:--:-- / -:--:--");
449   }
450   else
451   {
452     SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
453     logger->log("VRadioRec", Log::DEBUG, buffer);
454   }
455 */
456
457   ULONG currentSeconds = player->getCurrentSeconds();
458   ULONG lengthSeconds = player->getLengthSeconds();
459   char buffer[100];
460
461   if (lengthSeconds && (currentSeconds < lengthSeconds))
462   {
463     ULONG dcurrentSeconds = currentSeconds;
464     ULONG dlengthSeconds = lengthSeconds;
465
466     ULONG currentHours = dcurrentSeconds / 3600;
467     dcurrentSeconds %= 3600;
468     ULONG currentMinutes = dcurrentSeconds / 60;
469     dcurrentSeconds %= 60;
470
471     ULONG lengthHours = dlengthSeconds / 3600;
472     dlengthSeconds %= 3600;
473     ULONG lengthMinutes = dlengthSeconds / 60;
474     dlengthSeconds %= 60;
475
476     SNPRINTF(buffer, 99, "%01lu:%02lu:%02lu / %01lu:%02lu:%02lu", currentHours, currentMinutes, dcurrentSeconds, lengthHours, lengthMinutes, dlengthSeconds);
477     logger->log("VRadioRec", Log::DEBUG, buffer);
478   }
479   else
480   {
481     strcpy(buffer, "-:--:-- / -:--:--");
482   }
483
484   drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
485
486
487
488
489   // Draw progress bar
490   int progBarXbase = barRegion.x + 300;
491
492   rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
493   rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
494
495   if (currentSeconds > lengthSeconds) return;
496   if (lengthSeconds == 0) return;
497
498   // Draw yellow portion
499   int progressWidth = 302 * currentSeconds / lengthSeconds;
500   rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
501 /*
502
503   if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
504   {
505     int nrWidth = (int)(302 * ((double)(lengthFrames - 0) / lengthFrames)); // 0 inserted instead of getlengthframes
506
507     Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
508 //    Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
509     Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
510     rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
511   }
512 */
513
514   logger->log("VRadioRec", Log::DEBUG, "blips");
515
516   // Now calc position for start margin blips
517   int posPix;
518
519   posPix = 302 * startMargin / lengthSeconds;
520   logger->log("VRadioRec", Log::DEBUG, "posPix %i", posPix);
521
522   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
523   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
524
525   posPix = 302 * (lengthSeconds - endMargin) / lengthSeconds;
526
527   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
528   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
529 }
530
531 void VRadioRec::removeBar()
532 {
533   if (!barShowing) return;
534   timers->cancelTimer(this, 2);
535   barShowing = false;
536   rectangle(barRegion, Colour::BLACK);
537   ViewMan::getInstance()->updateView(this, &barRegion);
538 }