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